Compare commits

..

3 commits

Author SHA1 Message Date
f
a7809d13e8 deduplicar arduinos
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-05-14 18:59:22 -03:00
f
e085d5345d no multiplicar los arduinos 2022-05-14 18:36:04 -03:00
f
3a1b78f238 convertir valores en enteros antes de procesarlos 2022-05-13 12:54:41 -03:00
2 changed files with 23 additions and 2 deletions

View file

@ -21,7 +21,7 @@ class ReadingsController < ActionController::API
params[:arduinos]&.reject do |a| params[:arduinos]&.reject do |a|
a[:id].blank? || a[:sensores].empty? a[:id].blank? || a[:sensores].empty?
end&.each do |a| end&.each do |a|
arduino = reading.arduinos.build local_id: a[:id], raspberry: raspberry arduino = reading.arduinos.find_or_initialize_by(local_id: a[:id], raspberry: raspberry)
a[:sensores].each do |s| a[:sensores].each do |s|
arduino.sensors.build(sensor_params s) arduino.sensors.build(sensor_params s)
@ -73,7 +73,7 @@ class ReadingsController < ActionController::API
:sample, :sample,
:signature, :signature,
coordinates: %i[lat lng]).tap do |p| coordinates: %i[lat lng]).tap do |p|
p[:timestamp] = Time.at p[:timestamp] p[:timestamp] = Time.at p[:timestamp].to_i
rescue TypeError rescue TypeError
end end
end end

View file

@ -0,0 +1,21 @@
# frozen_string_literal: true
# Elimina arduinos multiplicados
class DeduplicateArduinos < ActiveRecord::Migration[6.1]
def up
Raspberry.find_each do |raspberry|
unique_local_ids = raspberry.arduinos.distinct(:local_id).pluck(:local_id)
unique_local_ids.each do |unique_local_id|
arduinos = raspberry.arduinos.where(local_id: unique_local_id)
unique_arduino = arduinos.first
duplicated_arduinos = arduinos.where.not(id: unique_arduino.id)
result = Sensor.where(arduino_id: duplicated_arduinos.pluck(:id)).update_all(arduino_id: unique_arduino.id)
duplicated_arduinos.destroy_all if result
end
end
end
def down; end
end