utils.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from django.utils import timezone
  2. from django.db.models import Count
  3. from review.models import Review
  4. from facebook_app.models import FacebookReview
  5. from yelp.models import YelpReview
  6. now = timezone.now()
  7. beginning_of_month = now.replace(day=1, hour=0, minute=0, second=0)
  8. beginning_of_last_month = now - timezone.timedelta(days=now.day+31)
  9. def get_google_review_report(location_id):
  10. pos = Review.objects.filter(
  11. location_id=location_id,
  12. create_time__range=(beginning_of_month, now),
  13. star_rating__gte=3
  14. ).count()
  15. neg = Review.objects.filter(
  16. location_id=location_id,
  17. create_time__range=(beginning_of_month, now),
  18. star_rating__lte=2
  19. ).count()
  20. pos_last_month = Review.objects.filter(
  21. location_id=location_id,
  22. create_time__range=(beginning_of_last_month, beginning_of_month),
  23. star_rating__gte=3
  24. ).count()
  25. neg_last_month = Review.objects.filter(
  26. location_id=location_id,
  27. create_time__range=(beginning_of_last_month, beginning_of_month),
  28. star_rating__lte=2
  29. ).count()
  30. pos_growth = 'inf' if pos_last_month == 0 else round(((((pos / now.day) * 30) - pos_last_month) / pos_last_month) * 100, 2)
  31. neg_growth = 'inf' if neg_last_month == 0 else round(((((neg / now.day) * 30) - neg_last_month) / neg_last_month) * 100, 2)
  32. total = pos + neg
  33. last_month_total = pos_last_month + neg_last_month
  34. total_growth = 'inf' if last_month_total == 0 else round(((((total / now.day) * 30) - last_month_total) / last_month_total) * 100, 2)
  35. return {
  36. 'positive': pos,
  37. 'positive_growth': pos_growth,
  38. 'negative': neg,
  39. 'negative_growth': neg_growth,
  40. 'total': total,
  41. 'total_growth': total_growth
  42. }
  43. def get_facebook_report(location_id):
  44. pos = FacebookReview.objects.filter(
  45. page__location_id=location_id,
  46. create_time__range=(beginning_of_month, now),
  47. recommendation_type=True
  48. ).count()
  49. neg = FacebookReview.objects.filter(
  50. page__location_id=location_id,
  51. create_time__range=(beginning_of_month, now),
  52. recommendation_type=False
  53. ).count()
  54. pos_last_month = FacebookReview.objects.filter(
  55. page__location_id=location_id,
  56. create_time__range=(beginning_of_last_month, beginning_of_month),
  57. recommendation_type=True
  58. ).count()
  59. neg_last_month = FacebookReview.objects.filter(
  60. page__location_id=location_id,
  61. create_time__range=(beginning_of_last_month, beginning_of_month),
  62. recommendation_type=False
  63. ).count()
  64. pos_growth = 'inf' if pos_last_month == 0 else round(((((pos / now.day) * 30) - pos_last_month) / pos_last_month) * 100, 2)
  65. neg_growth = 'inf' if neg_last_month == 0 else round(((((neg / now.day) * 30) - neg_last_month) / neg_last_month) * 100, 2)
  66. total = pos + neg
  67. last_month_total = pos_last_month + neg_last_month
  68. total_growth = 'inf' if last_month_total == 0 else round(((((total / now.day) * 30) - last_month_total) / last_month_total) * 100, 2)
  69. return {
  70. 'positive': pos,
  71. 'positive_growth': pos_growth,
  72. 'negative': neg,
  73. 'negative_growth': neg_growth,
  74. 'total': total,
  75. 'total_growth': total_growth
  76. }
  77. def get_yelp_review_report(location_id):
  78. pos = YelpReview.objects.filter(
  79. location__location_id=location_id,
  80. date_posted__range=(beginning_of_month, now),
  81. rating__gte=3
  82. ).count()
  83. neg = YelpReview.objects.filter(
  84. location__location_id=location_id,
  85. date_posted__range=(beginning_of_month, now),
  86. rating__lte=2
  87. ).count()
  88. pos_last_month = YelpReview.objects.filter(
  89. location__location_id=location_id,
  90. date_posted__range=(beginning_of_last_month, beginning_of_month),
  91. rating__gte=3
  92. ).count()
  93. neg_last_month = YelpReview.objects.filter(
  94. location__location_id=location_id,
  95. date_posted__range=(beginning_of_last_month, beginning_of_month),
  96. rating__lte=2
  97. ).count()
  98. pos_growth = 'inf' if pos_last_month == 0 else round(((((pos / now.day) * 30) - pos_last_month) / pos_last_month) * 100, 2)
  99. neg_growth = 'inf' if neg_last_month == 0 else round(((((neg / now.day) * 30) - neg_last_month) / neg_last_month) * 100, 2)
  100. total = pos + neg
  101. last_month_total = pos_last_month + neg_last_month
  102. total_growth = 'inf' if last_month_total == 0 else round(((((total / now.day) * 30) - last_month_total) / last_month_total) * 100, 2)
  103. return {
  104. 'positive': pos,
  105. 'positive_growth': pos_growth,
  106. 'negative': neg,
  107. 'negative_growth': neg_growth,
  108. 'total': total,
  109. 'total_growth': total_growth
  110. }
  111. def get_this_month_analytics(location_id):
  112. google_qs = Review.objects.filter(
  113. create_time__range=(beginning_of_month, now),
  114. location_id=location_id
  115. )\
  116. .values('create_time__day')\
  117. .annotate(total=Count('create_time__day'))
  118. google_qs_dict = {q['create_time__day']: q['total'] for q in google_qs}
  119. yelp_qs = YelpReview.objects.filter(
  120. location__location_id=location_id,
  121. date_posted__range=(beginning_of_month, now),
  122. )\
  123. .values('date_posted__day')\
  124. .annotate(total=Count('date_posted__day'))
  125. yelp_qs_dict = {q['date_posted__day']: q['total'] for q in yelp_qs}
  126. facebook_qs = FacebookReview.objects.filter(
  127. page__location_id=location_id,
  128. create_time__range=(beginning_of_month, now),
  129. )\
  130. .values('create_time__day')\
  131. .annotate(total=Count('create_time__day'))
  132. facebook_qs_dict = {q['create_time__day']: q['total'] for q in facebook_qs}
  133. label = []
  134. facebook = []
  135. yelp = []
  136. google = []
  137. for day in range(1, now.day+1):
  138. label.append(day)
  139. facebook.append(facebook_qs_dict.get(day, 0))
  140. yelp.append(yelp_qs_dict.get(day, 0))
  141. google.append(google_qs_dict.get(day, 0))
  142. return {
  143. 'label': label,
  144. 'google': google,
  145. 'facebook': facebook,
  146. 'yelp': yelp
  147. }