mirror of
http://ghproxy.cn/https://github.com/multunus/onemdm-server
synced 2025-12-06 02:04:57 +00:00
Add heartbeat model, next_heartbeat_time and update_heartbeat_time on device registration
This commit is contained in:
@@ -2,8 +2,19 @@ class Device < ActiveRecord::Base
|
||||
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
|
||||
|
||||
def generate_access_token
|
||||
self.access_token = SecureRandom.uuid
|
||||
end
|
||||
|
||||
def update_last_heartbeats_time
|
||||
self.last_heartbeat_recd_time = self.updated_at
|
||||
self.save
|
||||
end
|
||||
|
||||
def next_heartbeat_time
|
||||
(self.last_heartbeat_recd_time + HEARTBEAT_INTERVAL).to_i
|
||||
end
|
||||
end
|
||||
|
||||
14
app/models/heartbeat.rb
Normal file
14
app/models/heartbeat.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
class Heartbeat < ActiveRecord::Base
|
||||
belongs_to :device, counter_cache: :heartbeats_count
|
||||
|
||||
after_create :update_device_latest_heartbeat_recd_time
|
||||
|
||||
def update_device_latest_heartbeat_recd_time
|
||||
self.device.last_heartbeat_recd_time = self.created_at
|
||||
self.device.save
|
||||
end
|
||||
|
||||
def next_heartbeat_time
|
||||
device.next_heartbeat_time
|
||||
end
|
||||
end
|
||||
@@ -38,4 +38,5 @@ Rails.application.configure do
|
||||
|
||||
# Raises error for missing translations
|
||||
# config.action_view.raise_on_missing_translations = true
|
||||
HEARTBEAT_INTERVAL = 1.hour
|
||||
end
|
||||
|
||||
@@ -76,4 +76,5 @@ Rails.application.configure do
|
||||
|
||||
# Do not dump schema after migrations.
|
||||
config.active_record.dump_schema_after_migration = false
|
||||
HEARTBEAT_INTERVAL = 1.hour
|
||||
end
|
||||
|
||||
@@ -39,4 +39,5 @@ Rails.application.configure do
|
||||
|
||||
# Raises error for missing translations
|
||||
# config.action_view.raise_on_missing_translations = true
|
||||
HEARTBEAT_INTERVAL = 1.hour
|
||||
end
|
||||
|
||||
9
db/migrate/20151109071857_create_heartbeats.rb
Normal file
9
db/migrate/20151109071857_create_heartbeats.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class CreateHeartbeats < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :heartbeats do |t|
|
||||
t.references :device, index: true, foreign_key: true
|
||||
|
||||
t.timestamps null: false
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
class AddHeartbeatsDetailsToDevices < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :devices, :heartbeats_count, :integer, default: 0
|
||||
add_column :devices, :last_heartbeat_recd_time, :datetime
|
||||
end
|
||||
end
|
||||
17
db/schema.rb
17
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20151016122334) do
|
||||
ActiveRecord::Schema.define(version: 20151109072450) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -54,8 +54,19 @@ ActiveRecord::Schema.define(version: 20151016122334) do
|
||||
t.string "unique_id"
|
||||
t.string "imei_number"
|
||||
t.string "access_token"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "heartbeats_count", default: 0
|
||||
t.datetime "last_heartbeat_recd_time"
|
||||
end
|
||||
|
||||
create_table "heartbeats", force: :cascade do |t|
|
||||
t.integer "device_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
add_index "heartbeats", ["device_id"], name: "index_heartbeats_on_device_id", using: :btree
|
||||
|
||||
add_foreign_key "heartbeats", "devices"
|
||||
end
|
||||
|
||||
6
spec/factories/heartbeats.rb
Normal file
6
spec/factories/heartbeats.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
FactoryGirl.define do
|
||||
factory :heartbeat do
|
||||
association :device
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Device, type: :model do
|
||||
let(:device) { create(:device) }
|
||||
let!(:device) { create(:device) }
|
||||
|
||||
it { should validate_presence_of :unique_id }
|
||||
it { should validate_presence_of :model }
|
||||
@@ -12,4 +12,14 @@ RSpec.describe Device, type: :model do
|
||||
expect(device.access_token).not_to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "heartbeat" do
|
||||
it "last heartbeat time to updated with updated_at time" do
|
||||
expect(Device.last.last_heartbeat_recd_time).not_to be_nil
|
||||
end
|
||||
|
||||
it "next hearbeat time" do
|
||||
expect(Device.last.next_heartbeat_time).not_to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
15
spec/models/heartbeat_spec.rb
Normal file
15
spec/models/heartbeat_spec.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Heartbeat, type: :model do
|
||||
let(:heartbeat){create(:heartbeat)}
|
||||
|
||||
it "update device heartbeat details" do
|
||||
device = heartbeat.device
|
||||
expect(device.last_heartbeat_recd_time).to eq(heartbeat.created_at)
|
||||
expect(device.heartbeats_count).to eq(1)
|
||||
end
|
||||
|
||||
it "next heartbeat" do
|
||||
expect(heartbeat.next_heartbeat_time).not_to be_nil
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user