-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
103 lines (90 loc) · 3.21 KB
/
Copy pathdb.py
File metadata and controls
103 lines (90 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import sqlite3
class SQLiteHandler:
DB_FILE = "system_data.db"
@staticmethod
def init_db():
conn = sqlite3.connect(SQLiteHandler.DB_FILE)
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS system_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
time TEXT,
cpu REAL,
memory REAL,
gpu REAL,
network_kb REAL
)
""")
c.execute("""
CREATE TABLE IF NOT EXISTS user_settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
setting_name TEXT UNIQUE,
setting_value TEXT
)
""")
c.execute("""
CREATE TABLE IF NOT EXISTS hardware_info (
id INTEGER PRIMARY KEY AUTOINCREMENT,
cpu_name TEXT,
gpu_name TEXT,
ram_size_gb REAL,
os_name TEXT
)
""")
conn.commit()
conn.close()
@staticmethod
def insert_usage(data):
conn = sqlite3.connect(SQLiteHandler.DB_FILE)
c = conn.cursor()
c.execute("""
INSERT INTO system_usage (time, cpu, memory, gpu, network_kb)
VALUES (?, ?, ?, ?, ?)
""", (data["time"], data["cpu"], data["memory"], data["gpu"], data["network_kb"]))
conn.commit()
conn.close()
@staticmethod
def fetch_all_usage():
conn = sqlite3.connect(SQLiteHandler.DB_FILE)
c = conn.cursor()
c.execute("SELECT time, cpu, memory, gpu, network_kb FROM system_usage ORDER BY id")
rows = c.fetchall()
conn.close()
return [{"time": r[0], "cpu": r[1], "memory": r[2], "gpu": r[3], "network_kb": r[4]} for r in rows]
@staticmethod
def insert_setting(name, value):
conn = sqlite3.connect(SQLiteHandler.DB_FILE)
c = conn.cursor()
c.execute("""
INSERT INTO user_settings (setting_name, setting_value)
VALUES (?, ?)
ON CONFLICT(setting_name) DO UPDATE SET setting_value = excluded.setting_value
""", (name, value))
conn.commit()
conn.close()
@staticmethod
def fetch_settings():
conn = sqlite3.connect(SQLiteHandler.DB_FILE)
c = conn.cursor()
c.execute("SELECT setting_name, setting_value FROM user_settings")
rows = c.fetchall()
conn.close()
return [{"name": r[0], "value": r[1]} for r in rows]
@staticmethod
def insert_hardware(cpu_name, gpu_name, ram_size_gb, os_name):
conn = sqlite3.connect(SQLiteHandler.DB_FILE)
c = conn.cursor()
c.execute("""
INSERT INTO hardware_info (cpu_name, gpu_name, ram_size_gb, os_name)
VALUES (?, ?, ?, ?)
""", (cpu_name, gpu_name, ram_size_gb, os_name))
conn.commit()
conn.close()
@staticmethod
def fetch_hardware():
conn = sqlite3.connect(SQLiteHandler.DB_FILE)
c = conn.cursor()
c.execute("SELECT cpu_name, gpu_name, ram_size_gb, os_name FROM hardware_info")
rows = c.fetchall()
conn.close()
return [{"cpu": r[0], "gpu": r[1], "ram": r[2], "os": r[3]} for r in rows]