Skip to content

Instantly share code, notes, and snippets.

@zorec
Created May 22, 2014 20:09
Show Gist options
  • Save zorec/5c16485ae6a0b06aacf1 to your computer and use it in GitHub Desktop.
Save zorec/5c16485ae6a0b06aacf1 to your computer and use it in GitHub Desktop.
Ruby on Rails - measure frequency of actions (for optimalization or improving user interface)
# 3. wait few days (weeks) to gather data :)
# 4. display results by this rake task sorted from most frequent action to least frequent action
# rake action:show_frequency
# 5. The rule 80/20 says - users are doing 20% of actions in 80% of time
# -> increase performance, improve user experience, ... of these actions
namespace :action do
desc "This tasks shows freuquency of requests"
task show_frequency: :environment do
sum = Action.sum(:count)
Action.all.order('count desc').each_with_index do |a, index|
p "#{index}, #{a.controller}##{a.action_name}, " + ((a.count.to_f / sum.to_f) * 100).round(2).to_s + ", " + a.count.to_s
end
end
end
# 1. I am saving results to database
# rails generate model Action controller:string action_name:string count:integer
# 2. add after_filter to increase count of executed action
class ApplicationController < ActionController::Base
after_filter :save_action
# ... other logic
def save_action
a = Action.where(controller: params[:controller], action_name: params[:action]).first_or_create
a.increment!(:count)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment