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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 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 from errno import EEXIST
19 import json, tempfile, time, os
20
21 def show_error(message, start_response, status="400 Processing Error"):
22 start_response(status, [("Content-Type", "text/plain; charset=utf-8")])
23 return [message.encode("utf-8")]
24
25 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.
26 """
27 This logs the provided filterhits data as JSON to a file named after
28 the current timestamp in a directory named after the current date.
29 """
30 now = time.gmtime()
31
32 dir_name = time.strftime("%Y-%m-%d", now)
33 path = os.path.join(basepath, dir_name)
34 try:
35 os.makedirs(path)
36 except OSError as e:
37 if e.errno != EEXIST:
38 raise
39
40 with tempfile.NamedTemporaryFile(
41 prefix = str(int(time.mktime(now))) + "-",
42 suffix = ".log",
43 dir = path,
44 delete = False
45 ) as f:
46 f.write("[%s] \"%s\" %s\n" % (time.strftime('%d/%b/%Y:%H:%M:%S', now),
47 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.
48 return f.name
OLDNEW

Powered by Google App Engine
This is Rietveld