Display analytics up to yesterday instead of today (#17064)

Stop displaying admin analytics for current day, as the data is incomplete. Instead, show analytics for any given period up to yesterday
This commit is contained in:
Monica Mateiu 2022-04-04 20:17:32 +01:00 committed by GitHub
parent 378aebce65
commit 99401449d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 10 deletions

View file

@ -3,7 +3,7 @@ module Admin
layout "admin"
def index
@length = (params[:period] || 7).to_i
@labels = (0..@length - 1).map { |n| n.days.ago.strftime("%b %d") }.reverse
@labels = (1..@length).map { |n| n.days.ago.strftime("%b %d") }.reverse
@analytics = Admin::ChartsData.new(@length).call
@data_counts = Admin::DataCounts.call
end

View file

@ -5,7 +5,7 @@ module Admin
end
def call
period = @length.days.ago..Time.current
period = (@length + 1).days.ago..1.day.ago
previous_period = (@length * 2).days.ago..@length.days.ago
grouped_posts = Article.where(published_at: period).group("DATE(published_at)").size
@ -13,7 +13,7 @@ module Admin
grouped_reactions = Reaction.where(created_at: period).group("DATE(created_at)").size
grouped_users = User.where(registered_at: period).group("DATE(registered_at)").size
days_range = (@length - 1).downto(0)
days_range = @length.downto(1)
posts_values = days_range.map { |n| grouped_posts[n.days.ago.to_date] || 0 }
comments_values = days_range.map { |n| grouped_comments[n.days.ago.to_date] || 0 }
reactions_values = days_range.map { |n| grouped_reactions[n.days.ago.to_date] || 0 }

View file

@ -61,7 +61,7 @@
data-values="<%= values.join(",") %>"></canvas>
<footer class="flex justify-between items-center fs-xs color-secondary">
<span><%= @labels[0] %></span>
<span>Today</span>
<span>Yesterday</span>
</footer>
</div>
<% end %>

View file

@ -25,4 +25,82 @@ RSpec.describe "/admin", type: :request do
expect(response.body).to include(ENV["HEROKU_RELEASE_CREATED_AT"])
end
end
describe "analytics" do
subject(:body) { response.body }
before do
Timecop.freeze("2019-04-30T12:00:00Z")
get admin_path
end
after { Timecop.return }
it { is_expected.to include("Analytics and trends") }
it { is_expected.to include("Yesterday") }
it { is_expected.to include("Apr 23") }
it "displays correct number of posts from past week" do
create(:article, published_at: Time.zone.today)
create(:article, published_at: 1.day.ago)
create(:article, published_at: 7.days.ago)
create(:article, published_at: 8.days.ago)
get admin_path
expect(body).to include "2</span> Posts"
end
it "displays correct number of comments from past week" do
create(:comment, created_at: Time.zone.today)
create(:comment, created_at: 1.day.ago)
create(:comment, created_at: 8.days.ago)
get admin_path
expect(body).to include "1</span> Comments"
end
it "displays correct number of reactions from past week" do
create(:reaction, created_at: Time.zone.today)
create(:reaction, created_at: 3.days.ago)
create(:reaction, created_at: 2.weeks.ago)
get admin_path
expect(body).to include "1</span> Reaction"
end
it "displays correct number of new members from past week" do
create(:user, registered_at: Time.zone.today)
create(:user, registered_at: 2.days.ago)
create(:user, registered_at: 10.days.ago)
get admin_path
expect(body).to include "1</span> New members"
end
it "does not display data from previous weeks", :aggregate_failures do
create(:article, published_at: 8.days.ago)
create(:comment, created_at: 2.weeks.ago)
create(:reaction, created_at: 1.month.ago)
create(:user, registered_at: 10.days.ago)
get admin_path
expect(body).to include "0</span> Posts"
expect(body).to include "0</span> Comments"
expect(body).to include "0</span> Reactions"
expect(body).to include "0</span> New members"
end
it "does not display data from today", :aggregate_failures do
create(:article, published_at: Time.zone.today)
create(:comment, created_at: Time.zone.today)
create(:reaction, created_at: Time.zone.today)
create(:user, registered_at: Time.zone.today)
get admin_path
expect(body).to include "0</span> Posts"
expect(body).to include "0</span> Comments"
expect(body).to include "0</span> Reactions"
expect(body).to include "0</span> New members"
end
end
end

View file

@ -9,13 +9,8 @@ RSpec.describe Admin::ChartsData, type: :service do
expect(described_class.new.call.map(&:first)).to eq(["Posts", "Comments", "Reactions", "New members"])
end
it "returns proper current period number" do
create_list(:article, 3, published_at: 4.days.ago)
expect(described_class.new.call.first.second).to eq(3)
end
it "returns proper previous period number" do
create_list(:article, 2, published_at: 9.days.ago)
create_list(:article, 2, published_at: 8.days.ago)
expect(described_class.new.call.first.third).to eq(2)
end
@ -23,4 +18,27 @@ RSpec.describe Admin::ChartsData, type: :service do
expect(described_class.new(20).call.first.fourth.size).to eq(20)
end
describe "current period" do
it "returns proper number of items" do
create(:article, published_at: Time.zone.today)
create_list(:article, 3, published_at: 4.days.ago)
create_list(:article, 2, published_at: 7.days.ago)
create(:article, published_at: 8.days.ago)
expect(described_class.new.call.first.second).to eq(5)
end
it "ignores today" do
create(:article, published_at: Time.zone.today)
expect(described_class.new.call.first.second).to eq(0)
end
it "goes back seven days by default" do
create(:article, published_at: 7.days.ago)
create(:article, published_at: 8.days.ago)
expect(described_class.new.call.first.second).to eq(1)
end
end
end