Class: Agents::RecordLinkAgent

Inherits:
Agent
  • Object
show all
Defined in:
lib/huginn_record_link_agent/record_link_agent.rb

Instance Method Summary collapse

Instance Method Details



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 370

def create_record_link(event, data)
  source_system = data['source_system']
  source_type = data['source_type']
  source_id = data['source_id']

  target_system = data['target_system']
  target_type = data['target_type']
  target_id = data['target_id']

  log("Creating RecordLink from #{source_system} #{source_type} #{source_id} to #{target_system} #{target_type} #{target_id}")
  results = HuginnRecordLinkAgent::RecordLinkBuilder.create_links(user, source_system, source_type, source_id, target_system, target_type, target_id)

  if results[:link_status] == 200
    if boolify(options['emit_each']) || results[:links].length == 1
      results[:links].each do |record_link|
        payload = { link_status: results[:link_status] }.merge(record_link)
        emit(data, event, payload) if boolify(options['emit_events'])
      end
    else
      # :results will be a hash object with :link_status and :links keys
      payload = results
      emit(data, event, payload) if boolify(options['emit_events'])
    end

  else
    # :results will be a hash object with :link_status and :error_detail keys
    payload = results
    emit(data, event, payload) if boolify(options['emit_events'])
  end

end

#default_optionsObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 194

def default_options
  {
    'expected_receive_period_in_days' => '1',
    'source_system' => 'Target System',
    'source_type' => 'Target Model',
    'source_id' => '123',
    'target_system' => 'Target System',
    'target_type' => 'Target Type',
    'target_id' => '123',
    'create_link' => 'true',
    'emit_each' => 'true',
    'emit_events' => 'true',
    'output_mode' => 'clean',
  }
end

#emit(data, event, payload) ⇒ Object

————— UTILITY METHODS —————#



465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 465

def emit(data, event, payload)

  if (payload[:link_status] == 200)
    payload = { record_link: payload }
  else
    payload = { link_error: payload }
  end

  base_event = data['output_mode'].to_s == 'merge' ? event.payload.dup : {}
  payload = base_event.merge(payload)

  create_event(payload: payload)
end


443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 443

def find_all_links(event, data)
  # Required Params
  record_system = data['record_system']
  record_type = data['record_type']
  record_id = data['record_id']

  # Optional Filters
  filter_system = data['filter_system']
  filter_type = data['filter_type']

  # Ensure we have an array for iteration purposes
  lookup_ids = record_id.respond_to?('each') ? lookup_id : [lookup_id]
  results = HuginnRecordLinkAgent::RecordLinkLookupTool.lookup_records(user, record_system, record_type, record_ids, filter_system, filter_type)

  payloads = HuginnRecordLinkAgent::RecordLinkPayloadBuilder.build_payloads(results, boolify(options['emit_each']), boolify(options['require_all']))
  payloads.each do |payload|
    emit(data, event, payload) if boolify(options['emit_events'])
  end
end


402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 402

def find_source_links(event, data)
  # Required Params
  target_system = data['target_system']
  target_type = data['target_type']
  target_id = data['target_id']

  # Optional Filters
  source_system = data['source_system']
  source_type = data['source_type']

  # Ensure we have an array for iteration purposes
  lookup_ids = target_id.respond_to?('each') ? target_id : [target_id]
  results = HuginnRecordLinkAgent::RecordLinkLookupTool.lookup_records(user, target_system, target_type, lookup_ids, source_system, source_type, 'source')

  payloads = HuginnRecordLinkAgent::RecordLinkPayloadBuilder.build_payloads(results, boolify(options['emit_each']), boolify(options['require_all']))
  payloads.each do |payload|
    emit(data, event, payload) if boolify(options['emit_events'])
  end
end


422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 422

def find_target_links(event, data)
  # Required Params
  source_system = data['source_system']
  source_type = data['source_type']
  source_id = data['source_id']
  # Optional Filters
  target_system = data['target_system']
  target_type = data['target_type']

  # Ensure we have an array for iteration purposes
  lookup_ids = source_id.respond_to?('each') ? source_id : [source_id]
  results = HuginnRecordLinkAgent::RecordLinkLookupTool.lookup_records(user, source_system, source_type, lookup_ids, target_system, target_type, 'target')
  log(results.inspect)

  payloads = HuginnRecordLinkAgent::RecordLinkPayloadBuilder.build_payloads(results, boolify(options['emit_each']), boolify(options['require_all']))
  log(payloads.inspect)
  payloads.each do |payload|
    emit(data, event, payload) if boolify(options['emit_events'])
  end
