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 into a human-readable audit log entry.

Produces plain-language descriptions for sensors_to_show, flex_context, and flex_model changes. For generic JSON fields, nested dict/list structures are described recursively. Falls back to a simple before/after note for other fields or when an error occurs.

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:\n1. Added storage-efficiency: 99.92%'
>>> json = {
...     "field_name": "flex_context",
...     "old_value": {"site-production-capacity": "1500 kW", "site-peak-production": "20000kW"},
...     "new_value": {"site-production-capacity": "15000 kW", "site-peak-production": "20000kW"}
... }
>>> format_json_field_change(**json)
'Updated flex_context:\n1. Changed 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:\n1. Removed site-production-capacity (was: 15000 kW)'
>>> json = {
...     "field_name": "flex_model",
...     "old_value": {"soc-usage": ["3500 kW", {"sensor": 7}]},
...     "new_value": {"soc-usage": ["3500 kW", {"sensor": 8}]}
... }
>>> format_json_field_change(**json)
'Updated flex_model:\n1. Changed soc-usage[1].sensor: 7 → 8'
>>> json = {
...     "field_name": "sensors_to_show",
...     "old_value": [{"title": "Power", "plots": [{"sensor": 1}]}],
...     "new_value": [
...         {"title": "Power", "plots": [{"sensor": 1}, {"sensor": 2}]},
...         {"title": "Price", "plots": [{"sensor": 3}]},
...     ],
... }
>>> format_json_field_change(**json)
'Updated sensors_to_show:\n1. Changed graph 1 (Power): added plot 2: {"sensor": 2}\n2. Added graph 2: "Price" (sensors [3])'
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.