Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First sensor ID upon a fresh db install is 2 instead of 1 #604

Closed
Flix6x opened this issue Mar 9, 2023 · 2 comments · Fixed by #534
Closed

First sensor ID upon a fresh db install is 2 instead of 1 #604

Flix6x opened this issue Mar 9, 2023 · 2 comments · Fixed by #534
Assignees
Labels
Data documentation Improvements or additions to documentation

Comments

@Flix6x
Copy link
Contributor

Flix6x commented Mar 9, 2023

Problem

I expect the first sensor created to be assigned id = 1. I seemed to have encountered an inconsistency in the starting value of the auto-increment. See tech spike below for details.

Possible resolution

  • Somehow try to set is_called to f when the auto-increment table is created (the generic asset table does not seem to have this issue, i.e. the first asset has id = 1, so that could be a hint) OR
  • We could advise to run flexmeasures db-ops reset (just after flexmeasures db upgrade) when setting up the database for the first time.

Tech spike

Set your flexmeasures.cfg:

SQLALCHEMY_DATABASE_URI = "postgresql://toy:toy@127.0.0.1/toy"

Create a fresh database (set password to 'toy'):

sudo -i -u postgres
createdb -U postgres toy
createuser --pwprompt -U postgres toy
exit
flexmeasures db upgrade

Let's check the sensor table, and specifically, its autoincrement:

psql -U toy --password -h 127.0.0.1 -d toy
select * from sensor;
select * from sensor_id_seq;
 last_value | log_cnt | is_called 
------------+---------+-----------
          1 |       0 | t
(1 row)

Notice the last value is 1, and it has been called already, so the next call will yield the value 2. Now add the toy account:

\q
flexmeasures add toy-account --kind battery

Then check the battery sensor ID, which is the first to be created. Is it 1 or 2? 2!

Now let's test again after resetting the database:

flexmeasures db-ops reset

And check the sensor table again:

psql -U toy --password -h 127.0.0.1 -d toy
select * from sensor;
select * from sensor_id_seq;
 last_value | log_cnt | is_called 
------------+---------+-----------
          1 |       0 | f
(1 row)

Notice the last value is 1, but it hasn't been called yet. The next call will yield the value 1. Now add the toy account again:

\q
flexmeasures add toy-account --kind battery

Is the battery 1 or 2? 1!

Clean up:

sudo -i -u postgres
dropdb toy
dropuser toy
exit
@Flix6x
Copy link
Contributor Author

Flix6x commented Mar 9, 2023

Suspected root cause

When calling flexmeasures db upgrade, one of the migrations actually sets the is_called value to true for the sensor_id_seq table. Specifically:
data/migrations/versions/a528c3c81506_unique_generic_sensor_ids.py.
It does so in the light of moving any present structural data from our old model into our new model (as part of the March 2021 data migration).

@Flix6x
Copy link
Contributor Author

Flix6x commented Apr 5, 2023

Suggested remedy

  • Update the relevant migration file to set is_called to false (line 206).
  • Make sure that after calling the migration's upgrade command, creating a new sensor does not fail (the first time) on a duplicate ID key violation.
  • Update sensor IDs in the documentation (at least the toy tutorial is heavily affected).

