utils.py 3.6 KB

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