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: Added API tests, addressed comments and some other improvements. Created Feb. 28, 2015, 7:36 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
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-2014 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 18 import MySQLdb, json, os
19 from sitescripts.web import url_handler, showError 19 from datetime import datetime
20 from sitescripts.web import url_handler
20 from sitescripts.utils import get_config, setupStderr 21 from sitescripts.utils import get_config, setupStderr
22
21 import sitescripts.filterhits.common as common 23 import sitescripts.filterhits.common as common
Sebastian Noack 2015/03/02 10:04:01 Nit: from ... import ...
kzar 2015/03/02 10:39:03 Done.
22 24 import sitescripts.filterhits.db as db
23 config = get_config() 25 import sitescripts.filterhits.geometrical_mean as geometrical_mean
24 26
25 @url_handler("/submit") 27 @url_handler("/submit")
26 def submit(environ, start_response): 28 def submit(environ, start_response):
27 global config
28 setupStderr(environ["wsgi.errors"]) 29 setupStderr(environ["wsgi.errors"])
30 config = get_config()
29 31
30 # Check that this is a POST request 32 # Check that this is a POST request
31 if environ["REQUEST_METHOD"].upper() != "POST": 33 if environ["REQUEST_METHOD"] != "POST":
32 return showError("Unsupported request method", start_response) 34 return common.showError("Unsupported request method", start_response)
33 35
34 # Parse the submitted JSON 36 # Parse the submitted JSON
35 data = "{}"
36 try: 37 try:
37 data_length = int(environ.get("CONTENT_LENGTH", "0")) 38 data = json.loads(environ["wsgi.input"].read(int(environ["CONTENT_LENGTH"])) )
38 except ValueError: 39 except (KeyError, IOError, ValueError):
39 data_length = 0 40 return common.showError("Error while parsing JSON data.", start_response)
40 if data_length != 0:
41 data = environ["wsgi.input"].read(data_length)
42 try:
43 data = json.loads(data)
44 except json.decoder.JSONDecodeError:
45 return showError("Error while parsing JSON data.", start_response)
46 41
47 # Make sure it looks roughly valid 42 # Make sure the submitted data was contained within an object at least
48 if not common.valid_log_data(data): 43 if not isinstance(data, dict):
49 return showError("Data looks invalid.", start_response) 44 return common.showError("Error, data must be contained within an object.", s tart_response)
50 45
51 # Log the data to a file 46 # Log the data to a file
52 log_dir = config.get("filterhitstats", "log_dir") 47 if not db.testing:
48 log_dir = config.get("filterhitstats", "log_dir")
49 try:
50 log_file = common.log_filterhits(data, log_dir,
51 environ.get("QUERY_STRING", ""))
52 except (OSError, IOError):
53 return common.showError("Failed to write data to log file!", start_respons e,
54 "500 Logging error")
53 55
56 # Update the geometrical_mean aggregations in the database
57 interval = config.get("filterhitstats", "interval")
54 try: 58 try:
55 common.log_filterhits(data, log_dir) 59 db_connection = db.connect()
56 except OSError, IOError: 60 try:
57 return showError("Failed to save data!", start_response) 61 db.write(db_connection, geometrical_mean.update(interval, data))
62 finally:
63 db_connection.close()
64 except (KeyError, MySQLdb.Error), e:
65 # Updating the aggregations in the database failed for whatever reason,
66 # log the details but continue to return 200 OK to the client to avoid
67 # re-transmission of the data.
68 if not db.testing:
69 processing_error_log = os.path.join(config.get("filterhitstats", "log_dir" ),
70 "processing-errors.log")
71 with open(processing_error_log, "a+") as f:
72 if isinstance(e, KeyError):
73 message = "KeyError (%s) when processing data file %s\n" % (str(e.args [0]), log_file)
74 elif isinstance(e, MySQLdb.Error):
75 message = "MySQL error (%d) when processing data file %s: \"%s\"\n" % (e.args[0], log_file, e.args[1])
76 f.write("[%s] %s" % (datetime.now().strftime('%d/%b/%Y:%H:%M:%S %z'), me ssage))
58 77
59 # # TODO calculate the geometrical mean and update the database 78 # Send back a 200 OK response
60 # # Open DB connection set up cursor
61 # db = _get_db(config)
62 # cursor = db.cursor()
63 # for filter, filter_data in data['filters'].iteritems():
64 # for domain, domain_hits in itertools.chain(filter_data['thirdParty'].iteri tems(),
65 # filter_data['firstParty'].iteri tems()):
66 # 1 # UPDATE geometrical_mean SET hits=(??) timestamp=new_timestamp WHERE filter=filter AND domain=domain;
67 # # ^ SELECT current hits + timestamp + perform geometrical mean on combo
68 # # Commit / rollback and close
69 # # db.commit()
70 # db.close()
71
72 response_headers = [("Content-type", "text/plain")] 79 response_headers = [("Content-type", "text/plain")]
73 start_response("200 OK", response_headers) 80 start_response("200 OK", response_headers)
74 return [] 81 return []
75
76 # def _increment_entry(db, domain_id, status):
77 # cursor = db.cursor(MySQLdb.cursors.DictCursor)
78 # cursor.execute("INSERT INTO corrections(domain, status, curr_month, prev_mon th, curr_year, prev_year) VALUES (%s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDAT E curr_month = curr_month + 1, curr_year = curr_year + 1", (domain_id, status, 1 , 0, 1, 0))
OLDNEW
« sitescripts/filterhits/common.py ('K') | « sitescripts/filterhits/web/query.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld