Class: Cosmos::TriggerGroupModel

Inherits:
Model show all
Defined in:
lib/cosmos/models/trigger_group_model.rb

Overview

INPUT:

{
  "name": "FOOBAR",
  "color": "#000000",
}

Constant Summary collapse

PRIMARY_KEY =
'__TRIGGER__GROUP'.freeze

Instance Attribute Summary collapse

Attributes inherited from Model

#plugin

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#as_config, #destroy, filter, find_all_by_plugin, get_all_models, get_model, handle_config, set

Constructor Details

#initialize(name:, scope:, color: nil, updated_at: nil) ⇒ TriggerGroupModel

Returns a new instance of TriggerGroupModel.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cosmos/models/trigger_group_model.rb', line 72

def initialize(name:, scope:, color: nil, updated_at: nil)
  if name.nil? || scope.nil?
    raise GroupTriggerInputError.new "name, or scope must not be nil"
  end
  unless name.is_a?(String)
    raise TriggerGroupInputError.new "invalid name: '#{name}'"
  end
  if name.include?('_')
    raise TriggerGroupInputError.new "invalid name: '#{name}' can not include an underscore '_'"
  end
  super("#{scope}#{PRIMARY_KEY}", name: name, scope: scope)
  @microservice_name = "#{scope}__TRIGGER_GROUP__#{name}"
  update_color(color: color)
  @updated_at = updated_at
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



70
71
72
# File 'lib/cosmos/models/trigger_group_model.rb', line 70

def color
  @color
end

#nameObject (readonly)

Returns the value of attribute name.



70
71
72
# File 'lib/cosmos/models/trigger_group_model.rb', line 70

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



70
71
72
# File 'lib/cosmos/models/trigger_group_model.rb', line 70

def scope
  @scope
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



70
71
72
# File 'lib/cosmos/models/trigger_group_model.rb', line 70

def updated_at
  @updated_at
end

Class Method Details

.all(scope:) ⇒ Array<Hash>

Returns All the Key, Values stored under the name key.

Returns:

  • (Array<Hash>)

    All the Key, Values stored under the name key



46
47
48
# File 'lib/cosmos/models/trigger_group_model.rb', line 46

def self.all(scope:)
  super("#{scope}#{PRIMARY_KEY}")
end

.delete(name:, scope:) ⇒ Object

Check dependents before delete.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cosmos/models/trigger_group_model.rb', line 56

def self.delete(name:, scope:)
  model = self.get(name: name, scope: scope)
  if model.nil?
    raise TriggerGroupInputError.new "invalid group: #{name} not found"
  end
  triggers = TriggerModel.names(scope: scope, group: name)
  if triggers.empty?
    Store.hdel("#{scope}#{PRIMARY_KEY}", name)
    model.notify(kind: 'deleted')
  else
    raise TriggerGroupError.new "failed to delete #{name} triggers: #{triggers}"
  end
end

.from_json(json, name:, scope:) ⇒ TriggerGroupModel

Returns Model generated from the passed JSON.

Returns:



134
135
136
137
138
139
140
# File 'lib/cosmos/models/trigger_group_model.rb', line 134

def self.from_json(json, name:, scope:)
  json = JSON.parse(json) if String === json
  raise "json data is nil" if json.nil?

  json.transform_keys!(&:to_sym)
  self.new(**json, name: name, scope: scope)
end

.get(name:, scope:) ⇒ GroupModel

Return the object with the name at

Returns:

  • (GroupModel)

    Return the object with the name at



38
39
40
41
42
43
# File 'lib/cosmos/models/trigger_group_model.rb', line 38

def self.get(name:, scope:)
  json = super("#{scope}#{PRIMARY_KEY}", name: name)
  unless json.nil?
    self.from_json(json, name: name, scope: scope)
  end
end

.names(scope:) ⇒ Array<String>

Returns All the uuids stored under the name key.

Returns:

  • (Array<String>)

    All the uuids stored under the name key



