Merge pull request #892 from consul/public-stats

Public stats
This commit is contained in:
Enrique García
2016-02-15 13:34:00 +01:00
9 changed files with 212 additions and 29 deletions

View File

@@ -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

View File

@@ -0,0 +1,64 @@
<div class="stats row-full">
<div class="row">
<div class="small-12 column">
<h1><%= t "admin.stats.show.stats_title" %></h1>
<div class="row stats-numbers">
<div class="small-12 medium-4 column">
<p class="featured">
<%= t "stats.index.visits" %><br>
<span class="number"><%= number_with_delimiter(@visits) %></span>
</p>
<p>
<%= t "stats.index.debates" %><br>
<span class="number"><%= number_with_delimiter(@debates) %></span>
</p>
<p>
<%= t "stats.index.proposals" %><br>
<span class="number"><%= number_with_delimiter(@proposals) %></span>
</p>
<p>
<%= t "stats.index.comments" %><br>
<span class="number"><%= number_with_delimiter(@comments) %></span>
</p>
</div>
<div class="small-12 medium-4 column">
<p class="featured">
<%= t "stats.index.proposal_votes" %><br>
<span class="number"><%= number_with_delimiter(@proposal_votes) %><br></span>
</p>
<p>
<%= t "stats.index.debate_votes" %><br>
<span class="number"><%= number_with_delimiter(@debate_votes) %></span>
</p>
<p>
<%= t "stats.index.comment_votes" %><br>
<span class="number"><%= number_with_delimiter(@comment_votes) %></span>
</p>
<p>
<%= t "stats.index.votes" %><br>
<span class="number"><%= number_with_delimiter(@votes) %></span>
</p>
</div>
<div class="small-12 medium-4 column">
<p class="featured">
<%= t "stats.index.verified_users" %><br>
<span class="number"><%= number_with_delimiter(@verified_users) %></span>
</p>
<p>
<%= t "stats.index.unverified_users" %><br>
<span class="number"><%= number_with_delimiter(@unverified_users) %></span>
</p>
</div>
</div>
</div>
</div>

View File

@@ -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) %>'
}

View File

@@ -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:

View File

@@ -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:

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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