Class: InfluxdbSetup::LoadQueries

Inherits:
Command
  • Object
show all
Defined in:
lib/influxdb_setup/load_queries.rb

Defined Under Namespace

Classes: Query

Constant Summary collapse

FileFormatError =
Class.new(StandardError)

Instance Attribute Summary

Attributes inherited from Command

#config

Instance Method Summary collapse

Methods inherited from Command

#initialize, #log

Constructor Details

This class inherits a constructor from InfluxdbSetup::Command

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/influxdb_setup/load_queries.rb', line 18

def call
  queries_file = Pathname.new("db/influxdb_queries.yml")

  if queries_file.exist?
    db = @config.db_name
    root = @config.build_client(db)
    existing_queries = root.list_continuous_queries(db)
    raw = YAML.load(ERB.new(queries_file.read).result) || {}
    raise FileFormatError, "expected influxdb_queries.yml to be a hash, was a #{raw.class.name}" unless raw.is_a?(Hash)

    expected_queries = raw.map do |name, query|
      Query.new(name, query)
    end

    # Delete first in case an old name is getting overwritten
    existing_queries.each do |query|
      if expected_queries.none? {|expected| expected.name == query["name"]}
        log "Removing '#{query['name']}', was: '#{query['query']}'"
        root.delete_continuous_query(query["name"], db)
      end
    end

    expected_queries.each do |expected|
      if existing_queries.any? {|hash| hash["name"] == expected.name}
        log "Skipping '#{expected.raw_name}', a query by that name already exists"
      else
        log "Adding '#{expected.raw_name}': '#{expected.query}'"
        root.create_continuous_query(expected.name, db, expected.query)
      end
    end
  else
    log "No influxdb_queries.yml file found, skipping continuous queries setup"
  end
end