class SnowmanIO::App

Public Class Methods

time(name) { || ... } click to toggle source
# File lib/snowman-io/models/app.rb, line 18
def self.time(name, &block)
  app = App.where(system: true).first
  start = Time.now.to_f
  yield
  app.register_metric_value(name, "time", Time.now.to_f - start, Time.now)
end
value(name, v, at = Time.now) click to toggle source
# File lib/snowman-io/models/app.rb, line 25
def self.value(name, v, at = Time.now)
  app = App.where(system: true).first
  app.register_metric_value(name, "amount", v, at)
end

Public Instance Methods

as_json(options = {}) click to toggle source
Calls superclass method
# File lib/snowman-io/models/app.rb, line 30
def as_json(options = {})
  super(options.merge(methods: [:datapoints, :metric_ids])).tap do |o|
    o["id"] = o.delete("_id").to_s
    o["metric_ids"] = o["metric_ids"].map(&:to_s)
  end
end
daily_metrics(at) click to toggle source

Returns amount of datapoints for `at` and day before

# File lib/snowman-io/models/app.rb, line 38
def daily_metrics(at)
  json = {}

  today = at.beginning_of_day
  yesterday = at.beginning_of_day - 1.day
  json["today"] = {"at" => today.strftime("%Y-%m-%d"), "count" => 0}
  json["yesterday"] = {"at" => yesterday.strftime("%Y-%m-%d"), "count" => 0}
  json["total"] = {"count" => 0}

  # TODO: impl it more performable
  metrics.each do |metric|
    if aggr = metric.aggregations.where(precision: "daily", at: today).first
      json["today"]["count"] += aggr.count.to_i
    end

    if aggr = metric.aggregations.where(precision: "daily", at: yesterday).first
      json["yesterday"]["count"] += aggr.count.to_i
    end

    json["total"]["count"] += metric.aggregations.where(precision: "daily").sum(:count).to_i
  end

  json
end
datapoints() click to toggle source
# File lib/snowman-io/models/app.rb, line 70
def datapoints
  daily_metrics(Time.now)
end
register_metric_value(name, kind, value, at = Time.now) click to toggle source
# File lib/snowman-io/models/app.rb, line 63
def register_metric_value(name, kind, value, at = Time.now)
  metric = metrics.where(name: name, kind: kind).first_or_create!
  metric.update_attributes!(last_value: value, last_value_updated_at: Time.now)
  metric.data_points.create!(at: at, value: value)
  touch
end