Skip to content

Commit

Permalink
Fix rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
TimOrme committed Apr 24, 2023
1 parent 8922052 commit c5e77ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion aqimon/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ async def get_averaged_reads(dbconn: databases.Database, lookback_to: datetime)
lookback = int(lookback_to.timestamp())
result = await dbconn.fetch_one(
"SELECT "
"AVG(pm25) as avg_pm25, AVG(pm10) as avg_pm10, COUNT(*) as count, MIN(event_time) as oldest_time "
"ROUND(AVG(pm25), 2) as avg_pm25, ROUND(AVG(pm10), 2) as avg_pm10, "
"COUNT(*) as count, "
"MIN(event_time) as oldest_time "
"FROM read_log "
"WHERE (event_time >= :lookback) OR "
"(event_time = (SELECT MAX(event_time) FROM read_log WHERE event_time <= :lookback)) ORDER BY event_time ASC",
Expand Down
15 changes: 15 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ async def test_get_averaged_reads(database_conn):
assert result.oldest_read_time == current_time - timedelta(hours=8)


@pytest.mark.asyncio
async def test_get_averaged_reads_rounding(database_conn):
# Add reads every two hours
current_time = datetime(2020, 1, 1, 12, 0, 0)
lookback_to = current_time - timedelta(hours=8)
await database.add_read(database_conn, current_time - timedelta(hours=2), pm10=2, pm25=2)
await database.add_read(database_conn, current_time - timedelta(hours=4), pm10=4, pm25=3)
await database.add_read(database_conn, current_time - timedelta(hours=6), pm10=4, pm25=3)

result = await database.get_averaged_reads(database_conn, lookback_to)
assert result.count == 3
assert result.avg_pm10 == 3.33
assert result.avg_pm25 == 2.67


@pytest.mark.asyncio
async def test_get_averaged_reads_looks_past(database_conn):
current_time = datetime(2020, 1, 1, 12, 0, 0)
Expand Down

0 comments on commit c5e77ac

Please sign in to comment.