Commit 987ae62f by John Doe

working on data retrieval endpoint

parent 9ebe3e64
# frozen_string_literal: true
class DbElementsController < ApplicationController class DbElementsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
def index def index
req_elements = DbElement.find(params[:elements]) @elements = DbElement.find(JSON.decode(params[:elements]))
#make sure the user is allowed to view these elements # make sure the user is allowed to view these elements
req_elements.each do |elem| @elements.each do |elem|
unless current_user.views_nilm?(elem.db_stream.db.nilm) unless current_user.views_nilm?(elem.db_stream.db.nilm)
head :unauthorized head :unauthorized
return return
end end
end end
#make sure the time range makes sense end
@start_time = params[:start_time].to_i
@end_time = params[:end_time].to_i def data
unless @end_time>@start_time req_elements = DbElement.find(JSON.parse(params[:elements]))
head :unprocessable_entity # make sure the user is allowed to view these elements
return req_elements.each do |elem|
unless current_user.views_nilm?(elem.db_stream.db.nilm)
head :unauthorized
return
end
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 = 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 render status: @service.success? ? :ok : :unprocessable_entity
end end
end end
...@@ -3,14 +3,20 @@ ...@@ -3,14 +3,20 @@
# Loads data for specified elements # Loads data for specified elements
class LoadElementData class LoadElementData
include ServiceStatus include ServiceStatus
attr_reader :data attr_reader :data, :start_time, :end_time
def initialize() def initialize()
super() super()
@data = [] @data = []
@start_time = nil
@end_time = nil
end end
# load data for the array of specified elements # 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 # sets data
# data: # data:
# [{id: element_id, values: [...]}, # [{id: element_id, values: [...]},
...@@ -25,12 +31,30 @@ class LoadElementData ...@@ -25,12 +31,30 @@ class LoadElementData
req_streams << elem.db_stream req_streams << elem.db_stream
end end
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 #2 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)
data_service = LoadStreamData.new(adapter) 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? if data_service.success?
combined_data.concat(data_service.data) combined_data.concat(data_service.data)
else else
......
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
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
...@@ -8,7 +8,11 @@ Rails.application.routes.draw do ...@@ -8,7 +8,11 @@ Rails.application.routes.draw do
resources :dbs, only: [:show, :update] resources :dbs, only: [:show, :update]
resources :db_folders, only: [:show, :update] resources :db_folders, only: [:show, :update]
resources :db_streams, only: [: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' mount_devise_token_auth_for 'User', at: 'auth'
resources :users, only: [:index, :create, :destroy] resources :users, only: [:index, :create, :destroy]
......
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