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 some of Wladimir's comments Created March 27, 2015, 11:57 a.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
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 from sitescripts.utils import get_config
24 from sitescripts.filterhits import db, geometrical_mean
25
26 _last_log_file = None
27
28 def log_files(dir):
29 """
30 Provides a generator of filter hits log files for the given directory.
31 Works recursively, relative path of log file is returned.
32 """
33 for root, subdirs, files in os.walk(dir):
34 for f in files:
35 if os.path.splitext(f)[1] == ".log" and f[0].isdigit():
36 yield os.path.join(root, f)
37
38 def read_data(log_file):
39 """
40 Read, parse and return the JSON data for the given log file name.
41 (As a side effect sets the global _last_log_file to the log file name.)
42 """
43 global _last_log_file
44 try:
45 with open(log_file, "r") as f:
46 # Skip past the date and GET parameters
47 current = last = ""
48 while last + current != '" ':
49 last, current = current, f.read(1)
50 if not current:
51 sys.exit("Unexpected EOF in log file %s" % log_file)
52
53 # Read the JSON
54 data = json.load(f)
55 # Keep track of the current log file in global variable in case we need to
56 # identify it later if there's a problem. (This works because the files ar e
57 # processed lazily.)
58 _last_log_file = log_file
59 except IOError:
60 sys.exit("Could not read log file %s" % log_file)
61 return data
62
63 if __name__ == "__main__":
64 if not len(sys.argv) == 2:
65 print "Usage: python -m sitescripts.filterhits.bin.process_logs /path/to/log s"
66 sys.exit(1)
67
68 interval = get_config().get("filterhitstats", "interval")
69
70 def read_update(f):
71 return geometrical_mean.update(interval, read_data(f))
72
73 if sys.argv[1].endswith(".log"):
74 sql = read_update(sys.argv[1])
75 else:
76 sql = itertools.chain.from_iterable(itertools.imap(read_update,
77 log_files(sys.argv[1])))
78
79 try:
80 db_connection = db.connect()
81 except MySQLdb.Error:
82 sys.exit("Failed to connect to the MySQL database.")
83
84 try:
85 db.write(db_connection, sql)
86 except (KeyError, MySQLdb.Error), e:
87 if isinstance(e, KeyError):
88 message = "KeyError (%s)" % str(e.args[0])
89 elif isinstance(e, MySQLdb.Error):
90 message = "MySQL error (%d) \"%s\"" % (e.args[0], e.args[1])
91 sys.exit("Failed to process file %s, all changes rolled back. %s\n" % (
92 _last_log_file, message
93 ))
94 finally:
95 db_connection.close()
OLDNEW

Powered by Google App Engine
This is Rietveld