Commit e859908b by John Doe

cleaned up code

parent 65a0409f
...@@ -12,19 +12,11 @@ class DbAdapter ...@@ -12,19 +12,11 @@ class DbAdapter
# GET extended info stream list # GET extended info stream list
dump = self.class.get("#{@url}/stream/list?extended=1") dump = self.class.get("#{@url}/stream/list?extended=1")
dump.parsed_response.map do |entry| dump.parsed_response.map do |entry|
# TODO: implement global metadata dump, because this is sloooow metadata = __get_metadata(entry[0])
# GET metadata for the stream
dump = self.class.get("#{@url}/stream/get_metadata?path=#{entry[0]}")
metadata = JSON.parse(dump.parsed_response['config_key__'] || '{}')
# The streams are not pure attributes, pull them out # The streams are not pure attributes, pull them out
streams = metadata["streams"] || {} streams = metadata.delete(:streams) || {}
# Add plain-text metadata keys (retrofit for *info streams which keep
# attributes in seperate metadata tags
metadata.merge!(dump.parsed_response.slice("delete_locked",
"description",
"hidden",
"name"))
metadata.symbolize_keys!
# Create the schema: # Create the schema:
# 3 elements: path, attributes, streams # 3 elements: path, attributes, streams
{ path: entry[0], { path: entry[0],
...@@ -34,9 +26,22 @@ class DbAdapter ...@@ -34,9 +26,22 @@ class DbAdapter
end_time: entry[3] || 0, end_time: entry[3] || 0,
total_rows: entry[4], total_rows: entry[4],
total_time: entry[5] total_time: entry[5]
}.merge(metadata.except(:streams)), }.merge(metadata),
streams: streams streams: streams
} }
end end
end end
# retrieve metadata for a particular stream
def __get_metadata(path)
dump = self.class.get("#{@url}/stream/get_metadata?path=#{path}")
metadata = JSON.parse(dump.parsed_response['config_key__'] || '{}')
# Add plain-text metadata keys (retrofit for *info streams which keep
# attributes in seperate metadata tags
metadata.merge!(dump.parsed_response.slice('delete_locked',
'description',
'hidden',
'name'))
metadata.symbolize_keys
end
end end
...@@ -7,5 +7,4 @@ class DbDecimation < ActiveRecord::Base ...@@ -7,5 +7,4 @@ class DbDecimation < ActiveRecord::Base
def as_json(_options = {}) def as_json(_options = {})
super(except: [:created_at, :updated_at]) super(except: [:created_at, :updated_at])
end end
end end
...@@ -6,7 +6,6 @@ class DbFile < ActiveRecord::Base ...@@ -6,7 +6,6 @@ class DbFile < ActiveRecord::Base
has_many :db_streams, dependent: :destroy has_many :db_streams, dependent: :destroy
has_many :db_decimations, dependent: :destroy has_many :db_decimations, dependent: :destroy
def defined_attributes def defined_attributes
[:name, :name_abbrev, :description, :hidden] [:name, :name_abbrev, :description, :hidden]
end end
......
...@@ -8,5 +8,4 @@ class DbStream < ActiveRecord::Base ...@@ -8,5 +8,4 @@ class DbStream < ActiveRecord::Base
def as_json(_options = {}) def as_json(_options = {})
super(except: [:created_at, :updated_at]) super(except: [:created_at, :updated_at])
end end
end end
...@@ -60,9 +60,11 @@ class UpdateDb # rubocop:disable Metrics/ClassLength ...@@ -60,9 +60,11 @@ class UpdateDb # rubocop:disable Metrics/ClassLength
# if this folder has an info stream, find that entry and # if this folder has an info stream, find that entry and
# use its metadata to update the folder's attributes # use its metadata to update the folder's attributes
def __read_info_entry(entries) def __read_info_entry(entries)
info_entry = entries.detect{ |entry| info_entry = entries.detect do |entry|
entry[:chunks] == ['info']} || {} entry[:chunks] == ['info']
end
info_entry ||= {}
# if there is an info entry, remove it from the array # if there is an info entry, remove it from the array
# so we don't process it as a seperate file # so we don't process it as a seperate file
entries.delete(info_entry) entries.delete(info_entry)
...@@ -87,8 +89,8 @@ class UpdateDb # rubocop:disable Metrics/ClassLength ...@@ -87,8 +89,8 @@ class UpdateDb # rubocop:disable Metrics/ClassLength
return @root_folder if parent.nil? return @root_folder if parent.nil?
folder = parent.subfolders.find_by_path(path) folder = parent.subfolders.find_by_path(path)
folder ||= DbFolder.new(parent: parent, path: path) folder ||= DbFolder.new(parent: parent, path: path)
folder.update_attributes(info.slice( folder.update_attributes(
*folder.defined_attributes)) info.slice(*folder.defined_attributes))
folder.save! folder.save!
folder folder
end end
...@@ -139,20 +141,10 @@ class UpdateDb # rubocop:disable Metrics/ClassLength ...@@ -139,20 +141,10 @@ class UpdateDb # rubocop:disable Metrics/ClassLength
# determine if the entry groups constitute a single file # determine if the entry groups constitute a single file
def file?(entry_group) def file?(entry_group)
# if any entry_group has multiple chunks left or if the # if any entry_group has chunks left, this is a folder
# last chunk is 'info', this is a folder entry_group.select { |entry|
folder_entries = entry_group.select { |entry| !entry[:chunks].empty?
entry[:chunks].length > 1 || }.count == 0
entry[:chunks][0] == 'info' }.count
if(folder_entries > 0)
return false
end
# if the path's are the same up to a ~decimXX suffix
# this is a file, otherwise return false
num_files = entry_group.map { |entry|
entry[:path].gsub(decimation_tag, '')
}.uniq.count
num_files == 1
end end
# create or update a DbFile object at the # create or update a DbFile object at the
......
...@@ -4,9 +4,8 @@ ...@@ -4,9 +4,8 @@
# are usually returned by DbAdapter.schema # are usually returned by DbAdapter.schema
class DbSchemaHelper class DbSchemaHelper
# schema data # schema data
# rubocop:disable Metrics/MethodLength
def entry(path, type: 'uint8_1', metadata: {}, stream_count: 0) def entry(path, type: 'uint8_1', metadata: {}, stream_count: 0)
data = { {
path: path, path: path,
attributes: { attributes: {
data_type: type, data_type: type,
...@@ -16,7 +15,6 @@ class DbSchemaHelper ...@@ -16,7 +15,6 @@ class DbSchemaHelper
total_time: 0 }.merge(metadata), total_time: 0 }.merge(metadata),
streams: __build_streams(stream_count) streams: __build_streams(stream_count)
} }
end end
# rubocop:enable Metrics/MethodLength # rubocop:enable Metrics/MethodLength
......
...@@ -9,7 +9,6 @@ RSpec.describe 'DbFile' do ...@@ -9,7 +9,6 @@ RSpec.describe 'DbFile' do
specify { expect(db_file).to respond_to(:description) } specify { expect(db_file).to respond_to(:description) }
specify { expect(db_file).to respond_to(:db_streams) } specify { expect(db_file).to respond_to(:db_streams) }
specify { expect(db_file).to respond_to(:hidden) } specify { expect(db_file).to respond_to(:hidden) }
end end
describe 'remove' do describe 'remove' do
......
...@@ -10,7 +10,6 @@ RSpec.describe 'DbFolder' do ...@@ -10,7 +10,6 @@ RSpec.describe 'DbFolder' do
specify { expect(db_folder).to respond_to(:subfolders) } specify { expect(db_folder).to respond_to(:subfolders) }
specify { expect(db_folder).to respond_to(:db_files) } specify { expect(db_folder).to respond_to(:db_files) }
specify { expect(db_folder).to respond_to(:hidden) } specify { expect(db_folder).to respond_to(:hidden) }
end end
describe 'insert_file' do describe 'insert_file' do
......
...@@ -13,6 +13,5 @@ RSpec.describe 'DbStream' do ...@@ -13,6 +13,5 @@ RSpec.describe 'DbStream' do
specify { expect(db_stream).to respond_to(:offset) } specify { expect(db_stream).to respond_to(:offset) }
specify { expect(db_stream).to respond_to(:plottable) } specify { expect(db_stream).to respond_to(:plottable) }
specify { expect(db_stream).to respond_to(:discrete) } specify { expect(db_stream).to respond_to(:discrete) }
end end
end end
...@@ -114,10 +114,17 @@ describe 'UpdateDb' do ...@@ -114,10 +114,17 @@ describe 'UpdateDb' do
# corner cases # corner cases
describe 'cornercases:' do describe 'cornercases:' do
it 'handles empty folders' do it 'handles empty folders' do
schema = [helper.entry('/folder_lonley/info', metadata: { name: 'lonely'})] schema = [helper.entry('/folder_lonley/info',
metadata: { name: 'lonely' })]
update_with_schema(schema) update_with_schema(schema)
expect(@root.subfolders.find_by_name('lonely')).to be_present expect(@root.subfolders.find_by_name('lonely')).to be_present
end end
it 'handles chains of folders' do
schema = [helper.entry('/fa/fb/data', metadata: { name: 'the_file' })]
update_with_schema(schema)
file = DbFile.find_by_name('the_file')
expect(file.db_folder.parent.parent).to eq(@root)
end
end end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment