-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryReducer.py
32 lines (24 loc) · 1.33 KB
/
QueryReducer.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
import re
from myutils import parse_user_host
class QueryReducer:
def __init__(self, **kwargs):
self.ignore_queries = set(kwargs.get('ignore_queries') or [])
self.ignore_users = set(kwargs.get('ignore_users') or [])
self.unwanted_terms_re = re.compile('|'.join(kwargs.get('unwanted_terms') or []))
self.unwanted_starts_re = re.compile('|'.join('{0}.*'.format(x)
for x in (kwargs.get('unwanted_starts')
or [])))
def accept(self, user_host, query):
"""
@user_host - an entry from the 'user_host' column of the MySQL general_log
@query - an entry from the 'argument' column of the MySQL general_log
in a row where 'command_type' in ('Execute', 'Query'). Should
already be cleaned
Returns a tuple of (user, server, query) if the query should be stored
according to the rules of this QueryReducer, False otherwise
"""
user, server = parse_user_host(user_host)
if user in self.ignore_users or query in self.ignore_queries \
or self.unwanted_terms_re.search(query) or self.unwanted_starts_re.match(query):
return False
return user, server, query