utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from django.utils import timezone
  2. from review.models import Review
  3. from django.db.models import Count
  4. def last_data(date, location_id):
  5. res = Review.objects.filter(create_time__gte=date, location_id=location_id).values('star_rating')\
  6. .annotate(total=Count('star_rating')).order_by('star_rating')
  7. ratings = [0] * 5
  8. for r in res:
  9. ratings[r.get('star_rating')-1] = r.get('total')
  10. return ratings
  11. def get_review_count_by_month(location_id):
  12. now = timezone.now()
  13. day = 31 - now.day
  14. first_day_month = now - timezone.timedelta(days=now.day)
  15. this_month = last_data(first_day_month, location_id)
  16. sql = f'''
  17. SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_review
  18. FROM review_review
  19. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
  20. GROUP BY MONTH(create_time)
  21. ORDER BY DATE(create_time)
  22. '''
  23. qs = Review.objects.raw(sql)
  24. label = [q.month for q in qs]
  25. total_review = [q.total_review for q in qs]
  26. star_ratings = []
  27. for i in range(1, 6):
  28. sql = f'''
  29. SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_ratings
  30. FROM review_review
  31. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
  32. and location_id={location_id} and star_rating={i}
  33. GROUP BY MONTH(create_time)
  34. ORDER BY DATE(create_time)
  35. '''
  36. qs = Review.objects.raw(sql)
  37. ratings = [0]*len(label)
  38. for q in qs:
  39. ratings[label.index(q.month)] = q.total_ratings
  40. star_ratings.append(ratings)
  41. response = {
  42. 'label': label,
  43. 'total_reviews': total_review,
  44. 'one_star': star_ratings[0],
  45. 'two_star': star_ratings[1],
  46. 'three_star': star_ratings[2],
  47. 'four_star': star_ratings[3],
  48. 'five_star': star_ratings[4],
  49. 'this': this_month
  50. }
  51. return response
  52. def get_review_count_by_week(location_id):
  53. now = timezone.now()
  54. day = 6 - now.weekday()
  55. first_day_week = now - timezone.timedelta(days=(now.weekday()+1))
  56. this_week = last_data(first_day_week, location_id)
  57. sql = f'''
  58. SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_review
  59. FROM review_review
  60. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
  61. GROUP BY WEEK(create_time)
  62. ORDER BY DATE(create_time)
  63. '''
  64. qs = Review.objects.raw(sql)
  65. label = [q.week for q in qs]
  66. total_review = [q.total_review for q in qs]
  67. star_ratings = []
  68. for i in range(1, 6):
  69. sql = f'''
  70. SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_ratings
  71. FROM review_review
  72. WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
  73. and location_id={location_id} and star_rating={i}
  74. GROUP BY WEEK(create_time)
  75. ORDER BY DATE(create_time)
  76. '''
  77. qs = Review.objects.raw(sql)
  78. ratings = [0]*len(label)
  79. for q in qs:
  80. ratings[label.index(q.week)] = q.total_ratings
  81. star_ratings.append(ratings)
  82. response = {
  83. 'label': label,
  84. 'total_reviews': total_review,
  85. 'one_star': star_ratings[0],
  86. 'two_star': star_ratings[1],
  87. 'three_star': star_ratings[2],
  88. 'four_star': star_ratings[3],
  89. 'five_star': star_ratings[4],
  90. 'this': this_week
  91. }
  92. return response