class DTK::State::Component

Attributes

component_def[R]
name[R]
parent[R]
raw_attributes[RW]

Public Class Methods

create_from_kube_array(kube_components, crd_assembly) click to toggle source
# File lib/state/component.rb, line 117
def self.create_from_kube_array(kube_components, crd_assembly)
  kube_components.map do |component_hash|
    if component_hash.is_a?(String)
      component_name    = component_hash
      component_content = {}
    else
      component_name = component_hash.to_hash.keys.first
      component_content = component_hash[component_name]
    end
    Component.new(component_name, component_content, crd_assembly)
  end
end
get(crd_assembly_namespace, crd_assembly_name, component_name, opts = {}) click to toggle source

opts can have keys

task_id
# File lib/state/component.rb, line 20
def self.get(crd_assembly_namespace, crd_assembly_name, component_name, opts = {})
  crd_assembly = CrdAssembly.get(crd_assembly_namespace, crd_assembly_name, opts)
  if matching_component = Component.find_matching_component(crd_assembly, component_name)
    Component.new(component_name, Component.get_component_content(matching_component, component_name), crd_assembly, opts)
  end
end
get_attributes(crd_assembly_namespace, crd_assembly_name, component_name, opts = {}) click to toggle source

opts can have keys task_id format:

hash
kubernetes - return attributes in raw format from kubeclient
# File lib/state/component.rb, line 71
def self.get_attributes(crd_assembly_namespace, crd_assembly_name, component_name, opts = {})
  component = get(crd_assembly_namespace, crd_assembly_name, component_name, opts)
  component.attributes(opts)
end
get_with_influx_data(namespace, assembly_name, component_name, opts = {}) click to toggle source
# File lib/state/component.rb, line 27
def self.get_with_influx_data(namespace, assembly_name, component_name, opts = {})
  component = get(namespace, assembly_name, component_name, opts)
  return unless component
  attributes = component.raw_attributes.to_hash

  attr_type_info = component.component_def.attribute_type_info
  attr_type_info.each do |attr_info|
    if attr_info.temporal
      attribute_name = attr_info.name.to_sym
      influxdb = ::DTK::State::Component::Attribute::Influxdb.new(:attributes)
      influxdb_attribute = influxdb.get(namespace, component_name, assembly_name, attribute_name, opts)
      if valid_attribute = influxdb_attribute.first
        value = valid_attribute['_value']
        if attributes[attribute_name]
          if attributes[attribute_name].is_a?(String)
            attributes[attribute_name] = value
          else
            attributes[attribute_name][:value] = value
          end
        end
      end
    end
  end

  component.raw_attributes = attributes
  component
end
new(name, component_content, parent, opts = {}) click to toggle source
# File lib/state/component.rb, line 8
def initialize(name, component_content, parent, opts = {})
  @name           = name
  # @parent     = parent
  @component_defs = parent.references.component_def
  @component_def  = get_component_def(opts)
  @raw_attributes = component_content[:attributes] || {}

  @attribute_objs = Attribute.create_from_kube_hash(component_content[:attributes] || {})# convert_to_attribute_objects(component_content[:attributes])
end

Private Class Methods

find_matching_component(assembly, component_name) click to toggle source
# File lib/state/component.rb, line 136
def self.find_matching_component(assembly, component_name)
  assembly.components.find do |cmp|
    if cmp.is_a? String
      cmp == component_name
    else
      cmp.to_hash.keys.first.to_s == component_name
    end
  end
end
get_component_content(matching_component, component_name) click to toggle source
# File lib/state/component.rb, line 132
def self.get_component_content(matching_component, component_name)
  return matching_component.is_a?(String) ? {} : matching_component[component_name]
end

Public Instance Methods

attribute_metadata() click to toggle source
# File lib/state/component.rb, line 88
def attribute_metadata
  attributes         = @raw_attributes.to_hash
  attr_type_info     = @component_def.attribute_type_info
  attribute_metadata = {}

  attr_type_info.each do |attr_info|
    attr_info_hash = attr_info.to_hash
    attribute_name = attr_info_hash[:name].to_sym

    if attribute = attributes[attribute_name]
      if attribute.is_a?(String)
        attribute = { value: attribute }
      end

      attribute_metadata[attribute_name] = attr_info_hash.merge(attribute)
    end
  end

  attribute_metadata
end
attribute_values() click to toggle source
# File lib/state/component.rb, line 109
def attribute_values
  attribute_with_values = {}
  @raw_attributes.each do |name, content|
    attribute_with_values.merge!(name => content[:value])
  end
  attribute_with_values
end
attributes(opts = {}) click to toggle source
# File lib/state/component.rb, line 76
def attributes(opts = {})
  format = opts[:format]

  return @attribute_objs unless format

  if format.to_s == 'hash'
    @attribute_objs.map{ |attr| attr.to_hash }
  else
    fail Error.new "Unsupported format '#{format}'"
  end
end
to_hash() click to toggle source
# File lib/state/component.rb, line 55
def to_hash
  {
    name: @name,
    component_def: {
      name: @component_def.name,
      namespace: @component_def.namespace
    },
    attributes: @raw_attributes.to_hash
  }
end

Private Instance Methods

destructure_component_full_name() click to toggle source
# File lib/state/component.rb, line 153
def destructure_component_full_name
  regex = /(.*)\[(.*)\]/
  component_def_name, component_name = @name.match(regex).captures
  {
    component_def_name: "#{component_def_name}",
    component_name: component_name
  }
end
get_component_def(opts = {}) click to toggle source
# File lib/state/component.rb, line 146
def get_component_def(opts = {})
  destructured_component = destructure_component_full_name
  component_def = @component_defs.find { |component_def| component_def[:name] == destructured_component[:component_def_name] }
  # module_ref             = @module_refs.find { |module_ref| module_ref[:name] == destructured_component[:module_name] }
  ComponentDef.get(component_def[:namespace], destructured_component[:component_def_name], opts)
end