synchronization.service.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { resolve } from "path";
  2. import { map, Observable, of, Subject } from "rxjs";
  3. import { BaseMessage } from "../dependencies/fisappmessagejsutilty/dependencies/dependencies";
  4. import { LoggingService } from "../dependencies/fisloggingservice/services/logging-service";
  5. import { MessageLog } from "../dependencies/fisloggingservice/type/datatype";
  6. import { MessageSynchronisationServiceInterface, MessageSynchronisationServiceSetting } from "../type/datatype";
  7. export class MessageSyncrhonizationService implements MessageSynchronisationServiceInterface {
  8. private settings: MessageSynchronisationServiceSetting
  9. private sourceSrc: LoggingService = new LoggingService()
  10. private targetSrc: LoggingService = new LoggingService()
  11. public async init(settings: MessageSynchronisationServiceSetting): Promise<void> {
  12. this.settings = settings;
  13. // Wrap promise so that init must be instantiated with resolve before proceeding with subscribe method
  14. let promiseInit: Promise<void> = new Promise((resolve, reject) => {
  15. try {
  16. this.sourceSrc.init(settings.incomingSource).then((data) => {
  17. if (!data) reject()
  18. }).then(() => {
  19. this.targetSrc.init(settings.target).then((data) => {
  20. if (!data) reject()
  21. resolve()
  22. })
  23. })
  24. }
  25. catch (e) {
  26. console.error(e)
  27. }
  28. })
  29. return promiseInit
  30. }
  31. // Incoming obstriger serves as a trigger point to perform another synchronization
  32. public subscribe(obsTrigger: Observable<string>): Observable<BaseMessage> {
  33. // let subjectOutput = this.syncrhonize()
  34. // subjectOutput.subscribe({
  35. // next: missingMsg => console.log(`Synchronizing ${missingMsg.appData.msgId}`)
  36. // })
  37. let result = this.dataConversion()
  38. // let result = this.dataConversion()
  39. // result.subscribe({
  40. // next: e => console.log(e)
  41. // })
  42. return result
  43. }
  44. private dataConversion(): Observable<BaseMessage>{
  45. // let subjectOutput = this.syncrhonize()
  46. let obsOutput: Observable<BaseMessage> = this.syncrhonize().pipe(
  47. map((msg: MessageLog) => {
  48. // console.log(`Converting this ${msg.appData.msgId}`)
  49. return JSON.parse(<string>msg.appData.msgPayload)
  50. })
  51. )
  52. return obsOutput
  53. }
  54. private syncrhonize(): Subject<any> {
  55. let subjectOutput = new Subject()
  56. this.acquireData().then((data: { arr1: MessageLog[], arr2: MessageLog[] }) => {
  57. if (data.arr1.length === data.arr2.length) {
  58. console.log(`No synchronization needed`)
  59. } else {
  60. this.checkArrayDifferences(data).then((data: MessageLog[]) => {
  61. data.forEach(msgElement => {
  62. subjectOutput.next(msgElement)
  63. })
  64. })
  65. }
  66. }).catch((e) => console.error(e))
  67. return subjectOutput
  68. }
  69. // Acquires the available data from designated storage
  70. private async acquireData(): Promise<any> {
  71. const promiseQuery: Promise<any> = new Promise((resolve, reject) => {
  72. let allSets: {
  73. arr1: MessageLog[],
  74. arr2: MessageLog[]
  75. } = {
  76. arr1: [],
  77. arr2: []
  78. }
  79. let set1
  80. let set2
  81. this.sourceSrc.filter({ msgTag: this.settings.incomingSource.tags[0] }).then((data: MessageLog[]) => {
  82. set1 = data
  83. }).then(() => {
  84. this.targetSrc.filter({ msgTag: this.settings.target.tags[0] }).then((data: MessageLog[]) => {
  85. set2 = data
  86. allSets.arr1 = set1
  87. allSets.arr2 = set2
  88. resolve(allSets)
  89. })
  90. })
  91. })
  92. return promiseQuery
  93. }
  94. // compare results and return differences`
  95. private async checkArrayDifferences(args: { arr1?: any[], arr2?: any[] }): Promise<any[]> {
  96. return new Promise((resolve, reject) => {
  97. let missingMsg: MessageLog[] = []
  98. args.arr1.forEach((msgElement: MessageLog) => {
  99. if (args.arr2.some(obj => obj.appData.msgId === msgElement.appData.msgId)) {
  100. console.log(`Item Found!`)
  101. } else {
  102. console.log(`This ${msgElement.appData.msgId} is not found`)
  103. missingMsg.push(msgElement)
  104. resolve(missingMsg)
  105. }
  106. })
  107. })
  108. }
  109. }