def table(scored): for ratio, count, ccode in scored: arrow = '↓' if ratio < 0 else '↑' yield { 'Country': country_name(ccode), 'Change': '%s %d%%' % (arrow, ratio * 100), 'Users (past %d days)' % TREND_WINDOW: count }
def global_stats(stats): summarized = dict((ccode, sum(days.itervalues())) for ccode, days in stats.iteritems()) top = [{'Country': country_name(ccode), 'Users': users} for ccode, users in sorted(summarized.iteritems(), key=lambda x: x[1], reverse=True)][:TOP_N] for i, item in enumerate(top[:3]): text['top%d' % i] = item['Country'] yield {'type': 'map', 'label': 'User Distribution', 'size': (6, 3), 'data': summarized} yield {'type': 'table', 'label': 'Top Countries', 'size': (4, 3), 'data': top}
def trending(stats): def scores(): now = max(max(days) for days in stats.itervalues()) for ccode, days in stats.iteritems(): cutoff = now - TREND_WINDOW current = sum(days[day] for day in range(cutoff, now + 1)) past = sum(days[day] for day in range(now - (TREND_WINDOW * 2 + 1), cutoff)) if current > TREND_NOISE_LIMIT or past > TREND_NOISE_LIMIT: yield float(current - past) / max(1, past),\ current,\ ccode def table(scored): for ratio, count, ccode in scored: arrow = '↓' if ratio < 0 else '↑' yield { 'Country': country_name(ccode), 'Change': '%s %d%%' % (arrow, ratio * 100), 'Users (past %d days)' % TREND_WINDOW: count } scored = list(sorted(scores(), key=lambda x: abs(x[0]), reverse=True))[:TOP_N] if scored: text['trending'] = country_name(scored[0][2]) text['trend_change'] = Percent(scored[0][0]) yield { 'type': 'map', 'label': 'Trending Countries', 'size': (6, 3), 'color': 2, 'data': dict((ccode, abs(ratio)) for ratio, count, ccode in scored) } yield { 'type': 'table', 'label': 'Trending Countries', 'size': (4, 3), 'data': list(table(scored)) }
def trending(stats): def scores(): now = max(max(days) for days in stats.itervalues()) for ccode, days in stats.iteritems(): cutoff = now - TREND_WINDOW current = sum(days[day] for day in range(cutoff, now + 1)) past = sum(days[day] for day in range(now - (TREND_WINDOW * 2 + 1), cutoff)) if current > TREND_NOISE_LIMIT or past > TREND_NOISE_LIMIT: yield float(current - past) / max(1, past),\ current,\ ccode def table(scored): for ratio, count, ccode in scored: arrow = '↓' if ratio < 0 else '↑' yield {'Country': country_name(ccode), 'Change': '%s %d%%' % (arrow, ratio * 100), 'Users (past %d days)' % TREND_WINDOW: count} scored = list(sorted(scores(), key=lambda x: abs(x[0]), reverse=True))[:TOP_N] if scored: text['trending'] = country_name(scored[0][2]) text['trend_change'] = Percent(scored[0][0]) yield {'type': 'map', 'label': 'Trending Countries', 'size': (6, 3), 'color': 2, 'data': dict((ccode, abs(ratio)) for ratio, count, ccode in scored)} yield {'type': 'table', 'label': 'Trending Countries', 'size': (4, 3), 'data': list(table(scored))}
def global_stats(stats): summarized = dict( (ccode, sum(days.itervalues())) for ccode, days in stats.iteritems()) top = [{ 'Country': country_name(ccode), 'Users': users } for ccode, users in sorted( summarized.iteritems(), key=lambda x: x[1], reverse=True)][:TOP_N] for i, item in enumerate(top[:3]): text['top%d' % i] = item['Country'] yield { 'type': 'map', 'label': 'User Distribution', 'size': (6, 3), 'data': summarized } yield { 'type': 'table', 'label': 'Top Countries', 'size': (4, 3), 'data': top }
import bitdeli import operator from bitdeli.textutil import Percent, country_name from bitdeli.widgets import Map, Title, Description, set_theme set_theme('eighties') for profile in bitdeli.profiles(): pass countries = { 'IN': 0.3, 'PK': 0.18, 'LK': 0.15, 'NP': 0.12, 'BD': 0.08, 'MM': 0.06 } Map(size=(12, 7), data=countries) topcountry = max(countries.iteritems(), key=operator.itemgetter(1))[0] Title(u'Top trending country: {top} \u21E7{value}', { 'top': country_name(topcountry), 'value': Percent(countries[topcountry], 0) }) Description('Active users in {count} countries today', {'count': len(countries)})
import bitdeli import operator from bitdeli.textutil import Percent, country_name from bitdeli.widgets import Map, Title, Description, set_theme set_theme('eighties') for profile in bitdeli.profiles(): pass countries = { 'IN': 0.3, 'PK': 0.18, 'LK': 0.15, 'NP': 0.12, 'BD': 0.08, 'MM': 0.06 } Map(size=(12, 7), data=countries) topcountry = max(countries.iteritems(), key=operator.itemgetter(1))[0] Title(u'Top trending country: {top} \u21E7{value}', { 'top': country_name(topcountry), 'value': Percent(countries[topcountry], 0) }) Description('Active users in {count} countries today', { 'count': len(countries) })
def label(segment, model, params): ccode = segment_country(params) segment_name = "Users" if hasattr(model, "labels"): segment_name = model.labels[0] return "%s from %s" % (segment_name, textutil.country_name(ccode))
def country_label(ccode): name = textutil.country_name(ccode) return "%s (%s)" % (name, ccode) if name else ccode
def table(scored): for ratio, count, ccode in scored: arrow = '↓' if ratio < 0 else '↑' yield {'Country': country_name(ccode), 'Change': '%s %d%%' % (arrow, ratio * 100), 'Users (past %d days)' % TREND_WINDOW: count}