utils.py 4.5 KB

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