-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2020 from FarmBot/watchdog_reporting
Watchdog reporting
- Loading branch information
Showing
11 changed files
with
120 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Devices | ||
class Watchdog < Mutations::Command | ||
def execute | ||
affected_users | ||
end | ||
|
||
private | ||
|
||
def lower_limit | ||
@lower_limit ||= 2.days.ago | ||
end | ||
|
||
def upper_limit | ||
@upper_limit ||= 8.hours.ago | ||
end | ||
|
||
def time_window | ||
@time_window ||= lower_limit..upper_limit | ||
end | ||
|
||
def affected_users | ||
@affected_users ||= User.where(device_id: devices.pluck(:id)) | ||
end | ||
|
||
def eligible_devices | ||
@eligible_devices ||= Device | ||
.where(last_watchdog: nil) | ||
.or(Device.where("last_watchdog < ?", lower_limit)) | ||
end | ||
|
||
def devices | ||
eligible_devices.where(last_saw_api: time_window) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
db/migrate/20201120162403_add_watchdog_triggered_at_to_devices.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class AddWatchdogTriggeredAtToDevices < ActiveRecord::Migration[6.0] | ||
def change | ||
add_column :devices, :last_watchdog, :datetime | ||
remove_column :devices, :needs_reset, :boolean # Not used | ||
remove_column :devices, :last_saw_mq, :datetime # 0 production use | ||
|
||
if Object.const_defined?("Device") | ||
# Prevent a wave of watchdog alerts after first | ||
# deployment. | ||
puts "===== Setting `last_watchdog` to default value:" | ||
Device.update_all(last_watchdog: Time.now) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require "spec_helper" | ||
|
||
describe Devices::CreateSeedData do | ||
it "passes `none`" do | ||
device = FactoryBot.create(:device) | ||
previous_peripherals_count = device.peripherals.count | ||
previous_pin_bindings_count = device.pin_bindings.count | ||
previous_plants_count = device.plants.count | ||
previous_sensors_count = device.sensors.count | ||
previous_sequences_count = device.sequences.count | ||
previous_tool_slots_count = device.tool_slots.count | ||
previous_tools_count = device.tools.count | ||
Devices::CreateSeedData.run!(device: device, product_line: "none") | ||
device.reload | ||
expect(device.peripherals.count).to eq(previous_peripherals_count) | ||
expect(device.pin_bindings.count).to eq(previous_pin_bindings_count) | ||
expect(device.plants.count).to eq(previous_plants_count) | ||
expect(device.sensors.count).to eq(previous_sensors_count) | ||
expect(device.sequences.count).to eq(previous_sequences_count) | ||
expect(device.tool_slots.count).to eq(previous_tool_slots_count) | ||
expect(device.tools.count).to eq(previous_tools_count) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
require 'spec_helper' | ||
require "spec_helper" | ||
|
||
describe Devices::Update do | ||
let(:user) { FactoryBot.create(:user) } | ||
let(:device) { user.device } | ||
let(:user) { FactoryBot.create(:user) } | ||
let(:device) { user.device } | ||
|
||
|
||
it 'updates an existing device' do | ||
it "updates an existing device" do | ||
previous_name = device.name | ||
p = { last_saw_mq: Time.now.utc, name: "Farmbot", needs_reset: true } | ||
Devices::Update.run!({device: device}, p) | ||
p = { name: "Farmbot1231231" } | ||
Devices::Update.run!({ device: device }, p) | ||
device.reload | ||
expect(device.name).to eq(p[:name]) | ||
expect(device.needs_reset).to eq(p[:needs_reset]) | ||
expect(device.last_saw_mq.hour).to eq(p[:last_saw_mq].hour) | ||
expect(device.last_saw_mq.min).to eq(p[:last_saw_mq].min) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require "spec_helper" | ||
|
||
describe Devices::Watchdog do | ||
it "barks at devices when they go offline > 8 hours" do | ||
destroy_everything! | ||
too_old = FactoryBot | ||
.create(:user) | ||
.device | ||
.update!(last_saw_api: 3.days.ago, name: "too_old") | ||
woof_woof = FactoryBot | ||
.create(:user) | ||
.device | ||
.update!(last_saw_api: 9.hours.ago, name: "woof_woof") | ||
too_soon = FactoryBot | ||
.create(:user) | ||
.device | ||
.update!(last_saw_api: 6.hours.ago, name: "too_soon") | ||
actual = Devices::Watchdog.run!().map(&:device).map(&:name) | ||
expect(actual).to eq(["woof_woof"]) | ||
end | ||
end |