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. 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/736cec87-63bc-46d9-9ac0-4af50ed93a93%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to