Skip to content

Commit

Permalink
Merge pull request #114 from identity-research-lab/stats
Browse files Browse the repository at this point in the history
Statistics/dashboard page
  • Loading branch information
CoralineAda authored Aug 28, 2024
2 parents 9584f83 + e78df76 commit 7a1e803
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 22 deletions.
12 changes: 12 additions & 0 deletions app/assets/stylesheets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,18 @@ th a:hover {
background-color: var(--color-red-highlight);
}

.highlight-green {
background-color: var(--color-green-highlight);
}

.highlight-grey {
background-color: var(--color-light-grey);
}

.highlight-red {
background-color: var(--color-red-highlight);
}

.word-cloud {
display: flex;
flex-wrap: wrap;
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/codebooks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class CodebooksController < ApplicationController

USERS = { ENV['GENERAL_ADMISSION_USERNAME'] => ENV['GENERAL_ADMISSION_PASSWORD'] }

def index
@contexts = Question::QUESTIONS
end
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
class QuestionsController < ApplicationController

USERS = { ENV['GENERAL_ADMISSION_USERNAME'] => ENV['GENERAL_ADMISSION_PASSWORD'] }


def show
@question = Question.from(params[:id])
@responses = SurveyResponse.all.order(:created_at).reject{|sr| sr.read_attribute(@question.key).nil? }
end

end
33 changes: 33 additions & 0 deletions app/controllers/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class StatsController < ApplicationController

def index

sentiments = SurveyResponse.all.pluck(:sentiment)
@pronoun_sentiment_positive = (sentiments.select{|sentiment| sentiment == "positive"}.count / sentiments.count.to_f * 100).to_i
@pronoun_sentiment_neutral = (sentiments.select{|sentiment| sentiment == "neutral"}.count / sentiments.count.to_f * 100).to_i
@pronoun_sentiment_negative = (sentiments.select{|sentiment| sentiment == "negative"}.count / sentiments.count.to_f * 100).to_i

survey_response_count = SurveyResponse.count
question_count = Question::QUESTIONS.count
answer_count = survey_response_count * question_count
persona_count = Persona.count
identity_count = Identity.count
code_count = Code.count
category_count = Category.count
keyword_count = Keyword.count

@total_datapoints = survey_response_count + (question_count * survey_response_count) + identity_count + code_count + category_count + keyword_count + sentiments.compact.count

@stats = {
"Participant survey responses" => survey_response_count,
"Survey questions" => question_count,
"Survey answers" => answer_count,
"Personas" => persona_count,
"Self-expressed identities" => identity_count,
"Codes" => code_count,
"Derived categories" => category_count,
"Derived Keywords" => keyword_count
}
end

end
18 changes: 8 additions & 10 deletions app/controllers/survey_responses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
class SurveyResponsesController < ApplicationController

USERS = { ENV['GENERAL_ADMISSION_USERNAME'] => ENV['GENERAL_ADMISSION_PASSWORD'] }


def index
if @theme = params.permit(:theme)[:theme]
@responses = SurveyResponse.where("? = ANY (themes)", @theme).order(:created_at)
else
@responses = SurveyResponse.all.order(:created_at)
end
end

def show
@theme = params.permit(:theme)[:theme]
@response = SurveyResponse.find(params[:id])
Expand All @@ -18,15 +16,15 @@ def show

@previous_response = SurveyResponse.where("created_at < ?", @response.created_at).order("created_at DESC").limit(1).first
@next_response = SurveyResponse.where("created_at > ?", @response.created_at).order("created_at ASC").limit(1).first

persona = Persona.find_or_initialize_by(survey_response_id: @response.id)
@categories = persona.categories.sort{ |a,b| "#{a.context}.#{a.name}" <=> "#{b.context}.#{b.name}" }
@keywords = persona.keywords.sort{ |a,b| a.name <=> b.name }
end

def new
end

def create
ImportFromCsv.perform(params.permit(:csv)[:csv])
redirect_to survey_responses_path
Expand All @@ -35,7 +33,7 @@ def create
def update
@response = SurveyResponse.find(params[:id])
sanitized_params = {}
response_params.each do |key, value|
response_params.each do |key, value|
sanitized_params[key] = value.join(",").split(",").reject(&:empty?).compact.map(&:strip).map(&:downcase)
end
success = @response.update(sanitized_params)
Expand All @@ -48,7 +46,7 @@ def update
end

end

def enqueue_keywords
KeywordExtractorJob.perform_async(params[:survey_response_id])
redirect_to( action: :show, id: params[:survey_response_id], params: {enqueued_at: Time.now.strftime("%I:%M:%S %P (%Z)")} )
Expand All @@ -59,7 +57,7 @@ def enqueue_keywords
def response_params
params.require(:survey_response).permit(themes: [], age_exp_codes: [], klass_exp_codes: [], race_ethnicity_exp_codes: [], religion_exp_codes: [], disability_exp_codes: [], neurodiversity_exp_codes: [], gender_exp_codes: [], lgbtqia_exp_codes: [], age_id_codes: [], klass_id_codes: [], race_ethnicity_id_codes: [], religion_id_codes: [], disability_id_codes: [], neurodiversity_id_codes: [], gender_id_codes: [], lgbtqia_id_codes: [], pronouns_id_codes: [], pronouns_exp_codes: [], pronouns_feel_codes: [], affinity_codes: [], notes_codes: [])
end

end


1 change: 1 addition & 0 deletions app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<li><a href="/survey_responses">Responses</a></li>
<li><a href="/codebooks">Codebook</a></li>
<li><a href="/survey_responses/new">Upload</a></li>
<li><a href="/stats">Statistics</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
Expand Down
19 changes: 19 additions & 0 deletions app/views/stats/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%- title "TWI-Web Statistics" %>

<h1>
Statistics.
</h1>

<h2><%= number_with_delimiter(@total_datapoints) %> total datapoints:</h2>
<ul>
<% @stats.each do |label, value| %>
<li><%= label %>: <span class="highlight-green"><%= value.is_a?(String) ? value : number_with_delimiter(value) %></span></li>
<% end %>
</ul>

<h2>Overall identity reflection sentiment:</h2>
<ul>
<li><span class="highlight-green"><%= @pronoun_sentiment_positive %>% positive</span></li>
<li><span class="highlight-red"><%= @pronoun_sentiment_negative %>% negative</span></li>
<li><span class="highlight-grey"><%= @pronoun_sentiment_neutral %>% neutral</span></li>
</ul>
13 changes: 7 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@

# Defines the root path route ("/")
# root "posts#index"

root "survey_responses#index"

get "about", controller: "static", action: "about"

resources :survey_responses do
post "enqueue_keywords", action: "enqueue_keywords"
end


resources :codebooks do
post "enqueue_categories", action: "enqueue_categories"
end

resources :questions

resources :stats

end

0 comments on commit 7a1e803

Please sign in to comment.