Display status in admin console, Change the filters to include relevant fields, scopes for device status

This commit is contained in:
leenasn
2015-11-11 14:37:05 +05:30
parent 8116e4e751
commit da6b70c07b
7 changed files with 77 additions and 1 deletions

View File

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