Skip to content

Commit

Permalink
Refactor to remove kits.
Browse files Browse the repository at this point in the history
- Add migration to remove kits and relate components directly to device
- remove kit specific controllers, models, specs, policies, etc
- add test for new device sensor map implementation
- ensure components are created for new sensors on ingest, improve test robustness and error messages
- remove spurious, dupliated timestamps, eg recorded_at and added_at dates on device
- Add 'bus' column to components.
- Rename devices last_recorded_at column to last_reading_at, and remove the alias, for consistency.
- remove 'raw_value' and 'raw_prev_value' from the data exposed for devices.
- add last_reading_at to components and populate on store
  • Loading branch information
timcowlishaw committed Oct 27, 2023
1 parent af9a720 commit efc8929
Show file tree
Hide file tree
Showing 62 changed files with 551 additions and 639 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ end

group :development, :test do
# gem 'rspec_api_blueprint', require: false
gem "pry"
gem 'brakeman', github: 'presidentbeef/brakeman', require: false
gem 'byebug'
gem 'cane'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ DEPENDENCIES
pg
pg_search
premailer-rails
pry
pry-rails
puma
pundit
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/v0/components_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ def index
end

def show
@component = Component.includes(:board, :sensor).find(params[:id])
@component = Component.includes(:device, :sensor).find(params[:id])
authorize @component
end

private

def component_params
params.permit(
:board_id,
:board_type,
:sensor_id,
:equation
:device_id,
:sensor_id
)
end

Expand Down
8 changes: 3 additions & 5 deletions app/controllers/v0/devices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ class DevicesController < ApplicationController

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

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

# We are here customly adding multiple tags into the Ransack query.
# Ransack supports this, but how do we add multiple tag names in URL string? Which separator to use?
# See Issue #186 https://github.com/fablabbcn/smartcitizen-api/issues/186
Expand Down Expand Up @@ -87,11 +88,9 @@ def fresh_world_map
city: device.city,
country_code: device.country_code,
is_private: device.is_private,
kit_id: device.kit_id,
state: device.state,
system_tags: device.system_tags,
user_tags: device.user_tags,
added_at: device.added_at,
updated_at: device.updated_at,
last_reading_at: (device.last_reading_at.present? ? device.last_reading_at : nil)
}
Expand Down Expand Up @@ -122,7 +121,6 @@ def device_params
:notify_stopped_publishing,
:exposure,
:meta,
:kit_id,
:user_tags,
postprocessing_attributes: [:blueprint_url, :hardware_url, :latest_postprocessing, :meta, :forwarding_params],
]
Expand Down
48 changes: 0 additions & 48 deletions app/controllers/v0/kits_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/controllers/v0/onboarding/orphan_devices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def update
private

def orphan_device_params
params.permit(:name, :description, :kit_id, :exposure, :latitude, :longitude, :user_tags)
params.permit(:name, :description, :exposure, :latitude, :longitude, :user_tags)
end

def set_orphan_device
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/v0/readings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ def csv_archive
@device = Device.find(params[:id])
authorize @device, :update?

if @device.kit.nil?
render json: { id: "error", message: "Device does not have a kit", url: "", errors: "" }, status: 420
return
end

if !@device.csv_export_requested_at or (@device.csv_export_requested_at < 15.minutes.ago)
@device.update_column(:csv_export_requested_at, Time.now.utc)
if Rails.env.test?
Expand Down
13 changes: 6 additions & 7 deletions app/controllers/v0/static_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def home
current_user_url: [request.base_url, v0_me_index_path].join,
components_url: [request.base_url, v0_components_path].join,
devices_url: [request.base_url, v0_devices_path].join,
kits_url: [request.base_url, v0_kits_path].join,
measurements_url: [request.base_url, v0_measurements_path].join,
sensors_url: [request.base_url, v0_sensors_path].join,
users_url: [request.base_url, v0_users_path].join,
Expand All @@ -32,12 +31,12 @@ def metrics
private: Device.where(is_private: true).count,
test: Device.where(is_test: true).count,
online: {
now: Device.where('last_recorded_at > ?', 10.minutes.ago).count,
last_hour: Device.where('last_recorded_at > ?', 1.hour.ago).count,
today: Device.where('last_recorded_at > ?', Time.now.beginning_of_day).count,
this_month: Device.where('last_recorded_at > ?', Time.now.beginning_of_month).count,
this_year: Device.where('last_recorded_at > ?', Time.now.beginning_of_year).count,
all_time: Device.where.not(last_recorded_at: nil).count
now: Device.where('last_reading_at > ?', 10.minutes.ago).count,
last_hour: Device.where('last_reading_at > ?', 1.hour.ago).count,
today: Device.where('last_reading_at > ?', Time.now.beginning_of_day).count,
this_month: Device.where('last_reading_at > ?', Time.now.beginning_of_month).count,
this_year: Device.where('last_reading_at > ?', Time.now.beginning_of_year).count,
all_time: Device.where.not(last_reading_at: nil).count
},
readings: {
good: {
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/check_device_stopped_publishing_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CheckDeviceStoppedPublishingJob < ApplicationJob
def perform(*args)
# Do something later

devices = Device.where(notify_stopped_publishing: true).where("last_recorded_at < ?", 60.minutes.ago)
devices = Device.where(notify_stopped_publishing: true).where("last_reading_at < ?", 60.minutes.ago)
CheckupNotifyJob.perform_now("#{devices.count} devices with notification on: stopped_publishing at least an hour ago. Ids: #{devices.pluck(:id)}")

devices.each do |device|
Expand Down
1 change: 1 addition & 0 deletions app/lib/mqtt_messages_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def self.handle_readings(device, message)
end
rescue Exception => e
Sentry.capture_exception(e)
raise e if Rails.env.test?
#puts e.inspect
#puts message
end
Expand Down
8 changes: 5 additions & 3 deletions app/models/component.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# This joins a device with its sensors.

class Component < ActiveRecord::Base
belongs_to :board, polymorphic: true
belongs_to :device
belongs_to :sensor

validates_presence_of :board, :sensor
validates :sensor_id, :uniqueness => { :scope => [:board_id, :board_type] }
validates_presence_of :device, :sensor
validates :sensor_id, :uniqueness => { :scope => [:device_id] }

delegate :equation, :reverse_equation, to: :sensor

# Accepts a raw sensor reading and uses its equation to process and return
# a calibrated version
Expand Down
5 changes: 1 addition & 4 deletions app/models/concerns/data_parser/storer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ def sensor_reading(device, sensor)
key = sensor['id']
id = device.find_sensor_id_by_key(key)
end
component = device.components.detect{ |c| c["sensor_id"] == id }

#raise "This component does not have sensor_id: #{id}" if component.nil?
component = device.find_or_create_component_by_sensor_id(id)
return nil if component.nil?

value = component.normalized_value( (Float(sensor['value']) rescue sensor['value']) )
{
id: id,
Expand Down
Loading

0 comments on commit efc8929

Please sign in to comment.