Расчет процента и других показателей в день (последние 30 дней) django ORM - PullRequest
0 голосов
/ 03 июня 2019

Я новичок в 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)
...