Class: Analytics

Inherits:
Object
  • Object
show all
Defined in:
lib/mountain-goat/analytics.rb

Class Method Summary collapse

Class Method Details

.convert_date_to_javascript_time(item, dimension) ⇒ Object



54
55
56
57
58
59
# File 'lib/mountain-goat/analytics.rb', line 54

def self.convert_date_to_javascript_time(item, dimension)
  item.each do |i|
    date = i.send(dimension)
    i[dimension] = Time.utc(date.year, date.month, date.day).to_i * 1000
  end
end

.hash_color(str) ⇒ Object



97
98
99
100
# File 'lib/mountain-goat/analytics.rb', line 97

def self.hash_color(str)
   s = ( ( 7.times.map { str }.join.hash ** 2 ) % 2**24 ).to_s(16)
   (6 - s.length).times.map { "0" }.join << s
end

.insert_missing_dates_as_zero_through_today(item, dimension, start, valuemap) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mountain-goat/analytics.rb', line 21

def self.insert_missing_dates_as_zero_through_today(item, dimension, start, valuemap)
  #assume sorted object-- and that dates are on day-intervals
  
  result = []
  running_date = start #item[0].send(dimension)
  i = 0

  while running_date < ( Date.today + 1 )
    if i >= item.count
      break if running_date >= Date.today #skip missing data for today
      result.push( { dimension => running_date }.merge(valuemap) )
    else
      if running_date < item[i].send(dimension)
        result.push( { dimension => running_date }.merge(valuemap) )
      elsif running_date == item[i].send(dimension)
        map = { dimension => running_date }
        valuemap.each do |dim, val|
          map[dim] = item[i].send(dim)
        end
        result.push(map)
        i += 1
      else
        raise "Dates dont align on time"
        #running_date > item[i], we've run over the end of the list
      end
    end
    
    running_date = running_date + 1
  end
  
  result
end

.options_for_line_chart(h) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/mountain-goat/analytics.rb', line 86

def self.options_for_line_chart(h)
  res = {}
  h.each do |k, v|
    title = k || "Direct"
    res.merge!( title => { :collection => v, :x => :x, :y => :y, :options => { :color => "##{hash_color( title )}" }} )
  end

  res
end

.options_for_pie_chart(h) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/mountain-goat/analytics.rb', line 78

def self.options_for_pie_chart(h)
  res = {}
  h.each do |k, v|
    res.merge!( k => { :collection => [{ :x => 1, :val => v.to_i }], :x => :x, :y => :val, :options => { :color => "##{hash_color( k )}" } })
  end
  res
end

.options_for_titled_chart(h, t) ⇒ Object



74
75
76
# File 'lib/mountain-goat/analytics.rb', line 74

def self.options_for_titled_chart(h, t)
  { :collection => h, :x => :name, :y => :val }
end

.pivot(item, dimension) ⇒ Object

filters do

contains(:page_path, '/xxx')

end



11
12
13
14
15
16
17
18
19
# File 'lib/mountain-goat/analytics.rb', line 11

def self.pivot(item, dimension)
  item.sort! do |x,y|
  #logger.warn "x: #{x.send(:date)}, y: #{y.send(:date)}"
    x.send(dimension) <=> y.send(dimension)
  end
  
  item
  #item.map { |res| { dimension => res.send(dimension), value => res.send(value) } }
end

.pivot_by_date(collection, start_date, end_date = Time.zone.now) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mountain-goat/analytics.rb', line 102

def self.pivot_by_date(collection, start_date, end_date = Time.zone.now)
  #assume sorted object-- and that dates are on day-intervals
  
  result = {}
  start_date -= 60 * 60 * 24 #no day 0??
  end_date += 60 * 60 * 30 #let's just push this forward a bit..
  
  running_date = Date.new(start_date.year, start_date.month, start_date.day) #item[0].send(dimension)
  end_date = Date.new(end_date.year, end_date.month, end_date.day) #item[0].send(dimension)
  indices = {}
  collection.each { |source,dates| indices.merge!({ source => 0 }); result[source] = [] }
  #puts indices.inspect
  while running_date < end_date
    collection.each do |source, value|
      #Get the index in the current channel
      i = indices[source]
      
      #Otherwise, let's count the dates
      v = 0
      v += 1 and i += 1 while i < value.length && value[i] > running_date && value[i] < (running_date + 1)
      
      #Store results back
      indices[source] = i
      result[source].push( { :x => running_date, :y => v } )
    end
    
    running_date = running_date + 1
  end

  #result.each { |s| s.sort }
  result
end

.pivot_hash_to_titled(map) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mountain-goat/analytics.rb', line 61

def self.pivot_hash_to_titled(map)
  res = []
  res_titles = {}
  i = 0
  
  map.each do |k,v|
    res.push( { :name => i, :val => v.to_i } )
    res_titles.merge!({ i => k })
  end
  
  [res, res_titles]
end