Skip to content

Commit

Permalink
Fix/backport decidim awesome slowness on proposals index page (#631)
Browse files Browse the repository at this point in the history
* add env variable

* add UUID and IP to logs

* add weighted voting configuration

* add secrets for weighted voting

* update test to fix CI

* continue fix test file

* fix CI

* fix CI

* clean spec

* clean and add test
  • Loading branch information
BarbaraOliveira13 authored Nov 22, 2024
1 parent 56c0a4f commit c66b9c0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 39 deletions.
4 changes: 3 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ DECIDIM_ADMIN_PASSWORD_STRONG="false"
## Generate values with: bin/rails decidim:pwa:generate_vapid_keys
# VAPID_PUBLIC_KEY
# VAPID_PRIVATE_KEY
RAILS_LOG_LEVEL=warn
# RAILS_LOG_LEVEL=warn

# DECIDIM_AWESOME_WEIGHTED_PROPOSAL_VOTING_ENABLED=disabled # or enabled

# Default notifications sending frequency : (daily, weekly, none, real_time)
# NOTIFICATIONS_SENDING_FREQUENCY=daily
Expand Down
1 change: 1 addition & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@
# Setting this to 100 years should be enough
config.global_id.expires_in = 100.years
config.deface.enabled = ENV.fetch("DEFACE_ENABLED", nil) == "true"
config.log_tags = [:uuid, :remote_ip]
end
5 changes: 5 additions & 0 deletions config/initializers/decidim_awesome.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

Decidim::DecidimAwesome.configure do |config|
config.weighted_proposal_voting = Rails.application.secrets.dig(:decidim, :decidim_awesome, :weighted_proposal_voting_enabled)&.to_sym
end
2 changes: 2 additions & 0 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
default: &default
asset_host: <%= ENV["ASSET_HOST"] %>
decidim:
decidim_awesome:
weighted_proposal_voting_enabled: <%= ENV.fetch("DECIDIM_AWESOME_WEIGHTED_PROPOSAL_VOTING_ENABLED", "disabled") %>
admin_password:
expiration_days: <%= ENV.fetch("DECIDIM_ADMIN_PASSWORD_EXPIRATION_DAYS", 365).to_i %>
min_length: <%= ENV.fetch("DECIDIM_ADMIN_PASSWORD_MIN_LENGTH", 15).to_i %>
Expand Down
43 changes: 5 additions & 38 deletions spec/models/decidim/decidim_awesome/proposal_extra_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Decidim::DecidimAwesome

let(:extra_fields) { create(:awesome_proposal_extra_fields, proposal: create(:extended_proposal)) }
let(:proposal) { create(:extended_proposal) }
let(:component) { create(:component, settings: { awesome_voting_manifest: "default" }) }

it { is_expected.to be_valid }

Expand Down Expand Up @@ -183,44 +184,10 @@ module Decidim::DecidimAwesome
end
end

describe "all_vote_weights" do
let!(:extra_fields) { create(:awesome_proposal_extra_fields, proposal: proposal) }
let!(:another_extra_fields) { create(:awesome_proposal_extra_fields, proposal: another_proposal) }
let!(:unrelated_another_extra_fields) { create(:awesome_proposal_extra_fields, :with_votes, proposal: create(:extended_proposal)) }
let(:another_proposal) { create(:proposal, component: proposal.component) }
let!(:votes) do
vote = create(:proposal_vote, proposal: proposal, author: create(:user, organization: proposal.organization))
create(:awesome_vote_weight, vote: vote, weight: 1)
end
let!(:other_votes) do
vote = create(:proposal_vote, proposal: another_proposal, author: create(:user, organization: proposal.organization))
create(:awesome_vote_weight, vote: vote, weight: 2)
end

it "returns all vote weights for a component" do
expect(proposal.reload.all_vote_weights).to contain_exactly(1, 2)
expect(another_proposal.reload.all_vote_weights).to contain_exactly(1, 2)
expect(proposal.vote_weights).to eq({ "1" => 1, "2" => 0 })
expect(another_proposal.vote_weights).to eq({ "1" => 0, "2" => 1 })
end

context "when wrong cache exists" do
before do
# rubocop:disable Rails/SkipsModelValidations:
# we don't want to trigger the active record hooks
extra_fields.update_columns(vote_weight_totals: { "3" => 1, "4" => 1 })
# rubocop:enable Rails/SkipsModelValidations:
end

it "returns all vote weights for a component" do
expect(proposal.reload.extra_fields.vote_weight_totals).to eq({ "3" => 1, "4" => 1 })
expect(proposal.vote_weights).to eq({ "1" => 0, "2" => 0 })
proposal.update_vote_weights!
expect(proposal.vote_weights).to eq({ "1" => 1, "2" => 0 })
expect(another_proposal.reload.vote_weights).to eq({ "1" => 0, "2" => 1 })
expect(proposal.extra_fields.vote_weight_totals).to eq({ "1" => 1 })
expect(another_proposal.extra_fields.vote_weight_totals).to eq({ "2" => 1 })
end
describe "weighted_proposal_voting_enabled" do
it "is disabled by default" do
default_value = Rails.application.secrets.dig(:decidim, :decidim_awesome, :weighted_proposal_voting_enabled)
expect(default_value).to eq("disabled")
end
end

Expand Down

0 comments on commit c66b9c0

Please sign in to comment.