Module: TelemetryDaemon

Defined in:
lib/telemetry/daemon_config_parser.rb

Constant Summary collapse

@@flows_expire_in =
nil
@@begin_interval =

Code blocks users can call at the beginning and end of each interval

nil
@@end_interval =
nil

Instance Method Summary collapse

Instance Method Details

#api_token(token) ⇒ Object



5
6
7
# File 'lib/telemetry/daemon_config_parser.rb', line 5

def api_token(token)
  Telemetry.token = token
end

#barchart(tag, frequency = 0, offset = nil, &block) ⇒ Object



62
63
64
65
# File 'lib/telemetry/daemon_config_parser.rb', line 62

def barchart(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :barchart, tag, frequency, offset, block ]
end

#begin_interval(&block) ⇒ Object



46
47
48
# File 'lib/telemetry/daemon_config_parser.rb', line 46

def begin_interval(&block)
  @@begin_interval = block
end

#bulletchart(tag, frequency = 0, offset = nil, &block) ⇒ Object



67
68
69
70
# File 'lib/telemetry/daemon_config_parser.rb', line 67

def bulletchart(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :value, tag, frequency, offset, block ]
end

#countdown(tag, frequency = 0, offset = nil, &block) ⇒ Object



72
73
74
75
# File 'lib/telemetry/daemon_config_parser.rb', line 72

def countdown(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :countdown, tag, frequency, offset, block ]
end

#end_interval(&block) ⇒ Object



54
55
56
# File 'lib/telemetry/daemon_config_parser.rb', line 54

def end_interval(&block)
  @@end_interval = block
end

#flows_expire_in(i) ⇒ Object



22
23
24
# File 'lib/telemetry/daemon_config_parser.rb', line 22

def flows_expire_in(i)
  @@flows_expire_in = i
end

#gauge(tag, frequency = 0, offset = nil, &block) ⇒ Object



77
78
79
80
# File 'lib/telemetry/daemon_config_parser.rb', line 77

def gauge(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :gauge, tag, frequency, offset, block ]
end

#graph(tag, frequency = 0, offset = nil, &block) ⇒ Object



82
83
84
85
# File 'lib/telemetry/daemon_config_parser.rb', line 82

def graph(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :graph, tag, frequency, offset, block ]
end

#icon(tag, frequency = 0, offset = nil, &block) ⇒ Object



87
88
89
90
# File 'lib/telemetry/daemon_config_parser.rb', line 87

def icon(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :icon, tag, frequency, offset, block ]
end

#iframe(tag, frequency = 0, offset = nil, &block) ⇒ Object



92
93
94
95
# File 'lib/telemetry/daemon_config_parser.rb', line 92

def iframe(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :iframe, tag, frequency, offset, block ]
end

#interval(interval) ⇒ Object



17
18
19
# File 'lib/telemetry/daemon_config_parser.rb', line 17

def interval(interval)
  @@interval = interval
end

#log(tag, frequency = 0, offset = nil, &block) ⇒ Object



97
98
99
100
# File 'lib/telemetry/daemon_config_parser.rb', line 97

def log(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :log, tag, frequency, offset, block ]
end

#log_level(log_level) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/telemetry/daemon_config_parser.rb', line 9

def log_level(log_level)
  level = log_level.to_s
  if log_level == 'debug'
    Telemetry.logger.level = Logger::DEBUG
    Telemetry.logger.debug "Starting Debug Logging" 
  end
end

#map(tag, frequency = 0, offset = nil, &block) ⇒ Object



102
103
104
105
# File 'lib/telemetry/daemon_config_parser.rb', line 102

def map(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :map, tag, frequency, offset, block ]
end

#multigauge(tag, frequency = 0, offset = nil, &block) ⇒ Object



107
108
109
110
# File 'lib/telemetry/daemon_config_parser.rb', line 107

def multigauge(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :multigauge, tag, frequency, offset, block ]
end

#multivalue(tag, frequency = 0, offset = nil, &block) ⇒ Object



112
113
114
115
# File 'lib/telemetry/daemon_config_parser.rb', line 112

def multivalue(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :multivalue, tag, frequency, offset, block ]
end

#run_begin_intervalObject



50
51
52
# File 'lib/telemetry/daemon_config_parser.rb', line 50

def run_begin_interval
  @@begin_interval.yield if @@begin_interval
end

#run_end_intervalObject



58
59
60
# File 'lib/telemetry/daemon_config_parser.rb', line 58

def run_end_interval
  @@end_interval.yield if @@end_interval
end

#run_scheduled_flow_updatesObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/telemetry/daemon_config_parser.rb', line 167

