flexmeasures.data.services.generic_assets

Functions

flexmeasures.data.services.generic_assets.create_asset(asset_data: dict) GenericAsset

Create an asset.

Does not validate data or commit the session. Creates an audit log.

flexmeasures.data.services.generic_assets.delete_asset(asset: GenericAsset)

Delete an asset.

Does not commit the session. Creates an audit log.

flexmeasures.data.services.generic_assets.format_json_field_change(field_name: str, old_value, new_value) str

Format JSON field changes using dictdiffer.

This function attempts to provide a detailed diff of changes between two JSON-like structures. If the structures are not dicts or lists, or if an error occurs, it falls back to a simple change description.

Parameters:
  • field_name – Name of the field being changed.

  • old_value – The old value of the field.

  • new_value – The new value of the field.

Returns:

A formatted string describing the changes.

Examples

>>> json = {
...     "field_name": "flex_model",
...     "old_value": {"production-capacity": "15 kW"},
...     "new_value": {"production-capacity": "15 kW", "storage-efficiency": "99.92%"}
... }
>>> format_json_field_change(**json)
'Updated: flex_model, add storage-efficiency: 99.92%'
>>> json = {
...     "field_name": "flex_context",
...     "old_value": {"site-production-capacity": "1500 kW", "site-peak-production": "20000kW", "inflexible-device-sensors": []},
...     "new_value": {"site-production-capacity": "15000 kW", "site-peak-production": "20000kW", "inflexible-device-sensors": []}
... }
>>> format_json_field_change(**json)
'Updated: flex_context, change site-production-capacity: 1500 kW -> 15000 kW'
>>> json = {
...     "field_name": "flex_context",
...     "old_value": {"site-production-capacity": "15000 kW", "site-peak-production": "20000kW"},
...     "new_value": {"site-peak-production": "20000kW"}
... }
>>> format_json_field_change(**json)
'Updated: flex_context, remove site-production-capacity'
flexmeasures.data.services.generic_assets.patch_asset(db_asset: GenericAsset, asset_data: dict) GenericAsset

Patch an asset.

Throws validation error as it checks JSON fields (e.g. attributes) for validity explicitly. Does not commit the session. Creates an audit log.