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

Unified Diff: sitescripts/filterhits/common.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Display friendly message if processing script can't connect to DB. Created March 2, 2015, 11:14 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
« no previous file with comments | « sitescripts/filterhits/bin/process_logs.py ('k') | sitescripts/filterhits/db.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/filterhits/common.py
diff --git a/sitescripts/filterhits/common.py b/sitescripts/filterhits/common.py
index e32ffe9cb52349f726a2d2fe1565d5db7446c86c..661c5233a0fa1eb9b28cff45d29c0331ce43933e 100644
--- a/sitescripts/filterhits/common.py
+++ b/sitescripts/filterhits/common.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
@@ -15,71 +15,34 @@
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
-from datetime import datetime
-import json, tempfile, os, MySQLdb
+from errno import EEXIST
+import json, tempfile, time, os
-def _get_db(config):
- return MySQLdb.connect(
- user=config.get("filterhitstats", "dbuser"),
- passwd=config.get("filterhitstats", "dbpassword"),
- db=config.get("filterhitstats", "database"),
- use_unicode=True, charset="utf8"
- )
+def show_error(message, start_response, status="400 Processing Error"):
+ start_response(status, [("Content-Type", "text/plain; charset=utf-8")])
+ return [message.encode("utf-8")]
-def datetime_to_timestamp(dt):
- return int((dt - datetime(1970, 1, 1)).total_seconds())
-
-def valid_filter_hit(filter_hit):
- return (
- isinstance(filter_hit, dict) and
- "thirdParty" in filter_hit and
- isinstance(filter_hit["thirdParty"], dict) and
- "firstParty" in filter_hit and
- isinstance(filter_hit["firstParty"], dict) and
- "subscriptions" in filter_hit and
- isinstance(filter_hit["subscriptions"], (list, tuple))
- )
-
-def valid_log_data(data):
- """
- This returns True if the filterhits data passed is structured
- roughly OK. Used as a quick check, it's not comprehensive.
- """
- return (
- isinstance(data, dict) and
- "version" in data and
- "timeSincePush" in data and
- "addonName" in data and
- "version" in data and
- "timeSincePush" in data and
- "addonName" in data and
- "addonVersion" in data and
- "application" in data and
- "applicationVersion" in data and
- "platform" in data and
- "platformVersion" in data and
- "filters" in data and
- isinstance(data["filters"], dict) and
- (not len(data["filters"]) or
- valid_filter_hit(data["filters"].itervalues().next()))
- )
-
-def log_filterhits(data, basepath):
+def log_filterhits(data, basepath, query_string):
"""
- This logs the provided filterhits data as JSON to a file named after
- the current timestamp in a directory named after the current date.
+ This logs the provided filterhits data as JSON to a file named after
+ the current timestamp in a directory named after the current date.
"""
- now = datetime.now()
+ now = time.gmtime()
- dir_name = now.strftime("%Y-%m-%d")
+ dir_name = time.strftime("%Y-%m-%d", now)
path = os.path.join(basepath, dir_name)
- if not os.path.exists(path):
+ try:
os.makedirs(path)
+ except OSError as e:
+ if e.errno != EEXIST:
+ raise
with tempfile.NamedTemporaryFile(
- prefix = str(datetime_to_timestamp(now)) + "-",
+ prefix = str(int(time.mktime(now))) + "-",
suffix = ".log",
dir = path,
delete = False
) as f:
- f.write(json.dumps(data) + "\n")
+ f.write("[%s] \"%s\" %s\n" % (time.strftime('%d/%b/%Y:%H:%M:%S', now),
+ query_string, json.dumps(data)))
+ return f.name
« no previous file with comments | « sitescripts/filterhits/bin/process_logs.py ('k') | sitescripts/filterhits/db.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld