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
987ae62f
authored
Mar 30, 2017
by
John Doe
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
working on data retrieval endpoint
parent
9ebe3e64
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
67 additions
and
26 deletions
app/controllers/db_elements_controller.rb
app/services/data/load_element_data.rb
app/views/db_elements/data.json.jbuilder
app/views/db_elements/index.json.jbuilder
config/routes.rb
app/controllers/db_elements_controller.rb
View file @
987ae62f
# frozen_string_literal: true
class
DbElementsController
<
ApplicationController
before_action
:authenticate_user!
def
index
req_elements
=
DbElement
.
find
(
params
[
:elements
]
)
#make sure the user is allowed to view these elements
req_
elements
.
each
do
|
elem
|
@elements
=
DbElement
.
find
(
JSON
.
decode
(
params
[
:elements
])
)
#
make sure the user is allowed to view these elements
@
elements
.
each
do
|
elem
|
unless
current_user
.
views_nilm?
(
elem
.
db_stream
.
db
.
nilm
)
head
:unauthorized
return
end
end
#make sure the time range makes sense
@start_time
=
params
[
:start_time
].
to_i
@end_time
=
params
[
:end_time
].
to_i
unless
@end_time
>
@start_time
head
:unprocessable_entity
return
end
def
data
req_elements
=
DbElement
.
find
(
JSON
.
parse
(
params
[
:elements
]))
# make sure the user is allowed to view these elements
req_elements
.
each
do
|
elem
|
unless
current_user
.
views_nilm?
(
elem
.
db_stream
.
db
.
nilm
)
head
:unauthorized
return
end
end
#retrieve the data for the requested elements
# make sure the time range makes sense
start_time
=
(
params
[
:start_time
].
to_i
unless
params
[
:start_time
].
nil?
)
end_time
=
(
params
[
:end_time
].
to_i
unless
params
[
:end_time
].
nil?
)
# retrieve the data for the requested elements
@service
=
LoadElementData
.
new
@service
.
run
(
req_elements
,
@start_time
,
@end_time
)
@service
.
run
(
req_elements
,
start_time
,
end_time
)
@start_time
=
@service
.
start_time
@end_time
=
@service
.
end_time
render
status:
@service
.
success?
?
:
ok
:
:unprocessable_entity
end
end
app/services/data/load_element_data.rb
View file @
987ae62f
...
...
@@ -3,14 +3,20 @@
# Loads data for specified elements
class
LoadElementData
include
ServiceStatus
attr_reader
:data
attr_reader
:data
,
:start_time
,
:end_time
def
initialize
()
super
()
@data
=
[]
@start_time
=
nil
@end_time
=
nil
end
# load data for the array of specified elements
# start_time and end_time are unix us
# if start_time is nil it is set to earliest timestamp
# if end_time is nil it is set to latest timestamp
#
# sets data
# data:
# [{id: element_id, values: [...]},
...
...
@@ -25,12 +31,30 @@ class LoadElementData
req_streams
<<
elem
.
db_stream
end
end
#2 compute start and end times if nil
streams_with_data
=
req_streams
.
select
{
|
stream
|
stream
.
total_time
>
0
}
if
(
start_time
==
nil
||
end_time
==
nil
)
&&
streams_with_data
.
empty?
add_error
(
"no time bounds for requested elements, refresh database?"
)
return
end
@start_time
=
start_time
@end_time
=
end_time
if
start_time
==
nil
@start_time
=
streams_with_data
.
sort
{
|
a
,
b
|
a
.
start_time
<
b
.
start_time
}
.
first
.
start_time
end
if
end_time
==
nil
@end_time
=
streams_with_data
.
sort
{
|
a
,
b
|
a
.
end_time
>
b
.
end_time
}
.
first
.
end_time
end
#2 pull data from streams
combined_data
=
[]
req_streams
.
each
do
|
stream
|
adapter
=
DbAdapter
.
new
(
stream
.
db
.
url
)
data_service
=
LoadStreamData
.
new
(
adapter
)
data_service
.
run
(
stream
,
start_time
,
end_time
)
data_service
.
run
(
stream
,
@start_time
,
@
end_time
)
if
data_service
.
success?
combined_data
.
concat
(
data_service
.
data
)
else
...
...
app/views/db_elements/data.json.jbuilder
0 → 100644
View file @
987ae62f
json.data do
json.array! @service.data.each do |element_data|
json.element_id element_data[:id]
json.data element_data[:values]
json.start_time @start_time
json.end_time @end_time
end
end
json.partial! "helpers/messages", service: @service
app/views/db_elements/index.json.jbuilder
View file @
987ae62f
json.data do
json.array! @service.data.each do |element_data|
elem = DbElement.find(element_data[:id])
json.extract! elem, *DbElement.json_keys
json.path elem.name_path
json.data element_data[:values]
json.start_time @start_time
json.end_time @end_time
end
end
json.partial! "helpers/messages", service: @service
json.array! @elements do |element|
json.extract! element, *DbElement.json_keys
json.path element.name_path
end
config/routes.rb
View file @
987ae62f
...
...
@@ -8,7 +8,11 @@ Rails.application.routes.draw do
resources
:dbs
,
only:
[
:show
,
:update
]
resources
:db_folders
,
only:
[
:show
,
:update
]
resources
:db_streams
,
only:
[
:update
]
resources
:db_elements
,
only:
[
:index
]
resources
:db_elements
,
only:
[
:index
]
do
collection
do
get
'data'
end
end
mount_devise_token_auth_for
'User'
,
at:
'auth'
resources
:users
,
only:
[
:index
,
:create
,
:destroy
]
...
...
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