Example #1
0
    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))
Example #2
0
def traffic_totals():
    from r2.models import traffic

    impressions = traffic.AdImpressionsByCodename.historical_totals("day")
    clicks = traffic.ClickthroughsByCodename.historical_totals("day")
    traffic_data = traffic.zip_timeseries(impressions, clicks)
    return [(d.date(), v) for d, v in traffic_data]
Example #3
0
    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))
Example #4
0
def get_promo_traffic(thing, start, end):
    """Get traffic for a Promoted Link or PromoCampaign"""

    use_adserver_reporting = _use_adserver_reporting(thing)
    spent_fn = None

    if isinstance(thing, Link):
        if use_adserver_reporting:
            imp_fn = traffic.AdserverImpressionsByCodename.promotion_history
            click_fn = traffic.AdserverClickthroughsByCodename.promotion_history
            spent_fn = traffic.AdserverSpentPenniesByCodename.promotion_history
        else:
            imp_fn = traffic.AdImpressionsByCodename.promotion_history
            click_fn = traffic.ClickthroughsByCodename.promotion_history
    elif isinstance(thing, PromoCampaign):
        if use_adserver_reporting:
            imp_fn = traffic.AdserverTargetedImpressionsByCodename.promotion_history
            click_fn = traffic.AdserverTargetedClickthroughsByCodename.promotion_history
            spent_fn = traffic.AdserverTargetedSpentPenniesByCodename.promotion_history
        else:
            imp_fn = traffic.TargetedImpressionsByCodename.promotion_history
            click_fn = traffic.TargetedClickthroughsByCodename.promotion_history

    start = start.replace(tzinfo=None)
    end = end.replace(tzinfo=None)

    # adserver is daily aggregate.
    if use_adserver_reporting:
        start = start.replace(hour=0)
        end = end.replace(hour=0)

    imps = imp_fn(thing._fullname, start, end)
    clicks = click_fn(thing._fullname, start, end)
    spent_pennies = spent_fn and spent_fn(thing._fullname, start, end)

    if imps and not clicks:
        clicks = [(imps[0][0], (0, ))]
    if clicks and not imps:
        imps = [(clicks[0][0], (0, ))]

    if imps and not spent_pennies:
        spent_pennies = [(imps[0][0], (0, ))]
    elif clicks and not spent_pennies:
        spent_pennies = [(clicks[0][0], (0, ))]

    args = [
        imps,
        clicks,
    ]

    if use_adserver_reporting:
        spent = [(date, (values[0] / 100., ))
                 for date, values in spent_pennies]
        args.append(spent)

    history = traffic.zip_timeseries(*args, order="ascending")
    return history
Example #5
0
    def get_data_for_interval(self, interval, columns):
        columns[1]["title"] = _("impressions by %s" % interval)
        columns[1]["shortname"] = _("impressions")

        columns += [
            dict(shortname=_("unique clicks")),
            dict(color=COLORS.MISCELLANEOUS,
                 title=_("clicks by %s" % interval),
                 shortname=_("total clicks")),
        ]

        imps = traffic.AdImpressionsByCodename.history(interval, self.code)
        clicks = traffic.ClickthroughsByCodename.history(interval, self.code)
        return traffic.zip_timeseries(imps, clicks)
Example #6
0
    def make_tables(self):
        start, end = promote.get_total_run(self.thing)

        if not start or not end:
            self.history = []
            return

        cutoff = end - datetime.timedelta(days=31)
        start = max(start, cutoff)

        fullname = self.thing._fullname
        imps = traffic.AdImpressionsByCodename.promotion_history(fullname,
                                                                 start, end)
        clicks = traffic.ClickthroughsByCodename.promotion_history(fullname,
                                                                   start, end)

        # promotion might have no clicks, zip_timeseries needs valid columns
        if imps and not clicks:
            clicks = [(imps[0][0], (0, 0))]

        history = traffic.zip_timeseries(imps, clicks, order="ascending")
        computed_history = []
        self.total_impressions, self.total_clicks = 0, 0
        for date, data in history:
            u_imps, imps, u_clicks, clicks = data

            u_ctr = _clickthrough_rate(u_imps, u_clicks)
            ctr = _clickthrough_rate(imps, clicks)

            self.total_impressions += imps
            self.total_clicks += clicks
            computed_history.append((date, data + (u_ctr, ctr)))

        self.history = computed_history

        if self.total_impressions > 0:
            self.total_ctr = _clickthrough_rate(self.total_impressions,
                                                self.total_clicks)

        # XXX: _is_promo_preliminary correctly expects tz-aware datetimes
        # because it's also used with datetimes from promo code. this hack
        # relies on the fact that we're storing UTC w/o timezone info.
        # TODO: remove this when traffic is correctly using timezones.
        end_aware = end.replace(tzinfo=g.tz)
        self.is_preliminary = _is_promo_preliminary(end_aware)

        # we should only graph a sane number of data points (not everything)
        self.max_points = traffic.points_for_interval("hour")

        return computed_history
Example #7
0
    def get_data_for_interval(self, interval, columns):
        pageviews = traffic.PageviewsBySubreddit.history(interval, c.site.name)

        if interval == "day":
            columns.append(dict(color=COLORS.MISCELLANEOUS,
                                title=_("subscriptions by day"),
                                shortname=_("subscriptions")))

            sr_name = c.site.name
            subscriptions = traffic.SubscriptionsBySubreddit.history(interval,
                                                                     sr_name)

            return traffic.zip_timeseries(pageviews, subscriptions)
        else:
            return pageviews
