analytics/options.py
def calculate(cls, frequency=settings.STATISTIC_FREQUENCY_DAILY, verbose=settings.STATISTIC_CALCULATION_VERBOSE):
        """
        Runs the calculator for this type of statistic.
        """
        if verbose:
            print _("Calculating statistics for %(class)s...") % {'class': cls.get_label()}

        start_datetime = None
        end_datetime = None

        # get the latest statistic
        latest_stat = cls.get_latest(frequency)

        # work out today's date, truncated to midnight
        today = datetime.strptime(datetime.now().strftime("%Y %m %d"), "%Y %m %d") 
        now = datetime.now()

        # if this statistic only has cumulative stats available
        if cls.cumulative:
            if frequency == settings.STATISTIC_FREQUENCY_HOURLY:
                # truncate to the nearest hour
                start_datetime = datetime.strptime(now.strftime("%Y %m %d %H:00:00"), "%Y %m %d %H:%M:%S")
            elif frequency == settings.STATISTIC_FREQUENCY_DAILY:
                start_datetime = today
            elif frequency == settings.STATISTIC_FREQUENCY_WEEKLY:
                # truncate today to the start of this week
                start_datetime = datetime.strptime(today.strftime("%Y %W 0"), "%Y %W %w")
            elif frequency == settings.STATISTIC_FREQUENCY_MONTHLY:
                # truncate today to the start of this month
                start_datetime = datetime.strptime(today.strftime("%Y %m 1"), "%Y %m %d")

            stat, created = cls.objects.get_or_create(date_time=start_datetime, frequency=frequency)
            stat.cumulative_count = cls.get_cumulative()
            stat.count = (stat.cumulative_count-latest_stat.cumulative_count) if latest_stat else stat.cumulative_count
        else:
            # get the date/time at which we should start calculating
            start_datetime = cls.get_start_datetime() if latest_stat is None else latest_stat.date_time
            # truncate the start date/time to the appropriate frequency
            if frequency == settings.STATISTIC_FREQUENCY_HOURLY:
                start_datetime = datetime.strptime(start_datetime.strftime("%Y %m %d %H:00:00"), "%Y %m %d %H:%M:%S")
                end_datetime = start_datetime+timedelta(hours=1)
            elif frequency == settings.STATISTIC_FREQUENCY_DAILY:
                start_datetime = datetime.strptime(start_datetime.strftime("%Y %m %d"), "%Y %m %d")
                end_datetime = start_datetime+timedelta(days=1)
            elif frequency == settings.STATISTIC_FREQUENCY_WEEKLY:
                # start at the beginning of the week of the latest stat
                start_datetime = datetime.strptime(start_datetime.strftime("%Y %W 0"), "%Y %W %w")-timedelta(days=7)
                end_datetime = start_datetime+timedelta(days=7)
            elif frequency == settings.STATISTIC_FREQUENCY_MONTHLY:
                # start at the beginning of the month of the latest stat
                start_datetime = datetime.strptime(start_datetime.strftime("%Y %m 1"), "%Y %m %d")
                end_datetime = datetime.strptime((start_datetime+timedelta(days=33)).strftime("%Y %m 1"), "%Y %m %d")

            # if we're doing the normal count
            while start_datetime < now:
                count = cls.get_count(start_datetime, end_datetime)
                cumulative_count = 0
                if isinstance(count, tuple):
                    cumulative_count = count[1]
                    count = count[0]
                else:
                    cumulative_count = (latest_stat.cumulative_count+count) if latest_stat else count

                stat, created = cls.objects.get_or_create(date_time=start_datetime, frequency=frequency)
                stat.count = count
                stat.cumulative_count = cumulative_count
                stat.save()

                latest_stat = stat

                # update the dates/time window
                start_datetime = end_datetime
                if frequency == settings.STATISTIC_FREQUENCY_HOURLY:
                    end_datetime += timedelta(hours=1)
                elif frequency == settings.STATISTIC_FREQUENCY_DAILY:
                    end_datetime += timedelta(days=1)
                elif frequency == settings.STATISTIC_FREQUENCY_WEEKLY:
                    end_datetime += timedelta(days=7)
                elif frequency == settings.STATISTIC_FREQUENCY_MONTHLY:
                    end_datetime = datetime.strptime((start_datetime+timedelta(days=33)).strftime("%Y %m 1"), "%Y %m %d")
