mirror of
http://ghproxy.cn/https://github.com/multunus/onemdm-server
synced 2025-12-06 02:04:57 +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
|
||||
|
||||
Reference in New Issue
Block a user