Class: Agents::RecordLinkAgent
- Inherits:
-
Agent
- Object
- Agent
- Agents::RecordLinkAgent
- Defined in:
- lib/huginn_record_link_agent/record_link_agent.rb
Instance Method Summary collapse
- #create_record_link(event, data) ⇒ Object
- #default_options ⇒ Object
-
#emit(data, event, payload) ⇒ Object
————— UTILITY METHODS —————#.
- #find_all_links(event, data) ⇒ Object
- #find_source_links(event, data) ⇒ Object
- #find_target_links(event, data) ⇒ Object
- #receive(incoming_events) ⇒ Object
- #validate_all_lookup_params ⇒ Object
- #validate_create_params ⇒ Object
- #validate_options ⇒ Object
- #validate_source_lookup_params ⇒ Object
- #validate_target_lookup_params ⇒ Object
- #working? ⇒ Boolean
Instance Method Details
#create_record_link(event, data) ⇒ Object
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(['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(['emit_events']) end else # :results will be a hash object with :link_status and :links keys payload = results emit(data, event, payload) if boolify(['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(['emit_events']) end end |
#default_options ⇒ Object
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 { '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 |
#find_all_links(event, data) ⇒ Object
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(['emit_each']), boolify(['require_all'])) payloads.each do |payload| emit(data, event, payload) if boolify(['emit_events']) end end |
#find_source_links(event, data) ⇒ Object
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(['emit_each']), boolify(['require_all'])) payloads.each do |payload| emit(data, event, payload) if boolify(['emit_events']) end end |
#find_target_links(event, data) ⇒ Object
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(['emit_each']), boolify(['require_all'])) log(payloads.inspect) payloads.each do |payload| emit(data, event, payload) if boolify(['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_params ⇒ Object
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 ['record_system'].present? errors.add(:base, "when fetching all record links `record_system` is a required field") end unless ['record_type'].present? errors.add(:base, "when fetching all record links `record_type` is a required field") end unless ['record_id'].present? errors.add(:base, "when fetching all record links `record_id` is a required field") end if .has_key?('filter_type') unless .has_key?('filter_system') errors.add(:base, "If `filter_type` is provided, `filter_system` is required") end end end |
#validate_create_params ⇒ Object
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 .has_key?('emit_each') && !boolify(['emit_each']) errors.add(:base, "when `create_link` is true, `emit_each` must be true if provided") end unless ['source_system'].present? errors.add(:base, "when creating record links `source_system` is a required field") end unless ['source_type'].present? errors.add(:base, "when creating record links `source_type` is a required field") end unless ['source_id'].present? errors.add(:base, "when creating record links `source_id` is a required field") end unless ['target_system'].present? errors.add(:base, "when creating record links `target_system` is a required field") end unless ['target_type'].present? errors.add(:base, "when creating record links `target_type` is a required field") end unless ['target_id'].present? errors.add(:base, "when creating record links `target_id` is a required field") end end |
#validate_options ⇒ Object
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 if .has_key?('emit_each') && boolify(['emit_each']).nil? errors.add(:base, 'when provided, `emit_each` bust be either true or false') end if .has_key?('emit_events') && boolify(['emit_events']).nil? errors.add(:base, "if provided, emit_events must be true or false") end if ['output_mode'].present? && !['output_mode'].to_s.include?('{') && !%[clean merge].include?(['output_mode'].to_s) errors.add(:base, "if provided, output_mode must be 'clean' or 'merge'") end if .has_key?('create_link') && boolify(['create_link']) self.validate_create_params elsif .has_key?('lookup_type') if ['lookup_type'] == 'source' self.validate_source_lookup_params elsif ['lookup_type'] == 'target' self.validate_target_lookup_params elsif ['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_params ⇒ Object
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 ['target_system'].present? errors.add(:base, "when fetching source links `target_system` is a required field") end unless ['target_type'].present? errors.add(:base, "when fetching source links `target_type` is a required field") end unless ['target_id'].present? errors.add(:base, "when fetching source links `target_id` is a required field") end if .has_key?('source_type') unless .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_params ⇒ Object
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 ['source_system'].present? errors.add(:base, "when fetching target links `source_system` is a required field") end unless ['source_type'].present? errors.add(:base, "when fetching target links `source_type` is a required field") end unless ['source_id'].present? errors.add(:base, "when fetching target links `source_id` is a required field") end if .has_key?('target_type') unless .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
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 |