Class: OpenC3::WidgetModel

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

Constant Summary collapse

PRIMARY_KEY =
'openc3_widgets'

Instance Attribute Summary collapse

Attributes inherited from Model

#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, from_json, get_all_models, get_model, set, store, store_queued, #update

Constructor Details

#initialize(name:, updated_at: nil, plugin: nil, label: nil, items: false, needs_dependencies: false, disable_erb: nil, scope:) ⇒ WidgetModel

Returns a new instance of WidgetModel.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/openc3/models/widget_model.rb', line 87

def initialize(
  name:,
  updated_at: nil,
  plugin: nil,
  label: nil,
  items: false,
  needs_dependencies: false,
  disable_erb: nil,
  scope:
)
  super("#{scope}__#{PRIMARY_KEY}", name: name, plugin: plugin, updated_at: updated_at, scope: scope)
  @full_name = @name.capitalize + 'Widget'
  @filename = @full_name + '.umd.min.js'
  @bucket_key = 'widgets/' + @full_name + '/' + @filename
  @label = label
  # Ensure items is a boolean because it could be nil
  @items = items ? true : false
  @needs_dependencies = needs_dependencies
  @disable_erb = disable_erb
end

Instance Attribute Details

#bucket_keyObject

Returns the value of attribute bucket_key.



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

def bucket_key
  @bucket_key
end

#disable_erbObject

Returns the value of attribute disable_erb.



38
39
40
# File 'lib/openc3/models/widget_model.rb', line 38

def disable_erb
  @disable_erb
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

#full_nameObject

Returns the value of attribute full_name.



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

def full_name
  @full_name
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#needs_dependenciesObject

Returns the value of attribute needs_dependencies.



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

def needs_dependencies
  @needs_dependencies
end

Class Method Details

.all(scope: nil) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/openc3/models/widget_model.rb', line 54

def self.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

.all_scopesObject



62
63
64
65
66
67
68
69
70
# File 'lib/openc3/models/widget_model.rb', line 62

def self.all_scopes
  result = {}
  scopes = OpenC3::ScopeModel.all
  scopes.each do |key, _scope|
    widgets = all(scope: key)
    result.merge!(widgets)
  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



42
43
44
# File 'lib/openc3/models/widget_model.rb', line 42

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: “WIDGET”



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/openc3/models/widget_model.rb', line 73

def self.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:)
  case keyword
  when 'WIDGET'
    parser.verify_num_parameters(1, 3, "WIDGET <Name> <Label> <Select Items (true/false)>")
    # Label is optional and if it doesn't exist nil is fine
    # Select Items is optional and if it doesn't exist nil is fine
    items = ConfigParser.handle_true_false_nil(parameters[2])
    return self.new(name: parameters[0], plugin: plugin, label: parameters[1], items: items, needs_dependencies: needs_dependencies, scope: scope)
  else
    raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Widget: #{keyword} #{parameters.join(" ")}")
  end
  return nil
end

.names(scope: nil) ⇒ Object



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

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

Instance Method Details

#as_json(*a) ⇒ Object



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

def as_json(*a)
  {
    'name' => @name,
    'updated_at' => @updated_at,
    'plugin' => @plugin,
    'label' => @label,
    'items' => @items,
    'needs_dependencies' => @needs_dependencies,
    'disable_erb' => @disable_erb,
  }
end

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



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
# File 'lib/openc3/models/widget_model.rb', line 133

def deploy(gem_path, variables, validate_only: false)
  # Ensure tools bucket exists
  bucket = nil
  unless validate_only
    bucket = Bucket.getClient()
    bucket.ensure_public(ENV['OPENC3_TOOLS_BUCKET'])
  end

  filename = gem_path + "/tools/widgets/" + @full_name + '/' + @filename

  # Load widget file
  data = File.read(filename, mode: "rb")
  erb_disabled = check_disable_erb(filename)
  unless erb_disabled
    OpenC3.set_working_dir(File.dirname(filename)) do
      data = ERB.new(data.comment_erb(), trim_mode: "-").result(binding.set_variables(variables)) if data.is_printable?
    end
  end
  unless validate_only
    cache_control = BucketUtilities.get_cache_control(@filename)
    # TODO: support widgets that aren't just a single js file (and its associated map file)
    bucket.put_object(bucket: ENV['OPENC3_TOOLS_BUCKET'], content_type: 'application/javascript', cache_control: cache_control, key: @bucket_key, body: data)
    data = File.read(filename + '.map', mode: "rb")
    bucket.put_object(bucket: ENV['OPENC3_TOOLS_BUCKET'], content_type: 'application/json', cache_control: cache_control, key: @bucket_key + '.map', body: data)
  end
end

#handle_config(parser, keyword, parameters) ⇒ Object



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

def handle_config(parser, keyword, parameters)
  case keyword
  when 'DISABLE_ERB'
    # 0 to unlimited parameters
    @disable_erb ||= []
    if parameters
      @disable_erb.concat(parameters)
    end
  else
    raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Widget: #{keyword} #{parameters.join(" ")}")
  end
end

#undeployObject



160
161
162
163
164
165
166
# File 'lib/openc3/models/widget_model.rb', line 160

def undeploy
  bucket = Bucket.getClient()
  bucket.delete_object(bucket: ENV['OPENC3_TOOLS_BUCKET'], key: @bucket_key)
  bucket.delete_object(bucket: ENV['OPENC3_TOOLS_BUCKET'], key: @bucket_key + '.map')
rescue Exception => e
  Logger.error("Error undeploying widget model #{@name} in scope #{@scope} due to #{e}")
end