flexmeasures.api.common.utils.api_utils
Functions
- flexmeasures.api.common.utils.api_utils.catch_timed_belief_replacements(error: IntegrityError)
Catch IntegrityErrors due to a UniqueViolation on the TimedBelief primary key.
Return a more informative message.
- flexmeasures.api.common.utils.api_utils.convert_asset_json_fields(asset_kwargs)
Convert string fields in asset_kwargs to JSON where needed.
- flexmeasures.api.common.utils.api_utils.copy_asset(asset: GenericAsset, account=None, parent_asset=None) GenericAsset
Copy an asset subtree to a target account and/or under a target parent asset.
The copied subtree includes: - the selected asset - all descendant child assets (recursively) - all sensors directly attached to each copied asset
Resolution rules:
If neither
accountnorparent_assetis given, the copy is placed in the same account and under the same parent as the original (i.e. a sibling).If
accountis given butparent_assetis not, the copy becomes a top-level asset (no parent) in the given account.If
parent_assetis given butaccountis not, the copy is placed under the given parent and inherits that parent’s account.If both are given, the copy belongs to the given account and is placed under the given parent. This allows creating a copy that belongs to a different account than its parent.
- flexmeasures.api.common.utils.api_utils.enqueue_forecasting_jobs(forecasting_jobs: list[Job] | None = None)
Enqueue forecasting jobs.
- Parameters:
forecasting_jobs – list of forecasting Jobs for redis queues.
- flexmeasures.api.common.utils.api_utils.process_sensor_data_ingestion(sensor_id: int, user_id: int, sensor_data: dict | None = None, uploaded_files: list[dict] | None = None, upload_data: dict | None = None, forecasting_jobs: list[Job] | None = None, save_changed_beliefs_only: bool = True) tuple[dict, int] | tuple[dict, int, dict]
Process sensor data ingestion asynchronously when possible.
If an ingestion queue with connected workers is available, enqueue a background job and return
202 Accepted. Otherwise, process the data synchronously and return the resulting ingestion response.
- flexmeasures.api.common.utils.api_utils.queue_has_connected_workers(queue: Queue) bool
Return True when an RQ queue is reachable and has connected workers.
- flexmeasures.api.common.utils.api_utils.save_and_enqueue(data: BeliefsDataFrame | list[BeliefsDataFrame], forecasting_jobs: list[Job] | None = None, save_changed_beliefs_only: bool = True) tuple[dict, int] | tuple[dict, int, dict]
- flexmeasures.api.common.utils.api_utils.unique_ever_seen(iterable: Sequence, selector: Sequence)
Return unique iterable elements with corresponding lists of selector elements, preserving order.
>>> a, b = unique_ever_seen([[10, 20], [10, 20], [20, 40]], [1, 2, 3]) >>> print(a) [[10, 20], [20, 40]] >>> print(b) [[1, 2], 3]
- flexmeasures.api.common.utils.api_utils.upsample_values(value_groups: list[list[float]] | list[float], from_resolution: timedelta, to_resolution: timedelta) list[list[float]] | list[float]
Upsample the values (in value groups) to a smaller resolution. from_resolution has to be a multiple of to_resolution