diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb new file mode 100644 index 000000000..d600e2e9c --- /dev/null +++ b/app/controllers/stats_controller.rb @@ -0,0 +1,28 @@ +class StatsController < ApplicationController + include FeatureFlags + + feature_flag :public_stats + + skip_authorization_check + + def index + @visits = daily_cache('visits') { Visit.count } + @debates = daily_cache('debates') { Debate.with_hidden.count } + @proposals = daily_cache('proposals') { Proposal.with_hidden.count } + @comments = daily_cache('comments') { Comment.with_hidden.count } + + @debate_votes = daily_cache('debate_votes') { Vote.where(votable_type: 'Debate').count } + @proposal_votes = daily_cache('proposal_votes') { Vote.where(votable_type: 'Proposal').count } + @comment_votes = daily_cache('comment_votes') { Vote.where(votable_type: 'Comment').count } + @votes = daily_cache('votes') { Vote.count } + + @verified_users = daily_cache('verified_users') { User.with_hidden.level_two_or_three_verified.count } + @unverified_users = daily_cache('unverified_users') { User.with_hidden.unverified.count } + end + + private + + def daily_cache(key, &block) + Rails.cache.fetch("public_stats/#{Time.now.strftime("%Y-%m-%d")}/#{key}", &block) + end +end diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb new file mode 100644 index 000000000..4065c3c40 --- /dev/null +++ b/app/views/stats/index.html.erb @@ -0,0 +1,64 @@ +
+
+
+

<%= t "admin.stats.show.stats_title" %>

+ +
+
+ + +

+ <%= t "stats.index.debates" %>
+ <%= number_with_delimiter(@debates) %> +

+

+ <%= t "stats.index.proposals" %>
+ <%= number_with_delimiter(@proposals) %> +

+

+ <%= t "stats.index.comments" %>
+ <%= number_with_delimiter(@comments) %> +

+
+ +
+ + +

+ <%= t "stats.index.debate_votes" %>
+ <%= number_with_delimiter(@debate_votes) %> +

+ +

+ <%= t "stats.index.comment_votes" %>
+ <%= number_with_delimiter(@comment_votes) %> +

+ +

+ <%= t "stats.index.votes" %>
+ <%= number_with_delimiter(@votes) %> +

+
+ +
+ + +

+ <%= t "stats.index.unverified_users" %>
+ <%= number_with_delimiter(@unverified_users) %> +

