Compare commits
3 commits
3ef7da3317
...
a7809d13e8
Author | SHA1 | Date | |
---|---|---|---|
|
a7809d13e8 | ||
|
e085d5345d | ||
|
3a1b78f238 |
2 changed files with 23 additions and 2 deletions
|
@ -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
|
||||||
|
|
21
db/migrate/20220514213623_deduplicate_arduinos.rb
Normal file
21
db/migrate/20220514213623_deduplicate_arduinos.rb
Normal 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
|
Loading…
Reference in a new issue