mirror of
http://ghproxy.cn/https://github.com/multunus/onemdm-server
synced 2025-12-06 10:14:59 +00:00
Display status in admin console, Change the filters to include relevant fields, scopes for device status
This commit is contained in:
@@ -17,10 +17,22 @@ ActiveAdmin.register Device do
|
||||
index do
|
||||
selectable_column
|
||||
id_column
|
||||
column "Status" do | device |
|
||||
status = device.status
|
||||
status_tag status.titleize, STATUS_CLASSES[status.to_sym]
|
||||
end
|
||||
column "Model Name",:model
|
||||
column :created_at
|
||||
column :updated_at
|
||||
column :last_heartbeat_recd_time
|
||||
actions
|
||||
end
|
||||
filter :model
|
||||
filter :created_at
|
||||
filter :updated_at
|
||||
filter :last_heartbeat_recd_time
|
||||
|
||||
scope :active
|
||||
scope :missing
|
||||
scope :dead
|
||||
end
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
class Device < ActiveRecord::Base
|
||||
enum status: [:active,:missing,:dead]
|
||||
|
||||
attr_accessor :status
|
||||
|
||||
validates :unique_id, :model, presence: true
|
||||
validates :unique_id, uniqueness: true
|
||||
|
||||
before_create :generate_access_token
|
||||
after_create :update_last_heartbeats_time
|
||||
|
||||
has_many :heartbeats, dependent: :destroy
|
||||
|
||||
scope :active, -> {where("last_heartbeat_recd_time > '#{Time.now.utc - ACTIVE_TIMEFRAME}'")}
|
||||
scope :missing, -> {where("last_heartbeat_recd_time < '#{Time.now.utc - ACTIVE_TIMEFRAME}'AND last_heartbeat_recd_time > '#{Time.now.utc - MISSING_TIMEFRAME}'")}
|
||||
scope :dead, -> {where("last_heartbeat_recd_time < '#{Time.now.utc - MISSING_TIMEFRAME}'")}
|
||||
|
||||
def generate_access_token
|
||||
self.access_token = SecureRandom.uuid
|
||||
end
|
||||
@@ -17,4 +27,15 @@ class Device < ActiveRecord::Base
|
||||
def next_heartbeat_time
|
||||
(self.last_heartbeat_recd_time + HEARTBEAT_INTERVAL).to_i
|
||||
end
|
||||
|
||||
def status
|
||||
time_elapsed = Time.now - self.last_heartbeat_recd_time
|
||||
if time_elapsed < ACTIVE_TIMEFRAME
|
||||
self.status = Device.statuses.keys[0]
|
||||
elsif time_elapsed < MISSING_TIMEFRAME
|
||||
self.status = Device.statuses.keys[1]
|
||||
else
|
||||
self.status = Device.statuses.keys[2]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -35,3 +35,4 @@ module OnemdmServer
|
||||
config.generators.helper = false
|
||||
end
|
||||
end
|
||||
STATUS_CLASSES ={active: :ok,missing: :warning,dead: :error}
|
||||
|
||||
@@ -39,4 +39,8 @@ Rails.application.configure do
|
||||
# Raises error for missing translations
|
||||
# config.action_view.raise_on_missing_translations = true
|
||||
HEARTBEAT_INTERVAL = 1.minute
|
||||
|
||||
ACTIVE_TIMEFRAME = 1.hours
|
||||
MISSING_TIMEFRAME = 2.hours
|
||||
|
||||
end
|
||||
|
||||
@@ -77,4 +77,7 @@ Rails.application.configure do
|
||||
# Do not dump schema after migrations.
|
||||
config.active_record.dump_schema_after_migration = false
|
||||
HEARTBEAT_INTERVAL = 1.hour
|
||||
|
||||
ACTIVE_TIMEFRAME = 12.hours
|
||||
MISSING_TIMEFRAME = 48.hours
|
||||
end
|
||||
|
||||
@@ -40,4 +40,8 @@ Rails.application.configure do
|
||||
# Raises error for missing translations
|
||||
# config.action_view.raise_on_missing_translations = true
|
||||
HEARTBEAT_INTERVAL = 1.hour
|
||||
|
||||
ACTIVE_TIMEFRAME = 12.hours
|
||||
MISSING_TIMEFRAME = 48.hours
|
||||
|
||||
end
|
||||
|
||||
@@ -22,4 +22,35 @@ RSpec.describe Device, type: :model do
|
||||
expect(Device.last.next_heartbeat_time).not_to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "Device status" do
|
||||
let!(:device){FactoryGirl.create(:device)}
|
||||
it "Active" do
|
||||
expect(device.status).to eql Device.statuses.keys[0]
|
||||
end
|
||||
it "Missing" do
|
||||
device.update_attribute(:last_heartbeat_recd_time,Time.now - ACTIVE_TIMEFRAME)
|
||||
expect(device.status).to eql Device.statuses.keys[1]
|
||||
end
|
||||
it "Dead" do
|
||||
device.update_attribute(:last_heartbeat_recd_time,Time.now - MISSING_TIMEFRAME)
|
||||
expect(device.status).to eql Device.statuses.keys[2]
|
||||
end
|
||||
describe "Scope" do
|
||||
it "Active" do
|
||||
expect(Device.active).to eq([device])
|
||||
end
|
||||
it "Missing" do
|
||||
device.update_attribute(:last_heartbeat_recd_time,
|
||||
(Time.now - 15.hours))
|
||||
expect(Device.missing).to eq([device])
|
||||
end
|
||||
it "Dead" do
|
||||
device.update_attribute(:last_heartbeat_recd_time,
|
||||
Time.now - 1.week)
|
||||
expect(Device.dead).to eq([device])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user