Skip to content

Commit

Permalink
Merge pull request #46 from TimOrme/fix_epa_cleanup
Browse files Browse the repository at this point in the history
Fix EPA cleanup timing
  • Loading branch information
TimOrme authored Jun 1, 2023
2 parents d7d307a + 64e0af8 commit 30cec36
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
5 changes: 5 additions & 0 deletions aqimon/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ async def clean_old(dbconn: databases.Database, retention_minutes: int) -> None:
values={"last_week_timestamp": last_week_timestamp},
)

await dbconn.execute(
"DELETE FROM epa_aqi_log WHERE event_time < :last_week_timestamp",
values={"last_week_timestamp": last_week_timestamp},
)


async def add_epa_read(
dbconn: databases.Database,
Expand Down
6 changes: 3 additions & 3 deletions logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,57 @@ async def test_clean_old(database_conn):
current_time = datetime.now()
await database.add_read(database_conn, current_time - timedelta(hours=2), pm10=1, pm25=2)
await database.add_read(database_conn, current_time - timedelta(hours=4), pm10=2, pm25=3)

await database.add_epa_read(
database_conn,
current_time - timedelta(hours=2),
epa_aqi=1,
pollutant="test",
read_count=10,
oldest_read_time=datetime.now(),
)
await database.add_epa_read(
database_conn,
current_time - timedelta(hours=4),
epa_aqi=2,
pollutant="test",
read_count=10,
oldest_read_time=datetime.now(),
)

# These should be deleted
await database.add_read(database_conn, current_time - timedelta(hours=6), pm10=3, pm25=4)
await database.add_read(database_conn, current_time - timedelta(hours=8), pm10=4, pm25=5)

await database.add_epa_read(
database_conn,
current_time - timedelta(hours=6),
epa_aqi=3,
pollutant="test",
read_count=10,
oldest_read_time=datetime.now(),
)
await database.add_epa_read(
database_conn,
current_time - timedelta(hours=8),
epa_aqi=4,
pollutant="test",
read_count=10,
oldest_read_time=datetime.now(),
)

await database.clean_old(database_conn, retention_minutes=(60 * 4) + 30)

result = await database.get_all_reads(database_conn, lookback=None)
assert len(result) == 2
assert result[0].event_time == (current_time - timedelta(hours=4)).replace(microsecond=0)
assert result[1].event_time == (current_time - timedelta(hours=2)).replace(microsecond=0)

result2 = await database.get_all_epa_aqis(database_conn, lookback=None)
assert len(result2) == 2
assert result2[0].event_time == (current_time - timedelta(hours=4)).replace(microsecond=0)
assert result2[1].event_time == (current_time - timedelta(hours=2)).replace(microsecond=0)


@pytest.mark.asyncio
async def test_add_read(database_conn):
Expand Down

0 comments on commit 30cec36

Please sign in to comment.