def run_scheduled_flow_updates
  @@buffer = {}
  @@tasks ||= {}
  @@last_values ||= {}
  @@values_expires_at ||= {}
  @@next_run_at ||= {}

  @@tasks.each do |task| 
    @@h = {}
    variant, tag, frequency, offset, block = task
    now = Time.now


    # Check whether we should wait an interval before running
    if frequency > 0 
      Telemetry::logger.debug "Task #{task[0]} #{task[1]} (every #{task[2]}s)"
      #Telemetry::logger.debug "Update frequency is #{frequency} now #{Time.now.to_i} next #{@@next_run_at[tag]}"
      if @@next_run_at[tag] && @@next_run_at[tag] > now.to_i
        Telemetry::logger.debug "  - Not scheduled yet (waiting #{-(now.to_i - @@next_run_at[tag])}s)"
        next
      end
      @@next_run_at[tag] = now.to_i + frequency

      Telemetry::logger.debug "  - Running intermittant task at #{now}"

      # If an offset is defined then align runtimes to the offset
      # How close you can get to the desired offset depends on the global interval. So set it relatively small
      # when using this feature
      if offset and offset >= 0 and offset <= 86400
        this_morning = Time.new(now.year, now.month, now.day).to_i
        time_since_offset = now.to_i - this_morning - offset
        time_since_offset += 86400 if time_since_offset < 0

        @@next_run_at[tag] -= time_since_offset % frequency
        #Telemetry::logger.debug "#{now.to_i} #{@@next_run_at[tag]}"
      end
    else
      Telemetry::logger.debug "  - Task #{task[0]} #{task[1]}"
    end

    # Execute the flow
    Telemetry.logger.debug "  + Executing task #{task[0]} #{task[1]}"
    block.yield

    if @@h == {}
      Telemetry.logger.debug "  - Skipping empty task values #{task[0]} #{task[1]}"
      next
    end

     # Use global default to set expires_at field if necessary
    if @@flows_expire_in and not @@h[:expires_at]
       set expires_at: Time.now.to_i + @@flows_expire_in
     end

    # Append the variant
    values = @@h.merge({variant: variant})

    # Telemetry.logger.debug "  - Values for #{task[0]} #{task[1]}:\n#{values}\n#{@@last_values[tag]}"

    # Telemetry.logger.debug "LV\n\n #{@@last_values}\n\n"

    # Skip if the values haven't changed (though send 1/day regardless)
    if @@last_values[tag] != values || @@values_expires_at[tag] < now.to_i
      @@buffer[tag] = values
      @@last_values[tag] = values.clone # Save the value so we dont update unless it changes
      @@values_expires_at[tag] = now.to_i + 86400  # Force an update 1/day
    else
      Telemetry.logger.debug "  - Skipping update for unchanged #{task[0]} #{task[1]}:\n#{@@last_values[tag]}"
    end
  end

  @@buffer
end

#servers(tag, frequency = 0, offset = nil, &block) ⇒ Object



117
118
119
120
# File 'lib/telemetry/daemon_config_parser.rb', line 117

def servers(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :servers, tag, frequency, offset, block ]
end

#set(x) ⇒ Object



38
39
40
# File 'lib/telemetry/daemon_config_parser.rb', line 38

def set(x)
  x.each {|k,v| @@h[k.to_sym] = v}
end

#set_json(x) ⇒ Object



34
35
36
# File 'lib/telemetry/daemon_config_parser.rb', line 34

def set_json(x)
  x.each {|k,v| @@h[k.to_sym] = MultiJson.load(v)}
end

#status(tag, frequency = 0, offset = nil, &block) ⇒ Object



122
123
124
125
# File 'lib/telemetry/daemon_config_parser.rb', line 122

def status(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :servers, tag, frequency, offset, block ]
end

#table(tag, frequency = 0, offset = nil, &block) ⇒ Object



127
128
129
130
# File 'lib/telemetry/daemon_config_parser.rb', line 127

def table(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :table, tag, frequency, offset, block ]
end

#text(tag, frequency = 0, offset = nil, &block) ⇒ Object



132
133
134
135
# File 'lib/telemetry/daemon_config_parser.rb', line 132

def text(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :text, tag, frequency, offset, block ]
end

#tickertape(tag, frequency = 0, offset = nil, &block) ⇒ Object



137
138
139
140
# File 'lib/telemetry/daemon_config_parser.rb', line 137

def tickertape(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :tickertape, tag, frequency, offset, block ]
end

#timechart(tag, frequency = 0, offset = nil, &block) ⇒ Object



142
143
144
145
# File 'lib/telemetry/daemon_config_parser.rb', line 142

def timechart(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :timechart, tag, frequency, offset, block ]
end

#timeline(tag, frequency = 0, offset = nil, &block) ⇒ Object



147
148
149
150
# File 'lib/telemetry/daemon_config_parser.rb', line 147

def timeline(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :timeline, tag, frequency, offset, block ]
end

#timeseries(tag, frequency = 0, offset = nil, &block) ⇒ Object



152
153
154
155
# File 'lib/telemetry/daemon_config_parser.rb', line 152

def timeseries(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :timeseries, tag, frequency, offset, block ]
end

#upstatus(tag, frequency = 0, offset = nil, &block) ⇒ Object



157
158
159
160
# File 'lib/telemetry/daemon_config_parser.rb', line 157

def upstatus(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :upstatus, tag, frequency, offset, block ]
end

#value(tag, frequency = 0, offset = nil, &block) ⇒ Object



162
163
164
165
# File 'lib/telemetry/daemon_config_parser.rb', line 162

def value(tag, frequency = 0, offset=nil, &block)
  @@tasks ||= []
  @@tasks << [ :value, tag, frequency, offset, block ]
end

#wait_for_interval_from(timestamp) ⇒ Object

Ensure a minimum of 1 second between loops



27
28
29
30
31
32
# File 'lib/telemetry/daemon_config_parser.rb', line 27

def wait_for_interval_from(timestamp)
  @@interval ||= 60
  @@interval = 1.0 if @@interval < 1
  sleep_length = timestamp + @@interval.to_f - Time.now.to_f
  sleep sleep_length if sleep_length > 0
end