Created
May 22, 2014 20:09
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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