Commit e859908b by John Doe

cleaned up code

parent 65a0409f
......@@ -12,19 +12,11 @@ class DbAdapter
# GET extended info stream list
dump = self.class.get("#{@url}/stream/list?extended=1")
dump.parsed_response.map do |entry|
# TODO: implement global metadata dump, because this is sloooow
# GET metadata for the stream
dump = self.class.get("#{@url}/stream/get_metadata?path=#{entry[0]}")
metadata = JSON.parse(dump.parsed_response['config_key__'] || '{}')
metadata = __get_metadata(entry[0])
# The streams are not pure attributes, pull them out
streams = metadata["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!
streams = metadata.delete(:streams) || {}
# Create the schema:
# 3 elements: path, attributes, streams
{ path: entry[0],
......@@ -34,9 +26,22 @@ class DbAdapter
end_time: entry[3] || 0,
total_rows: entry[4],
total_time: entry[5]
}.merge(metadata.except(:streams)),
}.merge(metadata),
streams: streams
}
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
......@@ -7,5 +7,4 @@ class DbDecimation < ActiveRecord::Base
def as_json(_options = {})
super(except: [:created_at, :updated_at])
end
end
......@@ -6,7 +6,6 @@ class DbFile < ActiveRecord::Base
has_many :db_streams, dependent: :destroy
has_many :db_decimations, dependent: :destroy
def defined_attributes
[:name, :name_abbrev, :description, :hidden]
end
......
......@@ -8,5 +8,4 @@ class DbStream < ActiveRecord::Base
def as_json(_options = {})
super(except: [:created_at, :updated_at])
end
end
......@@ -61,8 +61,10 @@ class UpdateDb # rubocop:disable Metrics/ClassLength
# if this folder has an info stream, find that entry and
# use its metadata to update the folder's attributes
def __read_info_entry(entries)
info_entry = entries.detect{ |entry|
entry[:chunks] == ['info']} || {}
info_entry = entries.detect do |entry|
entry[:chunks] == ['info']
end
info_entry ||= {}
# if there is an info entry, remove it from the array
# so we don't process it as a seperate file
entries.delete(info_entry)
......@@ -87,8 +89,8 @@ class UpdateDb # rubocop:disable Metrics/ClassLength
return @root_folder if parent.nil?
folder = parent.subfolders.find_by_path(path)
folder ||= DbFolder.new(parent: parent, path: path)
folder.update_attributes(info.slice(
*folder.defined_attributes))
folder.update_attributes(
info.slice(*folder.defined_attributes))
folder.save!
folder
end
......@@ -139,20 +141,10 @@ class UpdateDb # rubocop:disable Metrics/ClassLength
# determine if the entry groups constitute a single file
def file?(entry_group)
# if any entry_group has multiple chunks left or if the
# last chunk is 'info', this is a folder
folder_entries = entry_group.select { |entry|
entry[:chunks].length > 1 ||
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
# if any entry_group has chunks left, this is a folder
entry_group.select { |entry|
!entry[:chunks].empty?
}.count == 0
end
# create or update a DbFile object at the
......
......@@ -4,9 +4,8 @@
# are usually returned by DbAdapter.schema
class DbSchemaHelper
# schema data
# rubocop:disable Metrics/MethodLength
def entry(path, type: 'uint8_1', metadata: {}, stream_count: 0)
data = {
{
path: path,
attributes: {
data_type: type,
......@@ -16,7 +15,6 @@ class DbSchemaHelper
total_time: 0 }.merge(metadata),
streams: __build_streams(stream_count)
}
end
# rubocop:enable Metrics/MethodLength
......
......@@ -9,7 +9,6 @@ RSpec.describe 'DbFile' do
specify { expect(db_file).to respond_to(:description) }
specify { expect(db_file).to respond_to(:db_streams) }
specify { expect(db_file).to respond_to(:hidden) }
end
describe 'remove' do
......
......@@ -10,7 +10,6 @@ RSpec.describe 'DbFolder' do
specify { expect(db_folder).to respond_to(:subfolders) }
specify { expect(db_folder).to respond_to(:db_files) }
specify { expect(db_folder).to respond_to(:hidden) }
end
describe 'insert_file' do
......
......@@ -13,6 +13,5 @@ RSpec.describe 'DbStream' do
specify { expect(db_stream).to respond_to(:offset) }
specify { expect(db_stream).to respond_to(:plottable) }
specify { expect(db_stream).to respond_to(:discrete) }
end
end
......@@ -114,10 +114,17 @@ describe 'UpdateDb' do
# corner cases
describe 'cornercases:' 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)
expect(@root.subfolders.find_by_name('lonely')).to be_present
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
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