FIX: admin reports would sometimes return fewer than 30 days of data, causing current 30 day period counts to be too small and prev30day counts to count the wrong days

This commit is contained in:
Neil Lalonde 2018-02-01 15:50:41 -05:00
parent 08db10491e
commit ef2a7ac0f7
2 changed files with 60 additions and 16 deletions

View File

@ -10,7 +10,7 @@ class Report
def initialize(type) def initialize(type)
@type = type @type = type
@start_date ||= 1.month.ago.beginning_of_day @start_date ||= Report.default_days.days.ago.beginning_of_day
@end_date ||= Time.zone.now.end_of_day @end_date ||= Time.zone.now.end_of_day
end end
@ -75,11 +75,12 @@ class Report
report.data << { x: date, y: count } report.data << { x: date, y: count }
end end
report.total = data.sum(:count) report.total = data.sum(:count)
report.prev30Days = data.where('date >= ? AND date <= ?',
(report.start_date - 31.days).to_date, report.prev30Days = data.where(
(report.end_date - 31.days).to_date) 'date >= ? AND date < ?',
.sum(:count) (report.start_date - 31.days).to_date, report.start_date.to_date
).sum(:count)
end end
def self.report_visits(report) def self.report_visits(report)

View File

@ -2,6 +2,50 @@ require 'rails_helper'
describe Report do describe Report do
describe "counting" do
describe "requests" do
before do
freeze_time DateTime.parse('2017-03-01 12:00')
# today, an incomplete day:
ApplicationRequest.create(date: 0.days.ago.to_time, req_type: ApplicationRequest.req_types['http_total'], count: 1)
# 60 complete days:
30.times do |i|
ApplicationRequest.create(date: (i + 1).days.ago.to_time, req_type: ApplicationRequest.req_types['http_total'], count: 10)
end
30.times do |i|
ApplicationRequest.create(date: (31 + i).days.ago.to_time, req_type: ApplicationRequest.req_types['http_total'], count: 100)
end
end
subject(:json) { Report.find("http_total_reqs").as_json }
it "counts the correct records" do
expect(json[:data].size).to eq(31) # today and 30 full days
expect(json[:data][0..-2].sum { |d| d[:y] }).to eq(300)
expect(json[:prev30Days]).to eq(3000)
end
end
describe "topics" do
before do
freeze_time DateTime.parse('2017-03-01 12:00')
((0..32).to_a + [60, 61, 62, 63]).each do |i|
Fabricate(:topic, created_at: i.days.ago)
end
end
subject(:json) { Report.find("topics").as_json }
it "counts the correct records" do
expect(json[:data].size).to eq(31)
expect(json[:prev30Days]).to eq(3)
end
end
end
describe 'visits report' do describe 'visits report' do
let(:report) { Report.find('visits') } let(:report) { Report.find('visits') }
@ -58,18 +102,17 @@ describe Report do
Fabricate(fabricator, created_at: 35.days.ago) Fabricate(fabricator, created_at: 35.days.ago)
end end
context 'returns a report with data' it "returns today's data" do
it "returns today's data" do expect(report.data.select { |v| v[:x].today? }).to be_present
expect(report.data.select { |v| v[:x].today? }).to be_present end
end
it 'returns total data' do it 'returns total data' do
expect(report.total).to eq 7 expect(report.total).to eq 7
end end
it "returns previous 30 day's data" do it "returns previous 30 day's data" do
expect(report.prev30Days).to be_present expect(report.prev30Days).to be_present
end end
end end
end end
end end