test3.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Test Observable //
  2. import mongoose, { Model } from "mongoose";
  3. import { Subject, from, map } from "rxjs";
  4. const used = process.memoryUsage();
  5. let MongooseConnection: mongoose.Connection
  6. let connectionStatus = 0
  7. let mongoStorage: any = {
  8. type: `MongoDB`,
  9. url: `mongodb://192.168.100.59:27017/fromEnzo`
  10. }
  11. let data: any[] = []
  12. // Connect to designated storage destination
  13. async function connectMongo(storage: Storage) {
  14. return new Promise((resolve, reject) => {
  15. try {
  16. console.log(`Connecting to ${storage.url}`)
  17. MongooseConnection = mongoose.createConnection(storage.url)
  18. connectionStatus = 1
  19. resolve(connectionStatus)
  20. }
  21. catch (error) {
  22. connectionStatus = 0
  23. console.error('An error occurred while connecting to the database:', error);
  24. setTimeout(() => {
  25. connectMongo(storage).then(() => {
  26. resolve(connectionStatus)
  27. })
  28. console.log(`Reconnecting...`)
  29. }, 3000);
  30. }
  31. })
  32. }
  33. // Acquire data from Mongo
  34. async function getMongoData(storage: Storage) {
  35. return new Promise<any>(async (resolve, reject) => {
  36. await connectMongo(storage);
  37. const Message: Model<any> = MongooseConnection.model('Message', require('../types/message.schema'));
  38. try {
  39. data = await Message.find().limit(100000)
  40. resolve(data)
  41. } catch (err) {
  42. console.error(err);
  43. }
  44. })
  45. }
  46. /* --------------- TEST --------------- */
  47. let testSubject: Subject<any> = new Subject()
  48. let count = 0
  49. // Using Subjects
  50. // getMongoData(mongoStorage).then((data) => {
  51. // /* putting array into next */
  52. // testSubject.next(data) // returns the entire array as a single value
  53. // })
  54. /* Letting the system to load the array 1 by 1 into the subject. Speed subject to system/machine */
  55. getMongoData(mongoStorage).then((data) => {
  56. data.forEach(element => {
  57. testSubject.next(element)
  58. })
  59. })
  60. testSubject.subscribe({
  61. next: (e) => {
  62. count++
  63. // console.log(count + '. ' + e.appData.msgId)
  64. console.log(count + '. ' + e[0])
  65. }
  66. });
  67. /* -------------- TEST 2 ------------- */
  68. // Just making Observable from an array
  69. // getMongoData(mongoStorage).then((data) => {
  70. // let observableArray = from(data)
  71. // observableArray.subscribe({
  72. // next(x: any) {
  73. // count ++
  74. // console.log(count + '. ' + x.appData.msgId)
  75. // },
  76. // error(err) {
  77. // console.error('something wrong occurred: ' + err);
  78. // },
  79. // complete() {
  80. // console.log('done');
  81. // },
  82. // })
  83. // })
  84. /* Additional Test */
  85. // let pipedObs = testSubject.pipe(
  86. // map((element) => {
  87. // let finalResponse: any = {
  88. // message: 'Piped element'
  89. // }
  90. // return finalResponse
  91. // })
  92. // )
  93. // pipedObs.subscribe(element => {
  94. // count ++
  95. // console.log(element.message + count)
  96. // })
  97. // function doThis() {
  98. // count++
  99. // console.log(`Task ${count} completed.`)
  100. // return count
  101. // }
  102. // // for (let i = 0; i < 500; i++) {
  103. // // setTimeout(() => {
  104. // // testSubject.next(doThis())
  105. // // }, 500)
  106. // // }
  107. // function callFunctionRepeatedlyWithFixedDelay() {
  108. // let count = 0;
  109. // const intervalId = setInterval(() => {
  110. // testSubject.next(doThis())
  111. // count++;
  112. // if (count === 500) {
  113. // clearInterval(intervalId);
  114. // }
  115. // }, 1000);
  116. // }
  117. // callFunctionRepeatedlyWithFixedDelay()
  118. // testSubject.subscribe((value) => {
  119. // console.log(`Task ${value} acknowledged!`)
  120. // })