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, execution_user_id: int | None = None) → AssetCopy

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

  • all automations on each copied asset, left inactive (see copy_automations)

Resolution rules:

  • If neither account nor parent_asset is given, the copy is placed in the same account and under the same parent as the original (i.e. a sibling).

  • If account is given but parent_asset is not, the copy becomes a top-level asset (no parent) in the given account.

  • If parent_asset is given but account is 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.

An automation that cannot be copied safely does not fail the copy: it is skipped, and reported in the returned AssetCopy.

Parameters:

execution_user_id – The user the copied automations run as (the user making the copy, if made through the API), whose permissions are checked again before each run. None leaves them without one, as automations created through the CLI are.

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.get_accessible_accounts() → list[Account]
flexmeasures.api.common.utils.api_utils.handle_belief_replacement_not_allowed(error: BeliefReplacementNotAllowed)

Answer a refused replacement with the INVALID_REPLACEMENT response.

flexmeasures.api.common.utils.api_utils.json_text_response(json_text: str) → Response

Send an already serialized JSON string with the media type application/json.

Flask sends a str returned from a view as text/html, so a browser opening the URL directly would render markup found in the JSON (for instance in a sensor name).

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, force_synchronous: bool = False) → 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. If no worker is available, process the data synchronously and return the resulting ingestion response. As a compatibility exception, callers can force synchronous processing for compatibility purposes.

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

Classes

class flexmeasures.api.common.utils.api_utils.AssetCopy(asset: GenericAsset, skipped_automations: list[SkippedAutomation])

The outcome of copying an asset subtree.

__init__(asset: GenericAsset, skipped_automations: list[SkippedAutomation]) → None