-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
170 lines (143 loc) · 5.26 KB
/
data.py
File metadata and controls
170 lines (143 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from pymongo import MongoClient
from dateutil.relativedelta import relativedelta
from datetime import datetime
from flask import Flask
from flask import g
from bson import json_util
import pymongo
import flask
import simplejson as json
import functools
import locale
import math
app = Flask(__name__)
def with_db(method):
@functools.wraps(method)
def wrapper(*args, **kwargs):
if not hasattr(g, 'mongo'):
g.mongo = MongoClient('localhost', 30000)
result = method(*args, **kwargs)
g.mongo.close()
return result
return wrapper
@app.route('/')
def index():
return flask.render_template('index.html')
@app.route('/monthly')
@with_db
def monthly():
return json.dumps([x for x in g.mongo.mongousage["gen.monthly"].find().sort("_id", pymongo.DESCENDING)])
@app.route('/weekly')
@with_db
def weekly():
return json.dumps([x for x in g.mongo.mongousage['gen.weekly'].find().sort('_id', pymongo.DESCENDING)])
@app.route('/os')
@with_db
def os():
os = []
for i in g.mongo.mongousage['gen.firstPiece.day7'].find().sort('value', pymongo.DESCENDING):
bad = False
for j in ('.', '-', 'log', 'stats'):
if i['_id'].find(j) >= 0:
bad = True
break
if bad:
continue
os.append(i)
return json.dumps(os)
@app.route('/os_monthly')
@with_db
def os_monthly():
return json.dumps([x for x in g.mongo.mongousage['gen.monthly.os'].find().sort('_id', pymongo.DESCENDING)])
@app.route('/versions')
@with_db
def versions():
return json.dumps([x for x in g.mongo.mongousage['gen.monthly.version'].find().sort('_id', pymongo.DESCENDING)], default=json_util.default)
@app.route('/numbers')
def numbers():
return flask.render_template('numbers.html')
@app.route('/viz')
def viz():
return flask.render_template('viz.html')
@app.route('/pie_viz')
def pie_viz():
return flask.render_template('pie_viz.html')
@app.route('/line_viz')
def line_viz():
return flask.render_template('line_viz.html')
@app.route('/stack')
def stack():
return flask.render_template('stack.html')
@app.route('/email_template')
@with_db
def email_template():
def average(data, key):
total = 0
for d in data:
total += d['value'][key]
return total / float(len(data))
def growth(data, key):
"""
Returns average growth from month to month
"""
total = 0
num = 0
for i, d in enumerate(data):
if i == len(data)-1:
break
total += data[i+1]['value'][key] - d['value'][key]
num += 1
return total / float(num)
def get_point(data, date):
for d in data:
if datetime.strptime(d['_id'], '%Y-%m') == date:
return d
def i(d):
locale.setlocale(locale.LC_ALL, 'en_US')
return locale.format("%d", d, grouping=True)
def f(i):
return "{0:.2f}%".format(i * 100)
monthly = [x for x in g.mongo.mongousage["gen.monthly"].find().sort("_id", pymongo.DESCENDING)]
now = datetime(2013, 12, 1)
previous = now - relativedelta(months=1)
now_point = get_point(monthly, now)
previous_point = get_point(monthly, previous)
total_growth = growth(monthly[1:], 'total') + 2000
unique_growth = growth(monthly[1:], 'unique') + 2000
projected_total = math.floor(average(monthly[1:], 'total') + total_growth)
projected_unique = math.floor(average(monthly[1:], 'unique') + unique_growth)
total_change_value = projected_total - previous_point['value']['total']
total_change_percent = float(total_change_value) / float(previous_point['value']['total'])
unique_change_value = projected_unique - previous_point['value']['unique']
unique_change_percent = float(unique_change_value) / float(previous_point['value']['unique'])
if total_change_value > 0:
total_change = 2
elif total_change_value < 0:
total_change = 1
else:
total_change = 0
if unique_change_value > 0:
unique_change = 2
elif unique_change_value < 0:
unique_change = 1
else:
unique_change = 0
return flask.render_template('email_template.html',
previous_date = previous,
current_date = now,
previous_total=i(previous_point['value']['total']),
projected_total=i(projected_total),
total_change_value=i(abs(total_change_value)),
total_change_percent=f(total_change_percent),
previous_unique=i(previous_point['value']['unique']),
projected_unique=i(projected_unique),
unique_change_value=i(abs(unique_change_value)),
unique_change_percent=f(unique_change_percent),
current_total=i(now_point['value']['total']),
current_unique=i(now_point['value']['unique']),
total_change=total_change,
unique_change=unique_change,
)
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=8000)