123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- // Test Observable //
- import mongoose, { Model } from "mongoose";
- import { Subject, from, map } from "rxjs";
- const used = process.memoryUsage();
- let MongooseConnection: mongoose.Connection
- let connectionStatus = 0
- let mongoStorage: any = {
- type: `MongoDB`,
- url: `mongodb://192.168.100.59:27017/fromEnzo`
- }
- let data: any[] = []
- // Connect to designated storage destination
- async function connectMongo(storage: Storage) {
- return new Promise((resolve, reject) => {
- try {
- console.log(`Connecting to ${storage.url}`)
- MongooseConnection = mongoose.createConnection(storage.url)
- connectionStatus = 1
- resolve(connectionStatus)
- }
- catch (error) {
- connectionStatus = 0
- console.error('An error occurred while connecting to the database:', error);
- setTimeout(() => {
- connectMongo(storage).then(() => {
- resolve(connectionStatus)
- })
- console.log(`Reconnecting...`)
- }, 3000);
- }
- })
- }
- // Acquire data from Mongo
- async function getMongoData(storage: Storage) {
- return new Promise<any>(async (resolve, reject) => {
- await connectMongo(storage);
- const Message: Model<any> = MongooseConnection.model('Message', require('../types/message.schema'));
- try {
- data = await Message.find().limit(100000)
- resolve(data)
- } catch (err) {
- console.error(err);
- }
- })
- }
- /* --------------- TEST --------------- */
- let testSubject: Subject<any> = new Subject()
- let count = 0
- // Using Subjects
- // getMongoData(mongoStorage).then((data) => {
- // /* putting array into next */
- // testSubject.next(data) // returns the entire array as a single value
- // })
- /* Letting the system to load the array 1 by 1 into the subject. Speed subject to system/machine */
- getMongoData(mongoStorage).then((data) => {
- data.forEach(element => {
- testSubject.next(element)
- })
- })
- testSubject.subscribe({
- next: (e) => {
- count++
- // console.log(count + '. ' + e.appData.msgId)
- console.log(count + '. ' + e[0])
- }
- });
- /* -------------- TEST 2 ------------- */
- // Just making Observable from an array
- // getMongoData(mongoStorage).then((data) => {
- // let observableArray = from(data)
- // observableArray.subscribe({
- // next(x: any) {
- // count ++
- // console.log(count + '. ' + x.appData.msgId)
- // },
- // error(err) {
- // console.error('something wrong occurred: ' + err);
- // },
- // complete() {
- // console.log('done');
- // },
- // })
- // })
- /* Additional Test */
- // let pipedObs = testSubject.pipe(
- // map((element) => {
- // let finalResponse: any = {
- // message: 'Piped element'
- // }
- // return finalResponse
- // })
- // )
- // pipedObs.subscribe(element => {
- // count ++
- // console.log(element.message + count)
- // })
- // function doThis() {
- // count++
- // console.log(`Task ${count} completed.`)
- // return count
- // }
- // // for (let i = 0; i < 500; i++) {
- // // setTimeout(() => {
- // // testSubject.next(doThis())
- // // }, 500)
- // // }
- // function callFunctionRepeatedlyWithFixedDelay() {
- // let count = 0;
- // const intervalId = setInterval(() => {
- // testSubject.next(doThis())
- // count++;
- // if (count === 500) {
- // clearInterval(intervalId);
- // }
- // }, 1000);
- // }
- // callFunctionRepeatedlyWithFixedDelay()
- // testSubject.subscribe((value) => {
- // console.log(`Task ${value} acknowledged!`)
- // })
|