Class: OpenC3::ToolModel

Inherits:
Model show all
Defined in:
lib/openc3/models/tool_model.rb

Constant Summary collapse

PRIMARY_KEY =
'openc3_tools'

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

#destroy, #destroyed?, filter, find_all_by_plugin, from_json, get_all_models, get_model, set, store, #update

Constructor Details

#initialize(name:, folder_name: nil, icon: '$astro-caution', url: nil, inline_url: nil, window: 'INLINE', category: nil, shown: true, position: nil, updated_at: nil, plugin: nil, needs_dependencies: false, scope:) ⇒ ToolModel

Returns a new instance of ToolModel.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/openc3/models/tool_model.rb', line 112

def initialize(
  name:,
  folder_name: nil,
  icon: '$astro-caution',
  url: nil,
  inline_url: nil,
  window: 'INLINE',
  category: nil,
  shown: true,
  position: nil,
  updated_at: nil,
  plugin: nil,
  needs_dependencies: false,
  scope:
)
  super("#{scope}__#{PRIMARY_KEY}", name: name, plugin: plugin, updated_at: updated_at, scope: scope)
  @folder_name = folder_name
  @icon = icon
  @url = url
  @inline_url = inline_url
  @window = window.to_s.upcase
  @category = category
  @shown = shown
  @position = position

  if @shown and @window == 'INLINE'
    @inline_url = 'js/app.js' unless @inline_url
    @url = "/tools/#{folder_name}" unless @url
  end
  @needs_dependencies = needs_dependencies
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



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

def category
  @category
end

#folder_nameObject

Returns the value of attribute folder_name.



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

def folder_name
  @folder_name
end

#iconObject

Returns the value of attribute icon.



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

def icon
  @icon
end

#inline_urlObject

Returns the value of attribute inline_url.



32
33
34
# File 'lib/openc3/models/tool_model.rb', line 32

def inline_url
  @inline_url
end

#needs_dependenciesObject

Returns the value of attribute needs_dependencies.



37
38
39
# File 'lib/openc3/models/tool_model.rb', line 37

def needs_dependencies
  @needs_dependencies
end

#positionObject

Returns the value of attribute position.



36
37
38
# File 'lib/openc3/models/tool_model.rb', line 36

def position
  @position
end

#shownObject

Returns the value of attribute shown.



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

def shown
  @shown
end

#urlObject

Returns the value of attribute url.



31
32
33
# File 'lib/openc3/models/tool_model.rb', line 31

def url
  @url
end

#windowObject

Returns the value of attribute window.



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

def window
  @window
end

Class Method Details

.all(scope: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openc3/models/tool_model.rb', line 53

def self.all(scope: nil)
  ordered_array = []
  tools = unordered_all(scope: scope)
  tools.each do |_name, tool|
    ordered_array << tool
  end
  ordered_array.sort! { |a, b| a['position'] <=> b['position'] }
  ordered_hash = {}
  ordered_array.each do |tool|
    ordered_hash[tool['name']] = tool
  end
  ordered_hash
end

.all_scopesObject



67
68
69
70
71
72
73
74
75
# File 'lib/openc3/models/tool_model.rb', line 67

def self.all_scopes
  result = {}
  scopes = OpenC3::ScopeModel.all
  scopes.each do |key, _scope|
    tools = unordered_all(scope: key)
    result.merge!(tools)
  end
  result
end

.get(name:, scope: nil) ⇒ Object

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



41
42
43
# File 'lib/openc3/models/tool_model.rb', line 41

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

.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:) ⇒ Object

Called by the PluginModel to allow this class to validate it’s top-level keyword: “TOOL”



78
79
80
81
82
83
84
85
86
87
# File 'lib/openc3/models/tool_model.rb', line 78

def self.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:)
  case keyword
  when 'TOOL'
    parser.verify_num_parameters(2, 2, "TOOL <Folder Name> <Name>")
    return self.new(folder_name: parameters[0], name: parameters[1], plugin: plugin, needs_dependencies: needs_dependencies, scope: scope)
  else
    raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Tool: #{keyword} #{parameters.join(" ")}")
  end
  return nil
end

.names(scope: nil) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/openc3/models/tool_model.rb', line 45

def self.names(scope: nil)
  array = []
  all(scope: scope).each do |name, _tool|
    array << name
  end
  array
end

.set_position(name:, position:, scope:) ⇒ Object

The ToolsTab.vue calls the ToolsController which uses this method to reorder the tools Position is index in the list starting with 0 = first



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/openc3/models/tool_model.rb', line 91

def self.set_position(name:, position:, scope:)
  position = Integer(position)
  next_position = position + 1

  # Go through all the tools and reorder
  all(scope: scope).each do |_tool_name, tool|
    tool_model = from_json(tool, scope: scope)
    # Update the requested model to the new position
    if tool_model.name == name
      tool_model.position = position
    # Move existing tools down in the order
    elsif position > 0 && position >= tool_model.position
      tool_model.position -= 1
    else # Move existing tools up in the order
      tool_model.position = next_position
      next_position += 1
    end
    tool_model.update
  end
end

