def make_tables(self): # overall promoted link traffic impressions = traffic.AdImpressionsByCodename.historical_totals("day") clicks = traffic.ClickthroughsByCodename.historical_totals("day") data = traffic.zip_timeseries(impressions, clicks) columns = [ dict(color=COLORS.UPVOTE_ORANGE, title=_("total impressions by day"), shortname=_("impressions")), dict(color=COLORS.DOWNVOTE_BLUE, title=_("total clicks by day"), shortname=_("clicks")), ] self.totals = TimeSeriesChart("traffic-ad-totals", _("ad totals"), "day", columns, data, self.traffic_last_modified, classes=["traffic-table"]) # get summary of top ads advert_summary = traffic.AdImpressionsByCodename.top_last_month() things = AdvertTrafficSummary.get_things( ad for ad, data in advert_summary) self.advert_summary = [] for id, data in advert_summary: name = AdvertTrafficSummary.get_ad_name(id, things=things) url = AdvertTrafficSummary.get_ad_url(id, things=things) self.advert_summary.append(((name, url), data))
def get_tables(self): self.tables = [] for interval in ("month", "day", "hour"): columns = [ dict(color=COLORS.UPVOTE_ORANGE, title=_("uniques by %s" % interval), shortname=_("uniques")), dict(color=COLORS.DOWNVOTE_BLUE, title=_("pageviews by %s" % interval), shortname=_("pageviews")), ] data = self.get_data_for_interval(interval, columns) title = _("traffic by %s" % interval) graph = TimeSeriesChart("traffic-" + interval, title, interval, columns, data, self.traffic_last_modified, classes=["traffic-table"]) self.tables.append(graph) try: self.dow_summary = self.get_dow_summary() except NotImplementedError: self.dow_summary = None else: uniques_total = collections.Counter() pageviews_total = collections.Counter() days_total = collections.Counter() # don't include the latest (likely incomplete) day for date, (uniques, pageviews) in self.dow_summary[1:]: dow = date.weekday() uniques_total[dow] += uniques pageviews_total[dow] += pageviews days_total[dow] += 1 # make a summary of the averages for each day of the week self.dow_summary = [] for dow in xrange(7): day_count = days_total[dow] if day_count: avg_uniques = uniques_total[dow] / day_count avg_pageviews = pageviews_total[dow] / day_count self.dow_summary.append( (dow, (avg_uniques, avg_pageviews))) else: self.dow_summary.append((dow, (0, 0))) # calculate the averages for *any* day of the week mean_uniques = sum(r[1][0] for r in self.dow_summary) / 7.0 mean_pageviews = sum(r[1][1] for r in self.dow_summary) / 7.0 self.dow_means = (round(mean_uniques), round(mean_pageviews))