51
52
53
# File 'lib/cosmos/models/trigger_group_model.rb', line 51

def self.names(scope:)
  super("#{scope}#{PRIMARY_KEY}")
end

Instance Method Details

#as_jsonHash

Returns generated from the TriggerGroupModel.

Returns:

  • (Hash)

    generated from the TriggerGroupModel



124
125
126
127
128
129
130
131
# File 'lib/cosmos/models/trigger_group_model.rb', line 124

def as_json
  return {
    'name' => @name,
    'scope' => @scope,
    'color' => @color,
    'updated_at' => @updated_at,
  }
end

#createObject



103
104
105
106
107
108
109
110
# File 'lib/cosmos/models/trigger_group_model.rb', line 103

def create
  unless Store.hget(@primary_key, @name).nil?
    raise TriggerGroupInputError.new "exsisting TriggerGroup found: #{@name}"
  end
  @updated_at = Time.now.to_nsec_from_epoch
  Store.hset(@primary_key, @name, JSON.generate(as_json()))
  notify(kind: 'created')
end

#create_microservice(topics:) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cosmos/models/trigger_group_model.rb', line 154

def create_microservice(topics:)
  # reaction Microservice
  microservice = MicroserviceModel.new(
    name: @microservice_name,
    folder_name: nil,
    cmd: ['ruby', "trigger_group_microservice.rb", @microservice_name],
    work_dir: '/cosmos/lib/cosmos/microservices',
    options: [],
    topics: topics,
    target_names: [],
    plugin: nil,
    scope: @scope
  )
  microservice.create
end

#deployObject



170
171
172
173
174
175
176
# File 'lib/cosmos/models/trigger_group_model.rb', line 170

def deploy
  topics = ["#{@scope}__cosmos_autonomic"]
  if MicroserviceModel.get_model(name: @microservice_name, scope: @scope).nil?
    AutonomicTopic.initialize_streams(topics)
    create_microservice(topics: topics)
  end
end

#notify(kind:, error: nil) ⇒ Object

Returns [] update the redis stream / trigger topic that something has changed.

Returns:

  • update the redis stream / trigger topic that something has changed



143
144
145
146
147
148
149
150
151
152
# File 'lib/cosmos/models/trigger_group_model.rb', line 143

def notify(kind:, error: nil)
  data = as_json()
  data['error'] = error unless error.nil?
  notification = {
    'kind' => kind,
    'type' => 'group',
    'data' => JSON.generate(data),
  }
  AutonomicTopic.write_notification(notification, scope: @scope)
end

#to_sString

Returns generated from the TriggerGroupModel.

Returns:

  • (String)

    generated from the TriggerGroupModel



119
120
121
# File 'lib/cosmos/models/trigger_group_model.rb', line 119

def to_s
  return "(TriggerGroupModel :: #{@name})"
end

#undeployObject



178
179
180
181
182
183
# File 'lib/cosmos/models/trigger_group_model.rb', line 178

def undeploy
  if TriggerModel.names(scope: scope, group: name).empty?
    model = MicroserviceModel.get_model(name: @microservice_name, scope: @scope)
    model.destroy if model
  end
end

#updateObject



112
113
114
115
116
# File 'lib/cosmos/models/trigger_group_model.rb', line 112

def update
  @updated_at = Time.now.to_nsec_from_epoch
  Store.hset(@primary_key, @name, JSON.generate(as_json()))
  notify(kind: 'updated')
end

#update_color(color: nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cosmos/models/trigger_group_model.rb', line 88

def update_color(color: nil)
  if color.nil?
    color = '#%06x' % (rand * 0xffffff)
  end
  valid_color = color =~ /(#*)([0-9,a-f,A-f]{6})/
  if valid_color.nil?
    raise TriggerGroupInputError.new "invalid color must be in hex format. #FF0000"
  end

  unless color.start_with?('#')
    color = "##{color}"
  end
  @color = color
end