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

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

Issue 29344572: Noissue - Removed obsolete sitescripts.filterhits package (Closed)
Patch Set: Created May 26, 2016, 12:05 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
« no previous file with comments | « sitescripts/filterhits/bin/__init__.py ('k') | sitescripts/filterhits/db.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2016 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 itertools
19 import json
20 import logging
21 import os
22 import sys
23
24 import MySQLdb
25
26 from sitescripts.utils import get_config
27 from sitescripts.filterhits import db, geometrical_mean
28
29 _last_log_file = None
30
31
32 def log_files(dir):
33 """
34 Provides a generator of filter hits log files for the given directory.
35 Works recursively, relative path of log file is returned.
36 """
37 for root, subdirs, files in os.walk(dir):
38 for f in files:
39 if os.path.splitext(f)[1] == ".log" and f[0].isdigit():
40 yield os.path.join(root, f)
41
42
43 def read_data(log_file):
44 """
45 Read, parse and return the JSON data for the given log file name.
46 (As a side effect sets the global _last_log_file to the log file name.)
47 """
48 global _last_log_file
49 try:
50 with open(log_file, "r") as f:
51 f.readline()
52 data = json.load(f)
53 # Keep track of the current log file in global variable in case we n eed to
54 # identify it later if there's a problem. (This works because the fi les are
55 # processed lazily.)
56 _last_log_file = log_file
57 except IOError:
58 sys.exit("Could not read log file %s" % log_file)
59 return data
60
61 if __name__ == "__main__":
62 if not len(sys.argv) == 2:
63 print "Usage: python -m sitescripts.filterhits.bin.reprocess_logs /path/ to/logs"
64 sys.exit(1)
65
66 interval = get_config().get("filterhitstats", "interval")
67
68 def read_update(f):
69 return geometrical_mean.update(interval, read_data(f))
70
71 if sys.argv[1].endswith(".log"):
72 sql = read_update(sys.argv[1])
73 else:
74 sql = itertools.chain.from_iterable(itertools.imap(read_update,
75 log_files(sys.argv[1] )))
76
77 db_connection = db.connect()
78
79 try:
80 db.write(db_connection, sql)
81 except:
82 logging.error("Failed to process file %s, all changes rolled back." % _l ast_log_file)
83 raise
84 finally:
85 db_connection.close()
OLDNEW
« no previous file with comments | « sitescripts/filterhits/bin/__init__.py ('k') | sitescripts/filterhits/db.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld