test3b.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* ----------------------- TEST3B {Mongo to File} ----------------------- */
  2. /* This test is focusing on comparing 2 different arrays of message logs from 2 different storage.
  3. Which is cloud mongo storage as the control/source, and then comparing the data from local file
  4. server data, and then synchronizing them */
  5. import { Observable, map, Subject, takeUntil, take, of, timer, from } from "rxjs";
  6. import { BaseMessage } from "../dependencies/msgutil/dependencies/dependencies";
  7. import { LogSetting, MessageLog } from "../dependencies/log/type/datatype";
  8. import { AcknowledgementService } from "../services/acknowledgement.service";
  9. import { IncomingMessageService } from "../services/incomingMessage.service";
  10. import { MessageAuditorService } from "../services/message-auditor.service";
  11. import { ErrorTrigger, MessageSynchronisationServiceSetting } from "../type/datatype";
  12. import { StreamingService } from "./test-streamOBS";
  13. /* Pre - Defined Data && Settings */
  14. const stream = new StreamingService()
  15. // Declare source Services && Observables (Using File Storage) Simulating Full Logs
  16. const source_synchronize = new MessageAuditorService()
  17. const source_payload: Observable<BaseMessage> = stream.stream().pipe(take(2))
  18. const source_incoming = new IncomingMessageService()
  19. const source_payload_subject: Subject<BaseMessage> = new Subject()
  20. source_payload.subscribe({
  21. next: (data) => {
  22. source_payload_subject.next(<BaseMessage>data)
  23. // console.log(data)
  24. }
  25. })
  26. // Declare target Services && Observables (Using MongoDB Storage) Simulating Partial Logs
  27. const target_payload: Observable<BaseMessage> = stream.stream().pipe(take(4))
  28. const target_payload_subject: Subject<BaseMessage> = new Subject()
  29. const target_incoming = new IncomingMessageService()
  30. target_payload.subscribe({
  31. next: (data) => {
  32. target_payload_subject.next(<BaseMessage>data)
  33. },
  34. error: e => console.error(e),
  35. complete: () => { `Target Payload Completed` }
  36. })
  37. // testing to see if data is sent in
  38. target_payload_subject.subscribe({
  39. next: element => {
  40. console.log(`target_payload_subject emits :00 ${element.header.messageID}`)
  41. }
  42. })
  43. source_payload_subject.subscribe({
  44. next: element => {
  45. console.log(`source_payload_subject emits :00 ${element.header.messageID}`)
  46. }
  47. })
  48. // Declare Source Storage
  49. let source_storage: LogSetting = {
  50. cacheMessageLimit: 0,
  51. storage: "MongoDB",
  52. setting: {
  53. appName: 'Default from client',
  54. appLocName: 'To be generated in client',
  55. logLocName: 'To be generated in client',
  56. },
  57. customSetting: {
  58. srv: true,
  59. user: "testDB",
  60. password: "h1nt1OyXw6QeUnzS",
  61. server: "cluster0.29sklte.mongodb.net",
  62. database: "log",
  63. }
  64. }
  65. let source_dataSet: LogSetting & { incomingObservable: Observable<BaseMessage> } = {
  66. storage: source_storage.storage,
  67. setting: source_storage.setting,
  68. customSetting: source_storage.customSetting,
  69. incomingObservable: source_payload_subject
  70. }
  71. //Declare Target Storage
  72. let target_storage: LogSetting = {
  73. storage: "File",
  74. setting: {
  75. appName: 'Default from client',
  76. appLocName: 'To be generated in client',
  77. logLocName: 'To be generated in client',
  78. }
  79. }
  80. let target_dataSet: LogSetting & { incomingObservable: Observable<BaseMessage> } = {
  81. storage: target_storage.storage,
  82. setting: target_storage.setting,
  83. customSetting: target_storage.customSetting,
  84. incomingObservable: target_payload_subject
  85. }
  86. // Combine source and target storage to form MessageSynchronisationServiceSetting
  87. let settings: MessageSynchronisationServiceSetting = {
  88. incomingSource: {
  89. //all of the settings to be combined here
  90. ...target_storage,
  91. tags: ['Incoming']
  92. }, //LogSetting & {tags:string[] },
  93. target: {
  94. ...source_storage,
  95. tags: ['Incoming']
  96. } //LogSetting & {tags:string[] }
  97. }
  98. /* -------- SYNCHRONIZATION --------- */
  99. function initializeData() {
  100. source_incoming.init(source_dataSet)
  101. target_incoming.init(target_dataSet)
  102. }
  103. // Done by appoximately 5-8 Seconds
  104. initializeData()
  105. source_synchronize.init(settings)
  106. // by 10th second
  107. setTimeout(() => {
  108. let sampleError: ErrorTrigger = {
  109. status: 1,
  110. message: "NO. I dont want to work"
  111. }
  112. let triggerSync = from([sampleError])
  113. let sync = source_synchronize.subscribe(triggerSync)
  114. sync.subscribe({
  115. next: (msgToBeSynched) => {
  116. // console.log(`synching ... ${msgToBeSynched.header.messageID}`)
  117. source_payload_subject.next(msgToBeSynched)
  118. }
  119. })
  120. }, 7000)//30s