Class: OpenC3::MicroserviceStatusModel

Inherits:
Model show all
Includes:
DbShardedModel
Defined in:
lib/openc3/models/microservice_status_model.rb

Constant Summary collapse

PRIMARY_KEY =
'openc3_microservice_status'

Instance Attribute Summary collapse

Attributes inherited from Model

#name, #plugin, #scope, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DbShardedModel

#_db_sharded_create, #_db_sharded_destroy, included

Methods inherited from Model

#check_disable_erb, #deploy, #destroyed?, #diff, filter, find_all_by_plugin, from_json, get_all_models, get_model, handle_config, set, store, store_queued, #undeploy, #update

Constructor Details

#initialize(name:, state: nil, count: 0, error: nil, custom: nil, updated_at: nil, plugin: nil, scope:) ⇒ MicroserviceStatusModel

Returns a new instance of MicroserviceStatusModel.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/openc3/models/microservice_status_model.rb', line 62

def initialize(
  name:,
  state: nil,
  count: 0,
  error: nil,
  custom: nil,
  updated_at: nil,
  plugin: nil,
  scope:
)
  super("#{scope}__#{PRIMARY_KEY}", name: name, updated_at: updated_at, plugin: plugin, scope: scope)
  @state = state
  @count = count
  @error = error
  @custom = custom
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



28
29
30
# File 'lib/openc3/models/microservice_status_model.rb', line 28

def count
  @count
end

#customObject

Returns the value of attribute custom.



30
31
32
# File 'lib/openc3/models/microservice_status_model.rb', line 30

def custom
  @custom
end

#errorObject

Returns the value of attribute error.



29
30
31
# File 'lib/openc3/models/microservice_status_model.rb', line 29

def error
  @error
end

#stateObject

Returns the value of attribute state.



27
28
29
# File 'lib/openc3/models/microservice_status_model.rb', line 27

def state
  @state
end

Class Method Details

._collect_db_shards(scope:) ⇒ Object

Collect all unique db_shard values from MicroserviceModels.



39
40
41
42
43
44
45
46
# File 'lib/openc3/models/microservice_status_model.rb', line 39

def self._collect_db_shards(scope:)
  db_shards = Set.new([0])
  Store.hgetall('openc3_microservices').each do |name, json|
    next if scope and name.split("__")[0] != scope
    db_shards << (JSON.parse(json, allow_nan: true, create_additions: true)['db_shard'] || 0).to_i
  end
  db_shards
end

._lookup_db_shard(name, scope:) ⇒ Object

Look up db_shard from the corresponding MicroserviceModel.



33
34
35
36
# File 'lib/openc3/models/microservice_status_model.rb', line 33

def self._lookup_db_shard(name, scope:) # NOSONAR
  json = Store.hget('openc3_microservices', name)
  json ? (JSON.parse(json, allow_nan: true, create_additions: true)['db_shard'] || 0).to_i : 0
end

.all(scope:) ⇒ Object



58
59
60
# File 'lib/openc3/models/microservice_status_model.rb', line 58

def self.all(scope:)
  _db_sharded_all("#{scope}__#{PRIMARY_KEY}", scope: scope)
end

.get(name:, scope:) ⇒ Object

NOTE: The following three class methods are used by the ModelController and are reimplemented to enable various Model class methods to work



50
51
52
# File 'lib/openc3/models/microservice_status_model.rb', line 50

def self.get(name:, scope:)
  _db_sharded_get("#{scope}__#{PRIMARY_KEY}", name: name, scope: scope)
end

.names(scope:) ⇒ Object



54
55
56
# File 'lib/openc3/models/microservice_status_model.rb', line 54

def self.names(scope:)
  _db_sharded_names("#{scope}__#{PRIMARY_KEY}", scope: scope)
end

Instance Method Details

#as_json(*a) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/openc3/models/microservice_status_model.rb', line 87

def as_json(*a)
  {
    'name' => @name,
    'state' => @state,
    'count' => @count,
    'error' => @error.as_json(),
    'custom' => @custom.as_json(),
    'plugin' => @plugin,
    'updated_at' => @updated_at
  }
end

#create(update: false, force: false, queued: false, isoformat: false) ⇒ Object



79
80
81
# File 'lib/openc3/models/microservice_status_model.rb', line 79

def create(update: false, force: false, queued: false, isoformat: false)
  _db_sharded_create(self.class._db_shard_for_name(@name, scope: @scope, use_cache: true), update: update, force: force, queued: queued, isoformat: isoformat)
end

#destroyObject



83
84
85
# File 'lib/openc3/models/microservice_status_model.rb', line 83

def destroy
  _db_sharded_destroy(self.class._db_shard_for_name(@name, scope: @scope))
end