Class: DTK::State::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/state/component.rb,
lib/state/component/attribute.rb

Defined Under Namespace

Classes: Attribute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, component_content, parent, opts = {}) ⇒ Component

Returns a new instance of Component.



8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#component_defObject (readonly)

Returns the value of attribute component_def.



5
6
7
# File 'lib/state/component.rb', line 5

def component_def
  @component_def
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/state/component.rb', line 5

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/state/component.rb', line 5

def parent
  @parent
end

#raw_attributesObject

Returns the value of attribute raw_attributes.



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

def raw_attributes
  @raw_attributes
end

Class Method Details

.create_from_kube_array(kube_components, crd_assembly) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# 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 = {}) ⇒ Object

opts can have keys

task_id


20
21
22
23
24
25
# 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 = {}) ⇒ Object

opts can have keys task_id format:

hash
kubernetes - return attributes in raw format from kubeclient


71
72
73
74
# 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 = {}) ⇒ Object



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
52
53
# 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

Instance Method Details

#attribute_metadataObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/state/component.rb', line 88

def 
  attributes         = @raw_attributes.to_hash
  attr_type_info     = @component_def.attribute_type_info
   = {}

  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_name] = attr_info_hash.merge(attribute)
    end
  end

  
end

#attribute_valuesObject



109
110
111
112
113
114
115
# 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 = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# 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_hashObject



55
56
57
58
59
60
61
62
63
64
# 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