Class: Agent
- Inherits:
-
Object
- Object
- Agent
- Includes:
- App47Logger, CheckinAble, Mongoid::Document, Mongoid::Timestamps
- Defined in:
- lib/models/agent.rb
Overview
Represents an agent, a specific install of an app on a device.
Class Method Summary collapse
-
.fetch(app, device, json) ⇒ Object
Fetch or create an agent.
Instance Method Summary collapse
-
#android? ⇒ Boolean
Is this agent an Android Agent.
-
#config_data ⇒ Object
return the configuration for this agent.
-
#encrypt_data(data) ⇒ Object
Encrypt the data unique to this agent.
-
#filter_json(json) ⇒ Object
filter only the json parameters we want to take into the device.
-
#find_user ⇒ Object
Find the user by the device id associated with the agent’s device.
-
#insert_metric_data(type, data, agent_ip) ⇒ Object
Main entry point for inserting metric data.
-
#ios? ⇒ Boolean
Is this agent an ios agent?.
-
#notify_new_app_version(new_version) ⇒ Object
Notify the main worker to update the app and agent configuration information with the new version.
-
#update!(attributes = {}) ⇒ Object
Safely update attributes from the document.
-
#update_attributes!(attributes = {}) ⇒ Object
Safely update attributes from the document.
-
#use_version_code? ⇒ Boolean
If the agent is capable of reporting the correct version code, basically is the agent version high enough on the respective platform to have the correct version code to report.
Methods included from App47Logger
log_debug, #log_debug, log_error, #log_error, log_exception, log_message, #log_message, #log_warn, log_warn
Methods included from CheckinAble
Class Method Details
.fetch(app, device, json) ⇒ Object
Fetch or create an agent
78 79 80 81 82 83 84 85 |
# File 'lib/models/agent.rb', line 78 def self.fetch(app, device, json) agent = Agent.find_or_create_by(device: device, app: app) device.apps << app if agent.new_record? # Add this app to the device if agent is new # new_version = json[:app_version] # agent.notify_new_app_version(new_version) unless new_version.nil? || new_version.eql?(agent.app_version) agent.update_attributes!(json) agent end |
Instance Method Details
#android? ⇒ Boolean
Is this agent an Android Agent
56 57 58 |
# File 'lib/models/agent.rb', line 56 def android? platform.eql?('Android') end |
#config_data ⇒ Object
return the configuration for this agent
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/models/agent.rb', line 63 def config_data data = App47Cache.get(config_cache_key) if data.nil? data = build_config_data else data[:server_time_epoch] = Time.now.to_i end App47Cache.set(config_cache_key, data, 30) data end |
#encrypt_data(data) ⇒ Object
Encrypt the data unique to this agent.
158 159 160 161 162 163 164 165 166 |
# File 'lib/models/agent.rb', line 158 def encrypt_data(data) cipher = OpenSSL::Cipher::AES256.new(:CBC) cipher.encrypt user_token = device.unique_identifier user_token += user_token while user_token.length < 48 cipher.key = user_token[0..31] cipher.iv = user_token[32..47] Base64.encode64(cipher.update(data) + cipher.final) end |
#filter_json(json) ⇒ Object
filter only the json parameters we want to take into the device. The agent sends up more than we need for the device.
147 148 149 150 151 152 153 |
# File 'lib/models/agent.rb', line 147 def filter_json(json) filtered_json = {} %i[agent_version app_version app_version_code app_environment].each do |field| filtered_json[field] = json[field] if json[field].present? end filtered_json end |
#find_user ⇒ Object
Find the user by the device id associated with the agent’s device
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/models/agent.rb', line 131 def find_user account = app.account device_identifier = device.unique_identifier device = UserDevice.find_device_by device_identifier user = device.user raise 'Invalid account' unless account.eql?(user.account) device.user rescue StandardError nil end |
#insert_metric_data(type, data, agent_ip) ⇒ Object
Main entry point for inserting metric data
90 91 92 93 94 95 |
# File 'lib/models/agent.rb', line 90 def insert_metric_data(type, data, agent_ip) # If the agent isn't enabled, then drop the data and return a hearty "Thank you!" return true unless app_agent_enabled? send "insert_#{type}_data", data.symbolize_keys, agent_ip end |
#ios? ⇒ Boolean
Is this agent an ios agent?
49 50 51 |
# File 'lib/models/agent.rb', line 49 def ios? platform.eql?('iOS') end |
#notify_new_app_version(new_version) ⇒ Object
Notify the main worker to update the app and agent configuration information with the new version
We also want to force the config to regenerate for the agent as the version will yield different results.
104 105 106 107 108 109 110 111 112 |
# File 'lib/models/agent.rb', line 104 def notify_new_app_version(new_version) # App47Cache.delete config_cache_key # App47Cache.delete policies_cache_key # QueueManager.push_worker job: 'app_upgrade', # app_id: app_id.to_s, # agent_id: id.to_s, # current_version: app_version, # new_version: new_version end |
#update!(attributes = {}) ⇒ Object
Safely update attributes from the document
124 125 126 |
# File 'lib/models/agent.rb', line 124 def update!(attributes = {}) super(filter_json(attributes)) end |
#update_attributes!(attributes = {}) ⇒ Object
Safely update attributes from the document
117 118 119 |
# File 'lib/models/agent.rb', line 117 def update_attributes!(attributes = {}) super(filter_json(attributes)) end |
#use_version_code? ⇒ Boolean
If the agent is capable of reporting the correct version code, basically is the agent version high enough on the respective platform to have the correct version code to report.
40 41 42 43 44 |
# File 'lib/models/agent.rb', line 40 def use_version_code? app.use_version_code?(platform) && (ios? && agent_version.to_f >= 4.4 || android? && agent_version.to_f >= 3.2) rescue StandardError false end |