Class: Mg::Goal

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/mountain-goat/models/mg/goal.rb

Overview

Mg::Goal represents a target for user interaction (E.g. Sign-up)

Attributes

goal_type

A symbol uniquely identifying this goal type (for code interactions)

name

A name for this goal

deleted_at

Is this goal deleted? (MG Console)

is_hidden

Is this goal hidden? (MG Console)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_type(s) ⇒ Object

Helper function to retrieve a goal by symbol



29
30
31
# File 'lib/mountain-goat/models/mg/goal.rb', line 29

def self.by_type(s)
  Mg::Goal.find( :first, :conditions => { :goal_type => s.to_s } )
end

Instance Method Details

#recent_records(time_frame) ⇒ Object

Get recent records based on start date



106
107
108
# File 'lib/mountain-goat/models/mg/goal.rb', line 106

def recent_records(time_frame)
  self.mg_records.find( :all, :conditions => [ "CREATED_AT > ?", time_frame.ago ] )  
end

#records_for_meta(var) ⇒ Object

Get all records for given meta (e.g. for “Referer”: { “Youtube” => Record1, } …)



34
35
36
37
38
# File 'lib/mountain-goat/models/mg/goal.rb', line 34

def records_for_meta(var)
  gmt = self.mg_goal_meta_types.find_by_var( var.to_s )
  return {} if gmt.nil?
  gmt.meta.map { |m| { m.data => m.record } }
end

#records_for_meta_val(var, data) ⇒ Object

Get all records for a given meta value (e.g. for “Referer”, “Facebook”: [ Record1, Record2, Record3 ] )



41
42
43
44
45
# File 'lib/mountain-goat/models/mg/goal.rb', line 41

def records_for_meta_val(var, data)
  gmt = self.mg_goal_meta_types.find_by_var( var.to_s )
  return [] if gmt.nil?
  gmt.meta.find(:all, :conditions => { :data => data } ).map { |m| m.record }
end

#records_for_meta_val_pivot(var, data, pivot) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mountain-goat/models/mg/goal.rb', line 64

def records_for_meta_val_pivot(var, data, pivot)
  res = {}
  gmt = self.mg_goal_meta_types.find_by_var( var.to_s )
  gmt_pivot = self.mg_goal_meta_types.find_by_var( pivot.to_s )
  return {} if gmt.nil? || gmt_pivot.nil?
  gmt.meta.find(:all, :select => "`#{gmt.meta.table_name}`.created_at, gm.data AS pivot", :conditions => { :data => data }, :joins => "LEFT JOIN `#{gmt_pivot.meta.table_name}` gm ON gm.mg_goal_meta_type_id = #{gmt_pivot.id} AND gm.mg_record_id = `#{gmt.meta.table_name}`.mg_record_id").each do |c|
    if !res.include?(c.pivot)
      res[c.pivot] = []
    end
    
    res[c.pivot].push c.created_at
  end
  
  res.each { |k,v| v.sort! }
  res
end

#records_for_meta_val_pivot_item(var, data, pivot, item) ⇒ Object

Pivot by given meta data by value (TODO: Document better)



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mountain-goat/models/mg/goal.rb', line 82

def records_for_meta_val_pivot_item(var, data, pivot, item)
  res = {}
  gmt = self.mg_goal_meta_types.find_by_var( var.to_s )
  gmt_pivot = self.mg_goal_meta_types.find_by_var( pivot.to_s )
  gmt_item = self.mg_goal_meta_types.find_by_var( item.to_s )
  return {} if gmt.nil? || gmt_pivot.nil? || gmt_item.nil?
  gmt.meta.find(:all, :select => "`#{gmt.meta.table_name}`.created_at, gm.data AS pivot, gi.data as item", :conditions => { :data => data }, :joins => "LEFT JOIN `#{gmt_pivot.meta.table_name}` gm ON gm.mg_goal_meta_type_id = #{gmt_pivot.id} AND gm.mg_record_id = `#{gmt.meta.table_name}`.mg_record_id LEFT JOIN `#{gmt_item.meta.table_name}` gi ON gi.mg_goal_meta_type_id = #{gmt_item.id} AND gi.mg_record_id = `#{gmt.meta.table_name}`.mg_record_id").each do |c|
    if !res.include?(c.pivot)
      res[c.pivot] = []
    end
    
    if gmt_item.meta_type == 'gi_meta'
      c.item.to_i.times { res[c.pivot].push c.created_at }
    else
      res[c.pivot].push c.created_at if c.item == 'yes' #what else?
    end
  end
  
  res.each { |k,v| v.sort! }
  res.delete_if { |k,v| v.count == 0 }
  res
end

