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

Fix EPA cleanup timing #46

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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