Skip to content

Commit

Permalink
Fix do not count blocked users to stats
Browse files Browse the repository at this point in the history
* Fix do not count blocked users to stats

* Add date condition for blocked or deleted users
  • Loading branch information
alecslupu committed Jun 14, 2023
1 parent 51b81b3 commit 6037140
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
12 changes: 6 additions & 6 deletions decidim-core/app/queries/decidim/metrics/users_metric_manage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 11,15 @@ def metric_name
private

def query
return @query if @query

@query = Decidim::User.where(organization: @organization)
@query = @query.where("created_at <= ?", end_time)
@query
@query ||= Decidim::User.where(organization: @organization)
.where("deleted_at IS NULL OR deleted_at > ?", end_time)
.where("blocked_at IS NULL OR blocked_at > ?", end_time)
.confirmed
.where("created_at <= ?", end_time)
end

def quantity
@quantity ||= @query.where("created_at >= ?", start_time).count
@quantity ||= query.where("created_at >= ?", start_time).count
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 5,25 @@
describe Decidim::Metrics::UsersMetricManage do
let(:organization) { create(:organization) }
let(:day) { Time.zone.yesterday }
let!(:users) { create_list(:user, 5, created_at: day, confirmed_at: nil, organization: organization) }
let!(:other_user) { create(:user, created_at: day) }
let!(:users) { create_list(:user, 5, :confirmed, created_at: day, organization: organization) }
let!(:other_user) { create(:user, :confirmed, created_at: day) }
let!(:unconfirmed_user) { create(:user, created_at: day, organization: organization) }

include_context "when managing metrics"

context "when executing" do
it "creates new metric records" do
registry = generate_metric_registry.first
shared_examples "computes the metric" do
it "creates new metric records" do
registry = generate_metric_registry.first

expect(registry.day).to eq(day)
expect(registry.cumulative).to eq(5)
expect(registry.quantity).to eq(5)
expect(registry.day).to eq(day)
expect(registry.cumulative).to eq(5)
expect(registry.quantity).to eq(5)
end
end

include_examples "computes the metric"

it "does not create any record if there is no data" do
registry = generate_metric_registry("2017-01-01")

Expand All @@ -34,5 39,56 @@
expect(registry.cumulative).to eq(5)
expect(registry.quantity).to eq(5)
end

context "when removing deleted users from query" do
context "when the user is deleted long before the end_time" do
let!(:deleted_user) { create(:user, :confirmed, created_at: day, deleted_at: day - 1.month, organization: organization) }

include_examples "computes the metric"
end
context "when the user is deleted before the end_time" do
let!(:deleted_user) { create(:user, :confirmed, created_at: day, deleted_at: day, organization: organization) }

include_examples "computes the metric"
end

context "when the user is deleted after the end_time" do
let!(:deleted_user) { create(:user, :confirmed, created_at: day, deleted_at: day 1.month, organization: organization) }

it "does not count them" do
registry = generate_metric_registry.first

expect(Decidim::Metric.count).to eq(1)
expect(registry.cumulative).to eq(6)
expect(registry.quantity).to eq(6)
end
end
end

context "when removing blocked users from query" do
context "when the user is blocked long before the end_time" do
let!(:deleted_user) { create(:user, :confirmed, created_at: day, blocked_at: day - 1.month, organization: organization) }

include_examples "computes the metric"
end

context "when the user is blocked before the end_time" do
let!(:deleted_user) { create(:user, :confirmed, created_at: day, blocked_at: day, organization: organization) }

include_examples "computes the metric"
end

context "when the user is blocked after the end_time" do
let!(:deleted_user) { create(:user, :confirmed, created_at: day, blocked_at: day 1.month, organization: organization) }

it "does not count them" do
registry = generate_metric_registry.first

expect(Decidim::Metric.count).to eq(1)
expect(registry.cumulative).to eq(6)
expect(registry.quantity).to eq(6)
end
end
end
end
end

0 comments on commit 6037140

Please sign in to comment.