| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (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, itertools, json, os, sys | |
|
Wladimir Palant
2015/03/26 22:56:50
Nit: PEP 8 requires one import per line and a blan
kzar
2015/03/27 11:59:57
Done.
Sebastian Noack
2015/03/27 13:12:19
While we are on it, please also add a new line bet
kzar
2015/03/27 15:10:50
Done.
| |
| 19 from sitescripts.utils import get_config | |
| 20 from sitescripts.filterhits import common, db, geometrical_mean | |
|
Wladimir Palant
2015/03/26 22:56:50
common isn't actually used here.
kzar
2015/03/27 11:59:57
Done.
| |
| 21 | |
| 22 last_log_file = None | |
|
Wladimir Palant
2015/03/26 22:56:50
Nit: IMHO this isn't meant to be exported, call it
kzar
2015/03/27 11:59:57
Done.
| |
| 23 | |
| 24 def log_files(dir): | |
| 25 """ | |
| 26 Provides a generator of filter hits log files for the given directory. | |
| 27 Works recursively, relative path of log file is returned. | |
| 28 """ | |
| 29 for root, subdirs, files in os.walk(dir): | |
| 30 for f in files: | |
| 31 if os.path.splitext(f)[1] == ".log" and f[0].isdigit(): | |
|
Wladimir Palant
2015/03/26 22:56:50
Why the requirement that the first letter is a dig
kzar
2015/03/27 11:59:57
That is a simple way to skip over other log files
Wladimir Palant
2015/03/27 16:29:06
Aren't all logs we are interested in inside of sub
kzar
2015/03/27 22:15:00
Not always, for example you might choose to only r
| |
| 32 yield os.path.join(root, f) | |
| 33 | |
| 34 def read_data(log_file): | |
| 35 """ | |
| 36 Read, parse and return the JSON data for the given log file name. | |
| 37 (As a side effect sets the global last_log_file to the log file name.) | |
| 38 """ | |
| 39 global last_log_file | |
| 40 try: | |
| 41 with open(log_file, "r") as f: | |
| 42 # Skip past the date and GET parameters | |
| 43 current = last = "" | |
| 44 while last + current != '" ': | |
| 45 last, current = current, f.read(1) | |
| 46 if not current: | |
| 47 sys.exit("Unexpected EOF in log file %s" % log_file) | |
|
Wladimir Palant
2015/03/26 22:56:50
Wouldn't that be a lot simpler if the date and the
kzar
2015/03/27 11:59:57
Sure, I suppose this logic would be simpler that w
Sebastian Noack
2015/03/27 13:12:19
I agree with Wladimir FWIW. I also wouldn't consid
kzar
2015/03/27 15:10:50
OK fair enough, Done.
Wladimir Palant
2015/03/27 16:29:06
Yes, that's why we have reviews - so that we don't
kzar
2015/03/27 22:15:00
Better yet, I changed it in the past :p. (See patc
| |
| 48 | |
| 49 # Read the JSON | |
| 50 data = json.load(f) | |
| 51 # Keep track of the current log file in global variable in case we need to | |
| 52 # identify it later if there's a problem. (This works because the files ar e | |
| 53 # processed lazily.) | |
| 54 last_log_file = log_file | |
| 55 except IOError: | |
| 56 sys.exit("Could not read log file %s" % log_file) | |
| 57 return data | |
| 58 | |
| 59 if __name__ == "__main__": | |
| 60 if not len(sys.argv) == 2: | |
| 61 print "Usage: python -m sitescripts.filterhits.bin.process_logs /path/to/log s" | |
| 62 sys.exit(1) | |
| 63 | |
| 64 interval = get_config().get("filterhitstats", "interval") | |
| 65 | |
| 66 def read_update(f): | |
| 67 return geometrical_mean.update(interval, read_data(f)) | |
| 68 | |
| 69 if sys.argv[1].endswith(".log"): | |
| 70 sql = read_update(sys.argv[1]) | |
| 71 else: | |
| 72 sql = itertools.chain.from_iterable(itertools.imap(read_update, | |
| 73 log_files(sys.argv[1]))) | |
| 74 | |
| 75 try: | |
| 76 db_connection = db.connect() | |
| 77 except MySQLdb.Error: | |
| 78 sys.exit("Failed to connect to the MySQL database.") | |
|
Wladimir Palant
2015/03/26 22:56:50
I have the impression that the original exception
kzar
2015/03/27 11:59:57
This was Sebastian's idea, not mine. (See the inli
Sebastian Noack
2015/03/27 13:12:19
There are no comments on patchset 5. You probably
kzar
2015/03/27 15:10:50
OK the discussion was in patch set 4... it doesn't
| |
| 79 | |
| 80 try: | |
| 81 db.write(db_connection, sql) | |
| 82 except (KeyError, MySQLdb.Error), e: | |
| 83 if isinstance(e, KeyError): | |
| 84 message = "KeyError (%s)" % str(e.args[0]) | |
| 85 elif isinstance(e, MySQLdb.Error): | |
| 86 message = "MySQL error (%d) \"%s\"" % (e.args[0], e.args[1]) | |
| 87 sys.exit("Failed to process file %s, all changes rolled back. %s\n" % ( | |
| 88 last_log_file, message | |
| 89 )) | |
| 90 finally: | |
| 91 db_connection.close() | |
| OLD | NEW |