Skip to content

Commit

Permalink
revert first_reading_at changes
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Nov 21, 2024
1 parent bdbd2ee commit 402cf96
Show file tree
Hide file tree
Showing 12 changed files with 4 additions and 108 deletions.
2 changes: 1 addition & 1 deletion app/lib/presenters/device_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions app/models/raw_storer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 0 additions & 14 deletions app/models/storer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion app/views/v0/devices/_device.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ json.(
:state,
:system_tags,
:user_tags,
:first_reading_at,
:last_reading_at,
:created_at,
:updated_at
Expand Down
5 changes: 0 additions & 5 deletions db/migrate/20241114053104_add_first_reading_at_to_devices.rb

This file was deleted.

3 changes: 1 addition & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
18 changes: 0 additions & 18 deletions lib/tasks/devices.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 0 additions & 28 deletions spec/models/raw_storer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})})
Expand Down
29 changes: 0 additions & 29 deletions spec/models/storer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
Expand Down
4 changes: 0 additions & 4 deletions spec/presenters/device_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/v0/devices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/v1/devices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 402cf96

Please sign in to comment.