| 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 json | |
| 19 import MySQLdb | |
| 20 import tempfile | |
| 21 import time | |
| 22 import os | |
| 23 from datetime import datetime | |
| 24 from errno import EEXIST | |
| 25 | |
| 26 from sitescripts.web import url_handler | |
| 27 from sitescripts.utils import get_config, setupStderr | |
| 28 from sitescripts.filterhits import common, db, geometrical_mean | |
| 29 | |
| 30 def log_filterhits(data, basepath, query_string): | |
| 31 """ | |
| 32 This logs the provided filterhits data as JSON to a file named after | |
| 33 the current timestamp in a directory named after the current date. | |
| 34 """ | |
| 35 now = time.gmtime() | |
| 36 | |
| 37 dir_name = time.strftime("%Y-%m-%d", now) | |
| 38 path = os.path.join(basepath, dir_name) | |
| 39 try: | |
| 40 os.makedirs(path) | |
| 41 except OSError as e: | |
| 42 if e.errno != EEXIST: | |
| 43 raise | |
| 44 | |
| 45 with tempfile.NamedTemporaryFile( | |
| 46 prefix = str(int(time.mktime(now))) + "-", | |
| 47 suffix = ".log", | |
| 48 dir = path, | |
| 49 delete = False | |
| 50 ) as f: | |
| 51 print >> f, "[%s] \"%s\"" % (time.strftime('%d/%b/%Y:%H:%M:%S', now), | |
| 52 query_string) | |
|
Wladimir Palant
2015/03/27 16:29:06
I wonder whether quotation marks in the query stri
kzar
2015/03/27 22:15:00
(Give me a little credit, I would not assume data
| |
| 53 print >> f, json.dumps(data) | |
|
Sebastian Noack
2015/03/27 16:31:04
json.dump(data, f)
kzar
2015/03/27 22:15:00
Done.
| |
| 54 return f.name | |
| 55 | |
| 56 @url_handler("/submit") | |
| 57 def submit(environ, start_response): | |
| 58 setupStderr(environ["wsgi.errors"]) | |
| 59 config = get_config() | |
| 60 | |
| 61 # Check that this is a POST request | |
| 62 if environ["REQUEST_METHOD"] != "POST": | |
| 63 return common.show_error("Unsupported request method", start_response) | |
| 64 | |
| 65 # Parse the submitted JSON | |
| 66 try: | |
| 67 data = json.loads(environ["wsgi.input"].read(int(environ["CONTENT_LENGTH"])) ) | |
| 68 except (KeyError, IOError, ValueError): | |
| 69 return common.show_error("Error while parsing JSON data.", start_response) | |
| 70 | |
| 71 # Make sure the submitted data was contained within an object at least | |
| 72 if not isinstance(data, dict): | |
| 73 return common.show_error("Error, data must be contained within an object.", start_response) | |
| 74 | |
| 75 # Log the data to a file | |
| 76 if not db.testing: | |
| 77 log_dir = config.get("filterhitstats", "log_dir") | |
| 78 try: | |
| 79 log_file = log_filterhits(data, log_dir, environ.get("QUERY_STRING", "")) | |
| 80 except (OSError, IOError): | |
| 81 return common.show_error("Failed to write data to log file!", start_respon se, | |
| 82 "500 Logging error") | |
|
Wladimir Palant
2015/03/27 16:29:06
This seems to be an unexpected error, one that is
kzar
2015/03/27 22:15:00
Done.
| |
| 83 | |
| 84 # Update the geometrical_mean aggregations in the database | |
| 85 interval = config.get("filterhitstats", "interval") | |
| 86 try: | |
| 87 db_connection = db.connect() | |
| 88 try: | |
| 89 db.write(db_connection, geometrical_mean.update(interval, data)) | |
| 90 finally: | |
| 91 db_connection.close() | |
| 92 except (KeyError, MySQLdb.Error), e: | |
| 93 # Updating the aggregations in the database failed for whatever reason, | |
| 94 # log the details but continue to return 200 OK to the client to avoid | |
| 95 # re-transmission of the data. | |
| 96 if not db.testing: | |
| 97 processing_error_log = os.path.join(config.get("filterhitstats", "log_dir" ), | |
| 98 "processing-errors.log") | |
| 99 with open(processing_error_log, "a+") as f: | |
| 100 if isinstance(e, KeyError): | |
| 101 message = "KeyError (%s) when processing data file %s" % (str(e.args[0 ]), log_file) | |
| 102 elif isinstance(e, MySQLdb.Error): | |
| 103 message = "MySQL error (%d) when processing data file %s: \"%s\"" % (e .args[0], log_file, e.args[1]) | |
| 104 print >> f, "[%s] %s" % (datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z') , message) | |
|
Wladimir Palant
2015/03/27 16:29:06
I'd suggest using traceback module here instead of
kzar
2015/03/27 22:15:00
Done.
| |
| 105 | |
| 106 # Send back a 200 OK response | |
| 107 response_headers = [("Content-type", "text/plain")] | |
| 108 start_response("200 OK", response_headers) | |
| 109 return [] | |
| OLD | NEW |