From 16db3f8537db214bc18a21f48018a9dad32046e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 8 Feb 2016 14:12:37 +0100 Subject: [PATCH 1/6] adds minimal public stats --- app/controllers/stats_controller.rb | 22 ++++++++++ app/views/stats/index.html.erb | 64 +++++++++++++++++++++++++++++ config/locales/en.yml | 12 ++++++ config/locales/es.yml | 12 ++++++ config/routes.rb | 2 + 5 files changed, 112 insertions(+) create mode 100644 app/controllers/stats_controller.rb create mode 100644 app/views/stats/index.html.erb diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb new file mode 100644 index 000000000..4d7755994 --- /dev/null +++ b/app/controllers/stats_controller.rb @@ -0,0 +1,22 @@ +class StatsController < ApplicationController + include FeatureFlags + + feature_flag :public_stats + + skip_authorization_check + + def index + @visits = Visit.count + @debates = Debate.with_hidden.count + @proposals = Proposal.with_hidden.count + @comments = Comment.with_hidden.count + + @debate_votes = Vote.where(votable_type: 'Debate').count + @proposal_votes = Vote.where(votable_type: 'Proposal').count + @comment_votes = Vote.where(votable_type: 'Comment').count + @votes = Vote.count + + @verified_users = User.with_hidden.level_two_or_three_verified.count + @unverified_users = User.with_hidden.unverified.count + 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/config/locales/en.yml b/config/locales/en.yml index 699923c5a..7f6f30fe4 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -418,6 +418,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 otes + 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 a69f3c364..68cd7f33c 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -419,6 +419,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 b48b4628f..749d3f5ee 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -67,6 +67,8 @@ Rails.application.routes.draw do resources :spending_proposals, only: [:index, :new, :create] + resources :stats, only: [:index] + resources :legislations, only: [:show] resources :annotations do From 0d6e5eebcbb161d3caae26e1bd51a841f4066f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 8 Feb 2016 14:13:15 +0100 Subject: [PATCH 2/6] flags public stats as a default feature --- db/seeds.rb | 1 + 1 file changed, 1 insertion(+) 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 From 3a51145bc9ddb715f5c2a2c754bb12cf6fe95854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 8 Feb 2016 14:16:06 +0100 Subject: [PATCH 3/6] moves existent stats specs to admin namespace --- spec/features/{ => admin}/stats_spec.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename spec/features/{ => admin}/stats_spec.rb (100%) diff --git a/spec/features/stats_spec.rb b/spec/features/admin/stats_spec.rb similarity index 100% rename from spec/features/stats_spec.rb rename to spec/features/admin/stats_spec.rb From 3e0dd544a6adcdcf8ead35fa1c4b57f2db9a77dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 8 Feb 2016 17:49:03 +0100 Subject: [PATCH 4/6] adds specs --- config/locales/en.yml | 2 +- spec/features/stats_spec.rb | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 spec/features/stats_spec.rb diff --git a/config/locales/en.yml b/config/locales/en.yml index 7f6f30fe4..d0664cdd3 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -427,7 +427,7 @@ en: proposal_votes: Votes on proposals debate_votes: Votes on debates comment_votes: Votes on comments - votes: Total otes + votes: Total votes verified_users: Verified users unverified_users: Unverified users unauthorized: diff --git a/spec/features/stats_spec.rb b/spec/features/stats_spec.rb new file mode 100644 index 000000000..89a31ef14 --- /dev/null +++ b/spec/features/stats_spec.rb @@ -0,0 +1,52 @@ +require 'rails_helper' + +feature 'Stats' do + + context 'Summary' do + + scenario 'General' do + create(:debate) + 2.times { create(:proposal) } + 3.times { create(:comment, commentable: Debate.first) } + 4.times { create(:visit) } + + visit 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 stats_path + + 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) } + 2.times { create(:user) } + + visit stats_path + + expect(page).to have_content "Verified users 3" + expect(page).to have_content "Unverified users 2" + end + + end + +end \ No newline at end of file From 07c009e0de1b4137716e07bdaa3f1830e6582623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Mon, 8 Feb 2016 17:49:49 +0100 Subject: [PATCH 5/6] adds json view for basic stats --- app/views/stats/index.json.erb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 app/views/stats/index.json.erb 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 From 2dafc74505cd349db6e922b5364734217e903397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Mon, 8 Feb 2016 18:23:13 +0100 Subject: [PATCH 6/6] cacheizes all public stats --- app/controllers/stats_controller.rb | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 4d7755994..d600e2e9c 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -6,17 +6,23 @@ class StatsController < ApplicationController skip_authorization_check def index - @visits = Visit.count - @debates = Debate.with_hidden.count - @proposals = Proposal.with_hidden.count - @comments = Comment.with_hidden.count + @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 = Vote.where(votable_type: 'Debate').count - @proposal_votes = Vote.where(votable_type: 'Proposal').count - @comment_votes = Vote.where(votable_type: 'Comment').count - @votes = Vote.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 = User.with_hidden.level_two_or_three_verified.count - @unverified_users = User.with_hidden.unverified.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