-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbotData.py
93 lines (83 loc) · 3.23 KB
/
botData.py
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
import os
import sqlite3
import time
class BotData:
def __init__(self):
self.data = {
'timing': [],
'cpu': [],
'mem': [],
'temp': []
}
self.db = sqlite3.connect(os.path.dirname(os.path.abspath(__file__))
+ "/data", check_same_thread=False)
try:
self.cursor = self.db.cursor()
self.cursor.execute('''CREATE TABLE IF NOT EXISTS \
stats(timing INTEGER PRIMARY KEY, \
cpu INTEGER, mem INTEGER, temp INTEGER)''')
self.db.commit()
except sqlite3.OperationalError:
print("database already exist")
self.charges()
self.show()
def charges(self):
self.cursor.execute('''SELECT timing, cpu, mem, temp FROM stats''')
all_rows = self.cursor.fetchall()
for row in all_rows:
self.data['timing'].append(row[0])
self.data['cpu'].append(row[1])
self.data['mem'].append(row[2])
self.data['temp'].append(row[3])
def populate(self):
i = 0
for t in self.data['timing']:
try:
print(("adding ", self.data['timing'][i]))
self.cursor.execute('''INSERT INTO stats(timing, cpu, mem, temp)
VALUES(?,?,?,?)''',
(int(self.data['timing'][i]),
int(self.data['cpu'][i]),
int(self.data['mem'][i]),
int(self.data['temp'][i])))
self.db.commit()
i += 1
except sqlite3.IntegrityError:
print(('Record already exists : ', self.data['timing']))
def show(self):
dataSize = len(self.data['timing'])
for idx in range(dataSize):
print(f"timing : {self.data['timing'][idx]} - ",
f"cpu : {self.data['cpu'][idx]} - ",
f"mem : {self.data['mem'][idx]} - ",
f"temp : {self.data['temp'][idx]}°C")
def appendData(self, cpu, mem, temp):
t = round(time.time())
self.data['timing'].append(t)
self.data['cpu'].append(round(cpu))
self.data['mem'].append(round(mem))
self.data['temp'].append(round(temp))
try:
self.cursor.execute('''INSERT INTO stats(timing, cpu, mem, temp)
VALUES(?,?,?,?)''', (t, cpu, mem, temp))
self.db.commit()
except sqlite3.IntegrityError:
print('Record already exists')
def getfromData(self, date):
self.cursor.execute('''SELECT timing, cpu, mem, \
temp FROM stats WHERE timing=?''', (date,))
data = self.cursor.fetchone()
return data[1], data[2], data[3]
def getFirstData(self):
self.cursor.execute('''SELECT timing FROM stats''')
data = self.cursor.fetchone()
return data[0]
def flushData(self):
self.cursor.execute('''DROP stats''')
self.data = {
'timing': [],
'cpu': [],
'mem': [],
'temp': []
}
return "Data are deleted !"