discourse/spec/models/web_hook_events_daily_aggregate_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
4.1 KiB
Ruby
Raw Normal View History

FEATURE: Add WebHookEventsDailyAggregate (#27542) * FEATURE: Add WebHookEventsDailyAggregate Add WebHookEventsDailyAggregate model to store daily aggregates of web hook events. Add AggregateWebHooksEvents job to aggregate web hook events daily. Add spec for WebHookEventsDailyAggregate model. * DEV: Update annotations for web_hook_events_daily_aggregate.rb * DEV: Update app/jobs/scheduled/aggregate_web_hooks_events.rb Co-authored-by: Martin Brennan <martin@discourse.org> * DEV: Address review feedback Solves: - https://github.com/discourse/discourse/pull/27542#discussion_r1646961101 - https://github.com/discourse/discourse/pull/27542#discussion_r1646958890 - https://github.com/discourse/discourse/pull/27542#discussion_r1646976808 - https://github.com/discourse/discourse/pull/27542#discussion_r1646979846 - https://github.com/discourse/discourse/pull/27542#discussion_r1646981036 * A11Y: Add translation to retain_web_hook_events_aggregate_days key * FEATURE: Purge old web hook events daily aggregate Solves: https://github.com/discourse/discourse/pull/27542#discussion_r1646961101 * DEV: Update tests for web_hook_events_daily_aggregate Update WebHookEventsDailyAggregate to not use save! at the end Solves: https://github.com/discourse/discourse/pull/27542#discussion_r1646984601 * PERF: Change job query to use WebHook table instead of WebHookEvent table * DEV: Update tests to use `fab!` * DEV: Address code review feedback. Add idempotency to job Add has_many to WebHook * DEV: add test case for job and change job query * DEV: Change AggregateWebHooksEvents job test name --------- Co-authored-by: Martin Brennan <martin@discourse.org>
2024-06-25 11:56:47 -05:00
# frozen_string_literal: true
RSpec.describe WebHookEventsDailyAggregate do
fab!(:web_hook)
fab!(:event) do
Fabricate(
:web_hook_event,
status: 200,
web_hook: web_hook,
created_at: 1.days.ago,
duration: 280,
)
end
fab!(:event_today) { Fabricate(:web_hook_event, status: 200, web_hook: web_hook, duration: 300) }
fab!(:failed_event) do
Fabricate(
:web_hook_event,
status: 400,
created_at: 1.days.ago,
web_hook: web_hook,
duration: 200,
)
end
fab!(:failed_event2) do
Fabricate(
:web_hook_event,
status: 400,
web_hook: web_hook,
created_at: 1.days.ago,
duration: 200,
)
end
fab!(:failed_event_today) do
Fabricate(:web_hook_event, status: 400, web_hook: web_hook, duration: 200)
end
describe ".purge_old" do
before { SiteSetting.retain_web_hook_events_aggregate_days = 1 }
it "should be able to purge old web hook event aggregates" do
web_hook = Fabricate(:web_hook)
WebHookEvent.create!(status: 200, web_hook: web_hook, created_at: 1.days.ago, duration: 180)
WebHookEvent.create!(status: 200, web_hook: web_hook, created_at: 2.days.ago, duration: 180)
yesterday_aggregate =
WebHookEventsDailyAggregate.create!(web_hook_id: web_hook.id, date: 1.days.ago)
WebHookEventsDailyAggregate.create!(
web_hook_id: web_hook.id,
date: 2.days.ago,
created_at: 2.days.ago,
)
expect { described_class.purge_old }.to change { WebHookEventsDailyAggregate.count }.by(-1)
expect(WebHookEventsDailyAggregate.find(yesterday_aggregate.id)).to eq(yesterday_aggregate)
end
end
describe "aggregation works" do
it "should be able to aggregate web hook events" do
yesterday_aggregate =
WebHookEventsDailyAggregate.create!(web_hook_id: web_hook.id, date: 1.days.ago)
yesterday_events = [event, failed_event, failed_event2]
expect(WebHookEventsDailyAggregate.count).to eq(1)
expect(yesterday_aggregate.web_hook_id).to eq(web_hook.id)
expect(yesterday_aggregate.date).to eq(1.days.ago.to_date)
expect(yesterday_aggregate.mean_duration).to eq(
yesterday_events.sum(&:duration) / yesterday_events.count,
)
expect(yesterday_aggregate.successful_event_count).to eq(1)
expect(yesterday_aggregate.failed_event_count).to eq(2)
end
it "should be able to filter by day" do
WebHookEventsDailyAggregate.create!(web_hook_id: web_hook.id, date: 1.days.ago)
WebHookEventsDailyAggregate.create!(web_hook_id: web_hook.id, date: 0.days.ago)
yesterday_events = [event, failed_event, failed_event2]
today_events = [event_today, failed_event_today]
yesterday_aggregate = WebHookEventsDailyAggregate.by_day(1.days.ago, 1.days.ago)
expect(yesterday_aggregate.count).to eq(1)
expect(yesterday_aggregate.first.date).to eq(1.days.ago.to_date)
expect(WebHookEventsDailyAggregate.count).to eq(2)
today_and_yesterday_aggregate = WebHookEventsDailyAggregate.by_day(1.days.ago, 0.days.ago)
expect(today_and_yesterday_aggregate.count).to eq(2)
expect(today_and_yesterday_aggregate.map(&:date)).to eq(
[0.days.ago.to_date, 1.days.ago.to_date],
)
expect(today_and_yesterday_aggregate.map(&:mean_duration)).to eq(
[
today_events.sum(&:duration) / today_events.count,
yesterday_events.sum(&:duration) / yesterday_events.count,
],
)
end
it "should not create a new WebHookEventsDailyAggregate row if AggregateWebHooksEvents runs twice" do
expect { Jobs::AggregateWebHooksEvents.new.execute(date: 1.days.ago) }.to change {
WebHookEventsDailyAggregate.count
}.by(1)
expect { Jobs::AggregateWebHooksEvents.new.execute(date: 1.days.ago) }.not_to change {
WebHookEventsDailyAggregate.count
}
end
it "should not fail if there are no events" do
expect { Jobs::AggregateWebHooksEvents.new.execute(date: 99.days.ago) }.not_to raise_error
expect(WebHookEventsDailyAggregate.count).to eq(1)
end
FEATURE: Add WebHookEventsDailyAggregate (#27542) * FEATURE: Add WebHookEventsDailyAggregate Add WebHookEventsDailyAggregate model to store daily aggregates of web hook events. Add AggregateWebHooksEvents job to aggregate web hook events daily. Add spec for WebHookEventsDailyAggregate model. * DEV: Update annotations for web_hook_events_daily_aggregate.rb * DEV: Update app/jobs/scheduled/aggregate_web_hooks_events.rb Co-authored-by: Martin Brennan <martin@discourse.org> * DEV: Address review feedback Solves: - https://github.com/discourse/discourse/pull/27542#discussion_r1646961101 - https://github.com/discourse/discourse/pull/27542#discussion_r1646958890 - https://github.com/discourse/discourse/pull/27542#discussion_r1646976808 - https://github.com/discourse/discourse/pull/27542#discussion_r1646979846 - https://github.com/discourse/discourse/pull/27542#discussion_r1646981036 * A11Y: Add translation to retain_web_hook_events_aggregate_days key * FEATURE: Purge old web hook events daily aggregate Solves: https://github.com/discourse/discourse/pull/27542#discussion_r1646961101 * DEV: Update tests for web_hook_events_daily_aggregate Update WebHookEventsDailyAggregate to not use save! at the end Solves: https://github.com/discourse/discourse/pull/27542#discussion_r1646984601 * PERF: Change job query to use WebHook table instead of WebHookEvent table * DEV: Update tests to use `fab!` * DEV: Address code review feedback. Add idempotency to job Add has_many to WebHook * DEV: add test case for job and change job query * DEV: Change AggregateWebHooksEvents job test name --------- Co-authored-by: Martin Brennan <martin@discourse.org>
2024-06-25 11:56:47 -05:00
end
end