Class: Cosmos::ReactionModel
- Defined in:
- lib/cosmos/models/reaction_model.rb
Overview
{
"description": "POSX greater than 200",
"snooze": 300,
"review": true,
"triggers": [
{
"name": "TV0-1234",
"group": "foo",
}
],
"actions": [
{
"type": "command",
"value": "INST CLEAR",
}
]
}
Constant Summary collapse
- PRIMARY_KEY =
'__cosmos__reaction'.freeze
- COMMAND_REACTION =
'command'.freeze
- SCRIPT_REACTION =
'script'.freeze
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#active ⇒ Object
readonly
Returns the value of attribute active.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#review ⇒ Object
readonly
Returns the value of attribute review.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#snooze ⇒ Object
readonly
Returns the value of attribute snooze.
-
#snoozed_until ⇒ Object
readonly
Returns the value of attribute snoozed_until.
-
#triggers ⇒ Object
readonly
Returns the value of attribute triggers.
Attributes inherited from Model
Class Method Summary collapse
-
.all(scope:) ⇒ Array<Hash>
All the Key, Values stored under the name key.
- .create_mini_id ⇒ Object
-
.delete(name:, scope:, force: false) ⇒ Object
Check dependents before delete.
-
.from_json(json, name:, scope:) ⇒ ReactionModel
Model generated from the passed JSON.
-
.get(name:, scope:) ⇒ ReactionModel
Return the object with the name at.
-
.names(scope:) ⇒ Array<String>
All the uuids stored under the name key.
- .reactions(scope:) ⇒ Array<ReactionModel>
Instance Method Summary collapse
- #activate ⇒ Object
-
#as_json ⇒ Hash
Generated from the ReactionModel.
- #awaken ⇒ Object
- #create ⇒ Object
- #create_microservice(topics:) ⇒ Object
- #deactivate ⇒ Object
- #deploy ⇒ Object
-
#initialize(name:, scope:, description:, snooze:, actions:, triggers:, active: true, review: true, snoozed_until: nil, updated_at: nil) ⇒ ReactionModel
constructor
A new instance of ReactionModel.
-
#notify(kind:) ⇒ Object
-
update the redis stream / reaction topic that something has changed.
-
- #sleep ⇒ Object
-
#to_s ⇒ String
Generated from the TriggerModel.
- #undeploy ⇒ Object
- #update ⇒ Object
- #validate_actions(actions:) ⇒ Object
- #validate_snooze(snooze:) ⇒ Object
- #validate_triggers(triggers:) ⇒ Object
- #verify_triggers ⇒ Object
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:, description:, snooze:, actions:, triggers:, active: true, review: true, snoozed_until: nil, updated_at: nil) ⇒ ReactionModel
Returns a new instance of ReactionModel.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/cosmos/models/reaction_model.rb', line 161 def initialize( name:, scope:, description:, snooze:, actions:, triggers:, active: true, review: true, snoozed_until: nil, updated_at: nil ) if name.nil? || scope.nil? || description.nil? || snooze.nil? || triggers.nil? || actions.nil? raise ReactionInputError.new "#{name}, #{scope}, #{description}, #{snooze}, #{triggers}, or #{actions} must not be nil" end super("#{scope}#{PRIMARY_KEY}", name: name, scope: scope) @microservice_name = "#{scope}__COSMOS__REACTION" @active = active @review = review @description = description @snoozed_until = snoozed_until @snooze = validate_snooze(snooze: snooze) @actions = validate_actions(actions: actions) @triggers = validate_triggers(triggers: triggers) @updated_at = updated_at end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
159 160 161 |
# File 'lib/cosmos/models/reaction_model.rb', line 159 def actions @actions end |
#active ⇒ Object (readonly)
Returns the value of attribute active.
159 160 161 |
# File 'lib/cosmos/models/reaction_model.rb', line 159 def active @active end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
159 160 161 |
# File 'lib/cosmos/models/reaction_model.rb', line 159 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
159 160 161 |
# File 'lib/cosmos/models/reaction_model.rb', line 159 def name @name end |
#review ⇒ Object (readonly)
Returns the value of attribute review.
159 160 161 |
# File 'lib/cosmos/models/reaction_model.rb', line 159 def review @review end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
159 160 161 |
# File 'lib/cosmos/models/reaction_model.rb', line 159 def scope @scope end |
#snooze ⇒ Object (readonly)
Returns the value of attribute snooze.
159 160 161 |
# File 'lib/cosmos/models/reaction_model.rb', line 159 def snooze @snooze end |
#snoozed_until ⇒ Object (readonly)
Returns the value of attribute snoozed_until.
159 160 161 |
# File 'lib/cosmos/models/reaction_model.rb', line 159 def snoozed_until @snoozed_until end |
#triggers ⇒ Object (readonly)
Returns the value of attribute triggers.
159 160 161 |
# File 'lib/cosmos/models/reaction_model.rb', line 159 def triggers @triggers end |
Class Method Details
.all(scope:) ⇒ Array<Hash>
Returns All the Key, Values stored under the name key.
79 80 81 |
# File 'lib/cosmos/models/reaction_model.rb', line 79 def self.all(scope:) super("#{scope}#{PRIMARY_KEY}") end |
.create_mini_id ⇒ Object
52 53 54 55 56 57 |
# File 'lib/cosmos/models/reaction_model.rb', line 52 def self.create_mini_id time = (Time.now.to_f * 10_000_000).to_i jitter = rand(10_000_000) key = "#{jitter}#{time}".to_i.to_s(36) return "RV0-#{key}" end |
.delete(name:, scope:, force: false) ⇒ Object
Check dependents before delete.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/cosmos/models/reaction_model.rb', line 89 def self.delete(name:, scope:, force: false) model = self.get(name: name, scope: scope) if model.nil? raise ReactionInputError.new "failed to find reaction: #{name}" end model.triggers.each do | trigger | trigger_model = TriggerModel.get(name: trigger['name'], group: trigger['group'], scope: scope) trigger_model.update_dependents(dependent: name, remove: true) trigger_model.update() end Store.hdel("#{scope}#{PRIMARY_KEY}", name) model.notify(kind: 'deleted') end |
.from_json(json, name:, scope:) ⇒ ReactionModel
Returns Model generated from the passed JSON.
274 275 276 277 278 279 280 |
# File 'lib/cosmos/models/reaction_model.rb', line 274 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:) ⇒ ReactionModel
Return the object with the name at
71 72 73 74 75 76 |
# File 'lib/cosmos/models/reaction_model.rb', line 71 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.
84 85 86 |
# File 'lib/cosmos/models/reaction_model.rb', line 84 def self.names(scope:) super("#{scope}#{PRIMARY_KEY}") end |
.reactions(scope:) ⇒ Array<ReactionModel>
60 61 62 63 64 65 66 67 68 |
# File 'lib/cosmos/models/reaction_model.rb', line 60 def self.reactions(scope:) reactions = Array.new Store.hgetall("#{scope}#{PRIMARY_KEY}").each do |key, value| data = JSON.parse(value) reaction = self.from_json(data, name: data['name'], scope: data['scope']) reactions << reaction if reaction.active end return reactions end |
Instance Method Details
#activate ⇒ Object
223 224 225 226 227 228 229 |
# File 'lib/cosmos/models/reaction_model.rb', line 223 def activate @active = true @snoozed_until = nil if @snoozed_until && @snoozed_until < Time.now.to_i @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json())) notify(kind: 'activated') end |
#as_json ⇒ Hash
Returns generated from the ReactionModel.
258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/cosmos/models/reaction_model.rb', line 258 def as_json return { 'name' => @name, 'scope' => @scope, 'active' => @active, 'review' => @review, 'description' => @description, 'snooze' => @snooze, 'snoozed_until' => @snoozed_until, 'triggers' => @triggers, 'actions' => @actions, 'updated_at' => @updated_at } end |
#awaken ⇒ Object
245 246 247 248 249 250 |
# File 'lib/cosmos/models/reaction_model.rb', line 245 def awaken @snoozed_until = nil @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json())) notify(kind: 'awaken') end |
#create ⇒ Object
206 207 208 209 210 211 212 213 214 |
# File 'lib/cosmos/models/reaction_model.rb', line 206 def create unless Store.hget(@primary_key, @name).nil? raise ReactionInputError.new "exsisting Reaction found: #{@name}" end verify_triggers() @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
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/cosmos/models/reaction_model.rb', line 292 def create_microservice(topics:) # reaction Microservice microservice = MicroserviceModel.new( name: @microservice_name, folder_name: nil, cmd: ['ruby', 'reaction_microservice.rb', @microservice_name], work_dir: '/cosmos/lib/cosmos/microservices', options: [], topics: topics, target_names: [], plugin: nil, scope: @scope ) microservice.create end |
#deactivate ⇒ Object
231 232 233 234 235 236 |
# File 'lib/cosmos/models/reaction_model.rb', line 231 def deactivate @active = false @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json())) notify(kind: 'deactivated') end |
#deploy ⇒ Object
308 309 310 311 312 313 |
# File 'lib/cosmos/models/reaction_model.rb', line 308 def deploy topics = ["#{@scope}__cosmos_autonomic"] if MicroserviceModel.get_model(name: @microservice_name, scope: @scope).nil? create_microservice(topics: topics) end end |
#notify(kind:) ⇒ Object
Returns [] update the redis stream / reaction topic that something has changed.
283 284 285 286 287 288 289 290 |
# File 'lib/cosmos/models/reaction_model.rb', line 283 def notify(kind:) notification = { 'kind' => kind, 'type' => 'reaction', 'data' => JSON.generate(as_json()), } AutonomicTopic.write_notification(notification, scope: @scope) end |
#sleep ⇒ Object
238 239 240 241 242 243 |
# File 'lib/cosmos/models/reaction_model.rb', line 238 def sleep @snoozed_until = Time.now.to_i + @snooze @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json())) notify(kind: 'sleep') end |
#to_s ⇒ String
Returns generated from the TriggerModel.
253 254 255 |
# File 'lib/cosmos/models/reaction_model.rb', line 253 def to_s return "(ReactionModel :: #{@name} :: #{@active} :: #{@review} :: #{@description} :: #{@snooze} :: #{@snoozed_until})" end |
#undeploy ⇒ Object
315 316 317 318 319 320 |
# File 'lib/cosmos/models/reaction_model.rb', line 315 def undeploy if ReactionModel.names(scope: @scope).empty? model = MicroserviceModel.get_model(name: @microservice_name, scope: @scope) model.destroy if model end end |
#update ⇒ Object
216 217 218 219 220 221 |
# File 'lib/cosmos/models/reaction_model.rb', line 216 def update verify_triggers() @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json())) notify(kind: 'updated') end |
#validate_actions(actions:) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cosmos/models/reaction_model.rb', line 138 def validate_actions(actions:) unless actions.is_a?(Array) raise ReactionInputError.new "invalid actions object: #{actions}" end actions.each do | action | unless action.is_a?(Hash) raise ReactionInputError.new "invalid action object: #{action}" end action_type = action['type'] if action_type.nil? raise ReactionInputError.new "reaction action must contain type: #{action_type}" elsif action['value'].nil? raise ReactionInputError.new "reaction action: #{action} does not contain 'value'" end unless [COMMAND_REACTION, SCRIPT_REACTION].include?(action_type) raise ReactionInputError.new "reaction action contains invalid type: #{action_type}" end end return actions end |
#validate_snooze(snooze:) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/cosmos/models/reaction_model.rb', line 104 def validate_snooze(snooze:) unless snooze.is_a?(Integer) raise ReactionInputError.new "invalid snooze value: #{snooze}" end if snooze < 30 raise ReactionInputError.new "invalid snooze: '#{snooze}' must be greater than 30" end return snooze end |
#validate_triggers(triggers:) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/cosmos/models/reaction_model.rb', line 115 def validate_triggers(triggers:) unless triggers.is_a?(Array) raise ReactionInputError.new "invalid operator: #{operator}" end trigger_hash = Hash.new() triggers.each do | trigger | unless trigger.is_a?(Hash) raise ReactionInputError.new "invalid trigger object: #{trigger}" end if trigger['name'].nil? || trigger['group'].nil? raise ReactionInputError.new "allowed: #{triggers}" end trigger_name = trigger['name'] unless trigger_hash[trigger_name].nil? raise ReactionInputError.new "no duplicate triggers allowed: #{triggers}" else trigger_hash[trigger_name] = 1 end end return triggers end |
#verify_triggers ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/cosmos/models/reaction_model.rb', line 188 def verify_triggers trigger_models = [] @triggers.each do | trigger | model = TriggerModel.get(name: trigger['name'], group: trigger['group'], scope: @scope) if model.nil? raise ReactionInputError.new "failed to find trigger: #{trigger}" end trigger_models << model end if trigger_models.empty? raise ReactionInputError.new "reaction must contain at least one valid trigger: #{@triggers}" end trigger_models.each do | trigger_model | trigger_model.update_dependents(dependent: @name) trigger_model.update() end end |