From a9dafcea0ff99d475f2ede75d0cfe88b97cc9be3 Mon Sep 17 00:00:00 2001 From: Tim Cowlishaw Date: Thu, 7 Mar 2024 13:54:34 +0100 Subject: [PATCH] display measurement info within sensor on devices endpoint --- app/controllers/v0/devices_controller.rb | 4 ++-- app/models/device.rb | 30 ++---------------------- app/models/measurement.rb | 4 ++++ 3 files changed, 8 insertions(+), 30 deletions(-) diff --git a/app/controllers/v0/devices_controller.rb b/app/controllers/v0/devices_controller.rb index 1dd49f7f..a993399f 100644 --- a/app/controllers/v0/devices_controller.rb +++ b/app/controllers/v0/devices_controller.rb @@ -6,7 +6,7 @@ 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 @@ -14,7 +14,7 @@ def show 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. diff --git a/app/models/device.rb b/app/models/device.rb index cf636208..8082e7e7 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -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 @@ -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 diff --git a/app/models/measurement.rb b/app/models/measurement.rb index c01c630a..d6865bbf 100644 --- a/app/models/measurement.rb +++ b/app/models/measurement.rb @@ -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