Class: OpenC3::TimelineModel

Inherits:
Model show all
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 collapse

Attributes inherited from Model

#name, #plugin, #scope, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

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, shard: 0, execute: true) ⇒ TimelineModel

Returns a new instance of TimelineModel.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openc3/models/timeline_model.rb', line 80

def initialize(name:, scope:, updated_at: nil, color: nil, shard: 0, execute: true)
  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
  @shard = shard.to_i # to_i to handle nil
  self.color = color
  self.execute = execute
end

Instance Attribute Details

#executeObject

Returns the value of attribute execute.



35
36
37
# File 'lib/openc3/models/timeline_model.rb', line 35

def execute
  @execute
end

Class Method Details

.allArray<Hash>

Returns All the Key, Values stored under the name key.

Returns:

  • (Array<Hash>)

    All the Key, Values stored under the name key



49
50
51
# File 'lib/openc3/models/timeline_model.rb', line 49

def self.all
  super(PRIMARY_KEY)
end

.delete(name:, scope:, force: false) ⇒ Object

Remove the sorted set.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/openc3/models/timeline_model.rb', line 59

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.

Returns:



74
75
76
77
78
# File 'lib/openc3/models/timeline_model.rb', line 74

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

Returns:



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

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

.namesArray<String>

Returns All the names stored under the name key.

Returns:

  • (Array<String>)

    All the names stored under the name key



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

def self.names
  super(PRIMARY_KEY)
end

Instance Method Details

#as_json(*a) ⇒ Hash

Returns generated from the TimelineModel.

Returns:

  • (Hash)

    generated from the TimelineModel



109
110
111
112
113
114
115
116
117
118
# File 'lib/openc3/models/timeline_model.rb', line 109

def as_json(*a)
  {
    'name' => @timeline_name,
    'color' => @color,
    'execute' => @execute,
    'shard' => @shard,
    'scope' => @scope,
    'updated_at' => @updated_at
  }
end

#color=(color) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/openc3/models/timeline_model.rb', line 93

def color=(color)
  if color.nil?
    color = '#%06x' % (rand * 0xffffff)
  end
  unless color =~ /#?([0-9a-fA-F]{6})/
    raise TimelineInputError.new "invalid color, must be in hex format, e.g. #FF0000"
  end
  color = "##{color}" unless color.start_with?('#')
  @color = color
end

#deployObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/openc3/models/timeline_model.rb', line 135

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-enterprise/lib/openc3-enterprise/microservices',
    options: [],
    topics: topics,
    target_names: [],
    plugin: nil,
    shard: @shard,
    scope: @scope
  )
  microservice.create
  notify(kind: 'created')
end

#notify(kind:) ⇒ Object

Returns [] update the redis stream / timeline topic that something has changed.

Returns:

  • update the redis stream / timeline topic that something has changed



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/openc3/models/timeline_model.rb', line 121

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

#undeployObject



154
155
156
157
158
159
160
# File 'lib/openc3/models/timeline_model.rb', line 154

def undeploy
  model = MicroserviceModel.get_model(name: @name, scope: @scope)
  if model
    model.destroy
    notify(kind: 'deleted')
  end
end