test3a.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { stat } from "fs";
  2. import { resolve } from "path";
  3. import { Observable, map, Subject, takeUntil, take, of, timer, from } from "rxjs";
  4. import { BaseMessage } from "../dependencies/fisappmessagejsutilty/dependencies/dependencies";
  5. import { LogSetting, MessageLog } from "../dependencies/fisloggingservice/type/datatype";
  6. import { AcknowledgementService } from "../services/acknowledgement.service";
  7. import { IncomingMessageService } from "../services/incomingMessage.service";
  8. import { MessageSyncrhonizationService } from "../services/synchronization.service";
  9. import { MessageSynchronisationServiceSetting } from "../type/datatype";
  10. import { StreamingService } from "./test-streamOBS";
  11. /* Pre - Defined Data && Settings */
  12. const stream = new StreamingService()
  13. // Declare source Services && Observables (Using File Storage) Simulating Full Logs
  14. const source_synchronize = new MessageSyncrhonizationService()
  15. const source_payload: Observable<BaseMessage> = stream.stream()
  16. const source_incoming = new IncomingMessageService()
  17. const source_payload_subject: Subject<BaseMessage> = new Subject()
  18. source_payload.subscribe({
  19. next: (data) => {
  20. source_payload_subject.next(data)
  21. // console.log(data)
  22. }
  23. })
  24. const source_payload_string = source_payload.pipe(
  25. map((data) => {
  26. return JSON.stringify(data);
  27. })
  28. )
  29. // Declare target Services && Observables (Using MongoDB Storage) Simulating Partial Logs
  30. const target_syncrhonize = new MessageSyncrhonizationService()
  31. const target_payload: Observable<BaseMessage> = stream.stream().pipe(take(2))
  32. const target_payload_subject: Subject<BaseMessage> = new Subject()
  33. const target_incoming = new IncomingMessageService()
  34. target_payload.subscribe({
  35. next: (data) => {
  36. target_payload_subject.next(<BaseMessage>data)
  37. },
  38. error: e => console.error(e),
  39. complete: () => { `Target Payload Completed` }
  40. })
  41. const target_payload_string = target_payload.pipe(
  42. map((data) => {
  43. return JSON.stringify(data);
  44. }),
  45. )
  46. // testing to see if data is sent in
  47. target_payload_subject.subscribe({
  48. next: element => {
  49. console.log(`target_payload_subject emits : ${element.header.messageID}`)
  50. // Missing MessageLog Data is sent in
  51. }
  52. })
  53. // Declare Source Storage
  54. let source_storage: LogSetting = {
  55. storage: "File",
  56. setting: {
  57. appName: 'Default from client',
  58. appLocName: 'To be generated in client',
  59. logLocName: 'To be generated in client',
  60. }
  61. }
  62. let source_dataSet: LogSetting & { incomingObservable: Observable<BaseMessage> } = {
  63. storage: source_storage.storage,
  64. setting: source_storage.setting,
  65. customSetting: source_storage.customSetting,
  66. incomingObservable: source_payload_subject
  67. }
  68. //Declare Target Storage
  69. let target_storage: LogSetting = {
  70. storage: "MongoDB",
  71. setting: {
  72. appName: 'Default from client',
  73. appLocName: 'To be generated in client',
  74. logLocName: 'To be generated in client',
  75. },
  76. customSetting: {
  77. srv: true,
  78. user: "testDB",
  79. password: "h1nt1OyXw6QeUnzS",
  80. server: "cluster0.29sklte.mongodb.net",
  81. collection: "log",
  82. }
  83. }
  84. let target_dataSet: LogSetting & { incomingObservable: Observable<BaseMessage> } = {
  85. storage: target_storage.storage,
  86. setting: target_storage.setting,
  87. customSetting: target_storage.customSetting,
  88. incomingObservable: target_payload_subject
  89. }
  90. // Combine source and target storage to form MessageSynchronisationServiceSetting
  91. let settings: MessageSynchronisationServiceSetting = {
  92. incomingSource: {
  93. //all of the settings to be combined here
  94. ...source_storage,
  95. tags: ['Incoming']
  96. }, //LogSetting & {tags:string[] },
  97. target: {
  98. ...target_storage,
  99. tags: ['Incoming']
  100. } //LogSetting & {tags:string[] }
  101. }
  102. const triggerSync = timer(5000).pipe(map(
  103. (value) => String(value)
  104. ))
  105. /* -------- SYNCHRONIZATION --------- */
  106. function initializeData() {
  107. source_incoming.init(source_dataSet)
  108. target_incoming.init(target_dataSet)
  109. }
  110. // Done by 4 seconds
  111. initializeData()
  112. source_synchronize.init(settings)
  113. // by 5th second
  114. setTimeout(() => {
  115. let triggerSync = from(['Newsynch'])
  116. let sync = source_synchronize.subscribe(triggerSync)
  117. sync.subscribe({
  118. next: (msgToBeSynched) => {
  119. // console.log(`synching ... ${msgToBeSynched.header.messageID}`)
  120. target_payload_subject.next(msgToBeSynched)
  121. }
  122. })
  123. }, 30000)//30s
  124. // To DO
  125. // more test files
  126. // synch an additional source message at runtime(after 30sec)
  127. // more comments to be more readable