flexmeasures.data.queries.generic_assets
Functions
- flexmeasures.data.queries.generic_assets.descendants_cte(root_asset_id: int, max_depth: int)
Build a recursive Common Table Expression (CTE) selecting all descendant assets of a given root asset.
This CTE walks the asset hierarchy by repeatedly following
parent_asset_idrelationships, starting from the given root asset. The result includes the root asset itself and all of its descendants up to a given depth.Use cases: - Server-side filtering of asset subtrees - Efficient hierarchical queries without Python-side traversal - Combining hierarchy constraints with search, sorting, or pagination
- Parameters:
root_asset_id – ID of the asset that acts as the root of the subtree.
- Returns:
A recursive SQLAlchemy CTE yielding asset IDs and parent IDs for the entire subtree.
- flexmeasures.data.queries.generic_assets.filter_assets_under_root(query: Select, root_asset: GenericAsset, max_depth: int) Select
Restrict an asset query to a specific asset subtree to a certain depth.
This function joins the given query against a recursive CTE so that only assets that are descendants of the specified root asset (including the root itself) are returned.
Characteristics: - Fully server-side - Supports arbitrary hierarchy depth - Compatible with sorting, pagination, and additional filters
- Parameters:
query – A SQLAlchemy
Selectstatement selecting fromGenericAsset.root_asset_id – Asset whose descendants should be included.
- Returns:
A modified
Selectstatement scoped to the specified asset subtree.
- flexmeasures.data.queries.generic_assets.get_asset_group_queries(group_by_type: bool = True, group_by_account: bool = False, group_by_location: bool = False, custom_aggregate_type_groups: dict[str, list[str]] | None = None) dict[str, Select]
An asset group is defined by Asset queries, which this function can generate. Each query has a name (for the asset group it represents). These queries still need an executive call, like all(), count() or first(). This function limits the assets to be queried to the current user’s account, if the user is not an admin. Note: Make sure the current user has the “read” permission on their account (on GenericAsset.__class__?? See https://github.com/FlexMeasures/flexmeasures/issues/200) or is an admin. :param group_by_type: If True, groups will be made for assets with the same type. We prefer pluralised group names here. Defaults to True. :param group_by_account: If True, groups will be made for assets within the same account. This makes sense for admins, as they can query across accounts. :param group_by_location: If True, groups will be made for assets at the same location. Naming of the location currently supports charge points (for EVSEs). :param custom_aggregate_type_groups: dict of asset type groupings (mapping group names to names of asset types). See also the setting FLEXMEASURES_ASSET_TYPE_GROUPS.
- flexmeasures.data.queries.generic_assets.get_location_queries(account_id: int | None = None) dict[str, Select[tuple[GenericAsset]]]
Make queries for grouping assets by location.
We group EVSE assets by location (if they share a location, they belong to the same Charge Point) Like get_asset_group_queries, the values in the returned dict still need an executive call, like all(), count() or first(). Note that this function will still load and inspect assets to do its job.
The Charge Points are named on the basis of the first EVSE in their list, using either the whole EVSE name or that part that comes before a “ -” delimiter. For example: If:
evse_name = “Seoul Hilton - charger 1”
- Then:
charge_point_name = “Seoul Hilton (Charge Point)”
A Charge Point is a special case. If all assets on a location are of type EVSE, we can call the location a “Charge Point”.
- Parameters:
account_id – Pass in an account ID if you want to query an account other than your own. This only works for admins. Public assets are always queried.
- flexmeasures.data.queries.generic_assets.group_assets_by_location(asset_list: list[GenericAsset]) list[list[GenericAsset]]
- flexmeasures.data.queries.generic_assets.query_assets_by_search_terms(search_terms: list[str] | None, filter_statement: bool = True, sort_by: str | None = None, sort_dir: str | None = None) Select
- flexmeasures.data.queries.generic_assets.query_assets_by_type(type_names: list[str] | str, account_id: int | None = None, query: Select | None = None) Select
Return a query which looks for GenericAssets by their type.
- Parameters:
type_names – Pass in a list of type names or only one type name.
account_id – Pass in an account ID if you want to query an account other than your own. This only works for admins. Public assets are always queried.
query – Pass in an existing Query object if you have one.