Skip to content

Commit

Permalink
#18 added alembic migration to add hourly_avg view
Browse files Browse the repository at this point in the history
  • Loading branch information
Nik Sauer committed Nov 7, 2024
1 parent c9b2592 commit 5d1fccc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
32 changes: 32 additions & 0 deletions code/alembic/versions/8e1c45de1275_create_hourly_avg_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""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, 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;""")



def downgrade() -> None:
op.execute("drop view if exists hourly_avg")
2 changes: 1 addition & 1 deletion code/services/data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ def import_station_data(db: Session, station_data, sensors):
db.commit()
logging.debug("Alle Daten wurden erfolgreich in die Datenbank geschrieben.")

return {"status": "success"}
return {"status": "success"}
2 changes: 1 addition & 1 deletion code/services/hourly_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def calculate_hourly_average(station_id: int, db: Session):
)
db.add(hourly_avg)

db.commit()
db.commit()

0 comments on commit 5d1fccc

Please sign in to comment.