-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_query_interface.py
More file actions
66 lines (55 loc) · 2.17 KB
/
log_query_interface.py
File metadata and controls
66 lines (55 loc) · 2.17 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
import sqlite3
class LogQueryInterface:
def __init__(self, db_path):
self.db_path = db_path
self.create_table()
def get_connection(self):
return sqlite3.connect(self.db_path)
def create_table(self):
with self.get_connection() as conn:
query = '''
CREATE TABLE IF NOT EXISTS logs (
id INTEGER PRIMARY KEY,
level TEXT,
log_string TEXT,
timestamp TEXT,
source TEXT
)
'''
conn.execute(query)
conn.commit()
def insert_log(self, log_data):
with self.get_connection() as conn:
query = '''
INSERT INTO logs (level, log_string, timestamp, source)
VALUES (?, ?, ?, ?)
'''
conn.execute(query, (
log_data['level'],
log_data['log_string'],
log_data['timestamp'],
log_data['metadata']['source']
))
conn.commit()
def search_logs(self, filters):
with self.get_connection() as conn:
query = 'SELECT * FROM logs WHERE 1=1'
params = []
if 'level' in filters and filters['level']:
query += ' AND level = ?'
params.append(filters['level'])
if 'log_string' in filters and filters['log_string']:
query += ' AND log_string LIKE ?'
params.append(f"%{filters['log_string']}%")
if 'start_timestamp' in filters and filters['start_timestamp']:
query += ' AND timestamp >= ?'
params.append(filters['start_timestamp'])
if 'end_timestamp' in filters and filters['end_timestamp']:
query += ' AND timestamp <= ?'
params.append(filters['end_timestamp'])
if 'source' in filters and filters['source']:
query += ' AND source = ?'
params.append(filters['source'])
cursor = conn.execute(query, params)
logs = cursor.fetchall()
return logs