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:
@@ -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