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

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

Issue 29344572: Noissue - Removed obsolete sitescripts.filterhits package (Closed)
Patch Set: Created May 26, 2016, 12:05 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
« no previous file with comments | « sitescripts/filterhits/web/query.py ('k') | tox.ini » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2016 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 json
19 import os
20 import tempfile
21 import time
22 import traceback
23 from datetime import datetime
24 from errno import EEXIST
25
26 import MySQLdb
27
28 from sitescripts.web import url_handler
29 from sitescripts.utils import get_config, setupStderr
30 from sitescripts.filterhits import db, geometrical_mean
31 from sitescripts.filterhits.web import common
32
33
34 def log_filterhits(data, basepath, query_string):
35 """
36 This logs the provided filterhits data as JSON to a file named after
37 the current timestamp in a directory named after the current date.
38 """
39 now = time.gmtime()
40
41 dir_name = time.strftime("%Y-%m-%d", now)
42 path = os.path.join(basepath, dir_name)
43 try:
44 os.makedirs(path)
45 except OSError as e:
46 if e.errno != EEXIST:
47 raise
48
49 with tempfile.NamedTemporaryFile(
50 prefix=str(int(time.mktime(now))) + "-",
51 suffix=".log",
52 dir=path,
53 delete=False
54 ) as f:
55 print >> f, "[%s] %s" % (time.strftime("%d/%b/%Y:%H:%M:%S", now), query_ string)
56 json.dump(data, f)
57 return f.name
58
59
60 @url_handler("/submit")
61 def submit(environ, start_response):
62 setupStderr(environ["wsgi.errors"])
63 config = get_config()
64
65 # Check that this is a POST request
66 if environ["REQUEST_METHOD"] != "POST":
67 return common.show_error("Unsupported request method", start_response)
68
69 # Parse the submitted JSON
70 try:
71 data = json.loads(environ["wsgi.input"].read(int(environ["CONTENT_LENGTH "])))
72 except (KeyError, IOError, ValueError):
73 return common.show_error("Error while parsing JSON data.", start_respons e)
74
75 # Make sure the submitted data was contained within an object at least
76 if not isinstance(data, dict):
77 return common.show_error("Error, data must be contained within an object .", start_response)
78
79 # Log the data to a file
80 log_dir = config.get("filterhitstats", "log_dir")
81 try:
82 log_file = log_filterhits(data, log_dir, environ.get("QUERY_STRING", "") )
83 except (OSError, IOError):
84 traceback.print_exc()
85 return common.show_error("Failed to write data to log file!", start_resp onse,
86 "500 Logging error")
87
88 # Update the geometrical_mean aggregations in the database
89 interval = config.get("filterhitstats", "interval")
90 try:
91 db_connection = db.connect()
92 try:
93 db.write(db_connection, geometrical_mean.update(interval, data))
94 finally:
95 db_connection.close()
96 except:
97 # Updating the aggregations in the database failed for whatever reason,
98 # log the details but continue to return 204 to the client to avoid the
99 # re-transmission of data.
100 processing_error_log = os.path.join(config.get("filterhitstats", "log_di r"),
101 "processing-errors.log")
102 with open(processing_error_log, "a+") as f:
103 message = "Problem processing data file %s:\n%s" % (
104 log_file, traceback.format_exc()
105 )
106 print >> f, "[%s] %s" % (datetime.now().strftime("%d/%b/%Y:%H:%M:%S %z"), message)
107
108 # Send back a 204 No Content
109 start_response("204 No Content", [])
110 return []
OLDNEW
« no previous file with comments | « sitescripts/filterhits/web/query.py ('k') | tox.ini » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld