Class: OpenC3::TimelineModel
- Defined in:
- lib/openc3/models/timeline_model.rb
Constant Summary collapse
- PRIMARY_KEY =
MUST be equal to ActivityModel::PRIMARY_KEY without leading __
'openc3_timelines'.freeze
- KEY =
'__TIMELINE__'.freeze
Instance Attribute Summary
Attributes inherited from Model
#name, #plugin, #scope, #updated_at
Class Method Summary collapse
-
.all ⇒ Array<Hash>
All the Key, Values stored under the name key.
-
.delete(name:, scope:, force: false) ⇒ Object
Remove the sorted set.
-
.from_json(json, name:, scope:) ⇒ TimelineModel
Model generated from the passed JSON.
-
.get(name:, scope:) ⇒ TimelineModel
Return the object with the name at.
-
.names ⇒ Array<String>
All the names stored under the name key.
Instance Method Summary collapse
-
#as_json(*a) ⇒ Hash
Generated from the TimelineModel.
- #deploy ⇒ Object
-
#initialize(name:, scope:, updated_at: nil, color: nil) ⇒ TimelineModel
constructor
A new instance of TimelineModel.
-
#notify(kind:) ⇒ Object
-
update the redis stream / timeline topic that something has changed.
- #undeploy ⇒ Object
- #update_color(color: nil) ⇒ Object
Methods inherited from Model
#check_disable_erb, #create, #destroy, #destroyed?, filter, find_all_by_plugin, get_all_models, get_model, handle_config, set, store, store_queued, #update
Constructor Details
#initialize(name:, scope:, updated_at: nil, color: nil) ⇒ TimelineModel
Returns a new instance of TimelineModel.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/openc3/models/timeline_model.rb', line 77 def initialize(name:, scope:, updated_at: nil, color: nil) if name.nil? || scope.nil? raise TimelineInputError.new "name or scope must not be nil" end super(PRIMARY_KEY, name: "#{scope}#{KEY}#{name}", scope: scope) @updated_at = updated_at @timeline_name = name update_color(color: color) end |
Class Method Details
.all ⇒ Array<Hash>
Returns All the Key, Values stored under the name key.
46 47 48 |
# File 'lib/openc3/models/timeline_model.rb', line 46 def self.all super(PRIMARY_KEY) end |
.delete(name:, scope:, force: false) ⇒ Object
Remove the sorted set.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/openc3/models/timeline_model.rb', line 56 def self.delete(name:, scope:, force: false) key = "#{scope}__#{PRIMARY_KEY}__#{name}" z = Store.zcard(key) if force == false && z > 0 raise TimelineError.new "timeline contains activities, must force remove" end Store.multi do |multi| multi.del(key) multi.hdel(PRIMARY_KEY, "#{scope}#{KEY}#{name}") end return name end |
.from_json(json, name:, scope:) ⇒ TimelineModel
Returns Model generated from the passed JSON.
71 72 73 74 75 |
# File 'lib/openc3/models/timeline_model.rb', line 71 def self.from_json(json, name:, scope:) json = JSON.parse(json, :allow_nan => true, :create_additions => true) if String === json raise "json data is nil" if json.nil? self.new(**json.transform_keys(&:to_sym), name: name, scope: scope) end |
.get(name:, scope:) ⇒ TimelineModel
Return the object with the name at
38 39 40 41 42 43 |
# File 'lib/openc3/models/timeline_model.rb', line 38 def self.get(name:, scope:) json = super(PRIMARY_KEY, name: "#{scope}#{KEY}#{name}") unless json.nil? self.from_json(json, name: name, scope: scope) end end |
.names ⇒ Array<String>
Returns All the names stored under the name key.
51 52 53 |
# File 'lib/openc3/models/timeline_model.rb', line 51 def self.names super(PRIMARY_KEY) end |
Instance Method Details
#as_json(*a) ⇒ Hash
Returns generated from the TimelineModel.
103 104 105 106 107 108 109 110 |
# File 'lib/openc3/models/timeline_model.rb', line 103 def as_json(*a) { 'name' => @timeline_name, 'color' => @color, 'scope' => @scope, 'updated_at' => @updated_at } end |
#deploy ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/openc3/models/timeline_model.rb', line 127 def deploy topics = ["#{@scope}__#{PRIMARY_KEY}"] # Timeline Microservice microservice = MicroserviceModel.new( name: @name, folder_name: nil, cmd: ['ruby', 'timeline_microservice.rb', @name], work_dir: '/openc3/lib/openc3/microservices', options: [], topics: topics, target_names: [], plugin: nil, scope: @scope ) microservice.create notify(kind: 'created') end |
#notify(kind:) ⇒ Object
Returns [] update the redis stream / timeline topic that something has changed.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/openc3/models/timeline_model.rb', line 113 def notify(kind:) notification = { 'data' => JSON.generate(as_json(:allow_nan => true)), 'kind' => kind, 'type' => 'timeline', 'timeline' => @timeline_name } begin TimelineTopic.write_activity(notification, scope: @scope) rescue StandardError => e raise TimelineInputError.new "Failed to write to stream: #{notification}, #{e}" end end |
#undeploy ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/openc3/models/timeline_model.rb', line 145 def undeploy model = MicroserviceModel.get_model(name: @name, scope: @scope) if model model.destroy notify(kind: 'deleted') end end |
#update_color(color: nil) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/openc3/models/timeline_model.rb', line 88 def update_color(color: nil) if color.nil? color = '#%06x' % (rand * 0xffffff) end valid_color = color =~ /[0-9a-fA-F]{6}/ if valid_color.nil? raise RuntimeError.new "invalid color but in hex format. #FF0000" end unless color.start_with?('#') color = "##{color}" end @color = color end |