This repository has been archived by the owner on Apr 12, 2018. It is now read-only.
/
sql.py
398 lines (361 loc) · 15 KB
/
sql.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import json
import time
import datetime
import uuid
import re
from . import StorageBase, StorageException, Base
from campaign.logger import LOG
from campaign.views import api_version
from sqlalchemy import (Column, Integer, String, Text,
text)
class Users(Base):
__tablename__ = 'users'
email = Column('email', String(100), primary_key=True)
id = Column('id', String(32), index=True)
sponsor = Column('sponsor', String(100))
time = Column('date', Integer)
level = Column('level', Integer)
class Campaign(Base):
__tablename__ = 'campaigns'
# Due to a miscommunication during design, the client was
# created requiring the 'id' to be a numeric. We use 'id' as
# a unique identifier string.
id = Column('id', String(25), primary_key=True)
priority = Column('priority', Integer, index=True)
specific = Column('specific', Integer, index=True)
channel = Column('channel', String(24), index=True, nullable=True)
version = Column('version', String(30), index=True, nullable=True)
product = Column('product', String(50), index=True, nullable=True)
platform = Column('platform', String(50), index=True, nullable=True)
lang = Column('lang', String(24), index=True, nullable=True)
locale = Column('locale', String(24), index=True, nullable=True)
start_time = Column('start_time', Integer, index=True)
end_time = Column('end_time', Integer, index=True, nullable=True)
idle_time = Column('idle_time', Integer, index=True, nullable=True)
note = Column('note', Text)
dest_url = Column('dest_url', Text)
author = Column('author', String(255), index=True)
created = Column('created', Integer, index=True)
title = Column('title', String(50))
status = Column('status', Integer)
class Scrapes(Base):
__tablename__ = 'scrapes'
id = Column('id', String(25), unique=True, primary_key=True)
served = Column('served', Integer, server_default=text('0'))
clicks = Column('clicks', Integer, server_default=text('0'))
last = Column('last', Integer, index=True, server_default=text('0'))
class CounterException(Exception):
pass
class Counter(StorageBase):
__database__ = 'campaign'
__tablename__ = 'scrapes'
def __init__(self, config, logger, **kw):
try:
super(Counter, self).__init__(config, **kw)
self.logger = logger
self._connect()
#TODO: add the most common index.
except Exception, e:
logger.log(msg='Could not initialize Storage "%s"' % str(e),
type='error', severity=LOG.CRITICAL)
raise e
def bulk_increment(self, conn, id, action, time=time.time()):
action = re.sub(r'[^0-9A-Za-z]', '', action)
try:
if (self.settings.get("db.type") == "sqlite"):
conn.execute(text("insert or ignore into " +
self.__tablename__ +
" (id)" +
" values (:id ); "),
{"id": id})
else:
dml = text("insert into " + self.__tablename__
+ " (id, %s) values (:id, 1) " % action
+ " on duplicate key update %s=%s+1, last=:last;"
% (action, action))
conn.execute(dml, {"id": id, "last": time})
except Exception, e:
self.logger.log(msg="Could not increment id: %s" % str(e),
type="error", severity=LOG.ERROR)
def increment(self, id, action, time):
with self.engine.begin() as conn:
self.bulk_increment(conn, id, action, time)
def fetched(self, data, time=time.time()):
with self.engine.begin() as conn:
for item in data:
self.bulk_increment(conn, item.get('token'), 'served', time)
def redir(self, data, time=time.time()):
self.increment(data.get('id'), 'clicks', time)
commands = {'redirect': redir,
'fetched': fetched}
def log(self, line):
for command in self.commands.keys():
if command + ' :' in line:
dt = datetime.strptime(line.split(',')[0],
'%Y-%m-%d %H:%M:%S')
timestamp = int(time.mktime(dt.timetuple()))
try:
data = json.loads(line.split(command + ' :')[1])
while (isinstance(data, basestring)):
data = json.loads(data)
self.commands.get(command)(self,
data,
timestamp)
except Exception, e:
self.logger.log(msg="Could not log %s" % str(e),
type="error", severity=LOG.ERROR)
raise e
def report(self, id):
with self.engine.begin() as conn:
resp = conn.execute(text(("select * from %s " %
self.__tablename__) +
"where id = :id"), {'id': id})
if resp.rowcount > 0:
result = resp.fetchone()
return dict(zip(resp.keys(), result))
else:
return {}
def parse(self, logfile):
try:
file = open(logfile, 'r')
for line in file:
self.log(line)
except Exception, e:
self.logger.log(msg="Could not parse %s" % str(e),
type="error", severity=LOG.ERROR)
pass
class Storage(StorageBase):
__database__ = 'campaign'
__tablename__ = 'campaigns'
def __init__(self, config, logger, **kw):
try:
super(Storage, self).__init__(config, **kw)
self.logger = logger
# Store off a link to the main table.
self.campaigns = Base.metadata.tables.get(Campaign.__tablename__)
self.users = Base.metadata.tables.get(Users.__tablename__)
self.scrapes = Base.metadata.tables.get(Scrapes.__tablename__)
self.counter = Counter(config, logger, **kw)
self._connect()
#TODO: add the most common index.
except Exception, e:
logger.log(msg='Could not initialize Storage "%s"' % str(e),
type='error', severity=LOG.CRITICAL)
raise e
def health_check(self):
try:
healthy = True
with self.engine.begin() as conn:
ins = self.campaigns.insert().values(
id="test",
product="test",
channel="test",
platform="test",
start_time=0,
end_time=0,
note="test",
dest_url="test",
author="test",
created=0)
conn.execute(ins)
sel = self.campaigns.select(self.campaigns.c.id == "test")
resp = conn.execute(sel)
rows = resp.fetchall()
if not len(rows):
healthy = False
conn.execute(self.campaigns.delete(self.campaigns.c.id ==
"test"))
except Exception, e:
import warnings
warnings.warn(str(e))
return False
return healthy
def resolve(self, token):
if token is None:
return None
sel = self.campaigns.select(self.campaigns.c.id == token)
items = self.engine.execute(sel)
row = items.fetchone()
if row is None:
return None
result = dict(zip(items.keys(), row))
return result
def put_announce(self, data, sessionHandle=None, now=None):
if sessionHandle:
session = sessionHandle
else:
session = self.Session()
if isinstance(data, list):
for item in data:
self.put_announce(item, session, now)
return self
if data.get('body') is None:
raise StorageException('Incomplete record. Skipping.')
specificity = 0
for col in ['lang', 'loc', 'platform',
'channel', 'version']:
if len(str(data.get(col, ''))):
specificity += 1
if data.get('idle_time') and int(data.get('idle_time')) != 0:
specificity += 1
data['specific'] = specificity
snip = self.normalize_announce(data, now)
campaign = Campaign(**snip)
session.add(campaign)
session.commit()
if not sessionHandle:
session.flush()
session.close()
return self
def get_announce(self, data, now=None):
# Really shouldn't allow "global" variables, but I know full well
# that they're going to want them.
params = {}
# The window allows the db to cache the query for the length of the
# window. This is because the database won't cache a query if it
# differs from a previous one. The timestamp will cause the query to
# not be cached.
#window = int(self.settings.get('db.query_window', 1))
window = 1
if now is None:
now = int(time.time() / window) * window
# Using raw sql here for performance reasons.
sql = ("select created, id, note, priority, `specific`, "
"start_time, idle_time from %s where " % self.__tablename__ +
" coalesce(cast(start_time as unsigned), %s) <= %s"
% (now - 1, now))
for field in ['product', 'platform', 'channel', 'lang', 'locale']:
if data.get(field):
sql += " and coalesce(%s, :%s) = :%s " % (field, field, field)
params[field] = data.get(field)
data['idle_time'] = data.get('idle', 0)
try:
if 'version' in data:
sql += " and coalesce(version, :version) = :version"
params['version'] = str(data['version']).split('.')[0]
except Exception:
pass
sql += " and coalesce(idle_time, 0) <= :idle_time "
params['idle_time'] = data.get('idle_time')
sql += " order by priority desc, `specific` desc, start_time desc"
if (self.settings.get('dbg.show_query', False)):
print sql
print params
if (self.settings.get('db.limit')):
sql += " limit :limit"
params['limit'] = self.settings.get('db.limit')
try:
items = self.engine.execute(text(sql), **dict(params))
except Exception, e:
self.logger.log(msg='SQL Error "%s" ' % str(e),
type='error', severity=LOG.CRITICAL)
result = []
for item in items:
# last_accessed may be actually set to 'None'
last_accessed = int(data.get('last_accessed') or '0')
if last_accessed:
start = item.start_time or item.created
if (item.idle_time and
last_accessed > start + (86400 * item.idle_time)):
continue
else:
if last_accessed > start:
continue
note = json.loads(item.note)
note.update({
# ID in this case is a unique integer per CM record
# it is used by the client to replace records.
'id': int(item.created * 100),
# token is stripped before being sent to the client.
# it's used for metrics tracking.
'token': item.id,
'created': item.created,
# This uses the server string ID for redirect/tracking
'url': self.settings.get('redir.url', 'http://%s/%s%s') % (
self.settings.get('redir.host', 'localhost'),
self.settings.get('redir.path',
'redirect/%s/' % api_version),
item.id)})
result.append(note)
return result
def get_all_announce(self, limit=None):
result = []
sql = ("select c.*,s.served,s.clicks from " +
"(campaigns as c left join scrapes " +
"as s on c.id=s.id) order by created desc ")
if limit:
sql += 'limit %d' % limit
items = self.engine.execute(text(sql))
for item in items:
counter = self.counter.report(item.id)
ann = dict(item)
ann.update(counter)
result.append(ann)
return result
def del_announce(self, keys):
#TODO: how do you safely do an "in (keys)" call?
session = self.Session()
sql = 'delete from %s where id = :key' % self.__tablename__
for key in keys:
self.engine.execute(text(sql), {"key": key})
session.commit()
def purge(self):
session = self.Session()
sql = 'delete from %s;' % self.__tablename__
self.engine.execute(text(sql))
session.commit()
def user_health_check(self):
#foo
try:
uid = self.add_user('test', '', 0)
self.rm_user(uid)
# with self.engine.begin() as conn:
except Exception, e:
import warnings
warnings.warn(str(e))
return False
return True
def is_user(self, email):
sel = self.users.select(self.users.c.email == email)
items = self.engine.execute(sel)
row = items.fetchone()
if row is None:
return False
return True
def user_list(self):
result = []
sql = "select * from users order by email"
items = self.engine.execute(text(sql))
for item in items:
result.append(dict(item))
return result
def add_user(self, email, sponsor, level=0):
try:
uid = uuid.uuid4().hex
with self.engine.begin() as conn:
ins = self.users.insert().values(
id=uid,
email=email,
sponsor=sponsor,
level=level,
date=int(time.time()))
conn.execute(ins)
return uid
except Exception, e:
self.logger.log(msg='Could not add user "%s", "%s"' %
(email, repr(e)),
type="error", severity=LOG.ERROR)
return None
def rm_user(self, id):
if len(id) == 0 or id == '0':
return False
try:
with self.engine.begin() as conn:
rm = self.users.delete().where(self.users.c.id == id)
conn.execute(rm)
return True
except Exception, e:
self.logger.log(msg='Could not delete user with id "%s", "%s"' %
(id, repr(e)),
type="error", severity=LOG.ERROR)
return False