Module: SnowmanIO::Aggregate

Defined in:
lib/snowman-io/aggregate.rb

Class Method Summary collapse

Class Method Details

.metrics_aggregate_5minObject

Aggregate 5mins metrics



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/snowman-io/aggregate.rb', line 4

def self.metrics_aggregate_5min
  Metric.all.each do |metric|
    aggr = {}

    # collect
    metric.data_points.each do |point|
      key = Utils.floor_5min(point.at)
      aggr[key] ||= []
      aggr[key] << point.value
    end

    # aggregrate
    aggr.each do |at, values|
      metric.aggregations.where(precision: "5min", at: at).first_or_create!.update_attributes!(
        count: values.length,
        min: values.min,
        avg: Utils.avg(values),
        up: Utils.up(values),
        max: values.max,
        sum: values.inject(&:+)
      )
    end
  end
end

.metrics_aggregate_dailyObject

Aggregate daily metrics



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/snowman-io/aggregate.rb', line 58

def self.metrics_aggregate_daily
  Metric.all.each do |metric|
    out = {}
    metric.aggregations.where(precision: "hour").each do |aggr|
      key = aggr.at.beginning_of_day
      out[key] ||= {"count" => 0, "avg" => 0, "up" => 0, "sum" => 0}
      out[key]["count"] += aggr["count"]
      out[key]["min"] = aggr["min"] if !out[key]["min"] || out[key]["min"] > aggr["min"]
      out[key]["avg"] += aggr["avg"]*aggr["count"]
      out[key]["up"] += aggr["up"]*aggr["count"]
      out[key]["max"] = aggr["max"] if !out[key]["max"] || out[key]["max"] < aggr["max"]
      out[key]["sum"] += aggr["sum"]
    end

    # normalize
    out.each do |__, chunk|
      chunk["avg"] /= chunk["count"]
      chunk["up"] /= chunk["count"]
    end

    # store
    out.each do |at, chunk|
      metric.aggregations.where(precision: "daily", at: at).first_or_create!.update_attributes!(chunk)
    end
  end

  # for meteor
  App.all.each(&:touch)
end

.metrics_aggregate_hourObject

Aggregate hour metrics



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/snowman-io/aggregate.rb', line 30

def self.metrics_aggregate_hour
  Metric.all.each do |metric|
    out = {}
    metric.aggregations.where(precision: "5min").each do |aggr|
      key = aggr.at.beginning_of_hour
      out[key] ||= {"count" => 0, "avg" => 0, "up" => 0, "sum" => 0}
      out[key]["count"] += aggr["count"]
      out[key]["min"] = aggr["min"] if !out[key]["min"] || out[key]["min"] > aggr["min"]
      out[key]["avg"] += aggr["avg"]*aggr["count"]
      out[key]["up"] += aggr["up"]*aggr["count"]
      out[key]["max"] = aggr["max"] if !out[key]["max"] || out[key]["max"] < aggr["max"]
      out[key]["sum"] += aggr["sum"]
    end

    # normalize
    out.each do |__, chunk|
      chunk["avg"] /= chunk["count"]
      chunk["up"] /= chunk["count"]
    end

    # store
    out.each do |at, chunk|
      metric.aggregations.where(precision: "hour", at: at).first_or_create!.update_attributes!(chunk)
    end
  end
end

.metrics_clean_oldObject

Clean old records



89
90
91
92
93
94
95
# File 'lib/snowman-io/aggregate.rb', line 89

def self.metrics_clean_old
  now = Time.now
  DataPoint.where(:at.lt => Utils.floor_5min(now - 6.minutes)).delete_all
  SnowmanIO::Aggregation.where(precision: "5min", :at.lt => (now - 2.hours).beginning_of_hour).delete_all
  SnowmanIO::Aggregation.where(precision: "hour", :at.lt => (now - 25.hours).beginning_of_day).delete_all
  SnowmanIO::Aggregation.where(precision: "daily", :at.lt => (now - 365.days).beginning_of_day).delete_all
end