Skip to content

Commit

Permalink
add a map and daily uniques
Browse files Browse the repository at this point in the history
  • Loading branch information
tuulos authored and bitdeli-commits committed Jan 4, 2013
1 parent db78a6c commit 96d472c
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion __main__.py
@@ -1 +1,72 @@
print 'stub'
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)

0 comments on commit 96d472c

Please sign in to comment.