message-auditor.service.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { map, Observable, of, Subject } from "rxjs";
  2. import { ErrorTrigger, MessageAuditorServiceInterface, MessageSynchronisationServiceSetting } from "../type/datatype";
  3. import { LoggingService } from "../dependencies/log/interface/export";
  4. import { BaseMessage } from "../dependencies/msgutil/interface/export";
  5. import { MessageLog } from "../dependencies/log/type/datatype";
  6. export class MessageAuditorService implements MessageAuditorServiceInterface {
  7. private settings: MessageSynchronisationServiceSetting
  8. private sourceSrc: LoggingService = new LoggingService()
  9. private targetSrc: LoggingService = new LoggingService()
  10. private missingMessageSubject: Subject<MessageLog> = new Subject()
  11. /* Set up the targets or points of synchronization. This is where it will register the 2 different location of
  12. the data to be synchronized */
  13. public init(settings: MessageSynchronisationServiceSetting): void {
  14. this.settings = settings;
  15. }
  16. /* This is the main interface of the message sync service. The argument will take in an observable stream of
  17. error notifications, prompting it to perform the subscription of the targeted sources and it's corresponding
  18. target. Essentially, this does not synchronize, but rather it checks against the two sources and compare
  19. and return the missing data, which will then be passed into the targeted subject stream as specified by the
  20. respective client. They can choose how they want to handle the missing messages returned. */
  21. public subscribe(obsTrigger: Observable<ErrorTrigger>): Observable<MessageLog> {
  22. // Subsribe to the errorTrigger obs to listen to any notification.
  23. obsTrigger.subscribe({
  24. next: obsTrigger => {
  25. console.log(obsTrigger.message)// just checking the message
  26. let missingMsg: Observable<MessageLog> = this.synchronize()
  27. missingMsg.subscribe({
  28. next: element => {
  29. this.missingMessageSubject.next(element)
  30. console.log(`AuditService: Returning missing messages ${element.appData.msgId} ....`)
  31. }
  32. })
  33. }
  34. })
  35. return this.missingMessageSubject
  36. }
  37. /* This is where the 'synching' operation takes place. */
  38. private synchronize(): Subject<MessageLog> {
  39. let subjectOutput: Subject<MessageLog> = new Subject()
  40. // Acquire the data from both location and return them as an array respectively.
  41. this.acquireData().then((data: { arr1: MessageLog[], arr2: MessageLog[] }) => {
  42. // In the case where there are differences in the array length, then extensive comparison
  43. // will be carried out to filter out the differences. Differences are the missing data.
  44. this.checkArrayDifferences(data).then((data: MessageLog[]) => {
  45. data.forEach(msgElement => {
  46. let refined = JSON.parse(JSON.stringify(msgElement))
  47. // Once the missing data has been weeded out, it is then passed into the Subject
  48. // to be returned for the subscribe method.`
  49. subjectOutput.next(refined)
  50. })
  51. })
  52. }).catch((e) => console.error(e))
  53. return subjectOutput
  54. }
  55. /* This is where the targeted data is queried. The process is pretty straightforward. */
  56. private async acquireData(): Promise<any> {
  57. const promiseQuery: Promise<any> = new Promise((resolve, reject) => {
  58. // declare what to expect.
  59. let allSets: {
  60. arr1: MessageLog[],
  61. arr2: MessageLog[]
  62. } = {
  63. arr1: [],
  64. arr2: []
  65. }
  66. let set1: MessageLog[]
  67. let set2: MessageLog[]
  68. // Initiate the source to find the location of the targeted data to be synched.
  69. this.sourceSrc.init(this.settings.incomingSource).then(() => {
  70. this.targetSrc.init(this.settings.target).then(() => {
  71. // Filter also carries out the query aspect of the operation, allowing it to acquire all the relevant data.
  72. this.sourceSrc.filter({ msgTag: this.settings.incomingSource.tags[0] }).then((data: MessageLog[]) => {
  73. set1 = data
  74. }).then(() => {
  75. this.targetSrc.filter({ msgTag: this.settings.target.tags[0] }).then((data: MessageLog[]) => {
  76. set2 = data
  77. allSets.arr1 = set1
  78. allSets.arr2 = set2
  79. resolve(allSets)
  80. })
  81. })
  82. })
  83. })
  84. })
  85. return promiseQuery
  86. }
  87. // compare results and return differences
  88. private async checkArrayDifferences(args: { arr1?: any[], arr2?: any[] }): Promise<MessageLog[]> {
  89. return new Promise((resolve, reject) => {
  90. let missingMsg: MessageLog[] = []
  91. args.arr1.forEach((msgElement: MessageLog) => {
  92. // In this case, we are just checking if the msgId matches within the given the array.
  93. // Just to save time, there's no need to check the entire message structure unless
  94. // the circumstances necessitates it.
  95. if (args.arr2.some(obj => obj.appData.msgId === msgElement.appData.msgId)) {
  96. console.log(`Item Found!`)
  97. } else {
  98. console.log(`This ${msgElement.appData.msgId} is missing`)
  99. missingMsg.push(msgElement)
  100. resolve(missingMsg)
  101. }
  102. })
  103. })
  104. }
  105. }