Files
cksd/services/statistics.py

49 lines
1.1 KiB
Python

import time
import aiosqlite
from database import get_conn
async def get_ammeter_left_ele(limit=24):
async with get_conn() as conn:
conn.row_factory = aiosqlite.Row
cursor = await conn.execute(
"""
SELECT created_at, left_ele
FROM ammeter_usage
ORDER BY created_at DESC
LIMIT ?
""",
(limit,),
)
rows = await cursor.fetchall()
rows.reverse()
times = [time.strftime("%H:%M", time.localtime(r[0] / 1000)) for r in rows]
values = [r[1] for r in rows]
return times, values
async def get_water_left(limit=24):
async with get_conn() as conn:
conn.row_factory = aiosqlite.Row
cursor = await conn.execute(
"""
SELECT created_at, left_water
FROM water_usage
ORDER BY created_at DESC
LIMIT ?
""",
(limit,),
)
rows = await cursor.fetchall()
rows.reverse()
times = [time.strftime("%H:%M", time.localtime(r[0] / 1000)) for r in rows]
values = [r[1] for r in rows]
return times, values