class NodesJson

Attributes

last_update[R]

Public Class Methods

new(data, links, alfred) click to toggle source

@param [Hash] data @param [Links] links

# File bin/ffmapquery, line 257
def initialize(data, links, alfred)
        groups = Hash.new
        begin
                # Gruppen einlesen
                Dir.entries($C[:groups_dir]).each { | group_name |
                        # bestimmte Einträge ignorieren
                        next if group_name[0] == '.'
                        next if group_name == 'README.md'
                        next if group_name == 'README.txt'
                        group_lines = File.readlines($C[:groups_dir] + '/' + group_name)
                        groups[remove_unsafe_chars(group_name)] = DataGroup.new(group_name, group_lines.first.chomp)
                } if $C[:groups_dir]
        rescue
                # ignored
                # there will simply be no groups or no groups after the failing group
        end
        @data = JSON.parse(data)
        last_update = @data['timestamp']
        @last_update = Time.new(last_update[0..3], last_update[5..6], last_update[8..9], last_update[11..12],
                                last_update[14..15], last_update[17..18], 0)
        @last_update.localtime
        puts "Last update: #{@last_update.to_s} (#{(Time.now - @last_update).to_i}s ago)"
        if($C[:interval_forced])
                $next_update = @last_update + $C[:interval]
        else
                $next_update = @last_update + $C[:interval] + 10 + ($C[:interval] * Random.rand(0.3))
        end
        $next_update.localtime
        @nodes = Hash.new
        $known_nodes = Array.new
        $node_map = Hash.new
        @data['nodes'].each_pair { | node_id, node |
                begin
                        $node_map[node_id] = remove_unsafe_chars(node['nodeinfo']['hostname'])
                rescue
                        # ignored
                end
        }
        post_body = Array.new
        @data['nodes'].each_pair { | node_id, node |
                dn = DataNode.new(node, links, alfred, @last_update)
                @nodes[node_id] = dn
                groups.each_value { | group |
                        # Just try to add to every group. The group will reject a node if
                        # the filter does not match.
                        group.add_member(dn)
                }
                post_body << dn.get_influx_lines
        }
        $stdout.puts(post_body.join("\n")) if $C[:debug]
        if (!$C[:dry_run])
                # Start the HTTP request...
                uri = URI.parse($C[:influx_url])
                http = Net::HTTP.new(uri.host, uri.port)
                post = Net::HTTP::Post.new(uri.request_uri)
                post.body = post_body.join("\n")
                http.start
                response = http.request(post)
                http.finish
                $stdout.puts("Sent #{post.body.length} bytes in #{post_body.length} lines, response #{response.code} #{response.msg}")
        end
end