每天(过去30天)django ORM计算百分比和其他指标

时间:2019-06-03 05:47:53

标签: python django django-orm metrics

我是Django的新手,正在尝试计算百分比和一些指标。 这种方法的问题在于我无法通过每天分组来获取数据。这是我的解决方案。请提出一个更好的方法。

def calculate_percentage(neumerator, denominator):
    percentage = (neumerator / denominator) * 100
    return percentage


def test_percentage():
    percentage_metric_details = []
    counter = 1

    today_ISO = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%dT%H:%M:%S')

    while counter < 31:
        todays_first_bucket_date = datetime.datetime.now()
        second_bucket_start = todays_first_bucket_date - timedelta(days=counter)
        last_ISO = datetime.datetime.fromtimestamp(second_bucket_start.timestamp()).strftime('%Y-%m-%dT%H:%M:%S')
        last_ISO = str(last_ISO).split("T")
        last_ISO = last_ISO[0] + " 00:00:00.000000"

        print("todays first bucket", todays_first_bucket_date)
        print("start date", today_ISO)
        print("last date", last_ISO)

        counter = counter + 1

        total_count = ArticleStatus.objects.filter(
            created_on__lte=today_ISO, created_on__gte=last_ISO).count()

        total_count_approved = ArticleStatus.objects.filter(
            created_on__lte=today_ISO, created_on__gte=last_ISO, reasonForReject="accepted").count()

        total_pending_count = ArticleStatus.objects.filter(
            created_on__lte=today_ISO, created_on__gte=last_ISO, reasonForReject="").count()
        print(total_count_approved)

        accept_percentage = calculate_percentage(total_count_approved, total_count)
        pending_percentage = calculate_percentage(total_pending_count, total_count)

        percentage_metric_details.append({
            "total_count": total_count,
            "approved_percentage": accept_percentage,
            "pending_percentage": pending_percentage
        })

        todays_first_bucket_date = last_ISO
        today_ISO = last_ISO


    return percentage_metric_details


z = test_percentage()
print(z)

0 个答案:

没有答案