Django ORM last 30 days report

18 views
Skip to first unread message

rohit haritash

unread,
Jun 3, 2019, 7:03:09 AM6/3/19
to Django users
Hi, I am new to Django and Django ORM. I am using Mongo DB as backed with DJANGO connector which supports Django native ORM. 
I have to generate a percentage metric for the last 30 days. I using the following function. CAn you please suggest a better way. The function is taking too long to execute. 

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)

This is my output. I will be showing it UI.


  RuntimeWarning)
v/lib/python3.7/site-packages/django/db/models/fields/__init__.py:1421: RuntimeWarning: DateTimeField NewsAPIOrg_new.publishedAt received a naive datetime (2019-05-04 00:00:00) while time zone support is active.
  RuntimeWarning)
    approved_percentage                        date  noaction_percentage  reject+percentage  total_count
0              0.000000         2019-06-03T06:16:56            90.485830           1.417004          988
1              0.000000  2019-06-02 00:00:00.000000            89.648799           0.092421         1082
2              0.000000  2019-06-01 00:00:00.000000            88.261997           0.064851         1542
3              0.186800  2019-05-31 00:00:00.000000            87.982565           0.435866         1606
4              0.544465  2019-05-30 00:00:00.000000            84.996975           0.362976         1653
5              0.436443  2019-05-29 00:00:00.000000            84.451718           0.545554         1833
6              0.242915  2019-05-28 00:00:00.000000            85.748988           0.161943         1235
7              0.000000  2019-05-27 00:00:00.000000            89.356984           0.000000          902
8              0.000000  2019-05-26 00:00:00.000000            87.864964           0.000000         1096
9              0.055928  2019-05-25 00:00:00.000000            88.926174           0.000000         1788
10             0.000000  2019-05-24 00:00:00.000000            89.312977           0.000000         1048

Thanks. 

Reply all
Reply to author
Forward
0 new messages