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
f8eb4299
authored
Jul 10, 2018
by
John Doe
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
added tests for node adapter factory
parent
e31a3af2
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
145 additions
and
68 deletions
app/adapters/joule/adapter.rb
app/services/joule_modules/update_joule_modules.rb → app/adapters/joule/update_modules.rb
app/adapters/nilmdb/update_db.rb
app/adapters/node_adapter_factory.rb
app/views/nilms/show.json.jbuilder
config/environments/production.rb
config/environments/test.rb
spec/services/joule_modules/update_joule_modules_spec.rb → spec/adapters/joule/update_modules_spec.rb
spec/adapters/node_adapter_factory_spec.rb
spec/controllers/nilms_controller_spec.rb
app/adapters/joule/adapter.rb
View file @
f8eb4299
module
Joule
class
Adapter
def
initialize
(
url
)
@backend
=
Backend
.
new
(
url
)
end
def
refresh
(
nilm
)
db_service
=
UpdateDb
.
new
(
db:
nilm
.
db
)
result
=
StubService
.
new
result
.
absorb_status
(
db_service
.
run
(
@backend
.
dbinfo
,
@backend
.
schema
))
module_service
=
UpdateModules
.
new
(
nilm
)
result
.
absorb_status
(
module_service
.
run
(
@backend
.
module_info
))
result
end
def
refresh_stream
(
db_stream
)
data
=
@backend
.
stream_info
(
db_stream
)
service
=
UpdateStream
.
new
(
db_stream
,
data
)
service
.
run
end
def
save_stream
(
db_stream
)
@backend
.
update_stream
(
db_stream
)
end
def
save_folder
(
db_folder
)
@backend
.
update_folder
(
db_folder
)
end
def
load_data
(
db_stream
,
start_time
,
end_time
,
elements
=
[],
resolution
=
nil
)
data_service
=
LoadStreamData
.
new
(
@backend
)
data_service
.
run
(
db_stream
,
start_time
,
end_time
,
elements
,
resolution
)
unless
data_service
.
success?
return
nil
end
{
data:
data_service
.
data
,
decimation_factor:
data_service
.
decimation_factor
}
end
def
node_type
'joule'
end
...
...
app/
services/joule_modules/update_joul
e_modules.rb
→
app/
adapters/joule/updat
e_modules.rb
View file @
f8eb4299
# frozen_string_literal: true
module
Joule
# Handles construction of database objects
class
UpdateJouleModules
include
ServiceStatus
# Handles construction of database objects
class
UpdateJouleModules
include
ServiceStatus
def
initialize
(
nilm
)
super
()
@nilm
=
nilm
end
def
initialize
(
nilm
)
super
()
@nilm
=
nilm
end
def
run
(
module_info
)
#module_info as returned by JouleBackend
if
module_info
.
nil?
add_error
(
"unable to retrieve module information"
)
return
self
end
#remove the previous modules
@nilm
.
joule_modules
.
destroy_all
module_info
.
each
do
|
info
|
@nilm
.
joule_modules
<<
_build_module
(
info
)
end
def
run
(
module_info
)
#module_info as returned by JouleAdapter
if
module_info
.
nil?
add_error
(
"unable to retrieve module information"
)
return
self
set_notice
(
"refreshed modules"
)
self
end
def
_build_module
(
info
)
# create JouleModule and associated pipes from
# hash returned by the JouleAdapter.module_info
params
=
info
.
extract!
(
*
JouleModule
.
joule_keys
)
m
=
JouleModule
.
new
(
params
)
# link inputs to database streams
info
[
:input_paths
].
each
do
|
name
,
path
|
m
.
joule_pipes
<<
JoulePipe
.
new
(
direction:
'input'
,
name:
name
,
db_stream:
_retrieve_stream
(
path
))
end
#remove the previous modules
@nilm
.
joule_modules
.
destroy_all
module_info
.
each
do
|
info
|
@nilm
.
joule_modules
<<
_build_module
(
info
)
info
[
:output_paths
].
each
do
|
name
,
path
|
m
.
joule_pipes
<<
JoulePipe
.
new
(
direction:
'output'
,
name:
name
,
db_stream:
_retrieve_stream
(
path
)
)
end
set_notice
(
"refreshed modules"
)
self
end
def
_build_module
(
info
)
# create JouleModule and associated pipes from
# hash returned by the JouleAdapter.module_info
params
=
info
.
extract!
(
*
JouleModule
.
joule_keys
)
m
=
JouleModule
.
new
(
params
)
# link inputs to database streams
info
[
:input_paths
].
each
do
|
name
,
path
|
m
.
joule_pipes
<<
JoulePipe
.
new
(
direction:
'input'
,
name:
name
,
db_stream:
_retrieve_stream
(
path
))
end
info
[
:output_paths
].
each
do
|
name
,
path
|
m
.
joule_pipes
<<
JoulePipe
.
new
(
direction:
'output'
,
name:
name
,
db_stream:
_retrieve_stream
(
path
))
return
m
end
return
m
end
def
_retrieve_stream
(
path
)
dbStream
=
@nilm
.
db
.
db_streams
.
find_by_path
(
path
)
if
dbStream
.
nil?
add_warning
(
"[
#{
path
}
] not in database"
)
def
_retrieve_stream
(
path
)
dbStream
=
@nilm
.
db
.
db_streams
.
find_by_path
(
path
)
if
dbStream
.
nil?
add_warning
(
"[
#{
path
}
] not in database"
)
end
dbStream
end
dbStream
end
end
app/adapters/nilmdb/update_db.rb
View file @
f8eb4299
...
...
@@ -12,7 +12,7 @@ module Nilmdb
def
run
(
dbinfo
,
schema
)
# check to make sure dbinfo and schema are set
# if either is nil, the database is not available
if
(
dbinfo
.
nil?
||
schema
.
nil?
)
if
dbinfo
.
nil?
||
schema
.
nil?
add_error
(
"cannot contact database at
#{
@db
.
url
}
"
)
@db
.
update_attributes
(
available:
false
)
return
self
...
...
app/adapters/node_adapter_factory.rb
View file @
f8eb4299
class
NodeAdapterFactory
include
HTTParty
default_timeout
5
open_timeout
5
read_timeout
5
def
self
.
from_url
(
url
)
begin
resp
=
self
.
class
.
get
(
url
)
resp
=
get
(
url
)
return
nil
unless
resp
.
success?
info
=
resp
.
parsed_response
rescue
return
nil
end
if
info
.
include?
'NilmDB'
return
Nilmdb
::
Adapter
(
url
)
return
Nilmdb
::
Adapter
.
new
(
url
)
elsif
info
.
include?
'Joule'
return
Joule
::
Adapter
(
url
)
return
Joule
::
Adapter
.
new
(
url
)
else
return
nil
end
end
def
self
.
from_nilm
(
nilm
)
if
nilm
.
type
==
'nilmdb'
return
Nilmdb
::
Adapter
(
nilm
.
url
)
elsif
nilm
.
type
==
'joule'
return
Joule
::
Adapter
(
nilm
.
url
)
if
nilm
.
node_
type
==
'nilmdb'
return
Nilmdb
::
Adapter
.
new
(
nilm
.
url
)
elsif
nilm
.
node_
type
==
'joule'
return
Joule
::
Adapter
.
new
(
nilm
.
url
)
else
# try to figure out what this nilm is
return
self
.
from_url
(
nilm
.
url
)
...
...
app/views/nilms/show.json.jbuilder
View file @
f8eb4299
...
...
@@ -4,6 +4,7 @@ json.data do
json.role @role
if @nilm.db != nil
json.max_points_per_plot @nilm.db.max_points_per_plot
json.available @nilm.db.available
json.root_folder do
if @nilm.db.root_folder != nil
json.partial! 'db_folders/db_folder',
...
...
@@ -14,6 +15,7 @@ json.data do
end
json.jouleModules(@nilm.joule_modules) do |m|
json.extract! m, *JouleModule.json_keys
json.url Rails.configuration.interface_url_template.call(m.joule_id)
json.nilm_id @nilm.id
end
end
...
...
config/environments/production.rb
View file @
f8eb4299
...
...
@@ -95,4 +95,6 @@ Rails.application.configure do
# Do not dump schema after migrations.
config
.
active_record
.
dump_schema_after_migration
=
false
end
config/environments/test.rb
View file @
f8eb4299
...
...
@@ -39,4 +39,10 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# set up interface subdomain
config
.
interface_url_template
=
lambda
do
|
id
|
return
"http://
#{
id
}
.interfaces.wattsworth.local"
end
end
spec/
services/joule_modules/update_joul
e_modules_spec.rb
→
spec/
adapters/joule/updat
e_modules_spec.rb
View file @
f8eb4299
...
...
@@ -13,7 +13,7 @@ describe 'UpdateJouleModules' do
outputs
=
{
o1:
'/path/2'
})
backend
.
add_module
(
"new2"
,
inputs
=
{
i1:
'/path/3'
,
i2:
'/path/4'
},
outputs
=
{
o1:
'/path/5'
,
o2:
'/path/5'
})
service
=
UpdateJoul
eModules
.
new
(
nilm
)
service
=
Joule
::
Updat
eModules
.
new
(
nilm
)
service
.
run
(
backend
.
module_info
)
expect
(
service
.
success?
).
to
be
true
# new modules are in the database
...
...
@@ -33,7 +33,7 @@ describe 'UpdateJouleModules' do
nilm
=
create
(
:nilm
)
backend
=
MockJouleAdapter
.
new
backend
.
add_module
(
"module"
,
outputs
=
{
output:
'/missing/path'
})
service
=
UpdateJoul
eModules
.
new
(
nilm
)
service
=
Joule
::
Updat
eModules
.
new
(
nilm
)
service
.
run
(
backend
.
module_info
)
expect
(
service
.
warnings?
).
to
be
true
end
...
...
@@ -44,21 +44,8 @@ describe 'UpdateJouleModules' do
backend
=
MockJouleAdapter
.
new
backend
.
add_module
(
"module"
,
inputs
=
{
input:
'/matched/path1'
},
outputs
=
{
output:
'/matched/path2'
})
service
=
UpdateJoul
eModules
.
new
(
nilm
)
service
=
Joule
::
Updat
eModules
.
new
(
nilm
)
service
.
run
(
backend
.
module_info
)
expect
(
service
.
warnings?
).
to
be
false
end
it
'returns error if Joule server is unavailable'
do
nilm
=
Nilm
.
create
(
name:
'test'
,
url:
'invalid'
)
mock_service
=
instance_double
(
UpdateDb
,
run:
StubService
.
new
)
allow
(
UpdateDb
).
to
receive
(
:new
)
.
and_return
(
mock_service
)
service
=
UpdateNilm
.
new
()
service
.
run
(
nilm
)
expect
(
service
.
success?
).
to
be
false
expect
(
mock_service
).
to_not
have_received
(
:run
)
end
end
spec/adapters/node_adapter_factory_spec.rb
0 → 100644
View file @
f8eb4299
# frozen_string_literal: true
require
'rails_helper'
describe
NodeAdapterFactory
do
# use the NUC office server
let
(
:nilmdb_url
)
{
'http://nuc/nilmdb'
}
let
(
:joule_url
)
{
'http://nuc:8088'
}
it
'creates_adapter_from_url'
,
:vcr
do
adapter
=
NodeAdapterFactory
.
from_url
(
nilmdb_url
)
expect
(
adapter
.
node_type
).
to
eq
(
'nilmdb'
)
adapter
=
NodeAdapterFactory
.
from_url
(
joule_url
)
expect
(
adapter
.
node_type
).
to
eq
(
'joule'
)
end
it
'returns nil with invalid url'
,
:vcr
do
%w(http://www.google.com invalid_url)
.
each
do
|
url
|
expect
(
NodeAdapterFactory
.
from_url
(
url
)).
to
be_nil
end
end
it
'creates_adapter_from_nilm'
do
nilm
=
create
(
:nilm
,
node_type:
'nilmdb'
)
adapter
=
NodeAdapterFactory
.
from_nilm
(
nilm
)
expect
(
adapter
.
node_type
).
to
eq
(
'nilmdb'
)
nilm
=
create
(
:nilm
,
node_type:
'joule'
)
adapter
=
NodeAdapterFactory
.
from_nilm
(
nilm
)
expect
(
adapter
.
node_type
).
to
eq
(
'joule'
)
end
it
'falls_back_to_url_when_node_type_is_invalid'
,
:vcr
do
nilm
=
create
(
:nilm
,
url:
joule_url
)
nilm
.
node_type
=
'invalid'
adapter
=
NodeAdapterFactory
.
from_nilm
(
nilm
)
expect
(
adapter
.
node_type
).
to
eq
(
'joule'
)
end
end
\ No newline at end of file
spec/controllers/nilms_controller_spec.rb
View file @
f8eb4299
...
...
@@ -132,7 +132,7 @@ RSpec.describe NilmsController, type: :request do
headers:
john
.
create_new_auth_token
body
=
JSON
.
parse
(
response
.
body
)
expect
(
body
[
'data'
][
'jouleModules'
][
0
][
'name'
]).
to
eq
(
test_module
.
name
)
expect
(
body
[
'data'
][
'jouleModules'
][
0
][
'url'
]).
to
start_with
(
"http://
#{
test_module
.
id
}
.modul
es"
)
expect
(
body
[
'data'
][
'jouleModules'
][
0
][
'url'
]).
to
start_with
(
"http://
#{
test_module
.
joule_id
}
.interfac
es"
)
end
it
'refreshes nilm data when requested'
do
@auth_headers
=
john
.
create_new_auth_token
...
...
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