Переглянути джерело

Change review collect method and contab execute in 6 hour interval

Mohidul Islam 5 роки тому
батько
коміт
0e040e2e47

+ 7 - 2
analytics/background_job.py

@@ -2,6 +2,7 @@ from django.utils import timezone
 from gauth.models import Location
 from review.models import Review
 from .send_email import send_email
+from django.db.models import Count
 from review.review_utils import get_bad_reviews
 
 
@@ -31,6 +32,10 @@ def send_email_bad_reviews():
 
 
 def weekly_report(location_id):
-    now = timezone.now()
-    date = now - timezone.timedelta(days=7)
+    date = timezone.now() - timezone.timedelta(days=7)
     reviews = Review.objects.filter(location_id=location_id, create_time__gte=date)
+    res = Review.objects.filter(create_time__gte=date, location_id=location_id).values('star_rating')\
+        .annotate(total=Count('star_rating')).order_by('star_rating')
+    # TODO:
+    # make a table with reviews count and bad reviews and send mail to a particular email add.
+    

+ 2 - 2
review/background_job.py

@@ -4,7 +4,7 @@ from django.utils import timezone
 from .models import Review, CustomReply, Reply
 from nlu_job.nlu_utils import is_a_name
 from review.review_utils import reply_review
-from review.review_utils import populate_reviews, get_bad_reviews
+from review.review_utils import populate_reviews
 from analytics.background_job import send_email_bad_reviews
 
 from nameparser import HumanName
@@ -57,7 +57,7 @@ def reply_uncommented_reviews():
     sleep(60*random.randint(2, 5))
 
 
-def background_task():
+def background_task_6_hours_interval():
     populate_reviews()
     send_email_bad_reviews()
     sleep(60 * random.randint(2, 5))

+ 42 - 44
review/review_utils.py

@@ -2,7 +2,7 @@ import json
 from requests import get, put
 from gauth.auth_utils import get_gmb_id, get_auth_header
 from gauth.location_utils import get_all_location_ids
-from django.db.models import Max
+# from django.db.models import Max
 from .models import Review, Reply
 from gauth.models import Location
 
@@ -10,12 +10,12 @@ from django.utils import timezone
 
 
 STAR_REVIEW_NUM = {'STAR_RATING_UNSPECIFIED': 0, 'ONE': 1, 'TWO': 2, 'THREE': 3, 'FOUR': 4, 'FIVE': 5}
-USER, GMB_ID = get_gmb_id()
 
 
-def get_review_list_url(loc_id, next_page_token=''):
+def get_review_list_url(location_id, next_page_token=''):
     # An helper function that make a url that need to consume GMB review api
-    return 'https://mybusiness.googleapis.com/v4/accounts/' + GMB_ID + '/locations/' + loc_id + '/reviews?pageToken='+next_page_token
+    _, account_id = get_gmb_id()
+    return f'https://mybusiness.googleapis.com/v4/accounts/{account_id}/locations/{location_id}/reviews?pageToken='+next_page_token
 
 
 def get_reply_url(location_id, review_id):
@@ -37,38 +37,38 @@ def reply_review(review, replied_text):
     return response
 
 
