-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_queries.py
197 lines (159 loc) · 4.27 KB
/
sql_queries.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# TODO: come up with a way to log database errors?
import sqlite3
from datetime import datetime
PROG_CHR = "*"
MISS_CHR = "-"
DB_NAME = 'boga.db'
# Create database on startup
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
cursor.execute((
"CREATE TABLE IF NOT EXISTS costs("
" user_id INTEGER,"
" date TEXT,"
" type TEXT,"
" cost REAL"
")"
))
conn.commit()
cursor.execute((
"CREATE INDEX IF NOT EXISTS date_index ON costs (date)"
))
conn.commit()
cursor.execute((
"CREATE INDEX IF NOT EXISTS user_id_index ON costs (user_id)"
))
conn.commit()
cursor.execute((
"CREATE TABLE IF NOT EXISTS usage("
" command TEXT,"
" count INTEGER"
")"
))
conn.commit()
cursor.close()
conn.close()
def log_command(command: str):
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
cursor.execute((
"SELECT command\n"
"FROM usage\n"
"WHERE command=?"),
(command,)
)
rows = cursor.fetchall()
if len(rows) == 0:
cursor.execute((
"INSERT INTO usage(\n"
" command,\n"
" count\n"
") VALUES(?, 0)"
), (command, )
)
conn.commit()
cursor.execute((
"UPDATE usage\n"
"SET\n"
" count = count + 1\n"
"WHERE command=?"
), (command,)
)
conn.commit()
cursor.close()
conn.close()
def get_command_usage():
try:
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
cursor.execute((
"SELECT *\n"
"FROM usage\n"
"ORDER BY\n"
" count DESC,\n"
" command ASC\n"
)
)
rows = cursor.fetchall()
cursor.execute((
"SELECT \n"
" SUM(count)\n"
"FROM usage"
)
)
total = cursor.fetchall()[0][0]
conn.close()
res = "```"
for row in rows:
percent = round((row[1] / total) * 100, 2)
progress = round((row[1] / total) * 10)
progress_bar = (PROG_CHR * progress) + (MISS_CHR * (10 - progress))
res += "/{:15} [{}] {}%\n".format(row[0], progress_bar, str(percent))
res += "```"
return res
except:
return "There was an issue generating usage, please try again later."
def log_cost(user_id: int, type: str, cost: float):
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
cursor.execute((
"INSERT INTO costs(\n"
" user_id,\n"
" date,\n"
" type,\n"
" cost\n"
") VALUES(?, ?, ?, ?)"
), (user_id, str(datetime.now()), type, cost)
)
conn.commit()
cursor.close()
conn.close()
def generate_user_bill(user_id: int, month=None, year=None):
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
start_date = datetime(year=year, month=month, day=1)
end_month = month + 1 if month != 12 else 1
end_year = year + 1 if end_month == 1 else year
end_date = datetime(year=end_year, month=end_month, day=1)
cursor.execute((
"SELECT\n"
" sum(cost)\n"
"FROM costs\n"
"WHERE user_id=?\n"
"AND date >=?\n"
"AND date <=?\n"
), (user_id, start_date, end_date)
)
rows = cursor.fetchall()
cursor.close()
conn.close()
total_cost = rows[0][0] if rows[0][0] is not None else 0
return "<@!{}> owes `${}` for `{}/{}`".format(user_id, total_cost, month, year)
def generate_statement():
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
cursor.execute((
"SELECT\n"
" user_id,\n"
" sum(cost)\n"
"FROM costs\n"
"GROUP BY \n"
" user_id\n"
"ORDER BY\n"
" sum(cost) DESC,\n"
" user_id"
)
)
rows = cursor.fetchall()
cursor.close()
conn.close()
if len(rows) == 0:
return "Nobody has used the bot yet"
res = "# Statement Summary starting from 04/2024\n\n"
total = 0
for row in rows:
tmp = "<@!{}> owes `${}`\n".format(row[0], row[1])
res += tmp
total += row[1]
res += "\nTotal: `${}`".format(total)
return res