Class: OpenC3::ToolModel
Constant Summary collapse
- PRIMARY_KEY =
'openc3_tools'- TOOL_SCOPE =
Tools are global to the entire COSMOS installation and are always stored in the DEFAULT scope. The frontend navigation always requests the tool list from DEFAULT, so a tool installed into any other scope would be inaccessible. Every scope parameter in this class is therefore ignored and replaced with TOOL_SCOPE.
'DEFAULT'
Instance Attribute Summary collapse
-
#category ⇒ Object
Returns the value of attribute category.
-
#disable_erb ⇒ Object
Returns the value of attribute disable_erb.
-
#folder_name ⇒ Object
Returns the value of attribute folder_name.
-
#icon ⇒ Object
Returns the value of attribute icon.
-
#import_map_items ⇒ Object
Returns the value of attribute import_map_items.
-
#inline_url ⇒ Object
Returns the value of attribute inline_url.
-
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
-
#position ⇒ Object
Returns the value of attribute position.
-
#shown ⇒ Object
Returns the value of attribute shown.
-
#url ⇒ Object
Returns the value of attribute url.
-
#window ⇒ Object
Returns the value of attribute window.
Attributes inherited from Model
#name, #plugin, #scope, #updated_at
Class Method Summary collapse
-
.all(scope: nil) ⇒ Object
NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE.
-
.all_scopes ⇒ Object
Tools only exist in the DEFAULT scope.
-
.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 NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE.
-
.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".
-
.names(scope: nil) ⇒ Object
NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE.
-
.set_position(name:, position:, scope: nil) ⇒ 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 NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE.
-
.unordered_all(scope: nil) ⇒ Object
Returns the list of tools or the default OpenC3 tool set if no tools have been created NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE.
Instance Method Summary collapse
- #as_json(*a) ⇒ Object
- #create(update: false, force: false, queued: false) ⇒ Object
- #deploy(gem_path, variables, validate_only: false) ⇒ Object
- #handle_config(parser, keyword, parameters) ⇒ Object
-
#initialize(name:, folder_name: nil, icon: 'astro:warning', url: nil, inline_url: nil, window: 'INLINE', category: nil, shown: true, position: nil, updated_at: nil, plugin: nil, needs_dependencies: false, disable_erb: nil, import_map_items: nil, scope: nil) ⇒ ToolModel
constructor
A new instance of ToolModel.
- #undeploy ⇒ Object
Methods inherited from Model
#check_disable_erb, #destroy, #destroyed?, #diff, filter, find_all_by_plugin, from_json, get_all_models, get_model, set, store, store_queued, #update
Constructor Details
#initialize(name:, folder_name: nil, icon: 'astro:warning', url: nil, inline_url: nil, window: 'INLINE', category: nil, shown: true, position: nil, updated_at: nil, plugin: nil, needs_dependencies: false, disable_erb: nil, import_map_items: nil, scope: nil) ⇒ ToolModel
Returns a new instance of ToolModel.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/openc3/models/tool_model.rb', line 131 def initialize( name:, folder_name: nil, icon: 'astro:warning', url: nil, inline_url: nil, window: 'INLINE', category: nil, shown: true, position: nil, updated_at: nil, plugin: nil, needs_dependencies: false, disable_erb: nil, import_map_items: nil, scope: nil # NOTE: deprecated since 7.3.0 as tools are always created in the DEFAULT scope, see TOOL_SCOPE ) # Tools are always created in the DEFAULT scope regardless of the scope # the plugin is being installed into, see TOOL_SCOPE super("#{TOOL_SCOPE}__#{PRIMARY_KEY}", name: name, plugin: plugin, updated_at: updated_at, scope: TOOL_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 = 'main.js' unless @inline_url @url = "/tools/#{folder_name}" unless @url end @needs_dependencies = needs_dependencies @disable_erb = disable_erb @import_map_items = import_map_items end |
Instance Attribute Details
#category ⇒ Object
Returns the value of attribute category.
40 41 42 |
# File 'lib/openc3/models/tool_model.rb', line 40 def category @category end |
#disable_erb ⇒ Object
Returns the value of attribute disable_erb.
44 45 46 |
# File 'lib/openc3/models/tool_model.rb', line 44 def disable_erb @disable_erb end |
#folder_name ⇒ Object
Returns the value of attribute folder_name.
35 36 37 |
# File 'lib/openc3/models/tool_model.rb', line 35 def folder_name @folder_name end |
#icon ⇒ Object
Returns the value of attribute icon.
36 37 38 |
# File 'lib/openc3/models/tool_model.rb', line 36 def icon @icon end |
#import_map_items ⇒ Object
Returns the value of attribute import_map_items.
45 46 47 |
# File 'lib/openc3/models/tool_model.rb', line 45 def import_map_items @import_map_items end |
#inline_url ⇒ Object
Returns the value of attribute inline_url.
38 39 40 |
# File 'lib/openc3/models/tool_model.rb', line 38 def inline_url @inline_url end |
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
43 44 45 |
# File 'lib/openc3/models/tool_model.rb', line 43 def needs_dependencies @needs_dependencies end |
#position ⇒ Object
Returns the value of attribute position.
42 43 44 |
# File 'lib/openc3/models/tool_model.rb', line 42 def position @position end |
#shown ⇒ Object
Returns the value of attribute shown.
41 42 43 |
# File 'lib/openc3/models/tool_model.rb', line 41 def shown @shown end |
#url ⇒ Object
Returns the value of attribute url.
37 38 39 |
# File 'lib/openc3/models/tool_model.rb', line 37 def url @url end |
#window ⇒ Object
Returns the value of attribute window.
39 40 41 |
# File 'lib/openc3/models/tool_model.rb', line 39 def window @window end |
Class Method Details
.all(scope: nil) ⇒ Object
NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/openc3/models/tool_model.rb', line 64 def self.all(scope: nil) ordered_array = [] tools = unordered_all() 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_scopes ⇒ Object
Tools only exist in the DEFAULT scope. Kept for backwards compatibility with the ToolsController importmap endpoint.
80 81 82 |
# File 'lib/openc3/models/tool_model.rb', line 80 def self.all_scopes unordered_all() 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 NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE
50 51 52 |
# File 'lib/openc3/models/tool_model.rb', line 50 def self.get(name:, scope: nil) super("#{TOOL_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"
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/openc3/models/tool_model.rb', line 85 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>") # NOTE: scope is intentionally ignored, see TOOL_SCOPE return self.new(folder_name: parameters[0], name: parameters[1], plugin: plugin, needs_dependencies: needs_dependencies, scope: TOOL_SCOPE) else raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Tool: #{keyword} #{parameters.join(" ")}") end return nil end |
.names(scope: nil) ⇒ Object
NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE
55 56 57 58 59 60 61 |
# File 'lib/openc3/models/tool_model.rb', line 55 def self.names(scope: nil) array = [] all().each do |name, _tool| array << name end array end |
.set_position(name:, position:, scope: nil) ⇒ 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 NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/openc3/models/tool_model.rb', line 100 def self.set_position(name:, position:, scope: nil) tool_scope = TOOL_SCOPE moving = from_json(get(name: name, scope: tool_scope), scope: tool_scope) old_pos = moving.position new_pos = position direction = :down if (old_pos == new_pos) return # we're not doing anything elsif (new_pos > old_pos) direction = :up end # Go through all the tools and reorder all(scope: tool_scope).each do |_tool_name, tool| tool_model = from_json(tool, scope: tool_scope) # Update the requested model to the new position if tool_model.name == name tool_model.position = position elsif direction == :down if tool_model.position >= new_pos && tool_model.position < old_pos tool_model.position += 1 end else # up if tool_model.position > old_pos && tool_model.position <= new_pos tool_model.position -= 1 end 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 NOTE: scope is deprecated since 7.3.0 as tools always live in the DEFAULT scope, see TOOL_SCOPE
324 325 326 327 328 329 330 |
# File 'lib/openc3/models/tool_model.rb', line 324 def self.unordered_all(scope: nil) tools = Store.hgetall("#{TOOL_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_json(*a) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/openc3/models/tool_model.rb', line 201 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, 'disable_erb' => @disable_erb, 'import_map_items' => @import_map_items, } end |
#create(update: false, force: false, queued: false) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/openc3/models/tool_model.rb', line 169 def create(update: false, force: false, queued: false) tools = self.class.all() # Make sure a tool with this folder_name doesn't already exist # NOTE: Tools are global (TOOL_SCOPE) so this check is across all scopes unless update if @folder_name tools.each do |_tool_name, tool| if tool['folder_name'] == @folder_name raise "Tool with folder_name #{@folder_name} already exists at create. Tools are global to the entire COSMOS installation and can only be installed once." end end end end # Autoset tool position unless @position _, tool = tools.max_by { |_tool_name, tool| tool['position'] } if tool @position = tool['position'] + 1 else @position = 0 end end if @url and !@url.start_with?('/') and @url !~ URI::RFC2396_PARSER.make_regexp raise "URL must be a full URL (http://domain.com/path) or a relative path (/path)" end super(update: update, force: force, queued: queued) end |
#deploy(gem_path, variables, validate_only: false) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/openc3/models/tool_model.rb', line 260 def deploy(gem_path, variables, validate_only: false) return unless @folder_name variables["tool_name"] = @name start_path = "/tools/#{@folder_name}/" # Sort files so dependencies are uploaded before dependents: # fonts first, then CSS, then index.html last (it triggers all other loads) filenames = Dir.glob(gem_path + start_path + "**/*") filenames.reject! { |f| f == '.' or f == '..' or File.directory?(f) } filenames.sort_by! do |filename| if filename.include?('/fonts/') [0, filename] elsif filename.include?('/css/') [1, filename] elsif File.basename(filename) == 'index.html' [3, filename] else [2, filename] end end filenames.each do |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") erb_disabled = check_disable_erb(filename) unless erb_disabled data = ERB.new(data.comment_erb(), trim_mode: "-").result(binding.set_variables(variables)) if data.is_printable? end unless validate_only client = Bucket.getClient() cache_control = BucketUtilities.get_cache_control(filename) client.put_object(bucket: ENV['OPENC3_TOOLS_BUCKET'], 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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/openc3/models/tool_model.rb', line 220 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 = ConfigParser.handle_nil(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 | SAME | NEW>") @window = parameters[0].to_s.upcase raise ConfigParser::Error.new(parser, "Invalid WINDOW setting: #{@window}") unless ['INLINE', 'IFRAME', 'SAME', '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]) when 'POSITION' parser.verify_num_parameters(1, 1, "POSITION <value>") @position = parameters[0].to_f when 'DISABLE_ERB' # 0 to unlimited parameters @disable_erb ||= [] if parameters @disable_erb.concat(parameters) end when 'IMPORT_MAP_ITEM' parser.verify_num_parameters(2, 2, "IMPORT_MAP_ITEM <key> <value>") @import_map_items ||= [] @import_map_items << [parameters[0], parameters[1]] else raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Tool: #{keyword} #{parameters.join(" ")}") end return nil end |
#undeploy ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/openc3/models/tool_model.rb', line 300 def undeploy if @folder_name and @folder_name.to_s.length > 0 bucket = Bucket.getClient prefix = "#{@folder_name}/" objects = bucket.list_objects(bucket: ENV['OPENC3_TOOLS_BUCKET'], prefix: prefix) keys = objects.map(&:key) if keys.length > 0 # Batch delete in chunks of 1000 (S3 limit) keys.each_slice(1000) do |key_batch| bucket.delete_objects(bucket: ENV['OPENC3_TOOLS_BUCKET'], keys: key_batch) end ConfigTopic.write({ kind: 'deleted', type: 'tool', name: @folder_name, plugin: @plugin }, scope: @scope) end end rescue Exception => e Logger.error("Error undeploying tool model #{@name} in scope #{@scope} due to #{e}") end |