+
+ + +
+
+
diff --git a/app/views/stats/index.json.erb b/app/views/stats/index.json.erb new file mode 100644 index 000000000..be3335bfc --- /dev/null +++ b/app/views/stats/index.json.erb @@ -0,0 +1,12 @@ +{ + '<%= t('stats.index.visits') %>' : '<%= number_with_delimiter(@visits) %>', + '<%= t('stats.index.debates') %>' : <%= number_with_delimiter(@debates) %>', + '<%= t('stats.index.proposals') %>' : '<%= number_with_delimiter(@proposals) %>', + '<%= t('stats.index.comments') %>' : '<%= number_with_delimiter(@comments) %>', + '<%= t('stats.index.proposal_votes') %>' : '<%= number_with_delimiter(@proposal_votes) %>', + '<%= t('stats.index.debate_votes') %>' : '<%= number_with_delimiter(@debate_votes) %>', + '<%= t('stats.index.comment_votes') %>' : '<%= number_with_delimiter(@comment_votes) %>', + '<%= t('stats.index.votes') %>' : '<%= number_with_delimiter(@votes) %>', + '<%= t('stats.index.verified_users') %>' : '<%= number_with_delimiter(@verified_users) %>', + '<%= t('stats.index.unverified_users') %>' : '<%= number_with_delimiter(@unverified_users) %>' +} \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 25fde7a71..e4dcf33a3 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -430,6 +430,18 @@ en: recommendation_two: Any proposal or comment suggesting illegal action will be deleted. recommendations_title: How to create a spending proposal start_new: Create spending proposal + stats: + index: + visits: Visits + debates: Debates + proposals: Proposals + comments: Comments + proposal_votes: Votes on proposals + debate_votes: Votes on debates + comment_votes: Votes on comments + votes: Total votes + verified_users: Verified users + unverified_users: Unverified users unauthorized: default: You do not have permission to access this page. manage: diff --git a/config/locales/es.yml b/config/locales/es.yml index f2ce6a259..4502475ec 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -431,6 +431,18 @@ es: recommendation_two: Cualquier propuesta o comentario que implique acciones ilegales será eliminada. recommendations_title: Cómo crear una propuesta de gasto start_new: Crear una propuesta de gasto + stats: + index: + visits: Visitas + debates: Debates + proposals: Propuestas ciudadanas + comments: Comentarios + proposal_votes: Votos en propuestas + debate_votes: Votos en debates + comment_votes: Votos en comentarios + votes: Votos + verified_users: Usuarios verificados + unverified_users: Usuarios sin verificar unauthorized: default: No tienes permiso para acceder a esta página. manage: diff --git a/config/routes.rb b/config/routes.rb index c270eaa73..8b2e6d280 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -70,6 +70,8 @@ Rails.application.routes.draw do resources :spending_proposals, only: [:index, :new, :create] + resources :stats, only: [:index] + resources :legislations, only: [:show] resources :annotations do diff --git a/db/seeds.rb b/db/seeds.rb index 659b3b2c0..fb4f26b8b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -56,3 +56,4 @@ Setting['feature.spending_proposals'] = true Setting['feature.twitter_login'] = true Setting['feature.facebook_login'] = true Setting['feature.google_login'] = true +Setting['feature.public_stats'] = true diff --git a/spec/features/admin/stats_spec.rb b/spec/features/admin/stats_spec.rb new file mode 100644 index 000000000..a7bb64f58 --- /dev/null +++ b/spec/features/admin/stats_spec.rb @@ -0,0 +1,72 @@ +require 'rails_helper' + +feature 'Stats' do + + background do + admin = create(:administrator) + login_as(admin.user) + visit root_path + end + + context 'Summary' do + + scenario 'General' do + create(:debate) + 2.times { create(:proposal) } + 3.times { create(:comment, commentable: Debate.first) } + 4.times { create(:visit) } + + visit admin_stats_path + + expect(page).to have_content "Debates 1" + expect(page).to have_content "Proposals 2" + expect(page).to have_content "Comments 3" + expect(page).to have_content "Visits 4" + end + + scenario 'Votes' do + debate = create(:debate) + create(:vote, votable: debate) + + proposal = create(:proposal) + 2.times { create(:vote, votable: proposal) } + + comment = create(:comment) + 3.times { create(:vote, votable: comment) } + + visit admin_stats_path + + expect(page).to have_content "Debate votes 1" + expect(page).to have_content "Proposal votes 2" + expect(page).to have_content "Comment votes 3" + expect(page).to have_content "Total votes 6" + end + + scenario 'Users' do + 1.times { create(:user, :level_three) } + 2.times { create(:user, :level_two) } + 3.times { create(:user) } + + visit admin_stats_path + + expect(page).to have_content "Level three users 1" + expect(page).to have_content "Level two users 2" + expect(page).to have_content "Verified users 3" + expect(page).to have_content "Unverified users 4" + expect(page).to have_content "Total users 7" + end + + end + + scenario 'Level 2 user' do + visit account_path + click_link 'Verify my account' + verify_residence + confirm_phone + + visit admin_stats_path + + expect(page).to have_content "Level 2 User (1)" + end + +end diff --git a/spec/features/stats_spec.rb b/spec/features/stats_spec.rb index a7bb64f58..89a31ef14 100644 --- a/spec/features/stats_spec.rb +++ b/spec/features/stats_spec.rb @@ -2,12 +2,6 @@ require 'rails_helper' feature 'Stats' do - background do - admin = create(:administrator) - login_as(admin.user) - visit root_path - end - context 'Summary' do scenario 'General' do @@ -16,7 +10,7 @@ feature 'Stats' do 3.times { create(:comment, commentable: Debate.first) } 4.times { create(:visit) } - visit admin_stats_path + visit stats_path expect(page).to have_content "Debates 1" expect(page).to have_content "Proposals 2" @@ -34,39 +28,25 @@ feature 'Stats' do comment = create(:comment) 3.times { create(:vote, votable: comment) } - visit admin_stats_path + visit stats_path - expect(page).to have_content "Debate votes 1" - expect(page).to have_content "Proposal votes 2" - expect(page).to have_content "Comment votes 3" + expect(page).to have_content "Votes on debates 1" + expect(page).to have_content "Votes on proposals 2" + expect(page).to have_content "Votes on comments 3" expect(page).to have_content "Total votes 6" end scenario 'Users' do 1.times { create(:user, :level_three) } 2.times { create(:user, :level_two) } - 3.times { create(:user) } + 2.times { create(:user) } - visit admin_stats_path + visit stats_path - expect(page).to have_content "Level three users 1" - expect(page).to have_content "Level two users 2" expect(page).to have_content "Verified users 3" - expect(page).to have_content "Unverified users 4" - expect(page).to have_content "Total users 7" + expect(page).to have_content "Unverified users 2" end end - scenario 'Level 2 user' do - visit account_path - click_link 'Verify my account' - verify_residence - confirm_phone - - visit admin_stats_path - - expect(page).to have_content "Level 2 User (1)" - end - -end +end \ No newline at end of file