Skip to content

Commit

Permalink
Add read_at column to sensor_readings table. Closes #1363
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed Dec 20, 2019
1 parent d2549c0 commit 077894b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/mutations/sensor_readings/create.rb
Expand Up @@ -10,13 +10,14 @@ class Create < Mutations::Command
end

optional do
time :read_at
integer :mode,
in: CeleryScriptSettingsBag::ALLOWED_PIN_MODES,
default: CeleryScriptSettingsBag::DIGITAL
end

def execute
SensorReading.create!(inputs)
SensorReading.create!(inputs.merge(read_at: read_at || Time.now))
end
end
end
9 changes: 8 additions & 1 deletion app/serializers/sensor_reading_serializer.rb
@@ -1,3 +1,10 @@
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
5 changes: 5 additions & 0 deletions db/migrate/20191220010646_add_read_at_to_sensor_readings.rb
@@ -0,0 +1,5 @@
class AddReadAtToSensorReadings < ActiveRecord::Migration[6.0]
def change
add_column :sensor_readings, :read_at, :datetime, default: nil
end
end
6 changes: 4 additions & 2 deletions db/structure.sql
Expand Up @@ -1483,7 +1483,8 @@ CREATE TABLE public.sensor_readings (
pin integer,
created_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
);


Expand Down Expand Up @@ -3444,6 +3445,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20191107170431'),
('20191119204916'),
('20191203163621'),
('20191219212755');
('20191219212755'),
('20191220010646');


1 change: 1 addition & 0 deletions frontend/__test_support__/fake_state/resources.ts
Expand Up @@ -229,6 +229,7 @@ export function fakeSensorReading(): TaggedSensorReading {
return fakeResource("SensorReading", {
id: idCounter++,
created_at: "2018-01-11T20:20:38.362Z",
read_at: "2018-01-11T20:20:38.362Z",
pin: 1,
value: 0,
mode: 0,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -45,7 +45,7 @@
"coveralls": "3.0.9",
"enzyme": "3.10.0",
"enzyme-adapter-react-16": "1.15.1",
"farmbot": "9.0.0-rc1",
"farmbot": "9.0.0",
"i18next": "19.0.2",
"install": "0.13.0",
"lodash": "4.17.15",
Expand Down
31 changes: 30 additions & 1 deletion spec/controllers/api/sensor_readings/controller_spec.rb
Expand Up @@ -7,11 +7,39 @@
let(:user) { FactoryBot.create(:user) }
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
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,
body: {
pin: 13,
value: 128,
x: nil,
y: 1,
z: 2,
mode: 1,
read_at: read_at
}.to_json,
params: { format: :json }

expect(response.status).to eq(200)
Expand All @@ -24,6 +52,7 @@
expect(json[:z]).to eq(2)
expect(json[:pin]).to eq(13)
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
end

Expand Down

0 comments on commit 077894b

Please sign in to comment.