-def get_max_date(loc_id):
-    '''
-    find the maximum date of any particular location. the latest date
-    :param loc_id: Integer -> Globally unique ID for google my business locations.
-    :return: DateTime Obj -> The latest reviews date of that location.
-    '''
-    # Function that takes a location id and return the latest updated review.
-    largest = Review.objects.filter(location_id=loc_id).aggregate(Max('create_time'))['create_time__max']
-    max_date = largest if largest else '1970-01-11 17:41:17.532740'
-    return max_date
+# def get_max_date(loc_id):
+#     '''
+#     find the maximum date of any particular location. the latest date
+#     :param loc_id: Integer -> Globally unique ID for google my business locations.
+#     :return: DateTime Obj -> The latest reviews date of that location.
+#     '''
+#     # Function that takes a location id and return the latest updated review.
+#     largest = Review.objects.filter(location_id=loc_id).aggregate(Max('create_time'))['create_time__max']
+#     max_date = largest if largest else '1970-01-11 17:41:17.532740'
+#     return max_date
 
 
-def filter_unrecorded_review_by_date(reviews, max_date):
-    # A function that return only those reviews whose has larger value than
-    # the max create_time value in review database.
-    filtered_reviews = []
-    for rev in reviews:
-        if rev['createTime'] >= str(max_date):
-            filtered_reviews.append(rev)
-    return filtered_reviews
+# def filter_unrecorded_review_by_date(reviews, max_date):
+#     # A function that return only those reviews whose has larger value than
+#     # the max create_time value in review database.
+#     filtered_reviews = []
+#     for rev in reviews:
+#         if rev['createTime'] >= str(max_date):
+#             filtered_reviews.append(rev)
+#     return filtered_reviews
 
 
-def insert_review_into_database(unrecorded_reviews, loc):
+def insert_review_into_database(reviews, loc):
     '''
     Insert reviews to database.
     :param unrecorded_reviews: all reviews for location.
     :param loc: location that unrecorded_reviews belongs to.
     :return: It insert all reviews if it is not exits in database and return nothing.
     '''
-    for review in unrecorded_reviews:
+    for review in reviews:
         review_id = review.get('reviewId')
-        # Check the review already in database then We don't need to store again.
+        # Check the review already exists in database then We don't need to store again.
         rev = Review.objects.filter(pk=review_id).first()
         if rev:
             continue
@@ -137,23 +137,23 @@ def update_location_data(loc, average_rating, total_reviews, total_reviews_db):
     loc.save()
 
 
-def fetch_all_review(loc_id):
-    loc = Location.objects.get(pk=loc_id)
-    max_date = get_max_date(loc_id)
-    headers = get_auth_header()
-    url = get_review_list_url(loc_id)
-    res = get(url, headers=headers)
-    if res.status_code == 401:
-        return
-    data = res.json()
-    reviews = data['reviews']
-    average_rating = data.get('averageRating')
-    total_reviews = data.get('totalReviewCount')
-    unrecorded_reviews = filter_unrecorded_review_by_date(reviews, max_date)
-    if len(unrecorded_reviews) != 0:
-        insert_review_into_database(unrecorded_reviews, loc)
-    total_reviews_db = Review.objects.filter(location_id=loc_id).count()
-    update_location_data(loc, average_rating, total_reviews, total_reviews_db)
+# def fetch_all_review(loc_id):
+#     loc = Location.objects.get(pk=loc_id)
+#     max_date = get_max_date(loc_id)
+#     headers = get_auth_header()
+#     url = get_review_list_url(loc_id)
+#     res = get(url, headers=headers)
+#     if res.status_code == 401:
+#         return
+#     data = res.json()
+#     reviews = data['reviews']
+#     average_rating = data.get('averageRating')
+#     total_reviews = data.get('totalReviewCount')
+#     unrecorded_reviews = filter_unrecorded_review_by_date(reviews, max_date)
+#     if len(unrecorded_reviews) != 0:
+#         insert_review_into_database(unrecorded_reviews, loc)
+#     total_reviews_db = Review.objects.filter(location_id=loc_id).count()
+#     update_location_data(loc, average_rating, total_reviews, total_reviews_db)
 
 
 def fetch_last_20_reviews(loc_id):
@@ -161,8 +161,6 @@ def fetch_last_20_reviews(loc_id):
     headers = get_auth_header()
     url = get_review_list_url(loc_id)+'&pageSize=20'
     res = get(url, headers=headers)
-    if res.status_code == 401:
-        return
     data = res.json()
     reviews = data['reviews']
     average_rating = data.get('averageRating')

+ 1 - 1
review_automation/settings.py

@@ -131,7 +131,7 @@ NLU_SERVER_URI = 'http://10.0.0.33:5005'
 
 # Cron-Jobs of the project
 CRONJOBS = [
-    ('0 6 * * *', 'review.background_job.background_task'),
+    ('0 */6 * * *', 'review.background_job.background_task_6_hours_interval'),
 ]