message-auditor.service.ts 6.0 KB

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