Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
wattsworth
/
lumen-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4d2b1f7e
authored
Jun 06, 2017
by
John Doe
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
added dynamic time bounds for requests with no start or end time specified
parent
781975d4
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
86 additions
and
13 deletions
app/adapters/db_adapter.rb
app/controllers/db_elements_controller.rb
app/services/data/load_element_data.rb
spec/adapters/db_adapter_spec.rb
spec/services/data/load_element_data_spec.rb
app/adapters/db_adapter.rb
View file @
4d2b1f7e
...
@@ -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
if
(
path
.
empty?
)
resp
=
self
.
class
.
get
(
"
#{
@url
}
/stream/list?extended=1"
)
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
))
...
...
app/controllers/db_elements_controller.rb
View file @
4d2b1f7e
...
@@ -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
app/services/data/load_element_data.rb
View file @
4d2b1f7e
...
@@ -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
...
...
spec/adapters/db_adapter_spec.rb
View file @
4d2b1f7e
...
@@ -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
)
...
...
spec/services/data/load_element_data_spec.rb
View file @
4d2b1f7e
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment