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

Side by Side Diff: sitescripts/filterhits/web/submit.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Created Dec. 19, 2014, 1:16 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
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2014 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, json, os
19 from datetime import datetime
20 from sitescripts.web import url_handler
21 from sitescripts.utils import get_config, setupStderr
22
23 import sitescripts.filterhits.common as common
24 import sitescripts.filterhits.db as db
25 import sitescripts.filterhits.geometrical_mean as geometrical_mean
26
27 @url_handler("/submit")
28 def submit(environ, start_response):
29 setupStderr(environ["wsgi.errors"])
30 config = get_config()
31
32 # Check that this is a POST request
33 if environ["REQUEST_METHOD"].upper() != "POST":
Sebastian Noack 2015/02/11 16:00:12 According to the specs REQUEST_METHOD is always al
kzar 2015/02/17 10:52:24 Done.
34 return common.showError("Unsupported request method", start_response)
35
36 # Parse the submitted JSON
37 data = "{}"
38 try:
39 data_length = int(environ.get("CONTENT_LENGTH", "0"))
40 except ValueError:
Sebastian Noack 2015/02/11 16:00:12 If you just catch the KeyError as well you can use
kzar 2015/02/17 10:52:24 Done.
41 data_length = 0
42 if data_length != 0:
43 data = environ["wsgi.input"].read(data_length)
44 try:
45 data = json.loads(data)
46 except json.decoder.JSONDecodeError:
47 return common.showError("Error while parsing JSON data.", start_response)
48
49 # Make sure it looks roughly valid
50 if not common.valid_log_data(data):
51 return common.showError("Data looks invalid.", start_response)
52
53 # Log the data to a file
54 log_dir = config.get("filterhitstats", "log_dir")
55 try:
56 log_file = common.log_filterhits(data, log_dir,
57 environ.get("QUERY_STRING", ""))
58 except OSError, IOError:
Sebastian Noack 2015/02/11 16:00:12 You need parentheses here. Otherwise - as it curre
kzar 2015/02/17 10:52:24 Done.
59 return common.showError("Failed to write data to log file!", start_response,
60 "500 Logging error")
61
62 # Update the geometrical_mean aggregations in the database
63 interval = config.get("filterhitstats", "interval")
64 try:
65 db.connect(config.get("filterhitstats", "dbuser"),
66 config.get("filterhitstats", "dbpassword"),
67 config.get("filterhitstats", "database"))
68 db.write(geometrical_mean.update(interval, data))
69 except MySQLdb.Error, e:
70 # Updating the aggregations in the database failed for whatever reason,
71 # log the details but continue to return 200 OK to the client to avoid
72 # re-transmission of the data.
73 mysql_error_log = os.path.join(config.get("filterhitstats", "log_dir"),
74 "mysql-errors.log")
75 with open(mysql_error_log, "a+") as f:
76 f.write("[%s] MySQL error (%d) when processing data file %s: \"%s\"\n" % (
77 datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z'),
78 e.args[0], log_file, e.args[1]
79 ))
80 finally:
81 db.disconnect()
82
83 # Send back a 200 OK response
84 response_headers = [("Content-type", "text/plain")]
85 start_response("200 OK", response_headers)
86 return []
OLDNEW
« sitescripts/filterhits/web/query.py ('K') | « sitescripts/filterhits/web/query.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld