Class: Scoped

Inherits:
Object
  • Object
show all
Includes:
MonitorUtilities
Defined in:
lib/rearview/templates/monitor.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MonitorUtilities

#collect_aberrations, #collect_comparisons, #deploy_check, #outside_limits?, #percentage_errors

Constructor Details

#initialize(timeout) ⇒ Scoped

Returns a new instance of Scoped.



59
60
61
62
63
# File 'lib/rearview/templates/monitor.rb', line 59

def initialize(timeout)
  @graph_data         = {}
  @graph_data.default = [] # make the default value an empty array
  @timeout            = timeout
end

Class Method Details

.to_sObject

Bolt on a to_s to @timeseries to pretty print the instances



92
93
94
# File 'lib/rearview/templates/monitor.rb', line 92

def @timeseries.to_s
  "[ #{self.join ", "} ]"
end

Instance Method Details

#fold_metrics(initial, &block) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rearview/templates/monitor.rb', line 69

def fold_metrics(initial, &block)
  iterations = @timeseries[0].values.length
  iterations.times.inject(initial) do |accum, i|
    entries = @timeseries.map { |series| series.entries[i] }
    block.call accum, *entries
  end
end

#graph_value(name, timestamp, value) ⇒ Object



65
66
67
# File 'lib/rearview/templates/monitor.rb', line 65

def graph_value(name, timestamp, value)
  @graph_data[name] += [[timestamp, value]]
end

#scoped_eval(namespace) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rearview/templates/monitor.rb', line 85

def scoped_eval(namespace)
  # Copy instance variables from top-level class into current scope
  namespace.keys.each do |v|
    instance_variable_set "#{v}".to_sym, namespace[v]
  end

  # Bolt on a to_s to @timeseries to pretty print the instances
  def @timeseries.to_s
    "[ #{self.join ", "} ]"
  end

  out = StringIO.new

  t = Thread.start do
    $stdout = out
    $SAFE=3
    begin
      graph_value = lambda { |name, timestamp, value| graph_value(name, timestamp, value) }
      %{expression}
    rescue RuntimeError => e
      out.write e
      @error = e.to_s
    end
  end

  t.abort_on_exception = false

  begin
    t.join
  rescue => e
    out.write e
    @error = e.to_s
  ensure
    $SAFE = 0
    $stdout = STDOUT
    $stderr = STDERR
  end

  { :graph_data => @graph_data, :output => out.string, :error => @error}.to_json
end

#with_metrics(&block) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/rearview/templates/monitor.rb', line 77

def with_metrics(&block)
  iterations = @timeseries[0].values.length
  iterations.times.each do |i|
    entries = @timeseries.map { |series| series.entries[i] }
    block.call *entries
  end
end