Class: Heart::Metric

Inherits:
Application show all
Defined in:
app/models/heart/metric.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.aggregate_daily_moving_averages(days = 30, where = "WHERE fulldate > SUBDATE(NOW(), 90)", groupby = nil, measurements = true, sum_avg = "AVG") ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/heart/metric.rb', line 82

def self.aggregate_daily_moving_averages(days=30, where="WHERE fulldate > SUBDATE(NOW(), 90)", groupby=nil, measurements=true, sum_avg="AVG")
  if groupby.nil? 
    #if groupby is nil, then we want the data for each date - not aggregated. 
    #Group on fulldate to get non-aggregated results for each date (so the aggregating functions like AVG() don't run across all dates.)
    groupby = "GROUP BY fulldate"
  else
    groupby = "GROUP BY #{groupby}"
  end
  measurements_with_avg = Array.new
  case measurements
  when false
  when Array
    measurements.each do |name|
      measurements_with_avg.push(" #{sum_avg}(#{name}) AS #{name}")
    end
  else
    metric = Metric.new
    metric.attributes.each do |name, value|
      next if Metric..include?(name)
      next if Metric.groupbydata.include?(name)
      measurements_with_avg.push(" #{sum_avg}(#{name}) AS #{name}")
    end
  end
  m_with_a = (measurements_with_avg.empty?) ? "" : "," + measurements_with_avg.join(",")
  self.find_by_sql("
    SELECT fulldate, fulldate AS d, year, monthofyear AS month, dayofyear AS day, '#{days}' AS movingaverage #{m_with_a} 
    FROM heart_metrics 
    #{where} AND movingaverage = #{days} #{groupby} 
    ORDER BY d ASC"
    )
end

.aggregate_daily_sums(days = nil, where = nil, groupby = nil, measurements = true) ⇒ Object



114
115
116
# File 'app/models/heart/metric.rb', line 114

def self.aggregate_daily_sums(days=nil, where=nil, groupby=nil, measurements=true)
  self.aggregate_daily_moving_averages(days, where, groupby, measurements, "SUM")
end

.find_or_create(date, average) ⇒ Object



53
54
55
56
# File 'app/models/heart/metric.rb', line 53

def self.find_or_create(date,average)
  Heart::Metric.where(fulldate: date).where(movingaverage: average).first ||
  Heart::Metric.create(fulldate: date, movingaverage: average)
end

.groupbydataObject

returns an array of possible GROUP BY fields



45
46
47
# File 'app/models/heart/metric.rb', line 45

def self.groupbydata
  ["fulldate","dayofyear","dayofweek","weekofyear","monthofyear","year"]
end

.metadataObject

returns an array of properties that contain meta data



40
41
42
# File 'app/models/heart/metric.rb', line 40

def self.
  ["id","fulldate","d","year","monthofyear","month","dayofyear","day","dayofweek","weekofyear","completed_at","movingaverage","created_at","updated_at","deleted_at"]
end

.recently_added(average = 0) ⇒ Object



49
50
51
# File 'app/models/heart/metric.rb', line 49

def self.recently_added(average=0)
  Heart::Metric.where("movingaverage = ?", average).order("fulldate DESC").limit(3)
end

.subselects_with_day(days = 30) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'app/models/heart/metric.rb', line 130

def self.subselects_with_day(days=30)
  subqueries = []
  metric = Metric.new
  metric.attributes.each do |name, value|
    next if Metric..include?(name)
    next if Metric.groupbydata.include?(name)
    subqueries.push("(SELECT AVG(#{name}) AS #{name} FROM heart_metrics WHERE movingaverage = 0 AND fulldate BETWEEN SUBDATE(d,#{days}) AND d) AS #{name}")
  end
  subqueries
end

Instance Method Details

#dayofmonthObject



23
24
25
# File 'app/models/heart/metric.rb', line 23

def dayofmonth
  self.fulldate.to_s.slice(8,10)
end

#fetch_allObject



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/models/heart/metric.rb', line 58

def fetch_all
  isometric = Heart::Isometric.find_or_create(self.fulldate,0)
  self.methods.each do |x|
    begin
      if /^fetch_/.match(x.to_s)
        dont_fetch = ["fetch_all"]
        next if dont_fetch.include?(x.to_s)
        puts "#{fulldate} #{x}"
        self.send(x)
        attribute = x.to_s.split(/_/)
        isometric.send(attribute[1]+"=", Time.now)
        self.save
        isometric.save
      end
    rescue RuntimeError => e
      puts e.message
      puts e.backtrace.inspect
    rescue NoMethodError => e
      puts e.message
      puts e.backtrace.inspect
    end
  end
end

#moving_averages!(days) ⇒ Object

TODO remove the following methods This method is used in the /metrics/:/:/cache route



120
121
122
123
124
125
126
127
128
# File 'app/models/heart/metric.rb', line 120

def moving_averages!(days)
  subselects = Metric.subselects_with_day(days).join(",")
  metric_with_calculated_averages = Metric.find_by_sql("SELECT fulldate, fulldate AS d, year, monthofyear AS month, dayofyear AS day, '#{days}' AS movingaverage, #{subselects} FROM heart_metrics WHERE fulldate = '#{fulldate}' AND movingaverage = 0")
  metric_with_calculated_averages.first.attributes.each do |name, value|
    next if Metric..include?(name)
    next if Metric.groupbydata.include?(name)
      self.send(name+"=",value)
  end
end

#validate_dayofweekObject



15
16
17
# File 'app/models/heart/metric.rb', line 15

def validate_dayofweek
  self.dayofweek = self.fulldate.wday
end

#validate_dayofyearObject



19
20
21
# File 'app/models/heart/metric.rb', line 19

def validate_dayofyear
  self.dayofyear = self.fulldate.yday
end

#validate_monthofyearObject



31
32
33
# File 'app/models/heart/metric.rb', line 31

def validate_monthofyear
  self.monthofyear = self.fulldate.mon
end

#validate_weekofyearObject



27
28
29
# File 'app/models/heart/metric.rb', line 27

def validate_weekofyear
  self.weekofyear = self.fulldate.cweek
end

#validate_yearObject



35
36
37
# File 'app/models/heart/metric.rb', line 35

def validate_year
  self.year = self.fulldate.year
end