mirror of
http://ghproxy.cn/https://github.com/multunus/onemdm-server
synced 2025-12-06 02:04:57 +00:00
Change the app_usages API to accept array of data
- Add foreignkey to device for AppUsage - Rename the route to app_usage from app_usage/create
This commit is contained in:
@@ -3,12 +3,11 @@ class AppUsagesController < ApplicationController
|
|||||||
respond_to :json
|
respond_to :json
|
||||||
|
|
||||||
def create
|
def create
|
||||||
app_usage = AppUsage.new(app_usage_params)
|
begin
|
||||||
if app_usage.save
|
@device.app_usages << AppUsage.create!(app_usage_params)
|
||||||
render json: {},
|
render json: {}, status: :created
|
||||||
status: :created
|
rescue Exception => e
|
||||||
else
|
logger.warn "Error while saving App Usage #{e.message}"
|
||||||
logger.warn "Error while saving App Usage #{app_usage.errors.full_messages}"
|
|
||||||
render json: {},
|
render json: {},
|
||||||
status: :unprocessable_entity
|
status: :unprocessable_entity
|
||||||
end
|
end
|
||||||
@@ -16,10 +15,10 @@ class AppUsagesController < ApplicationController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def app_usage_params
|
def app_usage_params
|
||||||
params.require(:app_usage).
|
params.permit(app_usage:
|
||||||
permit(:package_name,
|
[:package_name,
|
||||||
:usage_duration_in_seconds,
|
:usage_duration_in_seconds,
|
||||||
:used_on)
|
:used_on]).require(:app_usage)
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
class AppUsage < ActiveRecord::Base
|
class AppUsage < ActiveRecord::Base
|
||||||
validates :package_name, :usage_duration_in_seconds, :used_on, presence: true
|
validates :package_name, :usage_duration_in_seconds, :used_on, presence: true
|
||||||
|
belongs_to :device
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ class Device < ActiveRecord::Base
|
|||||||
|
|
||||||
has_many :heartbeats, dependent: :destroy
|
has_many :heartbeats, dependent: :destroy
|
||||||
has_many :installations, dependent: :destroy
|
has_many :installations, dependent: :destroy
|
||||||
|
has_many :app_usages, dependent: :nullify
|
||||||
|
|
||||||
scope :active, -> {where("last_heartbeat_recd_time > '#{Time.now.utc - ACTIVE_TIMEFRAME}'")}
|
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 :missing, -> {where("last_heartbeat_recd_time < '#{Time.now.utc - ACTIVE_TIMEFRAME}'AND last_heartbeat_recd_time > '#{Time.now.utc - MISSING_TIMEFRAME}'")}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
post 'app_usages/create', to: 'app_usages#create', :defaults => { :format => :json }
|
post 'app_usages', to: 'app_usages#create', :defaults => { :format => :json }
|
||||||
|
|
||||||
devise_for :admin_users, ActiveAdmin::Devise.config
|
devise_for :admin_users, ActiveAdmin::Devise.config
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ class CreateAppUsages < ActiveRecord::Migration
|
|||||||
t.string :package_name, null: false, index: false
|
t.string :package_name, null: false, index: false
|
||||||
t.integer :usage_duration_in_seconds, null: false, index: false
|
t.integer :usage_duration_in_seconds, null: false, index: false
|
||||||
t.date :used_on, null: false, index: false
|
t.date :used_on, null: false, index: false
|
||||||
|
t.belongs_to :device, foreign_key: true, index: true
|
||||||
|
|
||||||
t.timestamps null: false
|
t.timestamps null: false
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -53,10 +53,13 @@ ActiveRecord::Schema.define(version: 20160204094618) do
|
|||||||
t.string "package_name", null: false
|
t.string "package_name", null: false
|
||||||
t.integer "usage_duration_in_seconds", null: false
|
t.integer "usage_duration_in_seconds", null: false
|
||||||
t.date "used_on", null: false
|
t.date "used_on", null: false
|
||||||
|
t.integer "device_id"
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
add_index "app_usages", ["device_id"], name: "index_app_usages_on_device_id", using: :btree
|
||||||
|
|
||||||
create_table "apps", force: :cascade do |t|
|
create_table "apps", force: :cascade do |t|
|
||||||
t.string "name"
|
t.string "name"
|
||||||
t.string "package_name"
|
t.string "package_name"
|
||||||
@@ -105,6 +108,7 @@ ActiveRecord::Schema.define(version: 20160204094618) do
|
|||||||
add_index "installations", ["batch_installation_id"], name: "index_installations_on_batch_installation_id", using: :btree
|
add_index "installations", ["batch_installation_id"], name: "index_installations_on_batch_installation_id", using: :btree
|
||||||
add_index "installations", ["device_id"], name: "index_installations_on_device_id", using: :btree
|
add_index "installations", ["device_id"], name: "index_installations_on_device_id", using: :btree
|
||||||
|
|
||||||
|
add_foreign_key "app_usages", "devices"
|
||||||
add_foreign_key "batch_installations", "apps"
|
add_foreign_key "batch_installations", "apps"
|
||||||
add_foreign_key "heartbeats", "devices"
|
add_foreign_key "heartbeats", "devices"
|
||||||
add_foreign_key "installations", "batch_installations"
|
add_foreign_key "installations", "batch_installations"
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ RSpec.describe AppUsagesController, type: :controller do
|
|||||||
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token)
|
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(device.access_token)
|
||||||
end
|
end
|
||||||
it "Valid params" do
|
it "Valid params" do
|
||||||
post :create, app_usage: attributes_for(:app_usage), format: :json
|
post :create, app_usage: [attributes_for(:app_usage)], format: :json
|
||||||
expect(response).to have_http_status(:success)
|
expect(response).to have_http_status(:success)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "InValid Params" do
|
it "InValid Params" do
|
||||||
post :create, app_usage: attributes_for(:invalid_app_usage), format: :json
|
post :create, app_usage: [attributes_for(:invalid_app_usage)], format: :json
|
||||||
expect(response).to have_http_status(:unprocessable_entity)
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ RSpec.describe AppUsage, type: :model do
|
|||||||
it { should validate_presence_of :package_name }
|
it { should validate_presence_of :package_name }
|
||||||
it { should validate_presence_of :usage_duration_in_seconds }
|
it { should validate_presence_of :usage_duration_in_seconds }
|
||||||
it { should validate_presence_of :used_on }
|
it { should validate_presence_of :used_on }
|
||||||
|
it { should belong_to :device }
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user