Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API package gets and sets metadata on GenericAssets and Sensors (#243)
Make JSON attributes mutable and have the API package get and set metadata on GenericAssets and Sensors. Applies to all version of the API, but excludes CRUD functionality on assets in API v2.0. * Create draft PR for #239 * Db migration that copies over attributes from old data models * - In Asset.__init__, copy over attributes to GenericAsset. - Start having our model_spec_factory get attributes it needs from GenericAsset. - Rename variables, specifically, variables that were annotated as a union of our old sensor models were named generic_asset, which was too easily confused with instances of our GenericAsset class. * model_spec_factory now gets its attributes from GenericAsset instead of old sensor model types * More renaming to avoid confusion * Have db migration copy over sensor attributes: unit, event_resolution and knowledge horizons * In Asset.__init__, copy over sensor attributes: unit, event_resolution and knowledge horizons * model_spec_factory now gets event_resolution and name from Sensor * Fix tests * Factor out use of corresponding_generic_asset attribute * Factor out use of corresponding_generic_asset attribute * More renaming * Pass time series class to model configurator explicitly * Finally, model_spec_factory doesn't need the old sensor model anymore * Allow setting the collect function name for TBSeriesSpecs to something custom * In Asset.__init__, copy over additional asset attributes to GenericAsset * Planning subpackage uses sensors instead of assets * Move some simple attributes in the UI package * Refactor to stop explicitly passing the market to the scheduler, and instead have the scheduler check for an applicable market * Revert "Move some simple attributes in the UI package", because this needs to be done jointly with moving over asset crud (which we test for) This reverts commit 56ff279. * Create draft PR for #242 * Allow config setting specs as module variables, too. Support reading config setting specs from module (#237) * Support reading config setting specs from module * Add additional documentation (review suggestion) * Amend changelog entry * Create draft PR for #242 * Make JSON attributes mutable * Set Asset owner at initialization, so it is copied to GenericAsset * API v1_2 gets and sets asset attributes in the new data model * Deprecate use of Asset class in api v1_2 * Deprecate use of Asset class in api v1_3 * Revert "Allow config setting specs as module variables, too." This reverts commit 327b8b6. * Work around black issue by updating pre-commit-config * Deprecate use of Asset class in api v1_0 * Deprecate use of Asset class in part of api v2_0 * Fix docstring * Revert upgrade of black in pre-commit-config * Still battling black * Correct docstring * Deprecate use of Asset class in SensorField deserialization * Set Asset owner at initialization, so it is copied to GenericAsset * Simplify conftest * Add notes about how each attribute is to be copied from an old class to a new class * Rename variables * Refactor attribute copying to util function * In Market.__init__, copy over attributes from old models to new models * In Weather.__init__, copy over attributes from old models to new models * Deprecate use of Market class in SensorField deserialization * Deprecate use of WeatherSensor class in SensorField deserialization for fm1 * Simplify fm1 entity type schema for deserialization * Deprecate use of WeatherSensor class in SensorField deserialization for fm0 * Refactor query * Rename variable * Refactor query to arrive at a single combined query * Update todos * Intend to copy display_name to both GenericAsset and Sensor * Introduce Sensor attributes and copy most old model attributes there instead of to GenericAsset attributes * Adjust attribute copying in Asset.__init__ * Implement Sensor method to get an attribute * Give old sensor classes a generic_asset property * Give old sensor classes a get_attribute property * Derive Sensor class lat/lng location from GenericAsset * Get attributes from Sensor rather than from GenericAsset * Resolve merge conflict * Refactor after resolving merge conflict * Adjust attribute copying in Market.__init__ * Adjust attribute copying in WeatherSensor.__init__ * Post-merge cherry-pick: Set default attributes on generic assets, too * Post-merge cherry-pick: Add clarity to method docstring * Introduce has_attribute and set_attribute on the Sensor class, too * Remove redundant import * Get attributes from Sensor * Simplify (as requested in PR review) * Add docstring to migration util functions, explaining their parameters * Add module docstring * Add todos * Make Sensor attributes mutable, too * Avoid assumptions on db type (specifically, postgres) * Update upgrade migration docstring * Deprecate use of Market class in api v1_1 * Separate setup of market types and markets for tests, otherwise we run into flush issues * Remove redundant copy, now that we initialize super() first * Fix bugs: work on kwargs before copying from it and move up initialization of super() * Increase the chance of identifying a unique sensor by just its name, if you also know the name of its generic asset type * Simplify API tests by removing the owner id from event-type entity addresses, as the server ignores this optional field anyways * Simplify API tests by removing the owner id from event-type entity addresses, as the server ignores this optional field anyways * Deprecate use of Asset class in v1_3 tests * Deprecate use of Asset class in v1_2 tests * Deprecate use of Asset class in v1 tests * Deprecate use of Market class in v1_1 tests * Fix merge errors * Update string status code * Rename legacy migration module Co-authored-by: Flix6x <Flix6x@users.noreply.github.com> Co-authored-by: F.N. Claessen <felix@seita.nl> Co-authored-by: Felix Claessen <30658763+Flix6x@users.noreply.github.com>
- Loading branch information
1 parent
9729401
commit 0792962
Showing
36 changed files
with
708 additions
and
466 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
This module is part of our data model migration (see https://github.com/SeitaBV/flexmeasures/projects/9). | ||
It will become obsolete when we deprecate the fm0 scheme for entity addresses. | ||
""" | ||
|
||
from typing import List, Optional, Union | ||
|
||
from flexmeasures.api.common.responses import ( | ||
deprecated_api_version, | ||
unrecognized_market, | ||
ResponseTuple, | ||
) | ||
from flexmeasures.data.models.generic_assets import GenericAsset, GenericAssetType | ||
from flexmeasures.data.models.time_series import Sensor | ||
|
||
|
||
def get_sensor_by_unique_name( | ||
sensor_name: str, generic_asset_type_names: Optional[List[str]] = None | ||
) -> Union[Sensor, ResponseTuple]: | ||
"""Search a sensor by unique name, returning a ResponseTuple if it is not found. | ||
Optionally specify a list of generic asset type names to filter on. | ||
This function should be used only for sensors that correspond to the old Market class. | ||
""" | ||
# Look for the Sensor object | ||
query = Sensor.query.filter(Sensor.name == sensor_name) | ||
if generic_asset_type_names is not None: | ||
query = ( | ||
query.join(GenericAsset) | ||
.join(GenericAssetType) | ||
.filter(GenericAssetType.name.in_(generic_asset_type_names)) | ||
.filter(GenericAsset.generic_asset_type_id == GenericAssetType.id) | ||
.filter(Sensor.generic_asset_id == GenericAsset.id) | ||
) | ||
sensor = query.all() | ||
if len(sensor) == 0: | ||
return unrecognized_market(sensor_name) | ||
elif len(sensor) > 1: | ||
return deprecated_api_version( | ||
f"Multiple sensors were found named {sensor_name}." | ||
) | ||
return sensor[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.