Permalink
Browse files

Merge branch 'master' into widget-cookie-errors

  • Loading branch information...
2 parents 5a91030 + f912dc4 commit 4c5d7a2831a381276b4d733577a6b7cb44ac9846 @lukebaker lukebaker committed Aug 12, 2013
@@ -1,5 +1,7 @@
class AbingoDashboardController < ApplicationController
before_filter :admin_only
+
+ caches_action :show_set, :layout => false
def index
if params[:all]
@@ -11,87 +13,56 @@ def index
## group results from all wiki surveys
def show_groups
- @experiments = Abingo::Experiment.all
-
- @grouped_experiments = Hash.new()
-
- @experiments.each do |e|
- if e.test_name =~ /^.*test_icecream.*$/
- next
- end
- if e.test_name =~ /^.*_\d{1,5}_(.*)/
- potential_group = $1
- # group tests together by having "test_name|#{earl.name}"
- elsif e.test_name =~ /^(.*?)\|/
- potential_group = $1
- elsif e.test_name =~ /^bg_color_aa_/
- potential_group = 'bg_color_aa'
- else
- potential_group = "unknown"
- end
- @grouped_experiments[potential_group] = [] unless @grouped_experiments.has_key?(potential_group)
- @grouped_experiments[potential_group] << e
- end
-
+ @grouped_experiments = group_experiments(Abingo::Experiment.all)
end
def show_set
- ids = params[:ids].class == String ? params[:ids].split(/, */).uniq : params[:ids]
+ @grouped_experiments = group_experiments(Abingo::Experiment.all)
+ ids = @grouped_experiments[params[:id]]
+
@experiments = Abingo::Experiment.find(ids, :include => :alternatives)
- session_list = []
- admin_users = User.find(:all, :conditions => {:admin => true})
- admin_user_list = admin_users.inject([]){|list, u| list << u.id}
- @experiments.each do |experiment|
- session_list += get_session_list(experiment, admin_user_list)
- end
-
- session_list.uniq!
+ admin_users = User.find(:all, :select => 'id', :conditions => {:admin => true})
+ admin_user_list = admin_users.map{|u| u.id}
+ session_list = get_session_list(@experiments, admin_user_list)
+ session_ids = session_list.map{|s| s['session_id'] }
- theresponse = Session.post(:objects_by_session_ids, {}, {:session_ids => session_list}.to_json)
+ theresponse = Session.post(:objects_by_session_ids, {}, {:session_ids => session_ids}.to_json)
@objects_by_session_ids = JSON.parse(theresponse.body)
- @voter_distribution = initialize_distribution_hash(@experiments.first)
- @uploader_distribution= initialize_distribution_hash(@experiments.first)
-
- @experiments.each do |experiment|
- add_totals_to_distribution(experiment, @voter_distribution, @objects_by_session_ids, "votes", admin_user_list)
- add_totals_to_distribution(experiment, @uploader_distribution, @objects_by_session_ids, "ideas", admin_user_list)
- end
+
+ distributions = get_distributions(session_list, @objects_by_session_ids, @experiments.first.alternatives)
+ @voter_distribution = distributions[0]
+ @uploader_distribution = distributions[1]
@summary_stats = calculate_summary_stats(@experiments.first, @voter_distribution, @uploader_distribution)
@vote_distribution_chart = create_voter_distribution_chart(@experiments.first, @voter_distribution)
@experiment = @experiments.first
# change title to make it a bit prettier on display
@experiment.test_name = "#{ids.size} experiments aggregated together"
render :action => :show
+ if ActionController::Base.perform_caching
+ cache_key = 'views' + url_for(:controller => :abingo_dashboard, :action => 'show_set', :id => params[:id]).gsub(/^h.+:\//, '')
+ ActionController::Base.cache_store.delay(:run_at => Time.now.utc + 1.day).delete(cache_key)
+ end
end
def show
@experiment = Abingo::Experiment.find(params[:id], :include => :alternatives)
- admin_users = User.find(:all, :conditions => {:admin => true})
- admin_user_list = admin_users.inject([]){|list, u| list << u.id}
- # Make a list of sessions we are interested in to get info from the pairwise server
- #this is to check for irregularities that I believe will be solved by redis
+ admin_users = User.find(:all, :select => 'id', :conditions => {:admin => true})
+ admin_user_list = admin_users.map{|u| u.id}
session_list = get_session_list(@experiment, admin_user_list)
- origsize = session_list.size
-
- session_list.uniq!
-
- if origsize != session_list.size
- flash[:notice] = "Warning: Experiment #{@experiment.id} has duplicate session ids"
- end
+ session_ids = session_list.map{|s| s['session_id'] }
# Get the list from the server
# The Session class uses json for simplicity, we need to do some parsing here
# It's important that we send parameters in the body here, otherwise some undefined behavior occurs
# when the URI gets too long
- theresponse = Session.post(:objects_by_session_ids, {}, {:session_ids => session_list}.to_json)
+ theresponse = Session.post(:objects_by_session_ids, {}, {:session_ids => session_ids}.to_json)
@objects_by_session_ids = JSON.parse(theresponse.body)
- @voter_distribution = initialize_distribution_hash(@experiment)
- @uploader_distribution= initialize_distribution_hash(@experiment)
- add_totals_to_distribution(@experiment, @voter_distribution, @objects_by_session_ids, "votes", admin_user_list)
- add_totals_to_distribution(@experiment, @uploader_distribution, @objects_by_session_ids, "ideas", admin_user_list)
+ distributions = get_distributions(session_list, @objects_by_session_ids, @experiment.alternatives)
+ @voter_distribution = distributions[0]
+ @uploader_distribution = distributions[1]
# Calculate some summary stats
@summary_stats = calculate_summary_stats(@experiment, @voter_distribution, @uploader_distribution)
@@ -126,44 +97,72 @@ def median(array, already_sorted=false)
private
- def get_session_list(experiment, admin_user_list)
- session_list = []
- experiment.alternatives.each do |alt|
- alt.session_infos.each do |sess|
- if sess.user_id and admin_user_list.include?(sess.user_id)
- next
- else
- session_list << sess.session_id
- end
+ def group_experiments(experiments)
+ grouped_experiments = Hash.new()
+
+ experiments.each do |e|
+ if e.test_name =~ /^.*test_icecream.*$/
+ next
+ end
+ if e.test_name =~ /^.*_\d{1,5}_(.*)/
+ potential_group = $1
+ # group tests together by having "test_name|#{earl.name}"
+ elsif e.test_name =~ /^(.*?)\|/
+ potential_group = $1
+ elsif e.test_name =~ /^bg_color_aa_/
+ potential_group = 'bg_color_aa'
+ else
+ potential_group = "unknown"
end
+ grouped_experiments[potential_group] = [] unless grouped_experiments.has_key?(potential_group)
+ grouped_experiments[potential_group] << e.id
end
- session_list
+
+ grouped_experiments
end
- def initialize_distribution_hash(experiment)
- distribution = Hash.new(0)
- experiment.alternatives.each do |a|
- distribution[a.content] = Hash.new(0)
+ def get_session_list(experiment, admin_user_list)
+ if experiment.class == Array
+ experiment_ids = experiment.map{|e| e.id}
+ else
+ experiment_ids = [experiment.id]
end
- distribution
+ sql = "SELECT DISTINCT(s.session_id), a.content
+ FROM `experiments` e
+ LEFT JOIN `alternatives` a ON (a.experiment_id = e.id)
+ LEFT JOIN `trials` t ON (t.alternative_id = a.id)
+ LEFT JOIN `session_infos` s ON (s.id = t.session_info_id)
+ WHERE e.id IN (#{experiment_ids.join(",")})
+ AND s.session_id IS NOT NULL
+ AND (s.user_id NOT IN (#{admin_user_list.join(",")}) OR s.user_id IS NULL)"
+ Abingo::Experiment.connection.select_all(sql)
end
- def add_totals_to_distribution(experiment, distribution, objects_by_session_ids, object_type, admin_user_list)
- experiment.alternatives.each do |a|
- a.session_infos.each do |s|
- if s.user_id and admin_user_list.include?(s.user_id)
- next
- end
+ def get_distributions(session_list, objects_by_session_ids, alternatives)
+ voter_distribution = {}
+ uploader_distribution = {}
+ alternatives.each do |a|
+ voter_distribution[a.content] = Hash.new(0)
+ uploader_distribution[a.content] = Hash.new(0)
+ end
+ session_list.each do |s|
+ sess = objects_by_session_ids[s['session_id']]
+ if sess.nil?
+ num = 0
+ else
+ num = sess['votes']
+ end
+ voter_distribution[s['content']][num.to_i] +=1
- num = objects_by_session_ids[s.session_id][object_type] rescue nil
-
- if num
- distribution[a.content][num] +=1
- else
- distribution[a.content][0] +=1
- end
+ sess = objects_by_session_ids[s['session_id']]
+ if sess.nil?
+ num = 0
+ else
+ num = sess['ideas']
end
+ uploader_distribution[s['content']][num.to_i] +=1
end
+ [voter_distribution, uploader_distribution]
end
def calculate_summary_stats(experiment, voter_distribution, uploader_distribution)
@@ -1,5 +1,5 @@
%h1= "Automatically found groups of Ab test experiments"
--@grouped_experiments.each do |group, experiment_list|
- =link_to(group, :action => :show_set, :ids => experiment_list.map{|e| e.id}.join(','))
+-@grouped_experiments.each do |group, experiment_ids|
+ =link_to(group, :action => :show_set, :id => group)
%br
@@ -70,7 +70,7 @@
= f.label :default_lang, "Default language" + link_to(' <i class="icon-question-sign"></i>', 'http://blog.allourideas.org/post/5137327337/languages-and-all-our-ideas', :target=> '_blank'), :class => 'control-label'
.controls
= # Have English be the first, but sort the rest
- = f.select :default_lang, [["English", "en"]] + [["French", "fr"], ["Spanish", "es"], ["Portuguese", "pt"], ["Arabic", "ar"], ["Turkish", "tr"], ["Italian", "it"], ["Dutch", "nl"], ["Hebrew", "he"], ["German", "de"], ["Farsi", "fa"], ["Finnish", "fi"], ["Albanian", "sq"], ["Mandarin", "zh"]].sort
+ = f.select :default_lang, [["English", "en"]] + [["French", "fr"], ["Spanish", "es"], ["Portuguese", "pt"], ["Arabic", "ar"], ["Turkish", "tr"], ["Italian", "it"], ["Dutch", "nl"], ["Hebrew", "he"], ["German", "de"], ["Farsi", "fa"], ["Finnish", "fi"], ["Albanian", "sq"], ["Chinese", "zh"]].sort
.control-group
= f.label :ga_code, t('admin.google_analytics_code') + link_to(' <i class="icon-quesiton-sign"></i>', 'http://blog.allourideas.org/post/947544304/add-google-analytics-on-your-idea-marketplace', :target=> '_blank'), :class => 'control-label'
.controls
View
@@ -15,3 +15,4 @@
config.action_mailer.delivery_method = :smtp
end
+ActionController::Base.cache_store = :file_store, "#{Rails.root}/tmp/cache"

0 comments on commit 4c5d7a2

Please sign in to comment.