Class: DTK::State::Component::Attribute::Influxdb::SemanticType

Inherits:
Object
  • Object
show all
Defined in:
lib/state/component/providers/influxdb/semantictype.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, namespace) ⇒ SemanticType

Returns a new instance of SemanticType.



9
10
11
12
13
14
15
16
17
# File 'lib/state/component/providers/influxdb/semantictype.rb', line 9

def initialize(name, namespace)
  @name        = name
  @namespace   = namespace
  @client = Client.new
  @crd_content = get(name, namespace)
  @content_to_write = []
  @expanded_spec = ::DTK::CrdClient.get_kubeclient({}).get_semantictype(name, namespace).expandedSpec
  @expanded_semantictype_spec = expand(crd_content.to_h[:properties], @expanded_spec)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/state/component/providers/influxdb/semantictype.rb', line 6

def client
  @client
end

#content_to_writeObject

Returns the value of attribute content_to_write.



7
8
9
# File 'lib/state/component/providers/influxdb/semantictype.rb', line 7

def content_to_write
  @content_to_write
end

#crd_contentObject (readonly)

Returns the value of attribute crd_content.



6
7
8
# File 'lib/state/component/providers/influxdb/semantictype.rb', line 6

def crd_content
  @crd_content
end

#expanded_semantictype_specObject (readonly)

Returns the value of attribute expanded_semantictype_spec.



6
7
8
# File 'lib/state/component/providers/influxdb/semantictype.rb', line 6

def expanded_semantictype_spec
  @expanded_semantictype_spec
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/state/component/providers/influxdb/semantictype.rb', line 6

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/state/component/providers/influxdb/semantictype.rb', line 6

def namespace
  @namespace
end

Instance Method Details

#partial_write_update(component_and_attribute, path, field_name, field_value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/state/component/providers/influxdb/semantictype.rb', line 34

def partial_write_update(component_and_attribute, path, field_name, field_value)
  parent, child = validate_parameter(path)
  component_id, attribute_name = component_and_attribute.split('/')
  # getting previous value for given parameters
  previous_value = {}
  begin
    flux_query = 'from(bucket:"' + @client.connection_parameters[:bucket] + '") |> range(start:-5) |> filter(fn:(r) => r._measurement == "' + attribute_name + "_" + child[:type] + '") |> filter(fn: (r) => r.parent == "' + parent[:name] + '") |> filter(fn: (r) => r.name == "' + child[:name] + '")|> last()'
    result = @client.query(query: flux_query)
    previous_value = result.values.map(&:records).flatten.map(&:values)
  rescue => e
    raise "Partial write could not be completed. Previous point for given parameters not found!. Error: #{e}"
  end
  update_current(previous_value[0], get_path_to_object(path), field_name, field_value)
end

#write_semantictype_inventory(inventory, component_id) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/state/component/providers/influxdb/semantictype.rb', line 19

def write_semantictype_inventory(inventory, component_id)
  get_influxdb_properties(inventory)
  content_to_write.each do |point|
    point[:tags].merge!({ component_id: component_id, attribute_name: @name })
    @client.write_point({
                          name: point[:name],
                          tags: point[:tags],
                          fields: point[:fields],
                          time: (Time.new.to_f * 1000).to_i
                        })
  end
rescue => e
  raise "#{name} inventory write failed. Error: #{e}"
end