Commit 4d2b1f7e by John Doe

added dynamic time bounds for requests with no start or end time specified

parent 781975d4
...@@ -36,15 +36,20 @@ class DbAdapter ...@@ -36,15 +36,20 @@ class DbAdapter
} }
end end
def schema def schema(path='')
# GET extended info stream list # GET extended info stream list
begin begin
resp = self.class.get("#{@url}/stream/list?extended=1") if(path.empty?)
resp = self.class.get("#{@url}/stream/list?extended=1")
else
resp = self.class.get("#{@url}/stream/list?path=#{path}&extended=1")
end
return nil unless resp.success? return nil unless resp.success?
rescue rescue
return nil return nil
end end
# if the url exists but is not a nilm... # if the url exists but is not a nilm...
return nil unless resp.parsed_response.respond_to?(:map) return nil unless resp.parsed_response.respond_to?(:map)
resp.parsed_response.map do |entry| resp.parsed_response.map do |entry|
metadata = if entry[0].match(UpdateStream.decimation_tag).nil? metadata = if entry[0].match(UpdateStream.decimation_tag).nil?
...@@ -71,6 +76,25 @@ class DbAdapter ...@@ -71,6 +76,25 @@ class DbAdapter
end end
end end
# return latest info about the specified stream
# TODO: the HTTP API does not
# support wild cards so no decimations are returned
# {
# base_entry: ...,
# decimation_entries: [...]
# }
# this can be fed into UpdateStream service
def stream_info(stream)
entries = schema("#{stream.path}")
base_entry = entries
.select{|e| e[:path].match(UpdateStream.decimation_tag).nil?}
.first
{
base_entry: base_entry,
decimation_entries: entries - [base_entry] #whatever is left over
}
end
def set_folder_metadata(db_folder) def set_folder_metadata(db_folder)
_set_path_metadata("#{db_folder.path}/info", _set_path_metadata("#{db_folder.path}/info",
__build_folder_metadata(db_folder)) __build_folder_metadata(db_folder))
......
...@@ -33,15 +33,6 @@ class DbElementsController < ApplicationController ...@@ -33,15 +33,6 @@ class DbElementsController < ApplicationController
@start_time = @service.start_time @start_time = @service.start_time
@end_time = @service.end_time @end_time = @service.end_time
# update the user's home view if a data view instance is provided
if params[:redux_json]!=nil
service = CreateDataView.new()
service.run({redux_json: params[:redux_json]},
nil, current_user, home_view=true)
unless service.success?
Rails.logger.warn("error creating home view for #{current_user.id}")
end
end
render status: @service.success? ? :ok : :unprocessable_entity render status: @service.success? ? :ok : :unprocessable_entity
end end
end end
...@@ -32,7 +32,23 @@ class LoadElementData ...@@ -32,7 +32,23 @@ class LoadElementData
req_streams << elem.db_stream req_streams << elem.db_stream
end end
end end
#2 compute start and end times if nil #2 compute bounds by updating stream info if start/end are missing
if(start_time==nil || end_time==nil)
req_streams.map do |stream|
adapter = DbAdapter.new(stream.db.url)
entries = adapter.stream_info(stream)
service = UpdateStream.new(
stream,
entries[:base_entry],
entries[:decimation_entries]
)
unless service.run.success?
Rails.logger.warn("Error updating #{stream.name}: #{service.errors}")
end
end
end
#3 compute start and end times if nil
streams_with_data = req_streams.select{|stream| stream.total_time > 0} streams_with_data = req_streams.select{|stream| stream.total_time > 0}
if (start_time == nil || end_time == nil) && streams_with_data.empty? if (start_time == nil || end_time == nil) && streams_with_data.empty?
add_error("no time bounds for requested elements, refresh database?") add_error("no time bounds for requested elements, refresh database?")
...@@ -54,7 +70,7 @@ class LoadElementData ...@@ -54,7 +70,7 @@ class LoadElementData
add_error("invalid time bounds") add_error("invalid time bounds")
return return
end end
#2 pull data from streams #4 pull data from streams
combined_data = [] combined_data = []
req_streams.each do |stream| req_streams.each do |stream|
adapter = DbAdapter.new(stream.db.url) adapter = DbAdapter.new(stream.db.url)
...@@ -73,7 +89,7 @@ class LoadElementData ...@@ -73,7 +89,7 @@ class LoadElementData
add_warning("unable to retrieve data for #{stream.path}") add_warning("unable to retrieve data for #{stream.path}")
end end
end end
#3 extract requested elements from the stream datasets #5 extract requested elements from the stream datasets
req_element_ids = elements.pluck(:id) req_element_ids = elements.pluck(:id)
@data = combined_data.select{|d| req_element_ids.include? d[:id] } @data = combined_data.select{|d| req_element_ids.include? d[:id] }
return self return self
......
...@@ -16,6 +16,14 @@ describe DbAdapter do ...@@ -16,6 +16,14 @@ describe DbAdapter do
end end
end end
it 'retrieves stream specific schema', :vcr do
adapter = DbAdapter.new(url)
entries = adapter.stream_info(create(:db_stream,path:"/tutorial/pump-prep"))
expect(entries[:base_entry][:path]).to eq "/tutorial/pump-prep"
#TODO: support decimation lookup, need HTTP API to process wild cards
expect(entries[:decimation_entries].length).to eq 0
end
describe 'set_stream_metadata' do describe 'set_stream_metadata' do
it 'updates config_key in metadata', :vcr do it 'updates config_key in metadata', :vcr do
adapter = DbAdapter.new(url) adapter = DbAdapter.new(url)
...@@ -102,7 +110,7 @@ describe DbAdapter do ...@@ -102,7 +110,7 @@ describe DbAdapter do
end_time = 1361579612066315 end_time = 1361579612066315
path = '/tutorial/pump-events' path = '/tutorial/pump-events'
intervals = adapter.get_intervals(path,start_time, end_time) intervals = adapter.get_intervals(path,start_time, end_time)
expect(intervals.length).to eq(60) #20 intervals expect(intervals.length).to eq(60) #20 intervals
end end
end end
......
...@@ -92,4 +92,38 @@ RSpec.describe 'LoadElementData' do ...@@ -92,4 +92,38 @@ RSpec.describe 'LoadElementData' do
expect(@mock_stream_service.run_count).to eq 2 expect(@mock_stream_service.run_count).to eq 2
end end
end end
#NOTE: This is really quite a large integration test, it
#builds the full test nilm and then retrieves data from it.
#might be overkill but it really tests out the pipeline :)
#
describe 'when boundary times are not specified' do
let (:url) {'http://localhost:8080/nilmdb'}
let(:user) {create(:user)}
it 'updates the streams', :vcr do
adapter = DbAdapter.new(url)
service = CreateNilm.new
service.run(name: 'test', url: url, owner: user)
db = service.nilm.db
#request data from ac-power (15 Jun 2015 - 27 Jun 2015)
# pump-events (04 Feb 2013 - 23 Feb 2013)
elem1 = DbStream.find_by_path("/tutorial/ac-power").db_elements.first
elem2 = DbStream.find_by_path("/tutorial/pump-events").db_elements.first
#make sure the decimations are messed up by partial update
ndecims1 = elem1.db_stream.db_decimations.count
ndecims2 = elem2.db_stream.db_decimations.count
#artificially mess up time bounds to check if service updates the streams
elem1.db_stream.update(start_time: 0, end_time: 0)
elem2.db_stream.update(start_time: 0, end_time: 0)
service = LoadElementData.new
service.run([elem1,elem2], nil, nil)
#bounds taken from test nilm on vagrant instance
expect(service.start_time).to eq(1360017784000000)
expect(service.end_time).to eq(1435438182000001)
#make sure decimations are still here
expect(elem1.db_stream.reload.db_decimations.count).to eq ndecims1
expect(elem2.db_stream.reload.db_decimations.count).to eq ndecims2
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