@Flix6x Flix6x added documentation Improvements or additions to documentation Data labels Apr 5, 2023
@Flix6x Flix6x self-assigned this Apr 5, 2023
Flix6x added a commit that referenced this issue Apr 9, 2023
Signed-off-by: F.N. Claessen <felix@seita.nl>
Flix6x added a commit that referenced this issue Apr 12, 2023
…context of a building with PV (#534)

Expand toy tutorial and chart functionality (incl. a more sensible use of the color encoding and data source attribute in charts), by:

- Expanding the toy tutorial with creation of solar sensor and solar forecasts from CSV
- Expanding the toy tutorial with a new schedule that takes into account solar
- Allowing nesting of sensors in the `sensors_to_show` attribute, to denote that multiple sensors should be shown together, and dealing with all of its implications (axis labels, use of colors, etc.)
- Visually distinguishing forecasts, schedules and measurements by source type rather than by belief horizon (incl. a simple db migration)
- Adding a CLI command to create a new DataSource (so that forecasts are visually presented as originating from a forecaster)



* fix charging sensor name in tutorial

Signed-off-by: F.N. Claessen <felix@seita.nl>

* update price sensor name in tutorial (prefer non-capitalized sensor names)

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Print out account ID if toy account already exists

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Fix for changed default when reading in timezone naive data: we no longer assume utc, but raise instead (see PR #521)

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add solar power sensor to toy account, and allow existing toy accounts to be updated by simply running `flexmeasures add toy-account` again

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Support JSON and Callable arguments

Signed-off-by: F.N. Claessen <felix@seita.nl>

* get or create price sensor

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Refactor to keep original order of sensor ids

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add CLI option to pass inflexible device sensors

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Expand tutorial with scheduling against solar, which limits the available headroom for the battery

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add note about resampling

Signed-off-by: F.N. Claessen <felix@seita.nl>

* black

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Allow nesting sensors_to_show to support layering multiple sensors in a single row within the multi-row asset chart

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Textual changes

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Remove print statement

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add missing timezone

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Fix call to make_schedule

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Ensure storage specs, also when make_schedule is called directly (i.e. no job)

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Legend shows which asset a sensor belongs to, too, and sensors that share a name don't share a color (note that sensors belonging to the same asset aren't allowed to have the same name)

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add CLI command for adding a data source

Signed-off-by: F.N. Claessen <felix@seita.nl>

* DB migration for source types 'forecaster' and 'scheduler'

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Attribute toy solar forecasts to a new DataSource of type 'forecaster'

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Check common cases of shared sensor types

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add version identifier and missing parenthesis

Signed-off-by: F.N. Claessen <felix@seita.nl>

* black and flake8

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Fix test (no legend in PositionFieldDef)

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Remove redundant detail encodings

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Upgrade timely-beliefs for required feature from SeitaBV/timely-beliefs#122

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Merge db revisions

Signed-off-by: F.N. Claessen <felix@seita.nl>

* black

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Speed up viz by avoiding redundant client-side transformation

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Bump vega-lite and vegaembed

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Ensure full-width bar width

Signed-off-by: F.N. Claessen <felix@seita.nl>

* black

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Update CLI scheduling command by adding `for-storage`

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Improve introduction to using `flexmeasures add source`

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Introduce follow-up solar example

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Refactor: rename "scheduling script" to "scheduler" and "forecasting script" to "forecaster"

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Clarify event start domain setting

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Raise instead of warn in case toy account already exists

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Clarify loop over kwargs

Signed-off-by: F.N. Claessen <felix@seita.nl>

* black

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Merge steps

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Clarify when we choose not to show the sensor name

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add return type annotations

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add default sensors_to_show attribute to test asset

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add test cases for bad sensors_to_show attribute

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Validate sensors_to_show attribute

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Simplify code lines

Signed-off-by: F.N. Claessen <felix@seita.nl>

* black

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Refactor: move util function

Signed-off-by: F.N. Claessen <felix@seita.nl>

* flake8

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Changelog entry plus upgrade instructions

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add documentation for nested sensors_to_show attribute

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Resolve import error

Signed-off-by: F.N. Claessen <felix@seita.nl>

* redundant space

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Resolve circular import

Signed-off-by: F.N. Claessen <felix@seita.nl>

* add a missing output style

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

* Fix GH Issue #604

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Correct spacing of header markings

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add sensors_to_show attribute to tutorial printout

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Correct sensor name in tutorial

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Correct column name in tutorial

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Shift sensor ids in tutorial

Signed-off-by: F.N. Claessen <felix@seita.nl>

* fix capitalization

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Update uniplot output (looks like later versions have a more intuitive choice of y-axis ticks)

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Correct data source IDs

Signed-off-by: F.N. Claessen <felix@seita.nl>

* test

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Revert "test"

This reverts commit b393cad.

* Resolve circular import coming to light through mypy

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Generalize util function

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add documentation for new CLI command

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add documentation for new CLI option

Signed-off-by: F.N. Claessen <felix@seita.nl>

---------

Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: Nicolas Höning <nicolas@seita.nl>
Co-authored-by: Nicolas Höning <nicolas@seita.nl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Data documentation Improvements or additions to documentation
Projects
None yet
1 participant