Commit 1eb49f8a by John Donnal

added support for event count limit to enable plots with large numbers of events

parent 054c9415
......@@ -106,9 +106,9 @@ module Joule
# === END ANNOTATIONS ===
# === BEGIN EVENTS ===
def read_events(stream, start_time, end_time)
events = @backend.read_events(stream.joule_id, start_time, end_time)
{id: stream.id, valid: true, events: events}
def read_events(stream, max_events, start_time, end_time)
result = @backend.read_events(stream.joule_id, max_events, start_time, end_time)
{id: stream.id, valid: true, count: result[:count], events: result[:events]}
end
def node_type
......
......@@ -261,10 +261,11 @@ module Joule
end
# === EVENT METHODS ===
def read_events(stream_id, start_time, end_time)
def read_events(stream_id, max_events, start_time, end_time)
query = {'id': stream_id}
query['start'] = start_time unless start_time.nil?
query['end'] = end_time unless end_time.nil?
query['limit'] = max_events
options = {query: query}
begin
resp = self.class.get("#{@url}/event/data.json", options)
......@@ -272,7 +273,14 @@ module Joule
rescue
raise "connection error"
end
resp.parsed_response.map{|event| event.deep_symbolize_keys}
if resp.parsed_response.is_a?(Hash)
resp.parsed_response.deep_symbolize_keys!
resp
else # backwards compatibility
events = resp.parsed_response.map{|event| event.deep_symbolize_keys}
count = if events.nil? then 0 else events.length end
{count: count, events: events}
end
end
def delete_annotation(annotation_id)
......
......@@ -86,7 +86,7 @@ class NilmsController < ApplicationController
params.permit(:name, :description, :url)
end
def db_params
params.permit(:max_points_per_plot, :url)
params.permit(:max_points_per_plot, :max_events_per_plot, :url)
end
#authorization based on nilms
......
......@@ -21,11 +21,11 @@ class ReadEvents
@data = []
event_streams.each do |stream|
adapter = NodeAdapterFactory.from_nilm(stream.db.nilm)
result = adapter.read_events(stream, @start_time, @end_time)
result = adapter.read_events(stream, stream.db.max_events_per_plot, @start_time, @end_time)
if not result.nil?
@data.append(result)
else
@data.append({id: stream.id, valid: false, events: nil})
@data.append({id: stream.id, valid: false, count: 0, events: nil})
add_warning("unable to retrieve events for #{stream.path}")
end
end
......
......@@ -2,6 +2,7 @@ json.data do
json.array! @service.data.each do |event_stream|
json.id event_stream[:id]
json.valid event_stream[:valid]
json.count event_stream[:count]
json.events event_stream[:events]
json.start_time @start_time
json.end_time @end_time
......
......@@ -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.max_events_per_plot @nilm.db.max_events_per_plot
json.available @nilm.db.available
json.root_folder do
if @nilm.db.root_folder != nil
......
class AddMaxEventsPerPlotToDbs < ActiveRecord::Migration[6.0]
def change
add_column :dbs, :max_events_per_plot, :integer, default: 200
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_03_03_014945) do
ActiveRecord::Schema.define(version: 2022_01_04_010931) do
create_table "data_apps", force: :cascade do |t|
t.string "name"
......@@ -117,6 +117,7 @@ ActiveRecord::Schema.define(version: 2021_03_03_014945) do
t.string "version"
t.integer "max_points_per_plot", default: 3600
t.boolean "available"
t.integer "max_events_per_plot", default: 200
end
create_table "event_streams", force: :cascade do |t|
......
......@@ -11,20 +11,20 @@ describe Joule::Adapter do
raw = File.read(File.dirname(__FILE__) + "/events.json")
json = JSON.parse(raw)
json.deep_symbolize_keys!
expect(mock_backend).to receive(:read_events) { json }
nilm = FactoryBot.create(:nilm, name: "test")
stream = FactoryBot.create(:event_stream, db: nilm.db, db_folder: nilm.db.root_folder,
name: 'test_stream')
result = adapter.read_events(stream,1611421200000000, 1611421230000000)
result = adapter.read_events(stream, 200, 1611421200000000, 1611421230000000)
expect(result[:id]).to eq stream.id
expect(result[:valid]).to be true
expect(result[:events].length).to eq 4
expected_event = {
"start_time" => 1611421200000000,
"end_time" => 1611421205000000,
"content" => {"name" => "test event 0"}}
start_time: 1611421200000000,
end_time: 1611421205000000,
content: {name: "test event 0"}}
expect(result[:events][0]).to eq expected_event
end
......
......@@ -55,15 +55,16 @@ describe Joule::Backend do
backend = Joule::Backend.new(url, key)
start_time = 1611421210000000
end_time = 1611421235000000
events = backend.read_events(2, start_time, end_time)
result = backend.read_events(2, 200, start_time, end_time)
# should have 3 events
expect(events.length).to eq 3
expect(result[:count]).to eq 3
expect(result[:events].length).to eq 3
expected_event = {
"start_time":1611421210000000,
"end_time":1611421215000000,
"content":{
"name":"test event 1"}}
expect(events[0]).to eq expected_event
expect(result[:events][0]).to eq expected_event
end
it 'handles errors', :vcr do
backend = Joule::Backend.new(url, key)
......
[
{
"start_time":1611421200000000,
"end_time":1611421205000000,
"content":{
"name":"test event 0"
{
"count": 4,
"events": [
{
"start_time": 1611421200000000,
"end_time": 1611421205000000,
"content": {
"name": "test event 0"
}
},
{
"start_time": 1611421210000000,
"end_time": 1611421215000000,
"content": {
"name": "test event 1"
}
},
{
"start_time": 1611421220000000,
"end_time": 1611421225000000,
"content": {
"name": "test event 2"
}
},
{
"start_time": 1611421230000000,
"end_time": 1611421235000000,
"content": {
"name": "test event 3"
}
}
},
{
"start_time":1611421210000000,
"end_time":1611421215000000,
"content":{
"name":"test event 1"
}
},
{
"start_time":1611421220000000,
"end_time":1611421225000000,
"content":{
"name":"test event 2"
}
},
{
"start_time":1611421230000000,
"end_time":1611421235000000,
"content":{
"name":"test event 3"
}
}
]
\ No newline at end of file
]
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
http_interactions:
- request:
method: get
uri: https://127.0.0.1:3030/event/data.json?end=1611421235000000&id=2&start=1611421210000000
uri: https://127.0.0.1:3030/event/data.json?end=1611421235000000&id=2&limit=200&start=1611421210000000
body:
encoding: US-ASCII
string: ''
......
......@@ -289,7 +289,7 @@ http_interactions:
recorded_at: Mon, 08 Mar 2021 21:26:43 GMT
- request:
method: get
uri: https://localhost:3030/event/data.json?id=4
uri: https://localhost:3030/event/data.json?id=4&limit=200
body:
encoding: US-ASCII
string: ''
......@@ -348,7 +348,7 @@ http_interactions:
recorded_at: Mon, 08 Mar 2021 21:26:52 GMT
- request:
method: get
uri: https://localhost:3030/event/data.json?id=1
uri: https://localhost:3030/event/data.json?id=1&limit=200
body:
encoding: US-ASCII
string: ''
......
require 'rails_helper'
RSpec.describe EventStream, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
......@@ -48,7 +48,7 @@ RSpec.describe 'ReadEvents' do
expect(service.warnings.length).to eq 1
expect(service.data).to eq [
@event_stream1_data,
{id: @event_stream2.id, valid: false, events: nil}
{id: @event_stream2.id, valid: false, events: nil, count: 0}
]
expect(@mock_adapter.event_run_count).to eq 2
end
......
......@@ -16,7 +16,7 @@ class MockAdapter
end
{data: data, decimation_factor: 1}
end
def read_events(event_stream,start_time, end_time)
def read_events(event_stream,max_events, start_time, end_time)
data = @events.select{|d| d[:event_stream]==event_stream}.first[:data]
@event_run_count += 1
data
......
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