Skip to content

Commit

Permalink
display measurement info within sensor on devices endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Mar 7, 2024
1 parent 812f8f4 commit a9dafce
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/controllers/v0/devices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class DevicesController < ApplicationController

def show
@device = Device.includes(
:owner, :sensors,:tags).find(params[:id])
:owner,:tags, {sensors: :measurement}).find(params[:id])
authorize @device
@device
end

def index
raise_ransack_errors_as_bad_request do
@q = policy_scope(Device)
.includes(:owner, :tags, :components, :sensors)
.includes(:owner, :tags, :components, {sensors: :measurement})
.ransack(params[:q], auth_object: (current_user&.is_admin? ? :admin : nil))

# We are here customly adding multiple tags into the Ransack query.
Expand Down
30 changes: 2 additions & 28 deletions app/models/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ def formatted_data

components.sort_by {|c| c.sensor.name }.each do |component|
sensor = component.sensor
sa = sensor.attributes.except(*%w{key equation reverse_equation})
sa = sensor.attributes.except(*%w{key equation reverse_equation measurement_id})
sa = sa.merge(
measurement: sensor.measurement.for_sensor_json,
value: (data ? data["#{sensor.id}"] : nil),
prev_value: (old_data ? old_data["#{sensor.id}"] : nil),
last_reading_at: component.last_reading_at
Expand All @@ -233,33 +234,6 @@ def self.geocode_all_without_location
end
end

def self.old_for_world_map(authorized=false)
where
.not(latitude: nil)
.where.not(data: nil)
.where(is_test: false)
.includes(:owner,:tags)
.map do |device|
{
id: device.id,
name: device.name,
description: (device.description.present? ? device.description : nil),
owner_id: device.owner_id,
owner_username: device.owner_id ? device.owner_username : nil,
latitude: device.latitude,
longitude: device.longitude,
city: device.city,
hardware: device.hardware(authorized),
country_code: device.country_code,
state: device.state,
system_tags: device.system_tags,
user_tags: device.user_tags,
updated_at: device.updated_at,
last_reading_at: (device.last_reading_at.present? ? device.last_reading_at : nil)
}
end
end

def remove_mac_address_for_newly_registered_device!
update(old_mac_address: mac_address, mac_address: nil)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/measurement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class Measurement < ActiveRecord::Base
has_many :sensors
validates_presence_of :name, :description
validates_uniqueness_of :name

def for_sensor_json
attributes.except(*%w{created_at updated_at})
end
end

0 comments on commit a9dafce

Please sign in to comment.