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

Unified Diff: sitescripts/filterhits/web/submit.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Improvements regarding comments Created Feb. 17, 2015, 10:50 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« sitescripts/filterhits/web/query.py ('K') | « sitescripts/filterhits/web/query.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/filterhits/web/submit.py
diff --git a/sitescripts/filterhits/web/submit.py b/sitescripts/filterhits/web/submit.py
index 3b4bd1c14c4536d09b68c19d4f284f3b591d13bf..26e945613c57bb70566c64bbcd7b39b6db45de12 100644
--- a/sitescripts/filterhits/web/submit.py
+++ b/sitescripts/filterhits/web/submit.py
@@ -1,7 +1,7 @@
# coding: utf-8
# This file is part of the Adblock Plus web scripts,
-# Copyright (C) 2006-2014 Eyeo GmbH
+# Copyright (C) 2006-2015 Eyeo GmbH
#
# Adblock Plus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
@@ -30,14 +30,14 @@ def submit(environ, start_response):
config = get_config()
# Check that this is a POST request
- if environ["REQUEST_METHOD"].upper() != "POST":
+ if environ["REQUEST_METHOD"] != "POST":
return common.showError("Unsupported request method", start_response)
# Parse the submitted JSON
data = "{}"
try:
- data_length = int(environ.get("CONTENT_LENGTH", "0"))
- except ValueError:
+ data_length = int(environ["CONTENT_LENGTH"])
+ except (ValueError, KeyError):
data_length = 0
if data_length != 0:
data = environ["wsgi.input"].read(data_length)
@@ -46,26 +46,22 @@ def submit(environ, start_response):
except json.decoder.JSONDecodeError:
return common.showError("Error while parsing JSON data.", start_response)
- # Make sure it looks roughly valid
- if not common.valid_log_data(data):
- return common.showError("Data looks invalid.", start_response)
-
# Log the data to a file
log_dir = config.get("filterhitstats", "log_dir")
try:
log_file = common.log_filterhits(data, log_dir,
environ.get("QUERY_STRING", ""))
- except OSError, IOError:
+ except (OSError, IOError):
return common.showError("Failed to write data to log file!", start_response,
"500 Logging error")
# Update the geometrical_mean aggregations in the database
interval = config.get("filterhitstats", "interval")
try:
- db.connect(config.get("filterhitstats", "dbuser"),
- config.get("filterhitstats", "dbpassword"),
- config.get("filterhitstats", "database"))
- db.write(geometrical_mean.update(interval, data))
+ db_connection = db.connect(config.get("filterhitstats", "dbuser"),
+ config.get("filterhitstats", "dbpassword"),
+ config.get("filterhitstats", "database"))
+ db.write(db_connection, geometrical_mean.update(interval, data))
except MySQLdb.Error, e:
# Updating the aggregations in the database failed for whatever reason,
# log the details but continue to return 200 OK to the client to avoid
@@ -78,7 +74,8 @@ def submit(environ, start_response):
e.args[0], log_file, e.args[1]
))
finally:
- db.disconnect()
+ 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.
+ db_connection.close()
# Send back a 200 OK response
response_headers = [("Content-type", "text/plain")]
« 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