-
Notifications
You must be signed in to change notification settings - Fork 4
/
__main__.py
72 lines (54 loc) · 2.16 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
70
71
from bitdeli.widgets import set_theme, Description, Title
from bitdeli.chain import Profiles
from collections import Counter, defaultdict
from datetime import datetime, timedelta
import GeoIP
set_theme('eighties')
TIMELINE_DAYS = 30
TFORMAT = '%Y-%m-%d'
geoip = GeoIP.open('/usr/share/geoip/GeoLiteCity.dat', GeoIP.GEOIP_STANDARD)
text = {'window': TIMELINE_DAYS}
def countries(profiles):
stats = Counter()
for profile in profiles:
country = geoip.record_by_addr(profile.uid)['country_code']
stats[country] += 1
yield stats
def activity(profiles):
limit = datetime.now() - timedelta(days=TIMELINE_DAYS)
limit_str = limit.strftime(TFORMAT)
def recent_days(visits):
for visit in visits:
day = visit['tstamp'].split('T')[0]
if day >= limit_str:
yield day
def timeline(stats):
for i in range(TIMELINE_DAYS + 1):
day = (limit + timedelta(days=i)).strftime(TFORMAT)
yield day, stats[day]
def popularity(repos):
for repo, visits in repos.iteritems():
yield sum(visits.itervalues()), repo
repos = defaultdict(Counter)
for profile in profiles:
for repo, visits in profile['repos'].iteritems():
repos[repo].update(frozenset(recent_days(visits)))
pop = list(popularity(repos))
text['popular'] = max(pop)
text['total'] = sum(visits for visits, repo in pop)
yield {'type': 'text',
'size': (12, 1),
'head': 'Daily Unique Visitors'}
for repo, stats in sorted(repos.iteritems()):
yield {'type': 'line',
'label': repo.split('/')[1],
'data': list(timeline(stats)),
'size': (6, 2)}
Profiles().map(activity).show()
Profiles().map(countries).show('map',
label='Visitors',
size=(12, 4))
Title("Repos have {total} daily unique visitors in total over the last {window} days",
text)
Description("The most popular repository is *{popular[1]}* with {popular[0]} visitors.",
text)