Class: OpenC3::Model
Direct Known Subclasses
ActivityModel, EnvironmentModel, EphemeralModel, InterfaceModel, InterfaceStatusModel, MicroserviceModel, MicroserviceStatusModel, MigrationModel, NewsModel, OfflineAccessModel, PluginModel, PluginStoreModel, ProcessStatusModel, QueueModel, ReactionModel, ReingestJobModel, ScopeModel, ScriptEngineModel, ScriptStatusModel, SecretModel, SettingModel, SortedModel, StashModel, TargetModel, TimelineModel, ToolModel, TriggerGroupModel, TriggerModel, WidgetModel
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#plugin ⇒ Object
Returns the value of attribute plugin.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Class Method Summary collapse
-
.all(primary_key) ⇒ Array<Hash>
All the models (as Hash objects) stored under the primary key.
-
.filter(key, value, scope:, substr: false) ⇒ Object
Loops over all items and returns objects that match a key value pair.
-
.find_all_by_plugin(plugin:, scope:) ⇒ Array<Object>
All the models (as Model objects) stored under the primary key which have the plugin attribute.
-
.from_json(json, scope:) ⇒ Model
Model generated from the passed JSON.
-
.get(primary_key, name:) ⇒ Hash|nil
Hash of this model or nil if name not found under primary_key.
-
.get_all_models(scope:) ⇒ Array<Object>
All the models (as Model objects) stored under the primary key.
-
.get_model(name:, scope:) ⇒ Object|nil
Calls self.get and then from_json to turn the Hash configuration into a Ruby Model object.
- .handle_config(parser, keyword, parameters) ⇒ Object
-
.names(primary_key) ⇒ Array<String>
All the names stored under the primary key.
-
.set(json, scope:, queued: false) ⇒ Object
Sets (updates) the redis hash of this model.
- .store ⇒ Object
- .store_queued ⇒ Object
Instance Method Summary collapse
-
#as_json(*a) ⇒ Hash
JSON encoding of this model.
- #check_disable_erb(filename) ⇒ Object
-
#create(update: false, force: false, queued: false) ⇒ Object
Update the Redis hash at primary_key and set the field "name" to the JSON generated via calling as_json.
-
#deploy(gem_path, variables) ⇒ Object
Deploy the model into the OpenC3 system.
-
#destroy ⇒ Object
Delete the model from the Store.
-
#destroyed? ⇒ Boolean
Indicate if destroy has been called.
-
#diff(existing) ⇒ Array<String>
Compare this model's as_json with a previous Hash and return an array of human-readable change descriptions (e.g. "key: old -> new").
-
#initialize(primary_key, **kw_args) ⇒ Model
constructor
Store the primary key and keyword arguments.
-
#undeploy ⇒ Object
Undo the actions of deploy and remove the model from OpenC3.
-
#update(force: false, queued: false) ⇒ Object
Alias for create(update: true).
Constructor Details
#initialize(primary_key, **kw_args) ⇒ Model
Store the primary key and keyword arguments
126 127 128 129 130 131 132 133 |
# File 'lib/openc3/models/model.rb', line 126 def initialize(primary_key, **kw_args) @primary_key = primary_key @name = kw_args[:name] @updated_at = kw_args[:updated_at] @plugin = kw_args[:plugin] @scope = kw_args[:scope] @destroyed = false end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
24 25 26 |
# File 'lib/openc3/models/model.rb', line 24 def name @name end |
#plugin ⇒ Object
Returns the value of attribute plugin.
26 27 28 |
# File 'lib/openc3/models/model.rb', line 26 def plugin @plugin end |
#scope ⇒ Object
Returns the value of attribute scope.
27 28 29 |
# File 'lib/openc3/models/model.rb', line 27 def scope @scope end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
25 26 27 |
# File 'lib/openc3/models/model.rb', line 25 def updated_at @updated_at end |
Class Method Details
.all(primary_key) ⇒ Array<Hash>
Returns All the models (as Hash objects) stored under the primary key.
56 57 58 59 60 61 62 |
# File 'lib/openc3/models/model.rb', line 56 def self.all(primary_key) hash = store.hgetall(primary_key) hash.each do |key, value| hash[key] = JSON.parse(value, allow_nan: true, create_additions: true) end hash end |
.filter(key, value, scope:, substr: false) ⇒ Object
Loops over all items and returns objects that match a key value pair
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/openc3/models/model.rb', line 66 def self.filter(key, value, scope:, substr: false) filtered = {} results = all(scope: scope) results.each do |name, result| if result[key] == value || (substr && result[key].include?(value)) filtered[name] = result end end return filtered end |
.find_all_by_plugin(plugin:, scope:) ⇒ Array<Object>
Returns All the models (as Model objects) stored under the primary key which have the plugin attribute.
112 113 114 115 116 117 118 119 |
# File 'lib/openc3/models/model.rb', line 112 def self.find_all_by_plugin(plugin:, scope:) result = {} models = get_all_models(scope: scope) models.each do |name, model| result[name] = model if model.plugin == plugin end result end |
.from_json(json, scope:) ⇒ Model
Returns Model generated from the passed JSON.
85 86 87 88 89 90 |
# File 'lib/openc3/models/model.rb', line 85 def self.from_json(json, scope:) json = JSON.parse(json, allow_nan: true, create_additions: true) if String === json raise "json data is nil" if json.nil? json[:scope] = scope self.new(**json.transform_keys(&:to_sym), scope: scope) end |
.get(primary_key, name:) ⇒ Hash|nil
Returns Hash of this model or nil if name not found under primary_key.
41 42 43 44 45 46 47 48 |
# File 'lib/openc3/models/model.rb', line 41 def self.get(primary_key, name:) json = store.hget(primary_key, name) if json return JSON.parse(json, allow_nan: true, create_additions: true) else return nil end end |
.get_all_models(scope:) ⇒ Array<Object>
Returns All the models (as Model objects) stored under the primary key.
104 105 106 107 108 |
# File 'lib/openc3/models/model.rb', line 104 def self.get_all_models(scope:) models = {} all(scope: scope).each { |name, json| models[name] = from_json(json, scope: scope) } models end |
.get_model(name:, scope:) ⇒ Object|nil
Calls self.get and then from_json to turn the Hash configuration into a Ruby Model object.
94 95 96 97 98 99 100 101 |
# File 'lib/openc3/models/model.rb', line 94 def self.get_model(name:, scope:) json = get(name: name, scope: scope) if json return from_json(json, scope: scope) else return nil end end |
.handle_config(parser, keyword, parameters) ⇒ Object
121 122 123 |
# File 'lib/openc3/models/model.rb', line 121 def self.handle_config(parser, keyword, parameters) raise "must be implemented by subclass" end |
.names(primary_key) ⇒ Array<String>
Returns All the names stored under the primary key.
51 52 53 |
# File 'lib/openc3/models/model.rb', line 51 def self.names(primary_key) store.hkeys(primary_key).sort end |
.set(json, scope:, queued: false) ⇒ Object
Sets (updates) the redis hash of this model
78 79 80 81 82 |
# File 'lib/openc3/models/model.rb', line 78 def self.set(json, scope:, queued: false) json[:scope] = scope json.transform_keys!(&:to_sym) self.new(**json).create(force: true, queued: queued) end |
.store_queued ⇒ Object
33 34 35 |
# File 'lib/openc3/models/model.rb', line 33 def self.store_queued StoreQueued end |
Instance Method Details
#as_json(*a) ⇒ Hash
Returns JSON encoding of this model.
186 187 188 189 190 191 |
# File 'lib/openc3/models/model.rb', line 186 def as_json(*a) { 'name' => @name, 'updated_at' => @updated_at, 'plugin' => @plugin, 'scope' => @scope } end |
#check_disable_erb(filename) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/openc3/models/model.rb', line 209 def check_disable_erb(filename) erb_disabled = false if @disable_erb if @disable_erb.length == 0 # Disable all ERB erb_disabled = true else @disable_erb.each do |pattern| if filename =~ Regexp.new(pattern) erb_disabled = true break end end end end return erb_disabled end |
#create(update: false, force: false, queued: false) ⇒ Object
Update the Redis hash at primary_key and set the field "name" to the JSON generated via calling as_json
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/openc3/models/model.rb', line 137 def create(update: false, force: false, queued: false) unless force existing = self.class.store.hget(@primary_key, @name) if existing raise "#{@primary_key}:#{@name} already exists at create" unless update else raise "#{@primary_key}:#{@name} doesn't exist at update" if update end end @updated_at = Time.now.utc.to_nsec_from_epoch if queued write_store = self.class.store_queued else write_store = self.class.store end write_store.hset(@primary_key, @name, JSON.generate(self.as_json(), allow_nan: true)) end |
#deploy(gem_path, variables) ⇒ Object
Deploy the model into the OpenC3 system. Subclasses must implement this and typically create MicroserviceModels to implement.
163 164 165 |
# File 'lib/openc3/models/model.rb', line 163 def deploy(gem_path, variables) raise "must be implemented by subclass" end |
#destroy ⇒ Object
Delete the model from the Store
174 175 176 177 178 |
# File 'lib/openc3/models/model.rb', line 174 def destroy @destroyed = true undeploy() self.class.store.hdel(@primary_key, @name) end |
#destroyed? ⇒ Boolean
Indicate if destroy has been called
181 182 183 |
# File 'lib/openc3/models/model.rb', line 181 def destroyed? @destroyed end |
#diff(existing) ⇒ Array<String>
Compare this model's as_json with a previous Hash and return an array of human-readable change descriptions (e.g. "key: old -> new"). Skips the updated_at field since it always changes.
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/openc3/models/model.rb', line 198 def diff(existing) changes = [] new_json = as_json existing.each do |key, old_value| next if key == 'updated_at' new_value = new_json[key] changes << "#{key}: #{old_value} -> #{new_value}" if old_value != new_value end changes end |
#undeploy ⇒ Object
Undo the actions of deploy and remove the model from OpenC3. Subclasses must implement this as by default it is a noop.
169 170 171 |
# File 'lib/openc3/models/model.rb', line 169 def undeploy # empty for a reason end |
#update(force: false, queued: false) ⇒ Object
Alias for create(update: true)
157 158 159 |
# File 'lib/openc3/models/model.rb', line 157 def update(force: false, queued: false) create(update: true, force: force, queued: queued) end |