-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from luftdaten-at/18-test-background-task-to-c…
…alculate-hourly-averages 18 test background task to calculate hourly averages
- Loading branch information
Showing
9 changed files
with
198 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""test exclude view | ||
Revision ID: 06c69449c7d0 | ||
Revises: 8e1c45de1275 | ||
Create Date: 2024-11-08 13:00:41.414103 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '06c69449c7d0' | ||
down_revision: Union[str, None] = '8e1c45de1275' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('stationStatus', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('station_id', sa.Integer(), nullable=True), | ||
sa.Column('timestamp', sa.DateTime(), nullable=True), | ||
sa.Column('level', sa.Integer(), nullable=True), | ||
sa.Column('message', sa.String(), nullable=True), | ||
sa.ForeignKeyConstraint(['station_id'], ['stations.id'], ), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.create_index(op.f('ix_stationStatus_id'), 'stationStatus', ['id'], unique=False) | ||
op.drop_index('ix_StationStatus_id', table_name='StationStatus') | ||
op.drop_table('StationStatus') | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('StationStatus', | ||
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False), | ||
sa.Column('station_id', sa.INTEGER(), autoincrement=False, nullable=True), | ||
sa.Column('timestamp', postgresql.TIMESTAMP(), autoincrement=False, nullable=True), | ||
sa.Column('level', sa.INTEGER(), autoincrement=False, nullable=True), | ||
sa.Column('message', sa.VARCHAR(), autoincrement=False, nullable=True), | ||
sa.ForeignKeyConstraint(['station_id'], ['stations.id'], name='StationStatus_station_id_fkey'), | ||
sa.PrimaryKeyConstraint('id', name='StationStatus_pkey') | ||
) | ||
op.create_index('ix_StationStatus_id', 'StationStatus', ['id'], unique=False) | ||
op.drop_index(op.f('ix_stationStatus_id'), table_name='stationStatus') | ||
op.drop_table('stationStatus') | ||
# ### end Alembic commands ### |
33 changes: 33 additions & 0 deletions
33
code/alembic/versions/8e1c45de1275_create_hourly_avg_view.py
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,33 @@ | ||
"""create hourly_avg view | ||
Revision ID: 8e1c45de1275 | ||
Revises: 5516d2dd3e78 | ||
Create Date: 2024-11-07 14:21:42.941381 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '8e1c45de1275' | ||
down_revision: Union[str, None] = '5516d2dd3e78' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.execute("""create view hourly_avg as | ||
select station_id, hour, jsonb_object_agg(dimension, avg) as dimension_avg | ||
from (select station_id, dimension, date_trunc('hour', time_measured) as hour, avg(value) | ||
from measurements as m join values as v on v.measurement_id = m.id | ||
join stations as s on s.id = m.station_id | ||
group by station_id, dimension, hour | ||
order by station_id, dimension, hour) | ||
group by station_id, hour;""") | ||
|
||
|
||
def downgrade() -> None: | ||
op.execute("drop view if exists hourly_avg") |
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 |
---|---|---|
|
@@ -26,4 +26,4 @@ def calculate_hourly_average(station_id: int, db: Session): | |
) | ||
db.add(hourly_avg) | ||
|
||
db.commit() | ||
db.commit() |
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