Module: FFWD::Reporter

Overview

$LICENSE Copyright 2013-2014 Spotify AB. All rights reserved.

The contents of this file are licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_meta(instance, k) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ffwd/reporter.rb', line 21

def self.build_meta instance, k
  meta = instance.class.reporter_meta || {}

  if instance.respond_to?(:reporter_meta)
    meta = meta.merge(instance.send(:reporter_meta))
  end

  meta = meta.merge(k[:meta])
  meta[:unit] = "#{meta[:unit] || 'n'}/s"
  return meta
end

.included(mod) ⇒ Object



64
65
66
# File 'lib/ffwd/reporter.rb', line 64

def self.included mod
  mod.extend ClassMethods
end

.map_meta(meta) ⇒ Object



17
18
19
# File 'lib/ffwd/reporter.rb', line 17

def self.map_meta meta
  Hash[meta.map{|k, v| [k.to_s, v]}]
end

Instance Method Details

#increment(n, c = 1) ⇒ Object



72
73
74
# File 'lib/ffwd/reporter.rb', line 72

def increment n, c=1
  reporter_data[n] += c
end

#report!(diff) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ffwd/reporter.rb', line 76

def report! diff
  self.class.reporter_keys.each do |key|
    k = key[:key]
    v = reporter_data[k]
    reporter_data[k] = 0

    meta = ((@_reporter_meta ||= {})[k] ||= FFWD::Reporter.build_meta(self, key))
    rate = (v / diff).round(3)

    yield(:key => k, :value => rate, :meta => meta)
  end
end

#reporter_dataObject



68
69
70
# File 'lib/ffwd/reporter.rb', line 68

def reporter_data
  @_reporter_data ||= Hash[self.class.reporter_keys.map{|k| [k[:key], 0]}]
end