end

#receive(incoming_events) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 349

def receive(incoming_events)
  incoming_events.each do |event|

    data = interpolated(event)

    if boolify(data['create_link'])
      create_record_link(event, data)

    elsif (data['lookup_type'] == 'source')
      find_source_links(event, data)

    elsif(data['lookup_type'] == 'target')
      find_target_links(event, data)

    else
      find_all_links(event, data)
    end

  end
end

#validate_all_lookup_paramsObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 329

def validate_all_lookup_params
  unless options['record_system'].present?
    errors.add(:base, "when fetching all record links `record_system` is a required field")
  end

  unless options['record_type'].present?
    errors.add(:base, "when fetching all record links `record_type` is a required field")
  end

  unless options['record_id'].present?
    errors.add(:base, "when fetching all record links `record_id` is a required field")
  end

  if options.has_key?('filter_type')
    unless options.has_key?('filter_system')
      errors.add(:base, "If `filter_type` is provided, `filter_system` is required")
    end
  end
end

#validate_create_paramsObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 258

def validate_create_params

  if options.has_key?('emit_each') && !boolify(options['emit_each'])
    errors.add(:base, "when `create_link` is true, `emit_each` must be true if provided")
  end

  unless options['source_system'].present?
    errors.add(:base, "when creating record links `source_system` is a required field")
  end

  unless options['source_type'].present?
    errors.add(:base, "when creating record links `source_type` is a required field")
  end

  unless options['source_id'].present?
    errors.add(:base, "when creating record links `source_id` is a required field")
  end

  unless options['target_system'].present?
    errors.add(:base, "when creating record links `target_system` is a required field")
  end

  unless options['target_type'].present?
    errors.add(:base, "when creating record links `target_type` is a required field")
  end

  unless options['target_id'].present?
    errors.add(:base, "when creating record links `target_id` is a required field")
  end
end

#validate_optionsObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 220

def validate_options

  if options.has_key?('emit_each') && boolify(options['emit_each']).nil?
    errors.add(:base, 'when provided, `emit_each` bust be either true or false')
  end

  if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
    errors.add(:base, "if provided, emit_events must be true or false")
  end

  if options['output_mode'].present? && !options['output_mode'].to_s.include?('{') && !%[clean merge].include?(options['output_mode'].to_s)
    errors.add(:base, "if provided, output_mode must be 'clean' or 'merge'")
  end

  if options.has_key?('create_link') && boolify(options['create_link'])
    self.validate_create_params

  elsif options.has_key?('lookup_type')
    if options['lookup_type'] == 'source'
      self.validate_source_lookup_params

    elsif options['lookup_type'] == 'target'
      self.validate_target_lookup_params

    elsif options['lookup_type'] == 'all'
      self.validate_all_lookup_params

    else
      errors.add(:base, "when provided, `lookup_type` must be one of `source`, `target` or `all`")
    end

  else
    # By default, this agent returns ALL record links
    self.validate_all_lookup_params

  end
end

#validate_source_lookup_paramsObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 309

def validate_source_lookup_params
  unless options['target_system'].present?
    errors.add(:base, "when fetching source links `target_system` is a required field")
  end

  unless options['target_type'].present?
    errors.add(:base, "when fetching source links `target_type` is a required field")
  end

  unless options['target_id'].present?
    errors.add(:base, "when fetching source links `target_id` is a required field")
  end

  if options.has_key?('source_type')
    unless options.has_key?('source_system')
      errors.add(:base, "when looking up linked source records, if `source_type` is provided, `source_system` is required")
    end
  end
end

#validate_target_lookup_paramsObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 289

def validate_target_lookup_params
  unless options['source_system'].present?
    errors.add(:base, "when fetching target links `source_system` is a required field")
  end

  unless options['source_type'].present?
    errors.add(:base, "when fetching target links `source_type` is a required field")
  end

  unless options['source_id'].present?
    errors.add(:base, "when fetching target links `source_id` is a required field")
  end

  if options.has_key?('target_type')
    unless options.has_key?('target_system')
      errors.add(:base, "when looking up linked target records, if `target_type` is provided, `target_system` is required")
    end
  end
end

#working?Boolean

Returns:

  • (Boolean)


210
211
212
213
214
215
216
217
218
# File 'lib/huginn_record_link_agent/record_link_agent.rb', line 210

def working?
  return false if recent_error_logs?

  if interpolated['expected_receive_period_in_days'].present?
    return false unless last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago
  end

  true
end