Esempio n. 1
0
    def creation_milestone(self, banner_class, messages):
        """
        :param banner_class:
            Class for the type of banner used to create links used for
            this milestone, e.g. TextBanner.
        :param messages:
            Dictionary of messages to choose from.
        """
        links = filter(lambda link: isinstance(link.banner, banner_class),
                       self.user.link_set.all())
        link_count = len(links)

        # If no links have been created, show a future milestone.
        if link_count == 0:
            return self.future_date, unicode(messages['nothing_yet'])

        prev_milestone, next_milestone = self.surrounding_milestones(link_count,
                                                                     self.creation_milestones)

        # If we are within 1 link of the next milestone, show a message.
        if next_milestone and next_milestone - link_count == 1:
            return date_yesterday(), unicode(messages['close_to_milestone']).format(next_milestone)

        # As a last resort, show their previous milestone.
        sorted_links = sorted(links, lambda x, y: cmp(x.created, y.created))
        if prev_milestone:
            milestone_link = sorted_links[prev_milestone - 1]
            return (milestone_link.created.date(),
                    unicode(messages['achieved_milestone']).format(prev_milestone))

        # This shouldn't ever happen (no previous milestone yet at least
        # one banner created), but just in case, show when the last link
        # was created.
        return sorted_links[-1].created.date(), unicode(messages['link_created'])
Esempio n. 2
0
    def creation_milestone(self, banner_type, messages):
        """
        :param banner_type:
            banner_type of links to create a milestone from,
            e.g. text_banner.
        :param messages:
            Dictionary of messages to choose from.
        """
        links = self.user.link_set.filter(banner_type=banner_type)
        link_count = links.count()

        # If no links have been created, show a future milestone.
        if link_count == 0:
            return self.future_date, messages['nothing_yet']

        prev_milestone, next_milestone = self.surrounding_milestones(link_count,
                                                                     self.creation_milestones)

        # If we are within 1 link of the next milestone, show a message.
        if next_milestone and next_milestone - link_count == 1:
            return date_yesterday(), messages['close_to_milestone'].format(next_milestone)

        # As a last resort, show their previous milestone.
        if prev_milestone:
            milestone_link = links.order_by('created')[prev_milestone - 1]
            return (milestone_link.created.date(),
                    messages['achieved_milestone'].format(prev_milestone))

        # This shouldn't ever happen (no previous milestone yet at least
        # one banner created), but just in case, show when the last link
        # was created.
        return links.latest('created').created.date(), messages['link_created']
Esempio n. 3
0
    def handle(self, *args, **kwargs):
        try:
            service = AnalyticsService()
        except AnalyticsError as e:
            raise CommandError("Could not connect to analytics service: {0}".format(e), e)

        yesterday = date_yesterday()

        print "Downloading click counts from GA..."
        try:
            clicks = service.get_clicks_for_date(yesterday)
        except AnalyticsError as e:
            raise CommandError("Could not retrieve click data from analytics service: {0}".format(e))

        print "Adding datapoints to database..."
        datapoints = []
        for pk in Link.objects.values_list("id", flat=True):
            datapoint = DataPoint(link_id=pk, date=yesterday, link_clicks=clicks.get(unicode(pk), 0))
            datapoints.append(datapoint)

        try:
            DataPoint.objects.bulk_create(datapoints, batch_size=1000)
        except IntegrityError as e:
            raise CommandError("Could not insert datapoints into database due to IntegrityError.", e)

        print "Done!"
