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

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

Issue 5662084981325824: Fixed last day of month being below the fold and got rid of a duplicated constant (Closed)
Patch Set: Created Jan. 13, 2014, 7:24 a.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 | « no previous file | sitescripts/stats/common.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2013 Eyeo GmbH 4 # Copyright (C) 2006-2013 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 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 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 15 matching lines...) Expand all
26 return get_custom_template_environment({ 26 return get_custom_template_environment({
27 "monthname": lambda value: date(int(value[0:4]), int(value[4:]), 1).strftime ("%b %Y"), 27 "monthname": lambda value: date(int(value[0:4]), int(value[4:]), 1).strftime ("%b %Y"),
28 "weekday": lambda value: ["Monday", "Tuesday", "Wednesday", "Thursday", "Fri day", "Saturday", "Sunday"][int(value)], 28 "weekday": lambda value: ["Monday", "Tuesday", "Wednesday", "Thursday", "Fri day", "Saturday", "Sunday"][int(value)],
29 "countryname": lambda value: countrycodes.get(value, "Unknown"), 29 "countryname": lambda value: countrycodes.get(value, "Unknown"),
30 "sortfield": lambda value, field: (field["sort"] if "sort" in field else def ault_sort)(value), 30 "sortfield": lambda value, field: (field["sort"] if "sort" in field else def ault_sort)(value),
31 "maxhits": lambda items: max(itertools.chain((value["hits"] for key, value i n items), [1])), 31 "maxhits": lambda items: max(itertools.chain((value["hits"] for key, value i n items), [1])),
32 "maxbandwidth": lambda items: max(itertools.chain((value["bandwidth"] for ke y, value in items), [1])), 32 "maxbandwidth": lambda items: max(itertools.chain((value["bandwidth"] for ke y, value in items), [1])),
33 "sumhits": lambda items: max(sum(value["hits"] for key, value in items), 1), 33 "sumhits": lambda items: max(sum(value["hits"] for key, value in items), 1),
34 "sumbandwidth": lambda items: max(sum(value["bandwidth"] for key, value in i tems), 1), 34 "sumbandwidth": lambda items: max(sum(value["bandwidth"] for key, value in i tems), 1),
35 "isspecial": lambda name, field: field["isspecial"](name) if "isspecial" in field else False, 35 "isspecial": lambda name, field: field["isspecial"](name) if "isspecial" in field else False,
36 "defaultcount": get_default_count,
36 }) 37 })
37 38
38 @cached(float("inf")) 39 @cached(float("inf"))
39 def get_main_page_template(): 40 def get_main_page_template():
40 return get_template_environment().get_template(get_config().get("stats", "main PageTemplate")) 41 return get_template_environment().get_template(get_config().get("stats", "main PageTemplate"))
41 42
42 @cached(float("inf")) 43 @cached(float("inf"))
43 def get_file_stats_template(): 44 def get_file_stats_template():
44 return get_template_environment().get_template(get_config().get("stats", "file PageTemplate")) 45 return get_template_environment().get_template(get_config().get("stats", "file PageTemplate"))
45 46
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 "url": url, 87 "url": url,
87 "data": data 88 "data": data
88 }).dump(outputfile, encoding="utf-8") 89 }).dump(outputfile, encoding="utf-8")
89 90
90 def get_names(dir, needdirectories): 91 def get_names(dir, needdirectories):
91 for file in os.listdir(dir): 92 for file in os.listdir(dir):
92 path = os.path.join(dir, file) 93 path = os.path.join(dir, file)
93 if (needdirectories and os.path.isdir(path)) or (not needdirectories and os. path.isfile(path)): 94 if (needdirectories and os.path.isdir(path)) or (not needdirectories and os. path.isfile(path)):
94 yield common.filename_decode(file), path 95 yield common.filename_decode(file), path
95 96
97 def get_default_count(field):
98 return field.get("defaultcount", 30)
99
96 def generate_pages(datadir, outputdir): 100 def generate_pages(datadir, outputdir):
97 for server_type, server_type_dir in get_names(datadir, True): 101 for server_type, server_type_dir in get_names(datadir, True):
98 baseURL = get_config().get("stats", "baseURL_" + server_type) 102 baseURL = get_config().get("stats", "baseURL_" + server_type)
99 filedata = {} 103 filedata = {}
100 current_month = None 104 current_month = None
101 for month, month_dir in get_names(server_type_dir, True): 105 for month, month_dir in get_names(server_type_dir, True):
102 if current_month == None or month > current_month: 106 if current_month == None or month > current_month:
103 current_month = month 107 current_month = month
104 108
105 for filename, path in get_names(month_dir, False): 109 for filename, path in get_names(month_dir, False):
106 filename = re.sub(r"\.json$", "", filename) 110 filename = re.sub(r"\.json$", "", filename)
107 with codecs.open(path, "rb", encoding="utf-8") as file: 111 with codecs.open(path, "rb", encoding="utf-8") as file:
108 data = json.load(file) 112 data = json.load(file)
109 113
110 overview_url = "../../overview-" + common.filename_encode(filename + ".h tml") 114 overview_url = "../../overview-" + common.filename_encode(filename + ".h tml")
111 filtered_urls = {} 115 filtered_urls = {}
112 for field in common.fields: 116 for field in common.fields:
113 if field["name"] not in data: 117 if field["name"] not in data:
114 continue 118 continue
115 # Create filtered views for the first thirty values of a field if they 119 # Create filtered views for the first thirty values of a field if they
116 # have filtered data. 120 # have filtered data.
117 for name, value in get_template_environment().filters["sortfield"](dat a[field["name"]], field)[0:30]: 121 sorted_field = get_template_environment().filters["sortfield"](data[fi eld["name"]], field)
122 for name, value in sorted_field[0:get_default_count(field)]:
118 if filter(lambda k: k not in ("hits", "bandwidth"), value.iterkeys() ): 123 if filter(lambda k: k not in ("hits", "bandwidth"), value.iterkeys() ):
119 outputfile = os.path.join(outputdir, 124 outputfile = os.path.join(outputdir,
120 common.filename_encode(server_type), 125 common.filename_encode(server_type),
121 common.filename_encode(month), 126 common.filename_encode(month),
122 common.filename_encode(filename), 127 common.filename_encode(filename),
123 "filtered-%s-%s.html" % ( 128 "filtered-%s-%s.html" % (
124 common.filename_encode(field["name"]), 129 common.filename_encode(field["name"]),
125 common.filename_encode(name), 130 common.filename_encode(name),
126 )) 131 ))
127 generate_file_stats(outputfile, month, baseURL + filename, overvie w_url, 132 generate_file_stats(outputfile, month, baseURL + filename, overvie w_url,
(...skipping 30 matching lines...) Expand all
158 163
159 outputfile = os.path.join(outputdir, common.filename_encode(server_type), "i ndex.html") 164 outputfile = os.path.join(outputdir, common.filename_encode(server_type), "i ndex.html")
160 generate_main_page(outputfile, current_month, baseURL, monthdata) 165 generate_main_page(outputfile, current_month, baseURL, monthdata)
161 166
162 if __name__ == '__main__': 167 if __name__ == '__main__':
163 setupStderr() 168 setupStderr()
164 169
165 datadir = get_config().get("stats", "dataDirectory") 170 datadir = get_config().get("stats", "dataDirectory")
166 outputdir = get_config().get("stats", "outputDirectory") 171 outputdir = get_config().get("stats", "outputDirectory")
167 generate_pages(datadir, outputdir) 172 generate_pages(datadir, outputdir)
OLDNEW
« no previous file with comments | « no previous file | sitescripts/stats/common.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld