Class: Cosmos::TimelineModel
- Defined in:
- lib/cosmos/models/timeline_model.rb
Constant Summary collapse
- PRIMARY_KEY =
MUST be equal to ActivityModel::PRIMARY_KEY without leading __
'cosmos_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 ⇒ 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
#as_config, #create, #destroy, filter, find_all_by_plugin, get_all_models, get_model, handle_config, set, #update
Constructor Details
#initialize(name:, scope:, updated_at: nil, color: nil) ⇒ TimelineModel
Returns a new instance of TimelineModel.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cosmos/models/timeline_model.rb', line 76 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.
43 44 45 |
# File 'lib/cosmos/models/timeline_model.rb', line 43 def self.all super(PRIMARY_KEY) end |
.delete(name:, scope:, force: false) ⇒ Object
Remove the sorted set.
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cosmos/models/timeline_model.rb', line 53 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.
68 69 70 71 72 73 74 |
# File 'lib/cosmos/models/timeline_model.rb', line 68 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:) ⇒ TimelineModel
Return the object with the name at
35 36 37 38 39 40 |
# File 'lib/cosmos/models/timeline_model.rb', line 35 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.
48 49 50 |
# File 'lib/cosmos/models/timeline_model.rb', line 48 def self.names super(PRIMARY_KEY) end |
Instance Method Details
#as_json ⇒ Hash
Returns generated from the TimelineModel.
103 104 105 106 107 108 109 110 |
# File 'lib/cosmos/models/timeline_model.rb', line 103 def as_json { '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 144 |
# File 'lib/cosmos/models/timeline_model.rb', line 127 def deploy topics = ["#{@scope}__#{PRIMARY_KEY}"] TimelineTopic.initialize_streams(topics) # Timeline Microservice microservice = MicroserviceModel.new( name: @name, folder_name: nil, cmd: ['ruby', 'timeline_microservice.rb', @name], work_dir: '/cosmos/lib/cosmos/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/cosmos/models/timeline_model.rb', line 113 def notify(kind:) notification = { 'data' => JSON.generate(as_json()), '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
146 147 148 149 150 151 152 |
# File 'lib/cosmos/models/timeline_model.rb', line 146 def undeploy model = MicroserviceModel.get_model(name: @name, scope: @scope) if model model.destroy notify(kind: 'deleted') end end |
#update_color(color: nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cosmos/models/timeline_model.rb', line 87 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 RuntimeError.new "invalid color but in hex format. #FF0000" end unless color.start_with?('#') color = "##{color}" end @color = color end |