Merge pull request #2539 from betagouv/fix-stats

Improve the ratings stats
This commit is contained in:
gregoirenovel 2018-09-11 10:26:08 +02:00 committed by GitHub
commit 9a216ae705
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -129,19 +129,23 @@ class StatsController < ApplicationController
Feedback.ratings.fetch(:neutral) => "Neutres", Feedback.ratings.fetch(:neutral) => "Neutres",
Feedback.ratings.fetch(:unhappy) => "Mécontents" Feedback.ratings.fetch(:unhappy) => "Mécontents"
} }
interval = 6.weeks.ago.beginning_of_week..1.week.ago.beginning_of_week
totals = Feedback.where(created_at: 5.weeks.ago..Time.now).group_by_week(:created_at).count totals = Feedback
.where(created_at: interval)
.group_by_week(:created_at)
.count
Feedback.ratings.values.map do |rating| Feedback.ratings.values.map do |rating|
data = Feedback data = Feedback
.where(created_at: 5.weeks.ago..Time.now, rating: rating) .where(created_at: interval, rating: rating)
.group_by_week(:created_at) .group_by_week(:created_at)
.count .count
.map do |week, count| .map do |week, count|
total = totals[week] total = totals[week]
if total > 0 if total > 0
[week, (count.to_f / total).round(2)] [week, (count.to_f / total * 100).round(2)]
else else
0 0
end end