.unordered_all(scope: nil) ⇒ Object

Returns the list of tools or the default OpenC3 tool set if no tools have been created



254
255
256
257
258
259
260
# File 'lib/openc3/models/tool_model.rb', line 254

def self.unordered_all(scope: nil)
  tools = Store.hgetall("#{scope}__#{PRIMARY_KEY}")
  tools.each do |key, value|
    tools[key] = JSON.parse(value, :allow_nan => true, :create_additions => true)
  end
  return tools
end

Instance Method Details

#as_configObject



174
175
176
177
178
179
180
181
182
183
# File 'lib/openc3/models/tool_model.rb', line 174

def as_config
  result = "TOOL #{@folder_name ? @folder_name : 'nil'} \"#{@name}\"\n"
  result << "  URL #{@url}\n" if @url
  result << "  INLINE_URL #{@inline_url}\n" if @inline_url
  result << "  ICON #{@icon}\n" if @icon
  result << "  WINDOW #{@window}\n" unless @window == 'INLINE'
  result << "  CATEGORY #{@category}\n" if @category
  result << "  SHOWN false\n" unless @shown
  result
end

#as_json(*a) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/openc3/models/tool_model.rb', line 157

def as_json(*a)
  {
    'name' => @name,
    'folder_name' => @folder_name,
    'icon' => @icon,
    'url' => @url,
    'inline_url' => @inline_url,
    'window' => @window,
    'category' => @category,
    'shown' => @shown,
    'position' => @position,
    'updated_at' => @updated_at,
    'plugin' => @plugin,
    'needs_dependencies' => @needs_dependencies,
  }
end

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



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/openc3/models/tool_model.rb', line 144

def create(update: false, force: false)
  unless @position
    tools = self.class.all(scope: @scope)
    _, tool = tools.max_by { |_tool_name, tool| tool['position'] }
    if tool
      @position = tool['position'] + 1
    else
      @position = 0
    end
  end
  super(update: update, force: force)
end

#deploy(gem_path, variables, validate_only: false) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/openc3/models/tool_model.rb', line 212

def deploy(gem_path, variables, validate_only: false)
  return unless @folder_name

  # Ensure tools bucket exists
  OpenC3::S3Utilities.ensure_public_bucket('tools') unless validate_only

  variables["tool_name"] = @name
  start_path = "/tools/#{@folder_name}/"
  Dir.glob(gem_path + start_path + "**/*") do |filename|
    next if filename == '.' or filename == '..' or File.directory?(filename)

    key = filename.split(gem_path + '/tools/')[-1]
    extension = filename.split('.')[-1]
    content_type = Rack::Mime.mime_type(".#{extension}")

    # Load tool files
    data = File.read(filename, mode: "rb")
    data = ERB.new(data, trim_mode: "-").result(binding.set_variables(variables)) if data.is_printable?
    unless validate_only
      cache_control = OpenC3::S3Utilities.get_cache_control(filename)
      Aws::S3::Client.new.put_object(bucket: 'tools', content_type: content_type, cache_control: cache_control, key: key, body: data)
      ConfigTopic.write({ kind: 'created', type: 'tool', name: @folder_name, plugin: @plugin }, scope: @scope)
    end
  end
end

#handle_config(parser, keyword, parameters) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/openc3/models/tool_model.rb', line 185

def handle_config(parser, keyword, parameters)
  case keyword
  when 'URL'
    parser.verify_num_parameters(1, 1, "URL <URL>")
    @url = parameters[0]
  when 'INLINE_URL'
    parser.verify_num_parameters(1, 1, "INLINE_URL <URL>")
    @inline_url = parameters[0]
  when 'ICON'
    parser.verify_num_parameters(1, 1, "ICON <ICON Name>")
    @icon = parameters[0]
  when 'WINDOW'
    parser.verify_num_parameters(1, 1, "WINDOW <INLINE | IFRAME | NEW>")
    @window = parameters[0].to_s.upcase
    raise ConfigParser::Error.new(parser, "Invalid WINDOW setting: #{@window}") unless ['INLINE', 'IFRAME', 'NEW'].include?(@window)
  when 'CATEGORY'
    parser.verify_num_parameters(1, 1, "CATEGORY <Category Name>")
    @category = parameters[0].to_s
  when 'SHOWN'
    parser.verify_num_parameters(1, 1, "SHOWN <true/false>")
    @shown = ConfigParser.handle_true_false(parameters[0])
  else
    raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Tool: #{keyword} #{parameters.join(" ")}")
  end
  return nil
end

#undeployObject



238
239
240
241
242
243
244
245
246
247
# File 'lib/openc3/models/tool_model.rb', line 238

def undeploy
  if @folder_name and @folder_name.to_s.length > 0
    rubys3_client = Aws::S3::Client.new
    prefix = "#{@folder_name}/"
    rubys3_client.list_objects(bucket: 'tools', prefix: prefix).contents.each do |object|
      rubys3_client.delete_object(bucket: 'tools', key: object.key)
      ConfigTopic.write({ kind: 'deleted', type: 'tool', name: @folder_name, plugin: @plugin }, scope: @scope)
    end
  end
end