-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcedar_sqlite.py
executable file
·371 lines (272 loc) · 8.4 KB
/
cedar_sqlite.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import sqlite3
from pprint import pprint
import time
import json
import cedar_timestamp
from cedar_timestamp import ts_now
from cedar_timestamp import ts_jpg
from cedar_timestamp import ts_from_float
from cedar_vars import cedar_vars_json_obj
cj = cedar_vars_json_obj()
def sqlite_connect():
conn = sqlite3.connect("cedar_sqlite.db")
# print("sqlite_connect() conn.total_changes: %s" % conn.total_changes)
return conn
def drop_create_log_table():
conn = sqlite_connect()
cursor = conn.cursor()
sql = "DROP TABLE IF EXISTS log;"
print(sql)
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
sql = "CREATE TABLE IF NOT EXISTS log ("
sql += "id INTEGER PRIMARY KEY AUTOINCREMENT, "
sql += "fa TEXT NOT NULL, " # found, alert, or exception
sql += "name TEXT NOT NULL, "
sql += "conf TEXT NOT NULL, "
sql += "weights TEXT NOT NULL, "
sql += "source TEXT NOT NULL, "
sql += "save_path TEXT NOT NULL, "
sql += "ts TEXT NOT NULL, "
sql += "email_ts TEXT NOT NULL DEFAULT 'email_none', "
sql += "sms_ts TEXT NOT NULL DEFAULT 'sms_none', "
sql += "exception TEXT NOT NULL DEFAULT 'exception_none'"
sql += ");"
print(sql)
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
conn.commit()
sql = "SELECT name FROM sqlite_schema WHERE type ='table' AND name NOT LIKE 'sqlite_%';"
print(sql)
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
sql = "SELECT sql FROM sqlite_schema WHERE name='log';"
print(sql)
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
return results
def query(sql):
conn = sqlite_connect()
cursor = conn.cursor()
sql = "SELECT sql FROM sqlite_schema WHERE name='log';"
print(sql)
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
return results
def log_add(jstr):
if cj['disable_sqlite3']:
return
conn = sqlite_connect()
cursor = conn.cursor()
#print("log_add() jstr: %s" % jstr)
j = json.loads(jstr)
print("log_add() j: %s" % j)
fa = "found"; # default
if '"found"' in jstr:
fa = "found"
elif '"alert"' in jstr:
fa = "alert"
elif '"success"' in jstr: # look for success first because exception always exists
fa = "success"
elif '"exception"' in jstr:
fa = "exception"
else:
print("ERROR: fs not found")
sql = "INSERT INTO LOG "
sql += "(fa, name, conf, weights, source, save_path, ts, email_ts, sms_ts, exception) "
sql += "VALUES ("
# fa
sql += "'" + fa + "', "
# name
if fa == "found":
sql += "'" + j['found'] + "', "
elif fa == "alert":
sql += "'" + j['alert'] + "', "
elif "exception" in j:
sql += "'" + j['exception'] + "', "
elif "success" in j:
sql += "'" + j['success'] + "', "
sql += "'" + j['conf'] + "', "
sql += "'" + j['weights'] + "', "
sql += "'" + j['source'] + "', "
sql += "'" + j['save_path'] + "', "
sql += "'" + j['ts'] + "', "
sql += "'" + j['email_ts'] + "', "
sql += "'" + j['sms_ts'] + "', "
sql += "'" + j['exception'] + "'"
sql += ");"
print("log_add() sql: %s" % sql)
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
conn.commit()
# sql = "SELECT count(*) FROM log;"
# #print("log_add() sql: %s" % sql)
# cursor.execute(sql)
# results = cursor.fetchall()
# pprint(results)
# sql = "SELECT * FROM log WHERE source = '" + j['source'] + "';"
# print("log_add() sql: %s" % sql)
# cursor.execute(sql)
# results = cursor.fetchall()
# pprint(results)
return results
def log_update_email_sms(jstr):
conn = sqlite_connect()
cursor = conn.cursor()
print("log_update_email_sms() jstr: %s" % jstr)
j = json.loads(jstr)
print("log_update_email_sms() j: %s" % j)
ts_day_ago = float(time.time()) - 24*60*60
sql = "UPDATE log SET "
sql += "ts = '" + j['email_ts'] + "', "
sql += "sms_ts = '" + j['sms_ts'] + "' "
sql += "WHERE "
sql += "fa = 'alert' AND "
sql += "email_ts = 'email_not_sent' AND "
sql += "sms_ts = 'sms_not_sent';"
print("log_update_email_sms() sql: %s" % sql)
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
conn.commit()
return results
def log_found_hours(hours):
conn = sqlite_connect()
cursor = conn.cursor()
start = float(time.time()) - hours * 60 * 60
ts_start = ts_from_float(start)
sql = "SELECT * FROM log WHERE "
sql += "fa = 'found' AND "
sql += "ts > '" + ts_start + "' AND ";
sql += "ts < '" + ts_now() + "' "
sql += "ORDER BY ts;"
#print("log_found_hours() sql: %s" % sql)
cursor.execute(sql)
results = cursor.fetchall()
#pprint(results)
#print("log_found_hours() sql: %s" % sql)
sql = "SELECT count(*) FROM log WHERE "
sql += "fa = 'found' AND "
sql += "ts > '" + ts_start + "' AND ";
sql += "ts < '" + ts_now() + "' "
sql += "ORDER BY ts;"
#print("log_found_hours() sql: %s" % sql)
cursor.execute(sql)
count = cursor.fetchall()
#pprint(count)
return {"fa":"found", "count":count, "results":results}
def log_alert_hours(hours):
conn = sqlite_connect()
cursor = conn.cursor()
start = float(time.time()) - hours * 60 * 60
ts_start = ts_from_float(start)
sql = "SELECT * FROM log WHERE "
sql += "fa = 'alert' AND "
sql += "ts > '" + ts_start + "' AND ";
sql += "ts < '" + ts_now() + "' "
sql += "ORDER BY ts DESC;"
#print("log_alert_hours() hours: %s, sql: %s" % (hours, sql))
cursor.execute(sql)
results = cursor.fetchall()
#pprint(results)
#print("log_alert_hours() hours() type(results): %s" % type(results))
#print("log_alert_hours() hours: %s, sql: %s" % (hours, sql))
sql = "SELECT count(*) FROM log WHERE "
sql += "fa = 'alert' AND "
sql += "ts > '" + ts_start + "' AND ";
sql += "ts < '" + ts_now() + "' "
sql += "ORDER BY ts DESC;"
#print("log_alert_hours() hours: %s, sql: %s" % (hours, sql))
cursor.execute(sql)
count = cursor.fetchall()
#pprint(count)
return {"fa":"alert", "count":count, "results":results}
def log_exception_hours(hours):
conn = sqlite_connect()
cursor = conn.cursor()
start = float(time.time()) - hours * 60 * 60
ts_start = ts_from_float(start)
sql = "SELECT * FROM log WHERE "
sql += "fa = 'exception' AND "
sql += "ts > '" + ts_start + "' AND ";
sql += "ts < '" + ts_now() + "' "
sql += "ORDER BY ts;"
#print("log_exception_hours() sql: %s" % sql)
cursor.execute(sql)
results = cursor.fetchall()
#pprint(results)
#print("log_exception_hours() sql: %s" % sql)
sql = "SELECT count(*) FROM log WHERE "
sql += "fa = 'exception' AND "
sql += "ts > '" + ts_start + "' AND ";
sql += "ts < '" + ts_now() + "' "
sql += "ORDER BY ts;"
#print("log_exception_hours() sql: %s" % sql)
cursor.execute(sql)
count = cursor.fetchall()
#pprint(count)
#print("log_exception_hours() sql: %s" % sql)
return {"fa":"exception", "count":count, "results":results}
def log_success_hours(hours):
conn = sqlite_connect()
cursor = conn.cursor()
start = float(time.time()) - hours * 60 * 60
ts_start = ts_from_float(start)
sql = "SELECT * FROM log WHERE "
sql += "fa = 'success' AND "
sql += "ts > '" + ts_start + "' AND ";
sql += "ts < '" + ts_now() + "' "
sql += "ORDER BY ts;"
#print("log_success_hours() sql: %s" % sql)
cursor.execute(sql)
results = cursor.fetchall()
#pprint(results)
#print("log_success_hours() sql: %s" % sql)
sql = "SELECT count(*) FROM log WHERE "
sql += "fa = 'success' AND "
sql += "ts > '" + ts_start + "' AND ";
sql += "ts < '" + ts_now() + "' "
sql += "ORDER BY ts;"
#print("log_success_hours() sql: %s" % sql)
cursor.execute(sql)
count = cursor.fetchall()
#pprint(count)
#print("log_success_hours() sql: %s" % sql)
return {"fa":"success", "count":count, "results":results}
def log_select_all():
conn = sqlite_connect()
cursor = conn.cursor()
sql = "SELECT * FROM log;"
print("log_select_all() sql: %s" % sql)
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
return resuls
def sqlite_delete_old_rows():
conn = sqlite_connect()
cursor = conn.cursor()
sql = "DELETE FROM log "
sql += "WHERE id IN "
sql += "(SELECT id FROM log ";
sql += "ORDER BY id DESC "
sql += f"LIMIT -1 OFFSET {cj['cedar_sqlite3_rows_max']});"
print("sqlite_delete_old_rows() sql: %s" % sql)
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
conn.commit()
return results
if __name__ == "__main__":
drop_create_log_table() # for test or reset only
# sqlite_delete_old_rows()
# log_select_all()
# pprint(log_found_hours(24))
# pprint(log_success_hours(24))
# pprint(log_alert_hours(24))
# pprint(log_exception_hours(24))