utils.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from django.utils import timezone
  2. from review.models import Review
  3. def get_review_count_by_month(location_id):
  4. day = 31 - timezone.now().day
  5. sql = f'''
  6. SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_review
  7. FROM review_review
  8. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
  9. GROUP BY MONTH(create_time)
  10. ORDER BY DATE(create_time)
  11. '''
  12. qs = Review.objects.raw(sql)
  13. label = [q.month for q in qs]
  14. total_review = [q.total_review for q in qs]
  15. star_ratings = []
  16. for i in range(1, 6):
  17. sql = f'''
  18. SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_ratings
  19. FROM review_review
  20. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
  21. and location_id={location_id} and star_rating={i}
  22. GROUP BY MONTH(create_time)
  23. ORDER BY DATE(create_time)
  24. '''
  25. qs = Review.objects.raw(sql)
  26. ratings = [0]*len(label)
  27. for q in qs:
  28. ratings[label.index(q.month)] = q.total_ratings
  29. star_ratings.append(ratings)
  30. response = {
  31. 'label': label,
  32. 'total_reviews': total_review,
  33. 'one_star': star_ratings[0],
  34. 'two_star': star_ratings[1],
  35. 'three_star': star_ratings[2],
  36. 'four_star': star_ratings[3],
  37. 'five_star': star_ratings[4]
  38. }
  39. return response
  40. def get_review_count_by_week(location_id):
  41. day = 6 - timezone.now().weekday()
  42. sql = f'''
  43. SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_review
  44. FROM review_review
  45. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
  46. GROUP BY WEEK(create_time)
  47. ORDER BY DATE(create_time)
  48. '''
  49. qs = Review.objects.raw(sql)
  50. label = [q.week for q in qs]
  51. total_review = [q.total_review for q in qs]
  52. star_ratings = []
  53. for i in range(1, 6):
  54. sql = f'''
  55. SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_ratings
  56. FROM review_review
  57. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
  58. and location_id={location_id} and star_rating={i}
  59. GROUP BY WEEK(create_time)
  60. ORDER BY DATE(create_time)
  61. '''
  62. qs = Review.objects.raw(sql)
  63. ratings = [0]*len(label)
  64. for q in qs:
  65. ratings[label.index(q.week)] = q.total_ratings
  66. star_ratings.append(ratings)
  67. response = {
  68. 'label': label,
  69. 'total_reviews': total_review,
  70. 'one_star': star_ratings[0],
  71. 'two_star': star_ratings[1],
  72. 'three_star': star_ratings[2],
  73. 'four_star': star_ratings[3],
  74. 'five_star': star_ratings[4]
  75. }
  76. return response