浏览代码

fix a unseen bug

Mohidul Islam 5 年之前
父节点
当前提交
ca43f1290a
共有 1 个文件被更改,包括 7 次插入4 次删除
  1. 7 4
      analytics/utils.py

+ 7 - 4
analytics/utils.py

@@ -1,11 +1,13 @@
+from django.utils import timezone
 from review.models import Review
 
 
 def get_review_count_by_month(location_id):
+    day = timezone.now().day
     sql = f'''
     SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_review
     FROM review_review
-    WHERE DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR) <= create_time and location_id={location_id}
+    WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
     GROUP BY MONTH(create_time)
     ORDER BY DATE(create_time)
     '''
@@ -17,7 +19,7 @@ def get_review_count_by_month(location_id):
         sql = f'''
             SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_ratings
             FROM review_review
-            WHERE DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR) <= create_time
+            WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
             and location_id={location_id} and star_rating={i}
             GROUP BY MONTH(create_time)
             ORDER BY DATE(create_time)
@@ -40,10 +42,11 @@ def get_review_count_by_month(location_id):
 
 
 def get_review_count_by_week(location_id):
+    day = timezone.now().weekday()
     sql = f'''
     SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_review
     FROM review_review
-    WHERE DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR) <= create_time and location_id={location_id}
+    WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
     GROUP BY WEEK(create_time)
     ORDER BY DATE(create_time)
     '''
@@ -55,7 +58,7 @@ def get_review_count_by_week(location_id):
         sql = f'''
             SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_ratings
             FROM review_review
-            WHERE DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR) <= create_time
+            WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
             and location_id={location_id} and star_rating={i}
             GROUP BY WEEK(create_time)
             ORDER BY DATE(create_time)