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: Simplified geometrical_mean code and reduced filter inserts. Created March 16, 2015, 4:24 p.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
Index: sitescripts/filterhits/common.py
diff --git a/sitescripts/filterhits/common.py b/sitescripts/filterhits/common.py
new file mode 100644
index 0000000000000000000000000000000000000000..661c5233a0fa1eb9b28cff45d29c0331ce43933e
--- /dev/null
+++ b/sitescripts/filterhits/common.py
@@ -0,0 +1,48 @@
+# coding: utf-8
+
+# This file is part of the Adblock Plus web scripts,
+# 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
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# 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 errno import EEXIST
+import json, tempfile, time, os
+
+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 log_filterhits(data, basepath, query_string):
Wladimir Palant 2015/03/26 22:56:50 This isn't a common function, it is only being use
kzar 2015/03/27 11:59:57 Done.
+ """
+ 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 = time.gmtime()
+
+ dir_name = time.strftime("%Y-%m-%d", now)
+ path = os.path.join(basepath, dir_name)
+ try:
+ os.makedirs(path)
+ except OSError as e:
+ if e.errno != EEXIST:
+ raise
+
+ with tempfile.NamedTemporaryFile(
+ prefix = str(int(time.mktime(now))) + "-",
+ suffix = ".log",
+ dir = path,
+ delete = False
+ ) as f:
+ f.write("[%s] \"%s\" %s\n" % (time.strftime('%d/%b/%Y:%H:%M:%S', now),
+ query_string, json.dumps(data)))
Wladimir Palant 2015/03/26 22:56:50 Nit: you can use |print >>f, ...| - then you won't
kzar 2015/03/27 11:59:57 Well apparently that's "unpythonic" I don't mind e
Sebastian Noack 2015/03/27 13:12:19 I don't agree. I agree though that >> syntax is ki
kzar 2015/03/27 15:10:50 Done.
+ return f.name

Powered by Google App Engine
This is Rietveld