Class: SpreeCmCommissioner::Integrations::Base::SyncResult
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Integrations::Base::SyncResult
- Defined in:
- app/services/spree_cm_commissioner/integrations/base/sync_result.rb
Overview
Base class for sync results Provides standardized tracking methods for API calls and record changes
Persists to database as JSONB in CmIntegrationSyncSession#sync_result column Enables comprehensive sync monitoring and debugging:
- Track success/failure status
- Monitor API call counts and patterns
- Audit record creation/update counts
- Capture error details with automatic error codes
- Generate sync performance reports
- Identify patterns in failed syncs
Example persisted results:
Success:
{
"metrics": { "match": { "created": 2, "updated": 1, "total": 3 } },
"api_calls": { "get_matches": 1, "get_match_details": 3 }
}
Failure:
{
"metrics": { "match": { "created": 1, "total": 1 } },
"api_calls": { "get_matches": 1 },
"error": { "message": "Failed to sync matches: Connection timeout", "code": "ExternalClientError" }
}
Defined Under Namespace
Classes: RecordTracker
Instance Method Summary collapse
-
#increment_metric(type, action, amount = 1) ⇒ Object
Increment a metric counter Useful for tracking actions that don’t fit the created/updated pattern.
-
#initialize ⇒ SyncResult
constructor
A new instance of SyncResult.
-
#record_error(error) ⇒ Object
Record error.
-
#success? ⇒ Boolean
Check if sync was successful.
-
#to_h ⇒ Hash
Serialize to hash for JSONB persistence.
-
#track(type, record) { ... } ⇒ Object
Track and sync record with explicit tracker object Provides a tracker object with helper methods for explicit save/tracking operations.
-
#track_api_call(call_name) { ... } ⇒ Object
Track API call with block Automatically increments the call count and executes the block Failed calls (exceptions) are not counted.
-
#track_synced_record(type, record) { ... } ⇒ Object
Track synced record with block Automatically detects if record was created or updated based on new_record? and saved_changes.
Constructor Details
#initialize ⇒ SyncResult
Returns a new instance of SyncResult.
29 30 31 32 33 |
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 29 def initialize @metrics = {} @api_calls = {} @error = nil end |
Instance Method Details
#increment_metric(type, action, amount = 1) ⇒ Object
Increment a metric counter Useful for tracking actions that don’t fit the created/updated pattern
Example:
increment_metric(:match, :archived) # increment by 1
increment_metric(:inventory, :total_quantity_adjusted, 50) # increment by 50
154 155 156 157 158 159 160 |
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 154 def increment_metric(type, action, amount = 1) @metrics[type.to_sym] ||= {} @metrics[type.to_sym][action.to_sym] ||= 0 @metrics[type.to_sym][action.to_sym] += amount update_metric_total(type) end |
#record_error(error) ⇒ Object
Record error
164 165 166 167 168 169 |
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 164 def record_error(error) @error = { message: error., code: error.class.name.split('::').last } end |
#success? ⇒ Boolean
Check if sync was successful
173 174 175 |
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 173 def success? @error.nil? end |
#to_h ⇒ Hash
Serialize to hash for JSONB persistence
179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 179 def to_h { metrics: @metrics, api_calls: @api_calls, error: @error }.tap do |hash| hash.delete(:error) if @error.nil? hash.delete(:metrics) if @metrics.empty? hash.delete(:api_calls) if @api_calls.empty? end end |
#track(type, record) { ... } ⇒ Object
Track and sync record with explicit tracker object Provides a tracker object with helper methods for explicit save/tracking operations
Example:
track(:zone, product) do |tracker|
product.assign_attributes(name: 'Zone A', price: 100)
tracker.save_if_changed!(product)
end
89 90 91 92 93 |
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 89 def track(type, record) tracker = RecordTracker.new(self, type, record) yield tracker tracker end |
#track_api_call(call_name) { ... } ⇒ Object
Track API call with block Automatically increments the call count and executes the block Failed calls (exceptions) are not counted
Example:
external_matches = track_api_call('get_matches') { @client.get_matches! }
45 46 47 48 |
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 45 def track_api_call(call_name) @api_calls[call_name] = (@api_calls[call_name] || 0) + 1 yield end |
#track_synced_record(type, record) { ... } ⇒ Object
Track synced record with block Automatically detects if record was created or updated based on new_record? and saved_changes
Example:
track_synced_record(:match, match_taxon) do
match_taxon.assign_attributes(name: 'ISI Dangkorsenchey FC vs ANGKOR TIGER FC')
match_taxon.save!
end
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/services/spree_cm_commissioner/integrations/base/sync_result.rb', line 63 def track_synced_record(type, record) was_new = record.new_record? result = yield if was_new increment_metric(type, :created) elsif record.saved_changes.any? increment_metric(type, :updated) end result end |