Class: Metrify::MetrifyController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/metrify/metrify_controller.rb

Instance Method Summary collapse

Instance Method Details

#chart_dataObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/metrify/metrify_controller.rb', line 57

def chart_data
  
 @stat_names = @metrified_class.stat_names(parsed_stat_names, params[:filters])
 @unit = params[:unit] || unit

 @number_of_stats = params[:number_of_stats] || number_of_stats
 @historical_site_stats = @metrified_class.historical_values(Time.zone.now.beginning_of_week, number_of_stats, unit)

 json = @stat_names.map{|s| {:name => @metrified_class.display_name(s), 
                             :pointInterval => (1.send(@unit) * 1000), 
                             :pointStart => (@number_of_stats.send(@unit).ago.to_i * 1000), 
                             :data => @historical_site_stats.map{|h| h.send(s)}}}

 setup_historical_stats
 respond_to do |format|
   format.json {
     render :layout => false , :json => @stat_names.map{|s| {:name => @metrified_class.display_name(s), 
       :pointInterval => (1.send(@unit) * 1000), 
       :pointStart => (@number_of_stats.send(@unit).ago.to_i * 1000), 
       :data => get_stat_arr(s, @historical_site_stats)}}.to_json
       }
 end
 
end

#get_stat_arr(stat, historical_stats = @historical_site_stats) ⇒ Object

def set_metrify_model; end



84
85
86
87
88
89
90
91
92
# File 'app/controllers/metrify/metrify_controller.rb', line 84

def get_stat_arr(stat, historical_stats = @historical_site_stats)
  stat_over_time = []
  historical_stats.each do |s|
    value = s.send(stat)
    value = 0 if !value
    stat_over_time << value
  end
  stat_over_time
end

#graph_statsObject



46
47
48
49
50
# File 'app/controllers/metrify/metrify_controller.rb', line 46

def graph_stats
  prepare_for_graph

  @stat_names = parsed_stat_names
end

#indexObject

def self.included(base)

  base.class_eval do
    before_filter :set_metrify_class
  end
  base.extend ClassMethods
  base.send :include, InstanceMethods
  base.helper MetrifyHelper
end


15
16
17
18
19
# File 'app/controllers/metrify/metrify_controller.rb', line 15

def index
  setup_historical_stats
  @stat_names = @metrified_class.stat_names
  @historical_site_stats.reverse!
end

#number_of_statsObject



31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/metrify/metrify_controller.rb', line 31

def number_of_stats

 number = case unit.to_sym
   when :month then 12
   when :week then 52
   when :day then 30
   else 48 #hour
 end
 
end

#parsed_stat_namesObject



52
53
54
# File 'app/controllers/metrify/metrify_controller.rb', line 52

def parsed_stat_names
  !params[:stat_names].blank? ? params[:stat_names].split(',') : @metrified_class.stat_names
end

#set_metrify_classObject



21
22
23
# File 'app/controllers/metrify/metrify_controller.rb', line 21

def set_metrify_class
  @metrified_class = metrify_model
end

#setup_historical_statsObject



25
26
27
28
29
# File 'app/controllers/metrify/metrify_controller.rb', line 25

def setup_historical_stats
  @unit = unit
  @number_of_stats = number_of_stats
  @historical_site_stats = @metrified_class.historical_values(Time.zone.now.beginning_of_week, number_of_stats, unit)
end

#unitObject



42
43
44
# File 'app/controllers/metrify/metrify_controller.rb', line 42

def unit
  params[:unit] || :week
end