Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/filterhits/bin/process_logs.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Addressed Sebastian's and Wladimir's comments. Created March 27, 2015, 3:08 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # coding: utf-8
Wladimir Palant 2015/03/27 16:30:48 One more note: the purpose of this script wasn't c
kzar 2015/03/27 22:15:00 Done.
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH
5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17
18 import MySQLdb
19 import itertools
20 import json
21 import os
22 import sys
23
24 from sitescripts.utils import get_config
25 from sitescripts.filterhits import db, geometrical_mean
26
27 _last_log_file = None
28
29 def log_files(dir):
30 """
31 Provides a generator of filter hits log files for the given directory.
32 Works recursively, relative path of log file is returned.
33 """
34 for root, subdirs, files in os.walk(dir):
35 for f in files:
36 if os.path.splitext(f)[1] == ".log" and f[0].isdigit():
37 yield os.path.join(root, f)
38
39 def read_data(log_file):
40 """
41 Read, parse and return the JSON data for the given log file name.
42 (As a side effect sets the global _last_log_file to the log file name.)
43 """
44 global _last_log_file
45 try:
46 with open(log_file, "r") as f:
47 f.readline()
48 data = json.load(f)
49 # Keep track of the current log file in global variable in case we need to
50 # identify it later if there's a problem. (This works because the files ar e
51 # processed lazily.)
52 _last_log_file = log_file
53 except IOError:
54 sys.exit("Could not read log file %s" % log_file)
55 return data
56
57 if __name__ == "__main__":
58 if not len(sys.argv) == 2:
59 print "Usage: python -m sitescripts.filterhits.bin.process_logs /path/to/log s"
60 sys.exit(1)
61
62 interval = get_config().get("filterhitstats", "interval")
63
64 def read_update(f):
65 return geometrical_mean.update(interval, read_data(f))
66
67 if sys.argv[1].endswith(".log"):
68 sql = read_update(sys.argv[1])
69 else:
70 sql = itertools.chain.from_iterable(itertools.imap(read_update,
71 log_files(sys.argv[1])))
72
73 try:
74 db_connection = db.connect()
75 except MySQLdb.Error:
76 sys.exit("Failed to connect to the MySQL database.")
Wladimir Palant 2015/03/27 16:29:06 Given that Sebastian doesn't seem to disagree, cou
kzar 2015/03/27 22:15:00 Done.
77
78 try:
79 db.write(db_connection, sql)
80 except (KeyError, MySQLdb.Error), e:
Wladimir Palant 2015/03/27 16:29:06 The json module seems to raise a ValueError except
kzar 2015/03/27 22:15:00 Done.
81 if isinstance(e, KeyError):
82 message = "KeyError (%s)" % str(e.args[0])
83 elif isinstance(e, MySQLdb.Error):
84 message = "MySQL error (%d) \"%s\"" % (e.args[0], e.args[1])
85 sys.exit("Failed to process file %s, all changes rolled back. %s\n" % (
86 _last_log_file, message
87 ))
88 finally:
89 db_connection.close()
OLDNEW

Powered by Google App Engine
This is Rietveld