utils.py 4.8 KB

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