Skip to content

Commit

Permalink
Improve TableManager definition lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Feb 14, 2025
1 parent 92b60ed commit 05cca85
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
25 changes: 14 additions & 11 deletions openc3-cosmos-cmd-tlm-api/app/models/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# GNU Affero General Public License for more details.

# Modified by OpenC3, Inc.
# All changes Copyright 2022, OpenC3, Inc.
# All changes Copyright 2025, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
Expand Down Expand Up @@ -153,17 +153,20 @@ def self.get_definitions(scope, definition_filename, binary_filename = nil)
target = definition_filename.split('/')[0]
all = OpenC3::TargetFile.all(scope, ['tables'], target: target)
found = false
base_binary = File.basename(binary_filename)
base_binary = File.basename(binary_filename, File.extname(binary_filename))
all.each do |filename|
if filename.include?('config/')
# Config filenames are named like SomethingConfigTable_def.txt
# So strip the _def.txt and see if the binary includes that for a simple match.
# This gets simple things but anything more complex
# and the frontend will have to ask the user to select it.
if base_binary.include?(File.basename(filename).split('_')[0])
found = true
definition_filename = filename
end
next unless filename.include?('config/')
base_def = File.basename(filename, File.extname(filename))
base_def = base_def.sub('_def', '')
if base_binary == base_def
found = true
definition_filename = filename
break # We found an exact match
end
if base_binary.include?(base_def)
found = true
definition_filename = filename
# Don't break because we might find an exact match
end
end
if found
Expand Down
50 changes: 46 additions & 4 deletions openc3-cosmos-cmd-tlm-api/spec/models/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# GNU Affero General Public License for more details.

# Modified by OpenC3, Inc.
# All changes Copyright 2022, OpenC3, Inc.
# All changes Copyright 2025, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
Expand Down Expand Up @@ -42,13 +42,13 @@
end
end
@s3_miss = false
count = 0
@count = 0
allow(@s3).to receive(:get_object) do |args|
if args[:key].include?('nope')
raise Aws::S3::Errors::NoSuchKey.new('context','message')
else
if @s3_miss and count < @s3_miss and args[:key].include?('config')
count += 1
if @s3_miss and @count < @s3_miss and args[:key].include?('config')
@count += 1
nil
else
@get_object
Expand Down Expand Up @@ -342,4 +342,46 @@ def add_table(table_name)
expect { Table.load('DEFAULT', 'INST/tables/bin/ConfigTable2.bin', 'INST/tables/config/ConfigTable_def.txt') }.to raise_error(Table::NotFound, "Definition file 'INST/tables/config/ConfigTable_def.txt' not found")
end
end

describe "get_definitions" do
it "looks up a definition file based on a binary filename" do
add_table('DEFAULT/targets/INST/tables/config/table_data.txt')
add_table('DEFAULT/targets/INST/tables/config/table_binary.txt')
add_table('DEFAULT/targets/INST/tables/config/table_data_2.txt')
@s3_miss = 2 # Cause S3 to miss the original and modified versions
@get_object.body = OpenStruct.new
@get_object.body.read = 'definition'
root_definition, definition_filename = Table.get_definitions('DEFAULT', 'INST/tables/config/table_data.txt', 'INST/tables/bin/table_data.bin')
expect(definition_filename).to eql 'INST/tables/config/table_data.txt'
@count = 0
root_definition, definition_filename = Table.get_definitions('DEFAULT', 'INST/tables/config/table_data.txt', 'INST/tables/bin/table_data_2.bin')
expect(definition_filename).to eql 'INST/tables/config/table_data_2.txt'
@count = 0
root_definition, definition_filename = Table.get_definitions('DEFAULT', 'INST/tables/config/table_data.txt', 'INST/tables/bin/table_binary.bin')
expect(definition_filename).to eql 'INST/tables/config/table_binary.txt'
@count = 0
root_definition, definition_filename = Table.get_definitions('DEFAULT', 'INST/tables/config/table_data.txt', 'INST/tables/bin/table_binary_additional.bin')
expect(definition_filename).to eql 'INST/tables/config/table_binary.txt'
end

it "looks up a definition file with _def based on a binary filename" do
add_table('DEFAULT/targets/INST/tables/config/table_data_def.txt')
add_table('DEFAULT/targets/INST/tables/config/table_binary_def.txt')
add_table('DEFAULT/targets/INST/tables/config/table_data_2_def.txt')
@s3_miss = 2 # Cause S3 to miss the original and modified versions
@get_object.body = OpenStruct.new
@get_object.body.read = 'definition'
root_definition, definition_filename = Table.get_definitions('DEFAULT', 'INST/tables/config/table_data.txt', 'INST/tables/bin/table_data.tbl')
expect(definition_filename).to eql 'INST/tables/config/table_data_def.txt'
@count = 0
root_definition, definition_filename = Table.get_definitions('DEFAULT', 'INST/tables/config/table_data.txt', 'INST/tables/bin/table_data_2.tbl')
expect(definition_filename).to eql 'INST/tables/config/table_data_2_def.txt'
@count = 0
root_definition, definition_filename = Table.get_definitions('DEFAULT', 'INST/tables/config/table_data.txt', 'INST/tables/bin/table_binary.tbl')
expect(definition_filename).to eql 'INST/tables/config/table_binary_def.txt'
@count = 0
root_definition, definition_filename = Table.get_definitions('DEFAULT', 'INST/tables/config/table_data.txt', 'INST/tables/bin/table_binary_additional.tbl')
expect(definition_filename).to eql 'INST/tables/config/table_binary_def.txt'
end
end
end

0 comments on commit 05cca85

Please sign in to comment.