-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
126 lines (89 loc) · 4.15 KB
/
database.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import sqlite3 # library for working with the database
# from colorama import init, Fore, Style # library for colouring text in print
from datetime import datetime # library for recognising the current time
# init() # is used to colour text in the cmd
import matplotlib.pyplot as plt
import numpy as np
def sql_launch():
connection = sqlite3.connect('wolfram_database.db') # connecting to the database
cursor = connection.cursor()
# create tables, if they did not exist before (new file)
cursor.execute('''
CREATE TABLE IF NOT EXISTS message (
message TEXT,
name TEXT,
time TEXT,
id INT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS user (
name TEXT,
username TEXT,
id INTEGER PRIMARY KEY,
num_of_request INT,
first_request TEXT,
last_request TEXT
)
''')
connection.commit() # Save the changes to the database
connection.close() # close the database
def sql_user(name: str, username: str, user_id: int):
connection = sqlite3.connect('wolfram_database.db')
cursor = connection.cursor()
row = cursor.execute(f"SELECT * FROM user WHERE id = {user_id}").fetchall()
time = datetime.now().strftime("%d/%m/%Y")
if row is None or row == []:
print(row==[])
cursor.execute(f"INSERT INTO user(name, username, id, num_of_request, first_request, last_request) VALUES ('{name}', '{username}', {user_id}, 0, '{time}', '{time}')")
else:
cursor.execute(f'UPDATE user SET num_of_request = num_of_request+1 WHERE id = {user_id}')
"""
row = cursor.execute(f"SELECT * FROM user WHERE id = {user_id}").fetchall()
if name != row[0]: # [>>>name<<<, username, id, num_of_request, first_request, last_request]
cursor.execute(f'UPDATE user SET name = {name} WHERE id = {user_id}')
if username != row[1]: # [name, >>>username<<<, id, num_of_request, first_request, last_request]
cursor.execute(f'UPDATE user SET name = {name} WHERE id = {user_id}')
"""
connection.commit()
connection.close()
def sql_message(message: str, name: str, username: str, user_id: int) -> None:
connection = sqlite3.connect('wolfram_database.db')
cursor = connection.cursor()
sql_user(name, username, user_id)
time = datetime.now().strftime("%d.%m.%Y %H:%M")
cursor.execute("INSERT INTO message(message, name, time, id) VALUES (?, ?, ?, ?)", (message, name, time, user_id))
connection.commit()
connection.close()
def sql_statistic(file_name, admin):
# TODO: figure it out and retrieve data normally
connection = sqlite3.connect('wolfram_database.db')
cursor = connection.cursor()
time = datetime.now().strftime('%m.%Y')
recognition = len(cursor.execute(f"SELECT * FROM message WHERE time LIKE '%{time}' AND additionally LIKE '%Recognition%'").fetchall())
pictures = len(cursor.execute(f"SELECT * FROM message WHERE time LIKE '%{time}' AND additionally LIKE 'Pictures%'").fetchall())
text = len(cursor.execute(f"SELECT * FROM message WHERE time LIKE '%{time}' AND additionally LIKE 'Text%'").fetchall())
command = len(cursor.execute(f"SELECT * FROM message WHERE time LIKE '%{time}' AND additionally LIKE 'Command%'").fetchall())
fig, ax = plt.subplots(figsize=(8, 8))
wolfram = [pictures, text, 0, 0]
photo_recognition = [0, 0, recognition, 0]
command_list = [0, 0, 0, command]
labels = ['Command', 'Wolfram', 'Photo recognition']
legend = ['Pictures mode', 'Text mode', 'Recognition', 'Command']
data = [command_list, wolfram, photo_recognition]
# I don't know how it works.
data = np.array(data).T
positions = np.arange(len(labels)) + 1
bottom = np.zeros(len(labels))
for i, values in enumerate(data):
ax.bar(positions, values, bottom=bottom, label=legend[i])
bottom += values
ax.set_xticks(positions)
ax.set_xticklabels(labels)
ax.set_ylim(0, bottom.max() + 10)
ax.set_title('Total:' + str(recognition + pictures + text + command))
ax.legend()
plt.savefig(f'{file_name}.png')
plt.clf()
connection.close()
sql_launch()