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
9ad4c8b7
authored
Aug 22, 2016
by
John Donnal
Browse files
Options
_('Browse Files')
Download
Plain Diff
Merge branch 'master' of
http://git.wattsworth.net/wattsworth/control-api
parents
7aba763f
365f9a4d
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
46 additions
and
27 deletions
app/models/db_file.rb
app/services/db_file/update_file.rb
app/services/nilm/create_nilm.rb
spec/factories/db_schema_helper.rb
spec/models/db_file_spec.rb
spec/services/folder/update_folder_spec.rb
app/models/db_file.rb
View file @
9ad4c8b7
# frozen_string_literal: true
# frozen_string_literal: true
# validate that the file has the appropriate number
# of streams given the format string
class
DbDataTypeValidator
<
ActiveModel
::
Validator
def
validate
(
record
)
# streams might not be built yet
return
if
record
.
db_streams
.
count
==
0
# TODO: check for valid format strings (float32, uint8, etc)
unless
record
.
db_streams
.
count
==
record
.
column_count
record
.
errors
[
:base
]
<<
"must have
#{
record
.
column_count
}
\
streams for format
#{
record
.
data_type
}
"
end
end
end
# A file in the database, contains one or more Streams
# A file in the database, contains one or more Streams
class
DbFile
<
ActiveRecord
::
Base
class
DbFile
<
ActiveRecord
::
Base
belongs_to
:db_folder
belongs_to
:db_folder
has_many
:db_streams
,
dependent: :destroy
has_many
:db_streams
,
dependent: :destroy
has_many
:db_decimations
,
dependent: :destroy
has_many
:db_decimations
,
dependent: :destroy
validates_with
DbDataTypeValidator
def
defined_attributes
def
defined_attributes
[
:name
,
:name_abbrev
,
:description
,
:hidden
]
[
:name
,
:name_abbrev
,
:description
,
:hidden
]
end
end
...
@@ -15,6 +31,14 @@ class DbFile < ActiveRecord::Base
...
@@ -15,6 +31,14 @@ class DbFile < ActiveRecord::Base
destroy
destroy
end
end
def
data_format
/^(\w*)_\d*$/
.
match
(
data_type
)[
1
]
end
def
column_count
/^\w*_(\d*)$/
.
match
(
data_type
)[
1
].
to_i
end
def
as_json
(
_options
=
{})
def
as_json
(
_options
=
{})
file
=
super
(
except:
[
:created_at
,
:updated_at
])
file
=
super
(
except:
[
:created_at
,
:updated_at
])
file
[
:streams
]
=
db_streams
.
map
(
&
:as_json
)
file
[
:streams
]
=
db_streams
.
map
(
&
:as_json
)
...
...
app/services/db_file/update_file.rb
View file @
9ad4c8b7
...
@@ -46,11 +46,13 @@ class UpdateFile
...
@@ -46,11 +46,13 @@ class UpdateFile
# create or update DbStreams for the
# create or update DbStreams for the
# specified DbFile
# specified DbFile
def
__build_streams
(
file
:,
stream_data
:)
def
__build_streams
(
file
:,
stream_data
:)
return
if
stream_data
.
empty?
file
.
column_count
.
times
do
|
x
|
stream_data
.
each
do
|
entry
|
stream
=
file
.
db_streams
.
find_by_column
(
x
)
stream
=
file
.
db_streams
.
find_by_column
(
entry
[
:column
])
stream
||=
DbStream
.
new
(
db_file:
file
)
stream
||=
DbStream
.
new
(
db_file:
file
)
stream
.
update_attributes
(
entry
)
# check if there is stream metadata for column x
entry
=
stream_data
.
select
{
|
meta
|
meta
[
:column
]
==
x
}
# use the metadata if present
stream
.
update_attributes
(
entry
[
0
]
||
{})
stream
.
save!
stream
.
save!
end
end
end
end
...
...
app/services/nilm/create_nilm.rb
View file @
9ad4c8b7
...
@@ -18,6 +18,6 @@ class CreateNilm
...
@@ -18,6 +18,6 @@ class CreateNilm
db
=
Db
.
create
(
nilm:
@nilm
,
url:
db_url
)
db
=
Db
.
create
(
nilm:
@nilm
,
url:
db_url
)
service
=
UpdateDb
.
new
(
db:
db
)
service
=
UpdateDb
.
new
(
db:
db
)
adapter
=
DbAdapter
.
new
(
db
.
url
)
adapter
=
DbAdapter
.
new
(
db
.
url
)
service
.
run
(
db_adapter:
adapter
)
service
.
run
(
adapter
.
schema
)
end
end
end
end
spec/factories/db_schema_helper.rb
View file @
9ad4c8b7
...
@@ -4,11 +4,11 @@
...
@@ -4,11 +4,11 @@
# are usually returned by DbAdapter.schema
# are usually returned by DbAdapter.schema
class
DbSchemaHelper
class
DbSchemaHelper
# schema data
# schema data
def
entry
(
path
,
type:
'uint8_1'
,
metadata:
{},
stream_count:
0
)
def
entry
(
path
,
metadata:
{},
stream_count:
1
)
{
{
path:
path
,
path:
path
,
attributes:
{
attributes:
{
data_type:
type
,
data_type:
"float32_
#{
stream_count
}
"
,
start_time:
0
,
start_time:
0
,
end_time:
0
,
end_time:
0
,
total_rows:
0
,
total_rows:
0
,
...
...
spec/models/db_file_spec.rb
View file @
9ad4c8b7
...
@@ -11,7 +11,8 @@ RSpec.describe 'DbFile' do
...
@@ -11,7 +11,8 @@ RSpec.describe 'DbFile' do
specify
{
expect
(
db_file
).
to
respond_to
(
:hidden
)
}
specify
{
expect
(
db_file
).
to
respond_to
(
:hidden
)
}
end
end
it
'removes streams destroyed'
do
describe
'child streams'
do
it
'are destroyed with parent file'
do
stream
=
DbStream
.
create
stream
=
DbStream
.
create
file
=
DbFile
.
create
file
=
DbFile
.
create
file
.
db_streams
<<
stream
file
.
db_streams
<<
stream
...
@@ -19,24 +20,19 @@ RSpec.describe 'DbFile' do
...
@@ -19,24 +20,19 @@ RSpec.describe 'DbFile' do
expect
(
DbStream
.
find_by_id
(
stream
.
id
)).
to
be
nil
expect
(
DbStream
.
find_by_id
(
stream
.
id
)).
to
be
nil
end
end
describe
'remove'
do
it
'exist for every column in file datatype'
do
let
(
:db_streams
)
{
FactoryGirl
.
build_list
(
:db_stream
,
5
)
}
file
=
DbFile
.
create
(
data_type:
'float32_3'
)
let
(
:db_file
)
{
FactoryGirl
.
create
(
:db_file
)
}
file
.
db_streams
<<
DbStream
.
new
let
(
:db_service
)
{
double
(
remove_file:
true
)
}
# missing 3 streams
it
'destroys itself'
do
expect
(
file
.
valid?
).
to
be
false
db_file
.
remove
(
db_service:
db_service
)
expect
(
db_file
).
to
be_destroyed
end
it
'destroys its db_streams'
do
db_file
.
db_streams
<<
db_streams
db_file
.
remove
(
db_service:
db_service
)
db_streams
.
each
do
|
stream
|
expect
(
stream
).
to
be_destroyed
end
end
it
'do not exist for columns not in file datatype'
do
file
=
DbFile
.
create
(
data_type:
'float32_1'
)
2
.
times
do
|
x
|
file
.
db_streams
<<
DbStream
.
new
(
column:
x
)
end
end
it
'removes itself from the remote system using DbService'
do
expect
(
file
.
valid?
).
to
be
false
db_file
.
remove
(
db_service:
db_service
)
expect
(
db_service
).
to
have_received
(
:remove_file
)
end
end
end
end
end
end
spec/services/folder/update_folder_spec.rb
View file @
9ad4c8b7
...
@@ -20,7 +20,4 @@ describe 'UpdateFolder service' do
...
@@ -20,7 +20,4 @@ describe 'UpdateFolder service' do
folder
.
reload
folder
.
reload
expect
(
folder
.
name
).
to
eq
(
'new_name'
)
expect
(
folder
.
name
).
to
eq
(
'new_name'
)
end
end
# run update again with different streams
it
'removes missing streams'
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