Sha256: f3353f979f751e59621b33c553299964e9ed58d5f42a99d41f032d5e5e90b213

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

module SystemMetrics
  class Metric < ActiveRecord::Base

    set_table_name 'system_metrics'
    has_many :children, :class_name => self.name, :foreign_key => :parent_id
    belongs_to :parent, :class_name => self.name
    serialize :payload

    def ancestors
      ancestors = []
      metric = self
      while parent = metric.parent
        ancestors << parent
        metric = parent
      end
      ancestors
    end

    # Returns if the current node is the parent of the given node.
    # If this is a new record, we can use started_at values to detect parenting.
    # However, if it was already saved, we lose microseconds information from
    # timestamps and we must rely solely in id and parent_id information.
    def parent_of?(metric)
      if new_record?
        start = (started_at - metric.started_at) * 1000.0
        start <= 0 && (start + duration >= metric.duration)
      else
        self.id == metric.parent_id
      end
    end

    def child_of?(metric)
      metric.parent_of?(self)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
system-metrics-0.2.4 app/models/system_metrics/metric.rb
system-metrics-0.2.3 app/models/system_metrics/metric.rb
system-metrics-0.2.2 app/models/system_metrics/metric.rb
system-metrics-0.2.1 app/models/system_metrics/metric.rb
system-metrics-0.2.0 app/models/system_metrics/metric.rb
system-metrics-0.1.0 app/models/system_metrics/metric.rb