test3c.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, map, Subject, takeUntil, take, of, timer, from } 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 { BaseMessage, ResponseMessage } from "../dependencies/msgutil/interface/export";
  10. import { LoggingService } from "../dependencies/log/interface/export";
  11. import { LogSetting, MessageLog } from "../dependencies/log/type/datatype";
  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. server: "192.168.100.59:27017",
  60. database: "test"
  61. }
  62. }
  63. /* Same as above. Also declaring another designated database. But this one will be used
  64. as the target for synching. For such I purposely push only half the of the completed
  65. dataset in order to test out the sync later. I am using my own cloud atlas mongo
  66. database on this. The address can always be changed. */
  67. let subscriber_storage: LogSetting = {
  68. cacheMessageLimit: 0,
  69. storage: "MongoDB",
  70. setting: {
  71. appName: 'Default from client',
  72. appLocName: 'To be generated in client',
  73. logLocName: 'To be generated in client',
  74. },
  75. customSetting: {
  76. srv: true,
  77. user: "testDB",
  78. password: "h1nt1OyXw6QeUnzS",
  79. server: "cluster0.29sklte.mongodb.net",
  80. database: "test",
  81. }
  82. }
  83. // Combine source and target storage to form MessageSynchronisationServiceSetting
  84. let settings: MessageSynchronisationServiceSetting = {
  85. incomingSource: {
  86. //all of the settings to be combined here
  87. ...publisher_storage,
  88. tags: ['Incoming']
  89. }, //LogSetting & {tags:string[] },
  90. target: {
  91. ...subscriber_storage,
  92. tags: ['Incoming']
  93. } //LogSetting & {tags:string[] }
  94. }
  95. /* -------- SYNCHRONIZATION --------- */
  96. // This is where the real test begin. THe process before this were merely just configuring
  97. // the settings of where to sync. Here the initial intialize data will first log the
  98. // messages into the designated database as specified earlier.
  99. function initializeData() { // To store the data into the designated databases.
  100. publisher_Log.init(publisher_storage).then(() => {
  101. publisher_Log.subscribe(publisher) // Logging only occurs here
  102. })
  103. subscriber_log.init(subscriber_storage).then(() => {
  104. subscriber_log.subscribe(subscriber) // Logging only occurs here
  105. })
  106. }
  107. // Done by appoximately 5-8 Seconds
  108. initializeData() // Call the function to store the data into the designated databases.
  109. publisher_sync.init(settings)
  110. /* This is where the synchronization logic is called. The errorSubject will act as a trigger
  111. mechanism to execute the synchronization. */
  112. let errorSubject: Subject<ErrorTrigger> = new Subject()
  113. // Subscribe to errorSubject notification
  114. let sync = publisher_sync.subscribe(errorSubject)
  115. sync.subscribe({
  116. next: (msgToBeSynchronized: MessageLog) => {
  117. console.log(`passing missing message: ${msgToBeSynchronized.appData.msgId} into target/secondary subject.`)
  118. // the missing data returned will be pushed (next(message)) into the target payload.
  119. let raw = msgToBeSynchronized.appData.msgPayload
  120. let data : BaseMessage = JSON.parse(<string>raw)
  121. subscriber.next(data)
  122. }
  123. })
  124. // Set time oout for 5 seconds to allow the initial logging stage to complete it's logging
  125. // implementation first before proceedint to trigger the sync
  126. setTimeout(() => {
  127. // This wil act as the trigger error.Although the definition of this error is
  128. // still subject for enhancements in the near future.
  129. let sampleError: ErrorTrigger = {
  130. status: 1,
  131. message: "NO. I dont want to work"
  132. }
  133. errorSubject.next(sampleError)
  134. }, 10000)
  135. /* THis is testing for generating error message to be fed into the error subject
  136. to act as additional trigger to exectute the synchronization when there's no internet
  137. connection. */
  138. const dns = require('dns');
  139. // Function to check internet connectivity. Basically just look up the site of example.com
  140. // using the built in libray of DNS.
  141. function checkInternetConnectivity() {
  142. dns.lookup('example.com', (err) => {
  143. if (err && err.code === 'ENOTFOUND') {
  144. let errorMsg: ErrorTrigger = {
  145. status: 0,
  146. message: `No internet connection`
  147. }
  148. errorSubject.next(errorMsg)
  149. } else {
  150. // Emit a message indicating internet connectivity
  151. // console.log('Internet connection is available');
  152. }
  153. });
  154. }
  155. // Interval time (in milliseconds) for checking connectivity
  156. const intervalTime = 1000; // Check every 1 second
  157. // Start checking connectivity at intervals
  158. const interval = setInterval(checkInternetConnectivity, intervalTime);
  159. // Stop checking connectivity after a certain duration (e.g., 1 minute)
  160. const duration = 60000; // 1 minute
  161. setTimeout(function () {
  162. clearInterval(interval);
  163. console.log('Internet connectivity monitoring stopped');
  164. }, duration);
  165. function countdown() {
  166. let seconds = 0;
  167. const countUpInterval = setInterval(() => {
  168. console.log(`Elapsed seconds: ${seconds}`);
  169. seconds++;
  170. }, 1000); // Update every second (1000 milliseconds)
  171. }
  172. countdown()