def players_chart(daterange, engine=None): resolution = daterange_resolution(daterange) rows = _build_players_chart_data(daterange, engine) return Chart( id="players-chart", kind="LineChart", options={'title': 'Players per {0}'.format(resolution), 'width': "100%", 'height': 300, 'legend': 'none', 'hAxis': { 'maxTextLines': 1 }, 'vAxis': { 'minValue': 0, 'format': '#.##' }}, columns=[('string', 'Day'), ('number', 'Players')], rows=rows, formatters=[ Formatter( column=1, name="NumberFormat", options={ "fractionDigits": 2 }) ])
def _build_players_chart_data(daterange, engine): resolution = daterange_resolution(daterange) dateslices = RefreshBatch.slice(daterange, resolution) if not dateslices: return [] if resolution == "day": dateformat = "%Y-%m-%d %a" elif resolution == "hour": if dateslices[0].date() == dateslices.last().date(): dateformat = "%H:%M" else: dateformat = "%d %a %H:%M" rows = [] for dateslice in dateslices: dateslice = timezone.localtime(dateslice, timezone.utc) if resolution == "day": nextdate = dateslice + timedelta(days=1) elif resolution == "hour": nextdate = dateslice + timedelta(hours=1) count = BatchStatistics.avg_in_daterange(engine, (dateslice, nextdate)) rows.append((dateslice.strftime(dateformat), count.human_player_count)) return rows