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

Side by Side Diff: sitescripts/stats/bin/datamerger.py

Issue 5182947690807296: Centralize stats processing, have the stats server pull in logs (Closed)
Patch Set: Fixed comment and processing of non-Unicode keys Created Dec. 26, 2013, 2:09 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.example ('k') | sitescripts/stats/bin/logprocessor.py » ('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-2013 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 os, sys, re, codecs, subprocess, urllib, simplejson, traceback
19 import sitescripts.stats.common as common
20 from sitescripts.utils import get_config, setupStderr
21
22 def read_stats_file(path):
23 match = re.search(r"^ssh://(\w+)@([^/:]+)(?::(\d+))?", path)
24 if match:
25 command = ["ssh", "-q", "-o", "NumberOfPasswordPrompts 0", "-T", "-k", "-l", match.group(1), match.group(2)]
26 if match.group(3):
27 command[1:1] = ["-P", match.group(3)]
28 data = subprocess.check_output(command)
29 return simplejson.loads(data.decode("utf-8"))
30 elif path.startswith("http://") or path.startswith("https://"):
31 return simplejson.load(urllib.urlopen(path).read().decode("utf-8"))
32 elif os.path.exists(path):
33 with codecs.open(path, "rb", encoding="utf-8") as file:
34 return simplejson.load(file)
35
36 raise IOError("Path '%s' not recognized" % path)
37
38 def get_stats_files(mirrors):
39 config = get_config()
40
41 if len(mirrors) > 0:
42 options = map(lambda m: "mirror_" + m, mirrors)
43 else:
44 options = filter(lambda o: o.startswith("mirror_"), config.options("stats"))
45 for option in options:
46 if config.has_option("stats", option):
47 value = config.get("stats", option)
48 if " " in value:
49 yield value.split(None, 1)
50 else:
51 print >>sys.stderr, "Option '%s' has invalid value: '%s'" % (option, val ue)
52 else:
53 print >>sys.stderr, "Option '%s' not found in the configuration" % option
54
55 def merge_objects(object1, object2):
56 for key, value in object2.iteritems():
57 if key in object1:
58 if isinstance(value, int):
59 object1[key] += value
60 else:
61 merge_objects(object1[key], object2[key])
62 else:
63 object1[key] = value
64
65 def merge_stats_file(server_type, data):
66 base_dir = os.path.join(get_config().get("stats", "dataDirectory"), common.fil ename_encode(server_type))
67 for month, month_data in data.iteritems():
68 for name, file_data in month_data.iteritems():
69 path = os.path.join(base_dir, common.filename_encode(month), common.filena me_encode(name + ".json"))
70 if os.path.exists(path):
71 with codecs.open(path, "rb", encoding="utf-8") as file:
72 existing = simplejson.load(file)
73 else:
74 existing = {}
75
76 merge_objects(existing, file_data)
77
78 dir = os.path.dirname(path)
79 try:
80 os.makedirs(dir)
81 except OSError:
82 pass
83
84 with codecs.open(path, "wb", encoding="utf-8") as file:
85 simplejson.dump(existing, file, indent=2, sort_keys=True)
86
87 def merge_mirror_stats(mirrors):
88 for server_type, path in get_stats_files(mirrors):
89 try:
90 merge_stats_file(server_type, read_stats_file(path))
91 except:
92 print >>sys.stderr, "Unable to merge stats for '%s'" % path
93 traceback.print_exc()
94
95 if __name__ == "__main__":
96 setupStderr()
97 merge_mirror_stats(sys.argv[1:])
OLDNEW
« no previous file with comments | « .sitescripts.example ('k') | sitescripts/stats/bin/logprocessor.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld