Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 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 | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 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/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import json | 18 import json |
19 import MySQLdb | 19 import os |
Sebastian Noack
2015/03/31 07:55:21
Nit: Third-party modules go between corelib and ow
kzar
2015/03/31 09:48:56
Done.
| |
20 import tempfile | 20 import tempfile |
21 import time | 21 import time |
22 import traceback | 22 import traceback |
23 import os | |
24 from datetime import datetime | 23 from datetime import datetime |
25 from errno import EEXIST | 24 from errno import EEXIST |
25 | |
26 import MySQLdb | |
26 | 27 |
27 from sitescripts.web import url_handler | 28 from sitescripts.web import url_handler |
28 from sitescripts.utils import get_config, setupStderr | 29 from sitescripts.utils import get_config, setupStderr |
29 from sitescripts.filterhits import db, geometrical_mean | 30 from sitescripts.filterhits import db, geometrical_mean |
30 from sitescripts.filterhits.web import common | 31 from sitescripts.filterhits.web import common |
31 | 32 |
32 def log_filterhits(data, basepath, query_string): | 33 def log_filterhits(data, basepath, query_string): |
33 """ | 34 """ |
34 This logs the provided filterhits data as JSON to a file named after | 35 This logs the provided filterhits data as JSON to a file named after |
35 the current timestamp in a directory named after the current date. | 36 the current timestamp in a directory named after the current date. |
36 """ | 37 """ |
37 now = time.gmtime() | 38 now = time.gmtime() |
38 | 39 |
39 dir_name = time.strftime("%Y-%m-%d", now) | 40 dir_name = time.strftime("%Y-%m-%d", now) |
40 path = os.path.join(basepath, dir_name) | 41 path = os.path.join(basepath, dir_name) |
41 try: | 42 try: |
42 os.makedirs(path) | 43 os.makedirs(path) |
43 except OSError as e: | 44 except OSError as e: |
44 if e.errno != EEXIST: | 45 if e.errno != EEXIST: |
45 raise | 46 raise |
46 | 47 |
47 with tempfile.NamedTemporaryFile( | 48 with tempfile.NamedTemporaryFile( |
48 prefix = str(int(time.mktime(now))) + "-", | 49 prefix = str(int(time.mktime(now))) + "-", |
49 suffix = ".log", | 50 suffix = ".log", |
50 dir = path, | 51 dir = path, |
51 delete = False | 52 delete = False |
52 ) as f: | 53 ) as f: |
53 print >> f, "[%s] %s" % (time.strftime('%d/%b/%Y:%H:%M:%S', now), query_stri ng) | 54 print >> f, "[%s] %s" % (time.strftime("%d/%b/%Y:%H:%M:%S", now), query_stri ng) |
54 json.dump(data, f) | 55 json.dump(data, f) |
55 return f.name | 56 return f.name |
56 | 57 |
57 @url_handler("/submit") | 58 @url_handler("/submit") |
58 def submit(environ, start_response): | 59 def submit(environ, start_response): |
59 setupStderr(environ["wsgi.errors"]) | 60 setupStderr(environ["wsgi.errors"]) |
60 config = get_config() | 61 config = get_config() |
61 | 62 |
62 # Check that this is a POST request | 63 # Check that this is a POST request |
63 if environ["REQUEST_METHOD"] != "POST": | 64 if environ["REQUEST_METHOD"] != "POST": |
(...skipping 21 matching lines...) Expand all Loading... | |
85 # Update the geometrical_mean aggregations in the database | 86 # Update the geometrical_mean aggregations in the database |
86 interval = config.get("filterhitstats", "interval") | 87 interval = config.get("filterhitstats", "interval") |
87 try: | 88 try: |
88 db_connection = db.connect() | 89 db_connection = db.connect() |
89 try: | 90 try: |
90 db.write(db_connection, geometrical_mean.update(interval, data)) | 91 db.write(db_connection, geometrical_mean.update(interval, data)) |
91 finally: | 92 finally: |
92 db_connection.close() | 93 db_connection.close() |
93 except: | 94 except: |
94 # Updating the aggregations in the database failed for whatever reason, | 95 # Updating the aggregations in the database failed for whatever reason, |
95 # log the details but continue to return 200 OK to the client to avoid | 96 # log the details but continue to return 204 to the client to avoid the |
96 # re-transmission of the data. | 97 # re-transmission of data. |
97 processing_error_log = os.path.join(config.get("filterhitstats", "log_dir"), | 98 processing_error_log = os.path.join(config.get("filterhitstats", "log_dir"), |
98 "processing-errors.log") | 99 "processing-errors.log") |
99 with open(processing_error_log, "a+") as f: | 100 with open(processing_error_log, "a+") as f: |
100 message = "Problem processing data file %s:\n%s" % ( | 101 message = "Problem processing data file %s:\n%s" % ( |
101 log_file, traceback.format_exc() | 102 log_file, traceback.format_exc() |
102 ) | 103 ) |
103 print >> f, "[%s] %s" % (datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z'), message) | 104 print >> f, "[%s] %s" % (datetime.now().strftime("%d/%b/%Y:%H:%M:%S %z"), message) |
104 | 105 |
105 # Send back a 200 OK response | 106 # Send back a 204 No Content |
106 response_headers = [("Content-type", "text/plain")] | 107 start_response("204 No Content", []) |
Sebastian Noack
2015/03/31 07:55:21
Do we actually need to specify a content type for
kzar
2015/03/31 09:48:56
I guess not, I've removed it.
Wladimir Palant
2015/03/31 13:42:31
Yes, we do - the browser still needs to know how t
Sebastian Noack
2015/03/31 13:46:53
Well, if we would also set the correct status code
kzar
2015/04/01 19:09:39
OK so I tested returning a 200 with and without th
| |
107 start_response("200 OK", response_headers) | |
108 return [] | 108 return [] |
LEFT | RIGHT |