samples/consul-ui/timeline.json.erb in consul-templaterb-1.8.8 vs samples/consul-ui/timeline.json.erb in consul-templaterb-1.9.0

- old
+ new

@@ -2,20 +2,25 @@ require 'json' require 'set' services_blacklist = (ENV['EXCLUDE_SERVICES'] || 'consul-agent-http,mesos-slave,mesos-agent-watcher,mesos-exporter-slave').split(',') @current_time = Time.now.utc.iso8601 +@last_service_info = {} +@newest_index = 0 unless @newest_index cur_state = services.map do |service_name, _tags| next if services_blacklist.include?(service_name) snodes = service(service_name) cur_stats = { 'passing' => 0, 'warning' => 0, 'critical' => 0, 'total' => snodes.count } + cur_index = snodes.endpoint.x_consul_index.to_i + @newest_index = cur_index if @newest_index < cur_index + @last_service_info[service_name] = { 'idx' => cur_index, 'stats' => cur_stats } snodes.each do |snode| case snode.status.downcase when 'passing' cur_stats['passing'] += 1 when 'warning' @@ -30,13 +35,13 @@ ["#{instance['Node']['Node']}:#{instance['Service']['ID']}", { 'address' => instance.service_address, 'node' => instance['Node']['Node'], 'port' => instance['Service']['Port'], - 'idx' => instance['Service']['ModifyIndex'], + 'idx' => (instance['Service']['ModifyIndex'] || cur_index).to_i, 'status' => instance.status, - 'stats' => cur_stats, + 'stats' => cur_stats, 'checks' => instance['Checks'].map { |check| [check['CheckID'], { 'name' => check['Name'], 'status' => check['Status'], 'output' => check['Output'] }] }.to_h }] end.to_h [service_name, instances] end @@ -52,30 +57,31 @@ class RingBuffer < Array attr_reader :max_size def initialize(max_size:, enum: nil) @max_size = max_size + warn "Ringbuffer initialized with #{max_size}" enum&.each { |e| self << e } end def <<(element) - if size < @max_size || @max_size.nil? - super - else + return unless element + if size >= @max_size shift + end + previous_e = last + after = [] + while !previous_e.nil? && (previous_e['idx'] > element['idx']) + after.insert(0, pop) previous_e = last - after = [] - while !previous_e.nil? && previous_e['idx'] > element['idx'] - after << pop - previous_e = last - end - push(element) - push after end + push(element) + after.each do |x| + push(x) + end + self end - - alias push << end def diff(old, new_e) diff = OpenStruct.new diff.appeared = new_e - old @@ -90,20 +96,16 @@ def log_event(line) puts "#{Time.now.to_i} #{line}" if ENV['DEBUG_TIMELINE'] end -def store_event(service: service_name, instance: nil, old_state: nil, new_state: nil, instance_info: nil, checks: []) - STDERR.puts "empty instance_info for #{service} ; #{instance} ; #{new_state}" unless instance_info - @new_events << { 'service' => service, 'instance' => instance, 'idx' => instance_info['idx'], 'old_state' => old_state, 'new_state' => new_state, +def store_event(service: nil, instance: nil, old_state: nil, new_state: nil, instance_info: nil, checks: []) + @new_events << { 'service' => service, 'instance' => instance, 'old_state' => old_state, 'new_state' => new_state, 'ts' => @current_time, 'instance_info' => instance_info }.tap do |ev| ev['checks'] = checks if checks - ev['stats'] = if instance_info - instance_info['stats'] - else - {} - end + ev['stats'] = instance_info['stats'] + ev['idx'] = instance_info['idx'] end end def compute_checks(old_state, cur_state, service_name, instance_name) old_checks = old_state.dig(service_name, instance_name, 'checks') || {} @@ -131,15 +133,23 @@ service_diff = diff(old_state.keys, cur_state.keys) service_diff.disappeared.each do |service_name| old_state[service_name].each do |instance_name, instance_info| checks = compute_checks(old_state, cur_state, service_name, instance_name) + the_info = {}.merge(instance_info) + the_info['idx'] = @newest_index + the_info['stats'] = { + 'passing' => 0, + 'warning' => 0, + 'critical' => 0, + 'total' => 0, + } store_event(service: service_name, instance: instance_name, old_state: old_state[service_name][instance_name]['status'], new_state: nil, - instance_info: instance_info, + instance_info: the_info, checks: checks) end end def instances_are_equal(o_state, n_state) @@ -154,14 +164,17 @@ (service_diff.stayed + service_diff.appeared).each do |service_name| instance_diff = diff((old_state[service_name] || {}).keys, cur_state[service_name].keys) instance_diff.disappeared.each do |instance_name| checks = compute_checks(old_state, cur_state, service_name, instance_name) + the_info = {}.merge(old_state[service_name][instance_name]) + the_info['idx'] = @last_service_info[service_name]['idx'] + the_info['stats'] = @last_service_info[service_name]['stats'] store_event(service: service_name, old_state: old_state[service_name][instance_name]['status'], new_state: nil, instance: instance_name, - instance_info: old_state[service_name][instance_name], + instance_info: the_info, checks: checks) end instance_diff.appeared.each do |instance_name| checks = compute_checks(old_state, cur_state, service_name, instance_name) store_event(service: service_name, \ No newline at end of file