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: Simplified geometrical_mean code and reduced filter inserts. Created March 16, 2015, 4:24 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-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, json, os
19 from datetime import datetime
20 from sitescripts.web import url_handler
21 from sitescripts.utils import get_config, setupStderr
22 from sitescripts.filterhits import common, db, geometrical_mean
23
24 @url_handler("/submit")
25 def submit(environ, start_response):
26 setupStderr(environ["wsgi.errors"])
27 config = get_config()
28
29 # Check that this is a POST request
30 if environ["REQUEST_METHOD"] != "POST":
31 return common.show_error("Unsupported request method", start_response)
32
33 # Parse the submitted JSON
34 try:
35 data = json.loads(environ["wsgi.input"].read(int(environ["CONTENT_LENGTH"])) )
36 except (KeyError, IOError, ValueError):
37 return common.show_error("Error while parsing JSON data.", start_response)
38
39 # Make sure the submitted data was contained within an object at least
40 if not isinstance(data, dict):
41 return common.show_error("Error, data must be contained within an object.", start_response)
42
43 # Log the data to a file
44 if not db.testing:
45 log_dir = config.get("filterhitstats", "log_dir")
46 try:
47 log_file = common.log_filterhits(data, log_dir,
48 environ.get("QUERY_STRING", ""))
49 except (OSError, IOError):
50 return common.show_error("Failed to write data to log file!", start_respon se,
51 "500 Logging error")
52
53 # Update the geometrical_mean aggregations in the database
54 interval = config.get("filterhitstats", "interval")
55 try:
56 db_connection = db.connect()
57 try:
58 db.write(db_connection, geometrical_mean.update(interval, data))
59 finally:
60 db_connection.close()
61 except (KeyError, MySQLdb.Error), e:
62 # Updating the aggregations in the database failed for whatever reason,
63 # log the details but continue to return 200 OK to the client to avoid
64 # re-transmission of the data.
65 if not db.testing:
66 processing_error_log = os.path.join(config.get("filterhitstats", "log_dir" ),
67 "processing-errors.log")
68 with open(processing_error_log, "a+") as f:
69 if isinstance(e, KeyError):
70 message = "KeyError (%s) when processing data file %s\n" % (str(e.args [0]), log_file)
71 elif isinstance(e, MySQLdb.Error):
72 message = "MySQL error (%d) when processing data file %s: \"%s\"\n" % (e.args[0], log_file, e.args[1])
73 f.write("[%s] %s" % (datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z'), me ssage))
74
75 # Send back a 200 OK response
76 response_headers = [("Content-type", "text/plain")]
77 start_response("200 OK", response_headers)
78 return []
OLDNEW
« sitescripts/filterhits/geometrical_mean.py ('K') | « sitescripts/filterhits/web/query.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld