-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.py
executable file
·179 lines (147 loc) · 5.68 KB
/
matcher.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
#!/usr/bin/env python
"""
This is a refenrece implementation of a simple string matching algorithm
using python string matching. This is not about a fast implementation of
string matching but more a proof of concept for scalable and high-available
string matching using a scalable and high-available database
- Apache Cassandra -
"""
import argparse
from datetime import datetime
from subprocess import Popen, PIPE
import time
import traceback
from uuid import uuid1
import pycassa
import ujson as json
# import MySQLdb
def read_arguments():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--config', '-c', required=True)
p.add_argument('--container', '-t', required=True,
choices=['MemoryContainer', 'CassandraContainer'])
return p.parse_args()
class AbstractContainer(object):
''' Abstraction layer to interact with strings in a containers '''
def _load(self, key):
raise NotImplementedError
def _append(self, key, value, data):
raise NotImplementedError
def check(self, key, value):
data = self._load(key)
if value not in data:
data = self._append(key, value, data)
return data
# memory structure to match in memory
class MemoryContainer(AbstractContainer):
''' Abstraction layer to interact with strings in a containers '''
def __init__(self):
self.container = {}
def _load(self, key):
if key not in self.container:
self.container[key] = list()
return self.container[key]
def _append(self, key, value, data):
self.container[key].append(value)
return self.container[key]
class MysqlContainer(object):
''' Abstraction layer to interact with strings in a containers '''
def __init__(self, mysql_opts):
self.con = MySQLdb.connection(**mysql_opts)
def _load(key):
c = self.con.cursor()
c.execute(" SELECT time_uuid, value FROM ids WHERE key = ? ", [key])
data = list()
for (_, value) in c.fetchall():
data.append(tuple(json.loads(value)))
return data
def _append(key, value, data):
data.append(value)
time_uuid = uuid.uuid1().bytes.encode('base64').rstrip('=\n').replace('/', '_')
c.execute(" INSERT INTO ids (key, time_uuid, value) VALUES (?, ?, ?) ", [key, time_uuid, json.dumps(value)])
return data
class CassandraContainer(AbstractContainer):
''' Abstraction layer to interact with strings in a containers '''
def __init__(self, keyspace, server_list, cf):
self.pool = pycassa.pool.ConnectionPool(keyspace, server_list, timeout=None)
self.con = pycassa.columnfamily.ColumnFamily(self.pool, cf)
def _load(self, key):
data = list()
for _, value in self.con.xget(key):
data.append(tuple(json.loads(value)))
return data
def _append(self, key, value, data):
data.append(value)
time_uuid = pycassa.util.convert_time_to_uuid(datetime.utcnow())
self.con.insert(key, {time_uuid: json.dumps(value)})
return data
def x_read_events(files):
''' Reads the events '''
for filename in files:
with open(filename) as f:
for line in f:
try:
yield json.loads(line)
except Exception as _e:
print 'ERR:', _e
class Matcher(object):
"""
Class to match incoming events against each other
"""
def __init__(self, container, tracked_ids, hex_lens):
self.container = container
self.tracked_ids = tracked_ids
self.hex_lens = hex_lens
def ignore_event(self, event):
if 'campaign' in event and int(event['campaign']) in [1,2]:
return True
def get_tracked_fields(self, event):
fields = {}
# get all tracked fields
for tracked in self.tracked_ids:
id_value = event[tracked] if tracked in event else ''
# filter crappy ids
if len(id_value) not in self.hex_lens: continue
fields[tracked] = event[tracked]
return fields
def on_event(self, event):
try:
# filter test events
if self.ignore_event(event): return
# get the static fields to compare events later on
aid = event['actionId']
etype = int(event['TMEvent'])
etime = int(event['TMTimeEvent'])
data = (aid, etype, etime)
# get all tracked fields
tracked = self.get_tracked_fields(event)
# try to match
matches = []
match_type = 13 if etype == 14 else 14
for id_name, id_value in tracked.iteritems():
stored = data + (id_name,)
id_set = self.container.check(id_value, stored)
if len(id_set) > 1:
# find whether this was an event match
matches = [x for x in id_set if x[1] == match_type]
if matches:
print stored, id_value, matches
except Exception as _e:
print 'ERR:', _e
print traceback.format_exc()
def main(args):
# load config
with open(args.config) as f:
config = json.load(f)
# the container to store all the ids
container = globals()[args.container](**config[args.container])
# the matcher
matcher = Matcher(container, **config['matcher'])
for event in x_read_events(config['files']):
matcher.on_event(event)
if __name__ == '__main__':
t1 = time.time()
args = read_arguments()
main(args)
p = Popen(['ps aux | grep string_matching'], shell=True)
print time.time() - t1