def calculate(cls, frequency=settings.STATISTIC_FREQUENCY_DAILY, verbose=settings.STATISTIC_CALCULATION_VERBOSE):
        """
        Runs the calculator for this type of statistic.
        """
        if verbose:
            print _("Calculating statistics for %(class)s...") % {'class': cls.get_label()}

        start_datetime = None
        end_datetime = None

        # get the latest statistic
        latest_stat = cls.get_latest(frequency)

        # work out today's date, truncated to midnight
        today = datetime.strptime(datetime.now().strftime("%Y %m %d"), "%Y %m %d") 
        now = datetime.now()

        # if this statistic only has cumulative stats available
        if cls.cumulative:
            if frequency == settings.STATISTIC_FREQUENCY_HOURLY:
                # truncate to the nearest hour
                start_datetime = datetime.strptime(now.strftime("%Y %m %d %H:00:00"), "%Y %m %d %H:%M:%S")
            elif frequency == settings.STATISTIC_FREQUENCY_DAILY:
                start_datetime = today
            elif frequency == settings.STATISTIC_FREQUENCY_WEEKLY:
                # truncate today to the start of this week
                start_datetime = datetime.strptime(today.strftime("%Y %W 0"), "%Y %W %w")
            elif frequency == settings.STATISTIC_FREQUENCY_MONTHLY:
                # truncate today to the start of this month
                start_datetime = datetime.strptime(today.strftime("%Y %m 1"), "%Y %m %d")

            stat, created = cls.objects.get_or_create(date_time=start_datetime, frequency=frequency)
            stat.cumulative_count = cls.get_cumulative()
            stat.count = (stat.cumulative_count-latest_stat.cumulative_count) if latest_stat else stat.cumulative_count
        else:
            # get the date/time at which we should start calculating
            start_datetime = cls.get_start_datetime() if latest_stat is None else latest_stat.date_time
            # truncate the start date/time to the appropriate frequency
            if frequency == settings.STATISTIC_FREQUENCY_HOURLY:
                start_datetime = datetime.strptime(start_datetime.strftime("%Y %m %d %H:00:00"), "%Y %m %d %H:%M:%S")
                end_datetime = start_datetime+timedelta(hours=1)
            elif frequency == settings.STATISTIC_FREQUENCY_DAILY:
                start_datetime = datetime.strptime(start_datetime.strftime("%Y %m %d"), "%Y %m %d")
                end_datetime = start_datetime+timedelta(days=1)
            elif frequency == settings.STATISTIC_FREQUENCY_WEEKLY:
                # start at the beginning of the week of the latest stat
                start_datetime = datetime.strptime(start_datetime.strftime("%Y %W 0"), "%Y %W %w")-timedelta(days=7)
                end_datetime = start_datetime+timedelta(days=7)
            elif frequency == settings.STATISTIC_FREQUENCY_MONTHLY:
                # start at the beginning of the month of the latest stat
                start_datetime = datetime.strptime(start_datetime.strftime("%Y %m 1"), "%Y %m %d")
                end_datetime = datetime.strptime((start_datetime+timedelta(days=33)).strftime("%Y %m 1"), "%Y %m %d")

            # if we're doing the normal count
            while start_datetime < now:
                count = cls.get_count(start_datetime, end_datetime)
                cumulative_count = 0
                if isinstance(count, tuple):
                    cumulative_count = count[1]
                    count = count[0]
                else:
                    cumulative_count = (latest_stat.cumulative_count+count) if latest_stat else count

                stat, created = cls.objects.get_or_create(date_time=start_datetime, frequency=frequency)
                stat.count = count
                stat.cumulative_count = cumulative_count
                stat.save()

                latest_stat = stat

                # update the dates/time window
                start_datetime = end_datetime
                if frequency == settings.STATISTIC_FREQUENCY_HOURLY:
                    end_datetime += timedelta(hours=1)
                elif frequency == settings.STATISTIC_FREQUENCY_DAILY:
                    end_datetime += timedelta(days=1)
                elif frequency == settings.STATISTIC_FREQUENCY_WEEKLY:
                    end_datetime += timedelta(days=7)
                elif frequency == settings.STATISTIC_FREQUENCY_MONTHLY:
                    end_datetime = datetime.strptime((start_datetime+timedelta(days=33)).strftime("%Y %m 1"), "%Y %m %d")
Runs the calculator for this type of statistic.