Skip to content

Commit 3d89fe3

Browse files
committed
DB function to read last 24hr data
1 parent 6307e11 commit 3d89fe3

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ venv
33
.idea
44
__pycache__
55
server_metrics.db
6-
example.db
6+
example.db
7+
testgraph.py
8+
chart.png

app/core/bot.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
'''
2+
VALUES TO CHANGE WHEN IMPLEMENTING SETTINGS:
3+
readb.py > limit in read 24hr of data
4+
clock.py > day_cycle_clock() time
5+
clock.py > five_minute_clock trigger interval
6+
'''
7+
18
import asyncio
29
import os
310
from aiogram import Bot, Dispatcher

app/database/readb.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
LIMIT 1;
3030
'''
3131

32+
LATEST_24HR_ROW_SQL = '''
33+
SELECT timestamp, mem_total, mem_used, mem_prcnt, cpu_prcnt, cpu_freq, cpu_temp
34+
FROM Metrics
35+
ORDER BY timestamp DESC
36+
LIMIT 288;
37+
'''
38+
3239
def _read_latest_sync():
3340
with sqlite3.connect(DB_PATH) as connection:
3441
connection.row_factory = sqlite3.Row
@@ -38,6 +45,19 @@ def _read_latest_sync():
3845
row = cursor.fetchone()
3946
return dict(row) if row else None
4047

48+
def _read_latest_sync_24hr():
49+
with sqlite3.connect(DB_PATH) as connection:
50+
connection.row_factory = sqlite3.Row
51+
cursor = connection.cursor()
52+
cursor.execute(CREATE_TABLE_SQL)
53+
cursor.execute(LATEST_24HR_ROW_SQL)
54+
rows = cursor.fetchall()
55+
return [dict(row) for row in rows] if rows else None
56+
4157
async def read_latest():
4258
loop = asyncio.get_event_loop()
4359
return await loop.run_in_executor(_executor, _read_latest_sync)
60+
61+
async def read_latest_24hr():
62+
loop = asyncio.get_event_loop()
63+
return await loop.run_in_executor(_executor, _read_latest_sync_24hr)

0 commit comments

Comments
 (0)