Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined method in device stories page #807

Merged
merged 3 commits into from Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/helpers/device_stories_helper.rb
Expand Up @@ -79,7 +79,7 @@ def sensor_data_by_sensors(sensor_data)
data_by_dates = convert_by_dates(sensor_data.dig('aggregations', 'sensor_data', 'buckets') || [])
CATEGORY_SENSOR_NAMES.transform_values do |names|
names.each_with_object([]) do |name, arr|
arr << { name: name, value: data_by_dates[name] } if data_by_dates.key?(name)
arr << { name: name, data: data_by_dates[name] } if data_by_dates.key?(name)
end
end
end
Expand All @@ -95,7 +95,8 @@ def convert_by_dates(buckets)
end

def sensor_last_location
IngestMeasurement.query_last_sensor_location(@device_story.device_urn)
.response.hits.hits.first._source.ingest.location.to_hash
res = IngestMeasurement.query_last_sensor_location(@device_story.device_urn)
hits = res.response.hits.hits
hits.empty? ? {} : hits.first._source.ingest.location&.to_hash
end
end
2 changes: 1 addition & 1 deletion app/views/device_stories/show.html.erb
Expand Up @@ -28,7 +28,7 @@
<% end %>
</div>
</div>
<% sensor_data = sensor_data_by_sensors(IngestMeasurement.query_sensor_data(@device_story.device_urn)) %>
<% sensor_data = sensor_data_by_sensors(IngestMeasurement.query_sensor_data(@device_story.device_urn).response.to_h) %>
<%- if !sensor_data["radiation_sensors"].empty? %>
<div id="chart-panel" class="col-md-6 col order-1">
<div class="panel panel-default device-chart">
Expand Down
28 changes: 20 additions & 8 deletions spec/helpers/device_stories_helper_spec.rb
Expand Up @@ -33,14 +33,14 @@
expect(res).to include(
'temperature_C' => [{
name: 'temperature_C',
value: include(
data: include(
'2021-03-08 00' => 16.91751828158859,
'2021-03-08 12' => 16.28823545399834
)
}],
'temperature_F' => [{
name: 'temperature_F',
value: include(
data: include(
'2021-03-08 12' => 61.318823817197014
)
}],
Expand All @@ -51,10 +51,6 @@
end

describe '.sensor_last_location' do
let(:query_last_sensor_location_response) { String.new(<<~JSON) }
{"took":9919,"timed_out":false,"_shards":{"total":91,"successful":91,"skipped":0,"failed":0},"hits":{"total":{"value":10000,"relation":"gte"},"max_score":null,"hits":[{"_index":"ingest-measurements-2021-03-28","_type":"_doc","_id":"p7nkdngBj2uUTIA-3o17","_score":null,"_source":{"device_urn":"pointcast:10018","device_class":"pointcast","device_sn":"POINTCAST #10018","device":10018,"when_captured":"2021-03-28T03:33:33Z","loc_lat":35.615814,"loc_lon":139.625711,"loc_alt":24,"loc_olc":"8Q7XJJ8G+87G","env_temp":22.3,"service_uploaded":"2021-03-28T03:33:33Z","service_transport":"pointcast:103.67.223.4","service_handler":"i-051a2a353509414f0","@timestamp":"2021-03-28T03:33:33Z","ingest":{"location":{"lat":35.615814,"lon":139.625711}}},"sort":[1616902413000]}]}}
JSON

before do
# Remove this environment variable to fore elasticsearch client
# to request to localhost.
Expand All @@ -71,8 +67,24 @@
ENV['ELASTICSEARCH_URL'] = @old_elasticsearch_url
end

it 'return latest device location' do
expect(helper.sensor_last_location).to include('lat' => 35.615814, 'lon' => 139.625711)
context 'when there is a hit' do
let(:query_last_sensor_location_response) { String.new(<<~JSON) }
{"took":9919,"timed_out":false,"_shards":{"total":91,"successful":91,"skipped":0,"failed":0},"hits":{"total":{"value":10000,"relation":"gte"},"max_score":null,"hits":[{"_index":"ingest-measurements-2021-03-28","_type":"_doc","_id":"p7nkdngBj2uUTIA-3o17","_score":null,"_source":{"device_urn":"pointcast:10018","device_class":"pointcast","device_sn":"POINTCAST #10018","device":10018,"when_captured":"2021-03-28T03:33:33Z","loc_lat":35.615814,"loc_lon":139.625711,"loc_alt":24,"loc_olc":"8Q7XJJ8G+87G","env_temp":22.3,"service_uploaded":"2021-03-28T03:33:33Z","service_transport":"pointcast:103.67.223.4","service_handler":"i-051a2a353509414f0","@timestamp":"2021-03-28T03:33:33Z","ingest":{"location":{"lat":35.615814,"lon":139.625711}}},"sort":[1616902413000]}]}}
JSON

it 'return latest device location' do
expect(helper.sensor_last_location).to include('lat' => 35.615814, 'lon' => 139.625711)
end
end

context 'when there is no hit' do
let(:query_last_sensor_location_response) { String.new(<<~JSON) }
{"took":9919,"timed_out":false,"_shards":{"total":91,"successful":91,"skipped":0,"failed":0},"hits":{"hits":[]}}
JSON

it 'return latest device location' do
expect(helper.sensor_last_location).to be_empty
end
end
end
end