test4.ts 8.0 KB

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