Esempio n. 4
0
    def metric_milestone(self, metric, messages):
        """
        :param metric:
            Name of the metric to use, e.g. link_clicks.
        :param messages:
            Dictionary of messages to choose from.
        :return:
            Tuple of (milestone date, message).
        """
        metric_aggregate_total = self.user.metric_aggregate_total(metric)
        metric_total = self.user.metric_total(metric)

        # If the user has nothing at all, show a "future" milestone.
        if metric_total == 0:
            return (self.future_date, unicode(messages['nothing_yet']).format(
                self.metric_milestones[0]))

        # Find the last milestone we passed and the next one.
        prev_milestone, next_milestone = self.surrounding_milestones(
            metric_total, self.metric_milestones)

        # If we are within 10% of the next milestone, show a message.
        if self.close_to_milestone(metric_total, next_milestone):
            remaining = next_milestone - metric_total
            return (date_yesterday(),
                    unicode(messages['close_to_milestone']).format(remaining))

        # If the last milestone happened within the Datapoint
        # retentation period, show when it happened.
        if prev_milestone and metric_aggregate_total < prev_milestone:
            milestone_date = self.milestone_date(metric, prev_milestone,
                                                 metric_aggregate_total)
            if milestone_date:
                return (milestone_date, unicode(
                    messages['achieved_milestone']).format(prev_milestone))

        # As a last resort, show the metric total as of yesterday.
        if next_milestone:
            difference = next_milestone - metric_total
            return (date_yesterday(),
                    unicode(messages['total_next_milestone']).format(
                        metric_total, difference))
        else:
            return date_yesterday(), unicode(
                messages['total_no_milestone']).format(metric_total)
Esempio n. 5
0
    def metric_milestone(self, metric, messages):
        """
        :param metric:
            Name of the metric to use, e.g. link_clicks.
        :param messages:
            Dictionary of messages to choose from.
        :return:
            Tuple of (milestone date, message).
        """
        metric_aggregate_total = self.user.metric_aggregate_total(metric)
        metric_total = self.user.metric_total(metric)

        # If the user has nothing at all, show a "future" milestone.
        if metric_total == 0:
            return (self.future_date,
                    unicode(messages['nothing_yet']).format(self.metric_milestones[0]))

        # Find the last milestone we passed and the next one.
        prev_milestone, next_milestone = self.surrounding_milestones(metric_total,
                                                                     self.metric_milestones)

        # If we are within 10% of the next milestone, show a message.
        if self.close_to_milestone(metric_total, next_milestone):
            remaining = next_milestone - metric_total
            return (date_yesterday(), unicode(messages['close_to_milestone']).format(remaining))

        # If the last milestone happened within the Datapoint
        # retentation period, show when it happened.
        if prev_milestone and metric_aggregate_total < prev_milestone:
            milestone_date = self.milestone_date(metric, prev_milestone, metric_aggregate_total)
            if milestone_date:
                return (milestone_date,
                        unicode(messages['achieved_milestone']).format(prev_milestone))

        # As a last resort, show the metric total as of yesterday.
        if next_milestone:
            difference = next_milestone - metric_total
            return (date_yesterday(),
                    unicode(messages['total_next_milestone']).format(metric_total, difference))
        else:
            return date_yesterday(), unicode(messages['total_no_milestone']).format(metric_total)
Esempio n. 6
0
    def creation_milestone(self, banner_class, messages):
        """
        :param banner_class:
            Class for the type of banner used to create links used for
            this milestone, e.g. TextBanner.
        :param messages:
            Dictionary of messages to choose from.
        """
        links = filter(lambda link: isinstance(link.banner, banner_class),
                       self.user.link_set.all())
        link_count = len(links)

        # If no links have been created, show a future milestone.
        if link_count == 0:
            return self.future_date, unicode(messages['nothing_yet'])

        prev_milestone, next_milestone = self.surrounding_milestones(
            link_count, self.creation_milestones)

        # If we are within 1 link of the next milestone, show a message.
        if next_milestone and next_milestone - link_count == 1:
            return date_yesterday(), unicode(
                messages['close_to_milestone']).format(next_milestone)

        # As a last resort, show their previous milestone.
        sorted_links = sorted(links, lambda x, y: cmp(x.created, y.created))
        if prev_milestone:
            milestone_link = sorted_links[prev_milestone - 1]
            return (milestone_link.created.date(),
                    unicode(
                        messages['achieved_milestone']).format(prev_milestone))

        # This shouldn't ever happen (no previous milestone yet at least
        # one banner created), but just in case, show when the last link
        # was created.
        return sorted_links[-1].created.date(), unicode(
            messages['link_created'])