|
@@ -1,9 +1,21 @@
|
|
from django.utils import timezone
|
|
from django.utils import timezone
|
|
from review.models import Review
|
|
from review.models import Review
|
|
|
|
+from django.db.models import Count
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def last_data(date, location_id):
|
|
|
|
+ res = Review.objects.filter(create_time__gte=date, location_id=location_id).values('star_rating').annotate(total=Count('star_rating')).order_by('star_rating')
|
|
|
|
+ ratings = [0] * 5
|
|
|
|
+ for r in res:
|
|
|
|
+ ratings[r.get('star_rating')-1] = r.get('total')
|
|
|
|
+ return ratings
|
|
|
|
|
|
|
|
|
|
def get_review_count_by_month(location_id):
|
|
def get_review_count_by_month(location_id):
|
|
- day = 31 - timezone.now().day
|
|
|
|
|
|
+ now = timezone.now()
|
|
|
|
+ day = 31 - now.day
|
|
|
|
+ first_day_month = now - timezone.timedelta(days=now.day)
|
|
|
|
+ this_month = last_data(first_day_month, location_id)
|
|
sql = f'''
|
|
sql = f'''
|
|
SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_review
|
|
SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_review
|
|
FROM review_review
|
|
FROM review_review
|
|
@@ -26,6 +38,7 @@ def get_review_count_by_month(location_id):
|
|
'''
|
|
'''
|
|
qs = Review.objects.raw(sql)
|
|
qs = Review.objects.raw(sql)
|
|
ratings = [0]*len(label)
|
|
ratings = [0]*len(label)
|
|
|
|
+
|
|
for q in qs:
|
|
for q in qs:
|
|
ratings[label.index(q.month)] = q.total_ratings
|
|
ratings[label.index(q.month)] = q.total_ratings
|
|
star_ratings.append(ratings)
|
|
star_ratings.append(ratings)
|
|
@@ -36,13 +49,17 @@ def get_review_count_by_month(location_id):
|
|
'two_star': star_ratings[1],
|
|
'two_star': star_ratings[1],
|
|
'three_star': star_ratings[2],
|
|
'three_star': star_ratings[2],
|
|
'four_star': star_ratings[3],
|
|
'four_star': star_ratings[3],
|
|
- 'five_star': star_ratings[4]
|
|
|
|
|
|
+ 'five_star': star_ratings[4],
|
|
|
|
+ 'this': this_month
|
|
}
|
|
}
|
|
return response
|
|
return response
|
|
|
|
|
|
|
|
|
|
def get_review_count_by_week(location_id):
|
|
def get_review_count_by_week(location_id):
|
|
- day = 6 - timezone.now().weekday()
|
|
|
|
|
|
+ now = timezone.now()
|
|
|
|
+ day = 6 - now.weekday()
|
|
|
|
+ first_day_week = now - timezone.timedelta(days=now.weekday()+1)
|
|
|
|
+ this_week = last_data(first_day_week, location_id)
|
|
sql = f'''
|
|
sql = f'''
|
|
SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_review
|
|
SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_review
|
|
FROM review_review
|
|
FROM review_review
|
|
@@ -75,6 +92,7 @@ def get_review_count_by_week(location_id):
|
|
'two_star': star_ratings[1],
|
|
'two_star': star_ratings[1],
|
|
'three_star': star_ratings[2],
|
|
'three_star': star_ratings[2],
|
|
'four_star': star_ratings[3],
|
|
'four_star': star_ratings[3],
|
|
- 'five_star': star_ratings[4]
|
|
|
|
|
|
+ 'five_star': star_ratings[4],
|
|
|
|
+ 'this': this_week
|
|
}
|
|
}
|
|
return response
|
|
return response
|