Example #8
0
    def get_tables(self):
        start, end = promote.get_total_run(self.thing)

        if not start or not end:
            self.history = []
            return

        cutoff = end - datetime.timedelta(days=31)
        start = max(start, cutoff)

        fullname = self.thing._fullname
        imps = traffic.AdImpressionsByCodename.promotion_history(
            fullname, start, end)
        clicks = traffic.ClickthroughsByCodename.promotion_history(
            fullname, start, end)

        # promotion might have no clicks, zip_timeseries needs valid columns
        if imps and not clicks:
            clicks = [(imps[0][0], (0, 0))]

        history = traffic.zip_timeseries(imps, clicks, order="ascending")
        computed_history = []
        self.total_impressions, self.total_clicks = 0, 0
        for date, data in history:
            u_imps, imps, u_clicks, clicks = data

            u_ctr = self.calculate_clickthrough_rate(u_imps, u_clicks)
            ctr = self.calculate_clickthrough_rate(imps, clicks)

            self.total_impressions += imps
            self.total_clicks += clicks
            computed_history.append((date, data + (u_ctr, ctr)))

        self.history = computed_history

        if self.total_impressions > 0:
            self.total_ctr = (
                (float(self.total_clicks) / self.total_impressions) * 100.)

        # the results are preliminary until 1 day after the promotion ends
        now = datetime.datetime.utcnow()
        self.is_preliminary = end + datetime.timedelta(days=1) > now

        # we should only graph a sane number of data points (not everything)
        self.max_points = traffic.points_for_interval("hour")

        return computed_history
Example #9
0
def get_promo_traffic(thing, start, end):
    """Get traffic for a Promoted Link or PromoCampaign"""
    if isinstance(thing, Link):
        imp_fn = traffic.AdImpressionsByCodename.promotion_history
        click_fn = traffic.ClickthroughsByCodename.promotion_history
    elif isinstance(thing, PromoCampaign):
        imp_fn = traffic.TargetedImpressionsByCodename.promotion_history
        click_fn = traffic.TargetedClickthroughsByCodename.promotion_history

    imps = imp_fn(thing._fullname, start.replace(tzinfo=None),
                  end.replace(tzinfo=None))
    clicks = click_fn(thing._fullname, start.replace(tzinfo=None),
                      end.replace(tzinfo=None))

    if imps and not clicks:
        clicks = [(imps[0][0], (0, ))]

    history = traffic.zip_timeseries(imps, clicks, order="ascending")
    return history
Example #10
0
def traffic_totals():
    from r2.models import traffic
    impressions = traffic.AdImpressionsByCodename.historical_totals("day")
    clicks = traffic.ClickthroughsByCodename.historical_totals("day")
    traffic_data = traffic.zip_timeseries(impressions, clicks)
    return [(d.date(), v) for d, v in traffic_data]
Example #11
0
    def make_tables(self):
        now = datetime.datetime.utcnow().replace(minute=0, second=0,
                                                 microsecond=0)

        promo_start, promo_end = promote.get_total_run(self.thing)
        promo_end = min(now, promo_end)

        if not promo_start or not promo_end:
            self.history = []
            return

        if self.period:
            start = self.after
            end = self.before

            if not start and not end:
                end = promo_end
                start = end - self.period

            elif not end:
                end = start + self.period

            elif not start:
                start = end - self.period

            if start > promo_start:
                p = request.get.copy()
                p.update({'after':None, 'before':start.strftime('%Y%m%d%H')})
                self.prev = '%s?%s' % (request.path, urllib.urlencode(p))
            else:
                start = promo_start

            if end < promo_end:
                p = request.get.copy()
                p.update({'after':end.strftime('%Y%m%d%H'), 'before':None})
                self.next = '%s?%s' % (request.path, urllib.urlencode(p))
            else:
                end = promo_end
        else:
            start, end = promo_start, promo_end

        fullname = self.thing._fullname
        imps = traffic.AdImpressionsByCodename.promotion_history(fullname,
                                                                 start, end)
        clicks = traffic.ClickthroughsByCodename.promotion_history(fullname,
                                                                   start, end)

        # promotion might have no clicks, zip_timeseries needs valid columns
        if imps and not clicks:
            clicks = [(imps[0][0], (0, 0))]

        history = traffic.zip_timeseries(imps, clicks, order="ascending")
        computed_history = []
        self.total_impressions, self.total_clicks = 0, 0
        for date, data in history:
            u_imps, imps, u_clicks, clicks = data

            u_ctr = _clickthrough_rate(u_imps, u_clicks)
            ctr = _clickthrough_rate(imps, clicks)

            self.total_impressions += imps
            self.total_clicks += clicks
            computed_history.append((date, data + (u_ctr, ctr)))

        self.history = computed_history

        if self.total_impressions > 0:
            self.total_ctr = _clickthrough_rate(self.total_impressions,
                                                self.total_clicks)

        # XXX: _is_promo_preliminary correctly expects tz-aware datetimes
        # because it's also used with datetimes from promo code. this hack
        # relies on the fact that we're storing UTC w/o timezone info.
        # TODO: remove this when traffic is correctly using timezones.
        end_aware = end.replace(tzinfo=g.tz)
        self.is_preliminary = _is_promo_preliminary(end_aware)

        # we should only graph a sane number of data points (not everything)
        self.max_points = traffic.points_for_interval("hour")

        return computed_history