From c66b9c0812beff1828c82f4fcde6b885b337d0f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=99Barbara=20Oliveira?= <143180473+BarbaraOliveira13@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:01:08 +0100 Subject: [PATCH] Fix/backport decidim awesome slowness on proposals index page (#631) * 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 --- .env-example | 4 +- config/environments/development.rb | 1 + config/initializers/decidim_awesome.rb | 5 +++ config/secrets.yml | 2 + .../proposal_extra_field_spec.rb | 43 +++---------------- 5 files changed, 16 insertions(+), 39 deletions(-) create mode 100644 config/initializers/decidim_awesome.rb diff --git a/.env-example b/.env-example index 14c215885a..18d2e784f8 100644 --- a/.env-example +++ b/.env-example @@ -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 diff --git a/config/environments/development.rb b/config/environments/development.rb index d667dc6158..6ed8a74c31 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -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 diff --git a/config/initializers/decidim_awesome.rb b/config/initializers/decidim_awesome.rb new file mode 100644 index 0000000000..24a30cdbc2 --- /dev/null +++ b/config/initializers/decidim_awesome.rb @@ -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 diff --git a/config/secrets.yml b/config/secrets.yml index 2f166fdfd3..78f94c3731 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -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 %> diff --git a/spec/models/decidim/decidim_awesome/proposal_extra_field_spec.rb b/spec/models/decidim/decidim_awesome/proposal_extra_field_spec.rb index 1223979e07..b903553fbd 100644 --- a/spec/models/decidim/decidim_awesome/proposal_extra_field_spec.rb +++ b/spec/models/decidim/decidim_awesome/proposal_extra_field_spec.rb @@ -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 } @@ -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