-
Notifications
You must be signed in to change notification settings - Fork 6
/
all_revisions.py
183 lines (150 loc) · 6.06 KB
/
all_revisions.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
from bottle import route, run, JSONPlugin, request
import bottle
import time
import os
import oursql
from bottle_compressor import CompressorPlugin
import logging
import json
from functools import partial
compressor_plugin = CompressorPlugin(compress_level=3)
bottle.install(compressor_plugin)
#from ujson import dumps
from bottle import json_dumps as dumps
better_dumps = partial(dumps, ensure_ascii=False, separators=(',', ':'))
bottle.default_app().uninstall(JSONPlugin)
bottle.default_app().install(JSONPlugin(better_dumps))
# TODO: timestamp
ALL_PROPS = ["rev_sha1", "rev_len", "rev_text_id", "rev_timestamp", "rev_minor_edit", "rev_user_text", "rev_comment", "rev_parent_id", "rev_deleted", "rev_page", "rev_user", "rev_id"]
DESIRED_PROPS = ["rev_sha1", "rev_len", "rev_timestamp", "rev_minor_edit", "rev_user_text", "rev_comment", "rev_deleted", "rev_user", "rev_id"]
LOG_OUTSTANDING_LIMIT = 7200 # 2 hours
class AccessLogger(object):
def __init__(self, logfile):
self.logfile = logfile
self.logger = logging.getLogger('AccessLogger')
self.logger.setLevel(logging.INFO)
write_handler = logging.FileHandler(logfile)
self.logger.addHandler(write_handler)
def log(self, action, hostname, params, start_time):
self.logger.info(json.dumps({'time': time.time(), 'action': action, 'hostname': hostname, 'params': params, 'start_time': start_time}))
def outstanding(self):
# TODO: fix
lines = self.read(30)
recents = [line for line in lines if line['age'] < LOG_OUTSTANDING_LIMIT]
starts = set([(start['hostname'], start['params'], start['start_time']) for start in recents if start['action'] == 'start'])
completes = set([(finish['hostname'], finish['params'], finish['start_time']) for finish in recents if finish['action'] == 'complete'])
outstanding = starts - completes
return {'openlog': len(outstanding)}
def read(self, no):
ret = []
history = open(self.logfile, 'r')
lines = history.readlines()
if not lines[-no:]:
return []
else:
for line in lines[-no:]:
line = json.loads(line)
time_s = line['time']
line['time'] = time.ctime(time_s)
line['age'] = round(time.time() - time_s)
ret.append(line)
return ret[::-1]
LOG = AccessLogger('access.log')
class ArticleHistory(object):
'''article history object'''
def __init__(self, title, namespace=0):
try:
s_time = time.time()
db = oursql.connect(db='enwiki_p',
host="enwiki-p.rrdb.toolserver.org",
read_default_file=os.path.expanduser("~/.my.cnf"))
except oursql.ProgrammingError as (error_number, error_str, donno):
print str(error_number) + ': ' + error_str
self.revisions = {'error': error_str}
self.dur = time.time() - s_time
except:
raise
else:
cursor = db.cursor(oursql.DictCursor)
cursor.execute(u'''
SELECT {}
FROM revision
INNER JOIN page ON revision.rev_page = page.page_id
WHERE page_title = ? AND page.page_namespace = ?;
'''.format(', '.join(DESIRED_PROPS)), (title, namespace))
self.revisions = cursor.fetchall()
self.dur = time.time() - s_time
class WL(object):
'''article history object'''
def __init__(self, title, namespace=0):
try:
s_time = time.time()
db = oursql.connect(db='enwiki_p',
host="enwiki-p.rrdb.toolserver.org",
read_default_file=os.path.expanduser("~/.my.cnf"))
cursor = db.cursor(oursql.DictCursor)
except oursql.ProgrammingError as (error_number, error_str, donno):
print str(error_number) + ': ' + error_str
self.revisions = {'error': error_str}
self.dur = time.time() - s_time
except:
raise
else:
cursor.execute(u'''
SELECT count(ts_wl_user_touched_cropped)
FROM watchlist
WHERE wl_title = ? AND wl_namespace = ?;
''', (title, namespace))
self.wers = cursor.fetchall()
self.dur = time.time() - s_time
@route('/writelog/')
def write_log():
action = request.query.action
hostname = request.query.hostname
params = request.query.params
start_time = request.query.start_time
if action and hostname and params:
LOG.log(action, hostname, params, start_time)
return {'log': LOG.read(1), 'write': 'success'}
else:
return {'write': 'failure'}
@route('/readlog')
@route('/readlog/')
@route('/readlog/<lines:path>')
def read_log(lines=10):
if type(lines) is not int:
try:
lines = int(lines)
except ValueError:
lines = 10
return {'log': LOG.read(lines)}
@route('/openlog')
@route('/openlog/')
def print_open():
return LOG.outstanding()
@route('/revisions/<title:path>')
def get_revisions(title):
article = ArticleHistory(title)
return {'result': article.revisions}
@route('/all')
@route('/all/')
def get_everything():
title = request.query.title
talk = ArticleHistory(title, 1)
article = ArticleHistory(title)
return {'article': article.revisions, 'article_time': str(article.dur), 'talk': talk.revisions, 'talk_time': str(talk.dur)}
@route('/wl')
def get_wl():
title = request.query.title
w = WL(title)
return {'watchers': w.wers[0]['count(ts_wl_user_touched_cropped)'], 'query_duration': str(w.dur)}
@route('/uptime')
@route('/uptime/')
def get_uptime():
import subprocess
import socket
import os
uptime, _, load = subprocess.check_output(['uptime']).partition(', load average:')
return {'uptime': uptime.strip(), 'load': load.strip(), 'hostname': socket.gethostname(), 'uname': os.uname()}
if __name__ == '__main__':
run(host='0.0.0.0', port=8089, reloader=True, server='twisted', debug=True)