From 6c4f267c7007b5f01390b373bf1f2bb99f6e817f Mon Sep 17 00:00:00 2001 From: Tim Cowlishaw Date: Thu, 21 Nov 2024 11:01:11 +0100 Subject: [PATCH] revert first_reading_at changes --- app/lib/presenters/device_presenter.rb | 2 +- app/models/raw_storer.rb | 4 --- app/models/storer.rb | 14 --------- app/views/v0/devices/_device.jbuilder | 1 - ...4053104_add_first_reading_at_to_devices.rb | 5 ---- db/schema.rb | 3 +- lib/tasks/devices.rake | 18 ------------ spec/models/raw_storer_spec.rb | 28 ------------------ spec/models/storer_spec.rb | 29 ------------------- spec/presenters/device_presenter_spec.rb | 4 --- spec/requests/v0/devices_spec.rb | 2 +- spec/requests/v1/devices_spec.rb | 2 +- 12 files changed, 4 insertions(+), 108 deletions(-) delete mode 100644 db/migrate/20241114053104_add_first_reading_at_to_devices.rb diff --git a/app/lib/presenters/device_presenter.rb b/app/lib/presenters/device_presenter.rb index 01da9c2f..27565b53 100644 --- a/app/lib/presenters/device_presenter.rb +++ b/app/lib/presenters/device_presenter.rb @@ -13,7 +13,7 @@ def default_options end def exposed_fields - %i{id uuid name description state system_tags user_tags first_reading_at last_reading_at created_at updated_at notify device_token mac_address postprocessing location data_policy hardware owner components} + %i{id uuid name description state system_tags user_tags last_reading_at created_at updated_at notify device_token mac_address postprocessing location data_policy hardware owner components} end def notify diff --git a/app/models/raw_storer.rb b/app/models/raw_storer.rb index 24e4e972..ec903202 100644 --- a/app/models/raw_storer.rb +++ b/app/models/raw_storer.rb @@ -62,10 +62,6 @@ def store data, mac, version, ip, raise_errors=false device.update_columns(last_reading_at: parsed_ts, data: sql_data, state: 'has_published') end - if !device.first_reading_at || parsed_ts < device.first_reading_at - device.update_columns(first_reading_at: parsed_ts) - end - forward_readings(device, [sql_data]) rescue Exception => e diff --git a/app/models/storer.rb b/app/models/storer.rb index 810254db..e8700c23 100644 --- a/app/models/storer.rb +++ b/app/models/storer.rb @@ -13,10 +13,6 @@ def store device, readings update_device_last_data(device, parsed_reading[:parsed_ts], parsed_reading[:sql_data]) end - if index == (readings.length - 1) - update_device_first_reading_at(device, parsed_reading[:parsed_ts]) - end - rescue Exception => e Sentry.capture_exception(e) raise e if Rails.env.test? @@ -25,16 +21,6 @@ def store device, readings forward_readings(device, readings_to_forward) end - def update_device_first_reading_at(device, parsed_ts) - return if parsed_ts <= Time.at(0) - device.transaction do - device.lock! - if !device.first_reading_at || parsed_ts < device.first_reading_at - device.update_columns(first_reading_at: parsed_ts) - end - end - end - def update_device_last_data(device, parsed_ts, sql_data) return if parsed_ts <= Time.at(0) device.transaction do diff --git a/app/views/v0/devices/_device.jbuilder b/app/views/v0/devices/_device.jbuilder index 28a3137b..ec9d72e4 100644 --- a/app/views/v0/devices/_device.jbuilder +++ b/app/views/v0/devices/_device.jbuilder @@ -12,7 +12,6 @@ json.( :state, :system_tags, :user_tags, - :first_reading_at, :last_reading_at, :created_at, :updated_at diff --git a/db/migrate/20241114053104_add_first_reading_at_to_devices.rb b/db/migrate/20241114053104_add_first_reading_at_to_devices.rb deleted file mode 100644 index 4ba9090a..00000000 --- a/db/migrate/20241114053104_add_first_reading_at_to_devices.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddFirstReadingAtToDevices < ActiveRecord::Migration[6.1] - def change - add_column :devices, :first_reading_at, :timestamp - end -end diff --git a/db/schema.rb b/db/schema.rb index 3bec5370..435d9451 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2024_11_14_053104) do +ActiveRecord::Schema.define(version: 2024_11_13_155952) do # These are extensions that must be enabled in order to support this database enable_extension "adminpack" @@ -109,7 +109,6 @@ t.string "hardware_slug_override" t.boolean "precise_location", default: true, null: false t.boolean "enable_forwarding", default: false, null: false - t.datetime "first_reading_at" t.index ["device_token"], name: "index_devices_on_device_token", unique: true t.index ["geohash"], name: "index_devices_on_geohash" t.index ["last_reading_at"], name: "index_devices_on_last_reading_at" diff --git a/lib/tasks/devices.rake b/lib/tasks/devices.rake index c232047e..95ab21b5 100644 --- a/lib/tasks/devices.rake +++ b/lib/tasks/devices.rake @@ -5,22 +5,4 @@ namespace :devices do device.save!(validate: false) end end - - task :set_first_reading_at => :environment do - from_date = Device.order(:created_at).first.created_at - Device.where(state: "has_published").each do |device| - component = device.components.where("last_reading_at IS NOT NULL").first - if component - begin - readings = Kairos.query(id: device.id, sensor_key: component.key, function: "first", rollup: "1s", limit: 1, start_absolute: from_date)["readings"] - first_reading_timestamp = readings[0][0] if readings&.any? - device.update_columns(first_reading_at: first_reading_timestamp) if first_reading_timestamp - rescue JsonNull - nil - end - end - print "." - end - print "\n" - end end diff --git a/spec/models/raw_storer_spec.rb b/spec/models/raw_storer_spec.rb index cd0995c6..69772fb7 100644 --- a/spec/models/raw_storer_spec.rb +++ b/spec/models/raw_storer_spec.rb @@ -62,34 +62,6 @@ def to_ts(time) storer.store(json, device.mac_address, "1.1-0.9.0-A", "127.0.0.1", true) end - context "when the device has no first_reading_at timestamp" do - it "sets the first_reading_at timestamp" do - ts = Time.parse(json[:timestamp]) - storer.store(json, device.mac_address, "1.1-0.9.0-A", "127.0.0.1", true) - expect(device.reload.first_reading_at).to eq(ts) - end - end - - context "when the device's first_reading_at timestamp is after the reading timestamp" do - it "updates the device's first_reading_at timestamp" do - ts = Time.parse(json[:timestamp]) - device.first_reading_at = ts + 1.day - device.save - storer.store(json, device.mac_address, "1.1-0.9.0-A", "127.0.0.1", true) - expect(device.reload.first_reading_at).to eq(ts) - end - end - - context "when the device's first_reading_at timestamp is before the reading timestamp" do - it "does not update the device's first_reading_at timestamp" do - ts = Time.parse(json[:timestamp]) - previous_timestamp = device.first_reading_at = ts - 1.day - device.save - storer.store(json, device.mac_address, "1.1-0.9.0-A", "127.0.0.1", true) - expect(device.reload.first_reading_at).to eq(previous_timestamp) - end - end - it "will not be created with invalid future timestamp" do ts = { timestamp: to_ts(2.days.from_now) } includes_proxy = double({ where: double({last: device.reload})}) diff --git a/spec/models/storer_spec.rb b/spec/models/storer_spec.rb index fb41ee76..5de1fb04 100644 --- a/spec/models/storer_spec.rb +++ b/spec/models/storer_spec.rb @@ -55,35 +55,6 @@ end.not_to raise_error end - context "when the device has no first_reading_at timestamp" do - it "sets the first_reading_at timestamp" do - ts = Time.parse(@data["recorded_at"]) - storer.store(device, [@data]) - expect(device.reload.first_reading_at).to eq(ts) - end - end - - context "when the device's first_reading_at timestamp is after the reading timestamp" do - it "updates the device's first_reading_at timestamp" do - ts = Time.parse(@data["recorded_at"]) - device.first_reading_at = ts + 1.day - device.save - storer.store(device, [@data]) - expect(device.reload.first_reading_at).to eq(ts) - end - end - - context "when the device's first_reading_at timestamp is before the reading timestamp" do - it "does not update the device's first_reading_at timestamp" do - ts = Time.parse(@data["recorded_at"]) - previous_timestamp = device.first_reading_at = ts - 1.day - device.save - storer.store(device, [@data]) - expect(device.reload.first_reading_at).to eq(previous_timestamp) - end - end - - it "updates the component last_reading_at timestamp for each of the provided sensors" do expect(device).to receive(:update_component_timestamps).with( Time.parse(@data['recorded_at']), diff --git a/spec/presenters/device_presenter_spec.rb b/spec/presenters/device_presenter_spec.rb index c4c1fc30..2a89ebc6 100644 --- a/spec/presenters/device_presenter_spec.rb +++ b/spec/presenters/device_presenter_spec.rb @@ -91,10 +91,6 @@ expect(presenter.as_json[:user_tags]).to eq(device.user_tags) end - it "exposes the first_reading_at date" do - expect(presenter.as_json[:first_reading_at]).to eq(device.first_reading_at) - end - it "exposes the last_reading_at date" do expect(presenter.as_json[:last_reading_at]).to eq(device.last_reading_at) end diff --git a/spec/requests/v0/devices_spec.rb b/spec/requests/v0/devices_spec.rb index 43e63d1f..900be4e9 100644 --- a/spec/requests/v0/devices_spec.rb +++ b/spec/requests/v0/devices_spec.rb @@ -24,7 +24,7 @@ expect(json.length).to eq(2) # expect(json[0]['name']).to eq(first.name) # expect(json[1]['name']).to eq(second.name) - expect(json[0].keys).to eq(%w(id uuid name description state system_tags user_tags first_reading_at last_reading_at created_at updated_at notify device_token postprocessing location data_policy hardware owner data experiment_ids)) + expect(json[0].keys).to eq(%w(id uuid name description state system_tags user_tags last_reading_at created_at updated_at notify device_token postprocessing location data_policy hardware owner data experiment_ids)) end describe "when not logged in" do diff --git a/spec/requests/v1/devices_spec.rb b/spec/requests/v1/devices_spec.rb index 40ee0fd6..b782d580 100644 --- a/spec/requests/v1/devices_spec.rb +++ b/spec/requests/v1/devices_spec.rb @@ -25,7 +25,7 @@ expect(json.length).to eq(2) # expect(json[0]['name']).to eq(first.name) # expect(json[1]['name']).to eq(second.name) - expect(json[0].keys).to eq(%w(id uuid name description state system_tags user_tags first_reading_at last_reading_at created_at updated_at notify device_token postprocessing location data_policy hardware owner data experiment_ids)) + expect(json[0].keys).to eq(%w(id uuid name description state system_tags user_tags last_reading_at created_at updated_at notify device_token postprocessing location data_policy hardware owner data experiment_ids)) end describe "when not logged in" do