Sha256: c1b8961db34c485996083ac3ca2b64dc651793d37ff4a8f8391cdf2618e721ba

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

require 'active_support/notifications'

module SystemMetrics
  class NestedEvent < ActiveSupport::Notifications::Event
    attr_reader :action, :category

    def self.arrange(events, options={})
      events.sort! { |a, b| a.end <=> b.end } unless options[:presort] == false

      while event = events.shift
        if parent = events.find { |n| n.parent_of?(event) }
          parent.children << event
        elsif events.empty?
          root = event
        end
      end

      root
    end

    def initialize(*args)
      super
      @action, @category = name.split('.')
    end

    def exclusive_duration
      @exclusive_duration ||= duration - children.inject(0.0) { |sum, child| sum + child.duration }
    end

    def started_at
      self.time
    end

    def ended_at
      self.end
    end

    def children
      @children ||= []
    end

    def parent_of?(event)
      start = (started_at - event.started_at) * 1000.0
      start <= 0 && (start + duration >= event.duration)
    end

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

    def to_hash
      {
        :name => name,
        :action => action,
        :category => category,
        :started_at => started_at,
        :transaction_id => transaction_id,
        :payload => payload,
        :duration => duration,
        :exclusive_duration => exclusive_duration
      }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
system-metrics-0.2.5 lib/system_metrics/nested_event.rb
system-metrics-0.2.4 lib/system_metrics/nested_event.rb
system-metrics-0.2.3 lib/system_metrics/nested_event.rb
system-metrics-0.2.2 lib/system_metrics/nested_event.rb
system-metrics-0.2.1 lib/system_metrics/nested_event.rb
system-metrics-0.2.0 lib/system_metrics/nested_event.rb