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

Delta Between Two Patch Sets: sitescripts/filterhits/web/submit.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Left Patch Set: Improvements regarding comments Created Feb. 17, 2015, 10:50 a.m.
Right Patch Set: Addressed further comments from Sebastian. Created April 2, 2015, 10:13 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/filterhits/web/query.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 MySQLdb, json, os 18 import json
19 import os
20 import tempfile
21 import time
22 import traceback
19 from datetime import datetime 23 from datetime import datetime
24 from errno import EEXIST
25
26 import MySQLdb
27
20 from sitescripts.web import url_handler 28 from sitescripts.web import url_handler
21 from sitescripts.utils import get_config, setupStderr 29 from sitescripts.utils import get_config, setupStderr
30 from sitescripts.filterhits import db, geometrical_mean
31 from sitescripts.filterhits.web import common
22 32
23 import sitescripts.filterhits.common as common 33 def log_filterhits(data, basepath, query_string):
24 import sitescripts.filterhits.db as db 34 """
25 import sitescripts.filterhits.geometrical_mean as geometrical_mean 35 This logs the provided filterhits data as JSON to a file named after
36 the current timestamp in a directory named after the current date.
37 """
38 now = time.gmtime()
39
40 dir_name = time.strftime("%Y-%m-%d", now)
41 path = os.path.join(basepath, dir_name)
42 try:
43 os.makedirs(path)
44 except OSError as e:
45 if e.errno != EEXIST:
46 raise
47
48 with tempfile.NamedTemporaryFile(
49 prefix = str(int(time.mktime(now))) + "-",
50 suffix = ".log",
51 dir = path,
52 delete = False
53 ) as f:
54 print >> f, "[%s] %s" % (time.strftime("%d/%b/%Y:%H:%M:%S", now), query_stri ng)
55 json.dump(data, f)
56 return f.name
26 57
27 @url_handler("/submit") 58 @url_handler("/submit")
28 def submit(environ, start_response): 59 def submit(environ, start_response):
29 setupStderr(environ["wsgi.errors"]) 60 setupStderr(environ["wsgi.errors"])
30 config = get_config() 61 config = get_config()
31 62
32 # Check that this is a POST request 63 # Check that this is a POST request
33 if environ["REQUEST_METHOD"] != "POST": 64 if environ["REQUEST_METHOD"] != "POST":
34 return common.showError("Unsupported request method", start_response) 65 return common.show_error("Unsupported request method", start_response)
35 66
36 # Parse the submitted JSON 67 # Parse the submitted JSON
37 data = "{}"
38 try: 68 try:
39 data_length = int(environ["CONTENT_LENGTH"]) 69 data = json.loads(environ["wsgi.input"].read(int(environ["CONTENT_LENGTH"])) )
40 except (ValueError, KeyError): 70 except (KeyError, IOError, ValueError):
41 data_length = 0 71 return common.show_error("Error while parsing JSON data.", start_response)
42 if data_length != 0: 72
43 data = environ["wsgi.input"].read(data_length) 73 # Make sure the submitted data was contained within an object at least
44 try: 74 if not isinstance(data, dict):
45 data = json.loads(data) 75 return common.show_error("Error, data must be contained within an object.", start_response)
Sebastian Noack 2015/02/17 14:59:17 It seems that parsing an empty dictionary is unnec
Sebastian Noack 2015/02/17 17:09:47 data = {} try: data_length = int(environ["CONTEN
Sebastian Noack 2015/02/17 17:16:57 Possibly you could also just use json.load(): try
kzar 2015/02/24 18:05:11 Done.
46 except json.decoder.JSONDecodeError:
47 return common.showError("Error while parsing JSON data.", start_response)
48 76
49 # Log the data to a file 77 # Log the data to a file
50 log_dir = config.get("filterhitstats", "log_dir") 78 log_dir = config.get("filterhitstats", "log_dir")
51 try: 79 try:
52 log_file = common.log_filterhits(data, log_dir, 80 log_file = log_filterhits(data, log_dir, environ.get("QUERY_STRING", ""))
53 environ.get("QUERY_STRING", ""))
54 except (OSError, IOError): 81 except (OSError, IOError):
55 return common.showError("Failed to write data to log file!", start_response, 82 traceback.print_exc()
56 "500 Logging error") 83 return common.show_error("Failed to write data to log file!", start_response ,
84 "500 Logging error")
57 85
58 # Update the geometrical_mean aggregations in the database 86 # Update the geometrical_mean aggregations in the database
59 interval = config.get("filterhitstats", "interval") 87 interval = config.get("filterhitstats", "interval")
60 try: 88 try:
61 db_connection = db.connect(config.get("filterhitstats", "dbuser"), 89 db_connection = db.connect()
62 config.get("filterhitstats", "dbpassword"), 90 try:
63 config.get("filterhitstats", "database")) 91 db.write(db_connection, geometrical_mean.update(interval, data))
64 db.write(db_connection, geometrical_mean.update(interval, data)) 92 finally:
65 except MySQLdb.Error, e: 93 db_connection.close()
94 except:
66 # Updating the aggregations in the database failed for whatever reason, 95 # Updating the aggregations in the database failed for whatever reason,
67 # 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
68 # re-transmission of the data. 97 # re-transmission of data.
69 mysql_error_log = os.path.join(config.get("filterhitstats", "log_dir"), 98 processing_error_log = os.path.join(config.get("filterhitstats", "log_dir"),
70 "mysql-errors.log") 99 "processing-errors.log")
71 with open(mysql_error_log, "a+") as f: 100 with open(processing_error_log, "a+") as f:
72 f.write("[%s] MySQL error (%d) when processing data file %s: \"%s\"\n" % ( 101 message = "Problem processing data file %s:\n%s" % (
73 datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z'), 102 log_file, traceback.format_exc()
74 e.args[0], log_file, e.args[1] 103 )
75 )) 104 print >> f, "[%s] %s" % (datetime.now().strftime("%d/%b/%Y:%H:%M:%S %z"), message)
76 finally:
77 if db_connection:
Sebastian Noack 2015/02/17 14:59:17 This will result in a NameError, in case db_connec
kzar 2015/02/24 18:05:11 Done.
78 db_connection.close()
79 105
80 # Send back a 200 OK response 106 # Send back a 204 No Content
81 response_headers = [("Content-type", "text/plain")] 107 start_response("204 No Content", [])
82 start_response("200 OK", response_headers)
83 return [] 108 return []
LEFTRIGHT

Powered by Google App Engine
This is Rietveld