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
1eb49f8a
authored
Jan 04, 2022
by
John Donnal
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
added support for event count limit to enable plots with large numbers of events
parent
054c9415
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
61 additions
and
46 deletions
app/adapters/joule/adapter.rb
app/adapters/joule/backend.rb
app/controllers/nilms_controller.rb
app/services/event_stream/read_events.rb
app/views/events/data.json.jbuilder
app/views/nilms/show.json.jbuilder
db/migrate/20220104010931_add_max_events_per_plot_to_dbs.rb
db/schema.rb
spec/adapters/joule/adapter_spec.rb
spec/adapters/joule/backend_spec.rb
spec/adapters/joule/events.json
spec/cassettes/Joule_Backend/Events/read_events/loads_events.yml
spec/cassettes/ReadEvents/when_boundary_times_are_not_specified/updates_the_streams.yml
spec/models/event_stream_spec.rb
spec/services/events/read_events_spec.rb
spec/support/mock_adapter.rb
app/adapters/joule/adapter.rb
View file @
1eb49f8a
...
...
@@ -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
...
...
app/adapters/joule/backend.rb
View file @
1eb49f8a
...
...
@@ -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
)
...
...
app/controllers/nilms_controller.rb
View file @
1eb49f8a
...
...
@@ -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
...
...
app/services/event_stream/read_events.rb
View file @
1eb49f8a
...
...
@@ -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
...
...
app/views/events/data.json.jbuilder
View file @
1eb49f8a
...
...
@@ -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
...
...
app/views/nilms/show.json.jbuilder
View file @
1eb49f8a
...
...
@@ -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
...
...
db/migrate/20220104010931_add_max_events_per_plot_to_dbs.rb
0 → 100644
View file @
1eb49f8a
class
AddMaxEventsPerPlotToDbs
<
ActiveRecord
::
Migration
[
6.0
]
def
change
add_column
:dbs
,
:max_events_per_plot
,
:integer
,
default:
200
end
end
db/schema.rb
View file @
1eb49f8a
...
...
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
202
1_03_03_014945
)
do
ActiveRecord
::
Schema
.
define
(
version:
202
2_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
|
...
...
spec/adapters/joule/adapter_spec.rb
View file @
1eb49f8a
...
...
@@ -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
...
...
spec/adapters/joule/backend_spec.rb
View file @
1eb49f8a
...
...
@@ -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
)
...
...
spec/adapters/joule/events.json
View file @
1eb49f8a
[
{
"count"
:
4
,
"events"
:
[
{
"start_time"
:
1611421200000000
,
"end_time"
:
1611421205000000
,
"content"
:
{
"name"
:
"test event 0"
"start_time"
:
1611421200000000
,
"end_time"
:
1611421205000000
,
"content"
:
{
"name"
:
"test event 0"
}
},
{
"start_time"
:
1611421210000000
,
"end_time"
:
1611421215000000
,
"content"
:
{
"name"
:
"test event 1"
"start_time"
:
1611421210000000
,
"end_time"
:
1611421215000000
,
"content"
:
{
"name"
:
"test event 1"
}
},
{
"start_time"
:
1611421220000000
,
"end_time"
:
1611421225000000
,
"content"
:
{
"name"
:
"test event 2"
"start_time"
:
1611421220000000
,
"end_time"
:
1611421225000000
,
"content"
:
{
"name"
:
"test event 2"
}
},
{
"start_time"
:
1611421230000000
,
"end_time"
:
1611421235000000
,
"content"
:
{
"name"
:
"test event 3"
"start_time"
:
1611421230000000
,
"end_time"
:
1611421235000000
,
"content"
:
{
"name"
:
"test event 3"
}
}
]
\ No newline at end of file
]
}
\ No newline at end of file
spec/cassettes/Joule_Backend/Events/read_events/loads_events.yml
View file @
1eb49f8a
...
...
@@ -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
:
'
'
...
...
spec/cassettes/ReadEvents/when_boundary_times_are_not_specified/updates_the_streams.yml
View file @
1eb49f8a
...
...
@@ -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
:
'
'
...
...
spec/models/event_stream_spec.rb
deleted
100644 → 0
View file @
054c9415
require
'rails_helper'
RSpec
.
describe
EventStream
,
type: :model
do
pending
"add some examples to (or delete)
#{
__FILE__
}
"
end
spec/services/events/read_events_spec.rb
View file @
1eb49f8a
...
...
@@ -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
...
...
spec/support/mock_adapter.rb
View file @
1eb49f8a
...
...
@@ -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
...
...
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