test3c.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* ----------------------- TEST3C {Mongo to Mongo} ----------------------- */
  2. /* This test is focusing on comparing 2 different arrays of message logs from 2 different storage.
  3. Which is local file mongo as the control/source, and then comparing the data from cloud mongoDB
  4. server data, and then synchronizing them */
  5. import { Observable, Subject, take } from "rxjs";
  6. import { ErrorTrigger, MessageSynchronisationServiceSetting } from "../type/datatype";
  7. import { StreamingService } from "./test-streamOBS";
  8. import { MessageAuditorService } from "../services/message-auditor.service";
  9. import { LogSetting, MessageLog } from "../dependencies/log/type/datatype";
  10. import { BaseMessage, ResponseMessage } from "../dependencies/log/dependencies/msgutil/interface/export";
  11. import { LoggingService } from "../dependencies/log/interface/export";
  12. /* Pre - Defined Data && Settings */
  13. // This service will stream the messages from the local testRequest.json messages
  14. // into the designated location that will be specified later.
  15. const stream = new StreamingService()
  16. /* Using the instance of the streaming declared earlier, we feed 4 messages into the
  17. subscribers that are going to subsscribe to this source_payload. Please note that
  18. source_payload will emite the messages stream from the instance of stream service
  19. and further feed them into the other Subject which is called source_payload_subject. */
  20. const publisher_sync = new MessageAuditorService()
  21. const publisher_Log = new LoggingService()
  22. const publisher_take_four_messages: Observable<BaseMessage> = stream.stream().pipe(take(4))
  23. const publisher: Subject<BaseMessage> = new Subject()
  24. publisher.subscribe((e) => {
  25. console.log(`Primary Received ${e.header.messageID}`)
  26. })
  27. publisher_take_four_messages.subscribe({
  28. next: (data) => {
  29. publisher.next(data)
  30. }
  31. })
  32. /* Same thing as the above. The only difference is the we feed only 2 messages
  33. to simulate streaming error. We want to see if it will sync the other 2 later
  34. on. But generall the declarative structure is the same as the above. */
  35. const subscriber_log = new LoggingService()
  36. const subscriber_take_two_messagse: Observable<BaseMessage> = stream.stream().pipe(take(2))
  37. const subscriber: Subject<BaseMessage> = new Subject()
  38. subscriber.subscribe((e) => {
  39. console.log(`Secondary Received ${e.header.messageID}`)
  40. })
  41. subscriber_take_two_messagse.subscribe({
  42. next: (data) => {
  43. subscriber.next(<ResponseMessage>data)
  44. }
  45. })
  46. /* Declare the designated database. I am using windev's mongo storage to store the data.
  47. Hence here, is the block that definte the target and it's associated specifications.
  48. This will be the target and will receive the predefined set of data to be logged as
  49. prepared earlier in the code above.s */
  50. let publisher_storage: LogSetting = {
  51. cacheMessageLimit: 0,
  52. storage: "MongoDB",
  53. setting: {
  54. appName: 'Default from client',
  55. appLocName: 'To be generated in client',
  56. logLocName: 'To be generated in client',
  57. },
  58. customSetting: {
  59. srv: true,
  60. user: "testDB",
  61. password: "h1nt1OyXw6QeUnzS",
  62. server: "cluster0.29sklte.mongodb.net",
  63. database: "test2",
  64. }
  65. }
  66. /* Same as above. Also declaring another designated database. But this one will be used
  67. as the target for synching. For such I purposely push only half the of the completed
  68. dataset in order to test out the sync later. I am using my own cloud atlas mongo
  69. database on this. The address can always be changed. */
  70. let subscriber_storage: LogSetting = {
  71. cacheMessageLimit: 0,
  72. storage: "MongoDB",
  73. setting: {
  74. appName: 'Default from client',
  75. appLocName: 'To be generated in client',
  76. logLocName: 'To be generated in client',
  77. },
  78. customSetting: {
  79. srv: true,
  80. user: "testDB",
  81. password: "h1nt1OyXw6QeUnzS",
  82. server: "cluster0.29sklte.mongodb.net",
  83. database: "test",
  84. }
  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. ...publisher_storage,
  91. tags: ['Incoming']
  92. }, //LogSetting & {tags:string[] },
  93. target: {
  94. ...subscriber_storage,
  95. tags: ['Incoming']
  96. } //LogSetting & {tags:string[] }
  97. }
  98. /* -------- SYNCHRONIZATION --------- */
  99. // This is where the real test begin. THe process before this were merely just configuring
  100. // the settings of where to sync. Here the initial intialize data will first log the
  101. // messages into the designated database as specified earlier.
  102. function initializeData() { // To store the data into the designated databases.
  103. publisher_Log.init(publisher_storage).then(() => {
  104. publisher_Log.subscribe(publisher) // Logging only occurs here
  105. })
  106. subscriber_log.init(subscriber_storage).then(() => {
  107. subscriber_log.subscribe(subscriber) // Logging only occurs here
  108. })
  109. }
  110. // Done by appoximately 5-8 Seconds
  111. initializeData() // Call the function to store the data into the designated databases.
  112. publisher_sync.init(settings)
  113. /* This is where the synchronization logic is called. The errorSubject will act as a trigger
  114. mechanism to execute the synchronization. */
  115. let errorSubject: Subject<ErrorTrigger> = new Subject()
  116. // Subscribe to errorSubject notification
  117. let sync = publisher_sync.subscribe(errorSubject)
  118. sync.subscribe({
  119. next: (msgToBeSynchronized: MessageLog) => {
  120. console.log(`passing missing message: ${msgToBeSynchronized.appData.msgId} into target/secondary subject.`)
  121. // the missing data returned will be pushed (next(message)) into the target payload.
  122. let raw = msgToBeSynchronized.appData.msgPayload
  123. let data: BaseMessage = JSON.parse(<string>raw)
  124. subscriber.next(data)
  125. }
  126. })
  127. // Set time oout for 5 seconds to allow the initial logging stage to complete it's logging
  128. // implementation first before proceedint to trigger the sync
  129. setTimeout(() => {
  130. // This wil act as the trigger error.Although the definition of this error is
  131. // still subject for enhancements in the near future.
  132. let sampleError: ErrorTrigger = {
  133. status: 1,
  134. message: "NO. I dont want to work"
  135. }
  136. errorSubject.next(sampleError)
  137. }, 10000)
  138. /* THis is testing for generating error message to be fed into the error subject
  139. to act as additional trigger to exectute the synchronization when there's no internet
  140. connection. */
  141. const dns = require('dns');
  142. // Function to check internet connectivity. Basically just look up the site of example.com
  143. // using the built in libray of DNS.
  144. function checkInternetConnectivity() {
  145. dns.lookup('example.com', (err) => {
  146. if (err && err.code === 'ENOTFOUND') {
  147. let errorMsg: ErrorTrigger = {
  148. status: 0,
  149. message: `No internet connection`
  150. }
  151. errorSubject.next(errorMsg)
  152. } else {
  153. // Emit a message indicating internet connectivity
  154. // console.log('Internet connection is available');
  155. }
  156. });
  157. }
  158. // Interval time (in milliseconds) for checking connectivity
  159. const intervalTime = 1000; // Check every 1 second
  160. // Start checking connectivity at intervals
  161. const interval = setInterval(checkInternetConnectivity, intervalTime);
  162. // Stop checking connectivity after a certain duration (e.g., 1 minute)
  163. const duration = 60000; // 1 minute
  164. setTimeout(function () {
  165. clearInterval(interval);
  166. console.log('Internet connectivity monitoring stopped');
  167. }, duration);
  168. function countdown() {
  169. let seconds = 0;
  170. const countUpInterval = setInterval(() => {
  171. console.log(`Elapsed seconds: ${seconds}`);
  172. seconds++;
  173. }, 1000); // Update every second (1000 milliseconds)
  174. }
  175. countdown()