#records_pivot(pivot) ⇒ Object

Get all records pivoted by given meta (e.g. “Youtube” => [ Date, Date, Date ], “Facebook” => [ Date, Date, Date ])



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mountain-goat/models/mg/goal.rb', line 48

def records_pivot(pivot)
  res = {}
  gmt_pivot = self.mg_goal_meta_types.find_by_var( pivot.to_s )
  return {} if gmt_pivot.nil?
  gmt_pivot.meta.map { |c| { :created_at => c.created_at, :pivot => c.data } }.each do |c|
    if !res.include?(c[:pivot])
      res[c[:pivot]] = []
    end
    
    res[c[:pivot]].push c[:created_at]
  end
  
  res.each { |k,v| v.sort! }
  res
end

#reportable_chart_items(pivot) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mountain-goat/models/mg/goal.rb', line 133

def reportable_chart_items(pivot)
  #let's look for pageviews by day by source
  #rallies_for_meta_val( :clique_id, @clique.id )
  #logger.debug "sources: #{sources.inspect}"
  #We now have a map of { source => [ date1, date2, ... ], ... }
  
  #Now, we just need to insert missing data (and group)
  #Let's transpose that into { source => [ :x => day0, y => count ] }
  if pivot.nil?
    return Analytics.pivot_by_date( { :"Records" => self.mg_records.map { |r| r.created_at } }, self.created_at )
  elsif pivot.instance_of?(Mg::GoalMetaType)
    sources = self.records_pivot( pivot.var )
    logger.warn "sources: #{sources}"
    return Analytics.pivot_by_date(sources, self.created_at)
  end
end

#reportable_gerbil_chart(pivot) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/mountain-goat/models/mg/goal.rb', line 151

def reportable_gerbil_chart(pivot)
  #chart = GerbilCharts::Charts::LineChart.new( :width => 350, :height => 200, :style => 'brushmetal.css', :circle_data_points => true )
  
  if pivot.nil?
    data = Analytics.pivot_by_date( { :"Records" => self.mg_records.map { |r| r.created_at } }, self.created_at )
  elsif pivot.instance_of?(Mg::GoalMetaType)
    sources = self.records_pivot( pivot.var )
    data = Analytics.pivot_by_date(sources, self.created_at)
  end
  #logger.warn "data: #{data.inspect}"
  #logger.warn "ts: #{data.map { |line| line[1].map { |d| d[:x] }.to_a }[0].inspect}"
  #logger.warn "val: #{data.map { |line| [ line[0] ] + line[1].map { |d| d[:y] }.to_a }[0].inspect}"
  #chart.modelgroup = GerbilCharts::Models::SimpleTimeSeriesModelGroup.new(
  #    :title => "Rallies",
  #    :timeseries  =>  data.map { |line| line[1].map { |d| d[:x].to_time }.to_a }[0],
  #    :models => data.map { |line| [ line[0].to_s ] + line[1].map { |d| d[:y] }.to_a }
  #)
  #fields = { }
  #fields_simple = []
  #fields.merge!({ d[:x].to_time.to_i => d[:x].to_s } ); fields_simple.push(d[:x])
  graph = SVG::Graph::TimeSeries.new( :height => 350, :width => 700, :show_data_labels => false, :x_label_format => "%m/%d/%y", :graph_title => self.reportable_title(pivot), :show_graph_title => true, :show_data_values => false, :show_data_points => false, :area_fill => true )
  data.each do |line|
    res = []
    line[1].each { |d| res.push(d[:x].strftime('%m/%d/%y')).push(d[:y]) }
    graph.add_data :data => res, :title => line[0].to_s
  end
  
  #logger.warn "Fields: #{fields.inspect}"
  #logger.warn "Fields Simple: #{fields_simple.inspect}"
      
  graph
end

#reportable_title(pivot) ⇒ Object

Title in report charts



124
125
126
127
128
129
130
# File 'lib/mountain-goat/models/mg/goal.rb', line 124

def reportable_title(pivot)
  if pivot.nil?
    return self.name
  elsif pivot.instance_of?(Mg::GoalMetaType)
    return "#{self.name} by #{pivot.name}"
  end
end

#tally_reward_given(reward) ⇒ Object

Tally we have given a reward



111
112
113
114
115
116
117
118
119
# File 'lib/mountain-goat/models/mg/goal.rb', line 111

def tally_reward_given( reward )
  self.transaction do
    self.update_attribute(:rewards_total, 0) if self.rewards_total.nil? #we should merge this with the next line, but whatever
    Mg::Goal.update_counters(self.id, :rewards_given => 1)
    Mg::Goal.update_counters(self.id, :rewards_total => reward)
  end
  
  return self.reload
end