Add `read_at` column to `sensor_readings` table. Closes #1363

pull/1635/head
Rick Carlino 2019-12-19 19:32:51 -06:00
parent d2549c0208
commit 077894bb4d
7 changed files with 51 additions and 6 deletions

View File

@ -10,13 +10,14 @@ module SensorReadings
end end
optional do optional do
time :read_at
integer :mode, integer :mode,
in: CeleryScriptSettingsBag::ALLOWED_PIN_MODES, in: CeleryScriptSettingsBag::ALLOWED_PIN_MODES,
default: CeleryScriptSettingsBag::DIGITAL default: CeleryScriptSettingsBag::DIGITAL
end end
def execute def execute
SensorReading.create!(inputs) SensorReading.create!(inputs.merge(read_at: read_at || Time.now))
end end
end end
end end

View File

@ -1,3 +1,10 @@
class SensorReadingSerializer < ApplicationSerializer class SensorReadingSerializer < ApplicationSerializer
attributes :mode, :pin, :value, :x, :y, :z attributes :mode, :pin, :value, :x, :y, :z, :read_at
# This is for legacy support reasons.
# Very old sensor_readings will have a
# read_at value of `nil`, so we pre-populate it
# to `created_at` for the convinience of API users.
def read_at
object.read_at || object.created_at
end
end end

View File

@ -0,0 +1,5 @@
class AddReadAtToSensorReadings < ActiveRecord::Migration[6.0]
def change
add_column :sensor_readings, :read_at, :datetime, default: nil
end
end

View File

@ -1483,7 +1483,8 @@ CREATE TABLE public.sensor_readings (
pin integer, pin integer,
created_at timestamp without time zone NOT NULL, created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL,
mode integer DEFAULT 0 mode integer DEFAULT 0,
read_at timestamp without time zone
); );
@ -3444,6 +3445,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20191107170431'), ('20191107170431'),
('20191119204916'), ('20191119204916'),
('20191203163621'), ('20191203163621'),
('20191219212755'); ('20191219212755'),
('20191220010646');

View File

@ -229,6 +229,7 @@ export function fakeSensorReading(): TaggedSensorReading {
return fakeResource("SensorReading", { return fakeResource("SensorReading", {
id: idCounter++, id: idCounter++,
created_at: "2018-01-11T20:20:38.362Z", created_at: "2018-01-11T20:20:38.362Z",
read_at: "2018-01-11T20:20:38.362Z",
pin: 1, pin: 1,
value: 0, value: 0,
mode: 0, mode: 0,

View File

@ -45,7 +45,7 @@
"coveralls": "3.0.9", "coveralls": "3.0.9",
"enzyme": "3.10.0", "enzyme": "3.10.0",
"enzyme-adapter-react-16": "1.15.1", "enzyme-adapter-react-16": "1.15.1",
"farmbot": "9.0.0-rc1", "farmbot": "9.0.0",
"i18next": "19.0.2", "i18next": "19.0.2",
"install": "0.13.0", "install": "0.13.0",
"lodash": "4.17.15", "lodash": "4.17.15",

View File

@ -7,11 +7,39 @@ describe Api::SensorReadingsController do
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let (:reading) { FactoryBot.create(:sensor_reading, device: user.device) } let (:reading) { FactoryBot.create(:sensor_reading, device: user.device) }
it "sets a default `read_at` value" do
sign_in user
before = SensorReading.count
read_at = Time.now - 5.hours
post :create,
body: {
pin: 13,
value: 128,
x: nil,
y: 1,
z: 2,
mode: 1
}.to_json,
params: { format: :json }
expect(response.status).to eq(200)
expect(json[:created_at]).to eq(json[:read_at])
end
it "makes a sensor reading" do it "makes a sensor reading" do
sign_in user sign_in user
before = SensorReading.count before = SensorReading.count
read_at = Time.now - 5.hours
post :create, post :create,
body: { pin: 13, value: 128, x: nil, y: 1, z: 2, mode: 1 }.to_json, body: {
pin: 13,
value: 128,
x: nil,
y: 1,
z: 2,
mode: 1,
read_at: read_at
}.to_json,
params: { format: :json } params: { format: :json }
expect(response.status).to eq(200) expect(response.status).to eq(200)
@ -24,6 +52,7 @@ describe Api::SensorReadingsController do
expect(json[:z]).to eq(2) expect(json[:z]).to eq(2)
expect(json[:pin]).to eq(13) expect(json[:pin]).to eq(13)
expect(json[:mode]).to eq(1) expect(json[:mode]).to eq(1)
expect(read_at.as_json.first(23)).to eq(json[:read_at].first(23))
expect(before < SensorReading.count).to be_truthy expect(before < SensorReading.count).to be_truthy
end end