-
Notifications
You must be signed in to change notification settings - Fork 2
/
__main__.py
69 lines (58 loc) · 2.38 KB
/
__main__.py
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
from bitdeli import Profiles, set_theme, Title, Description
from bitdeli.textutil import Percent
from collections import Counter
from itertools import chain, groupby
from datetime import datetime, timedelta
NUM_WEEKS = 4
set_theme('sail')
text = {}
def is_active(profile):
return True
def daily_active(profiles):
def day(hour):
return datetime.utcfromtimestamp(hour * 3600)
days = Counter()
for profile in profiles:
if is_active(profile):
events = chain.from_iterable(profile['events'].itervalues())
days.update(frozenset(day(hour) for hour, freq in events))
return days.iteritems()
def week_to_week(daily_stats):
def weekly_counts():
for week, stats in groupby(daily_stats, lambda day: day[0].isocalendar()[1]):
yield week, sum(count for day, count in stats)
def weekly_growth(weekly_stats):
if len(weekly_stats) > 2:
it = iter(weekly_stats[-(NUM_WEEKS + 2):-1])
week, prev = it.next()
for week, count in it:
yield week, count, float(count - prev) / prev
prev = count
growth = list(weekly_growth(list(weekly_counts())))
for week, count, ratio in growth:
text['week-au'] = count
text['week-growth'] = Percent(ratio)
yield {'type': 'text',
'label': 'week %d' % week,
'size': (2, 1),
'data': {'text': '%d%% (%d AU)' % (100 * ratio, count)}}
avg = sum(ratio for week, count, ratio in growth) / len(growth)
text['growth'] = Percent(avg)
yield {'type': 'text',
'label': 'average week-to-week growth over the past %d weeks' % len(growth),
'size': (6, 2),
'color': 2,
'data': {'head': '%d%%' % (100 * avg)}}
def growth(daily):
stats = list(sorted(daily))
limit = max(stats)[0] - timedelta(days=(NUM_WEEKS + 1) * 7)
dau = {'type': 'line',
'label': 'Daily Active Users',
'data': [(day.isoformat(), count) for day, count in stats\
if day > limit],
'size': (12, 3)}
return chain([dau], week_to_week(stats))
Profiles().map(daily_active).map(growth).show()
Title("Average week-to-week growth is currently {growth}", text)
Description("Last week {week-au:,} active users were recorded. The number {week-growth.verb} by {week-growth} from the week before.",
text)