Jelajahi Sumber

updated to support Observable storage option draft

Enzo 1 tahun lalu
induk
melakukan
4715c44f87
4 mengubah file dengan 131 tambahan dan 44 penghapusan
  1. 2 1
      package.json
  2. 0 35
      schemas/message.ts
  3. 15 8
      services/query.service.ts
  4. 114 0
      test/test4.ts

+ 2 - 1
package.json

@@ -10,7 +10,8 @@
     "start": "node index.js",
     "test1": "node test/test1.js",
     "test2": "node test/test2.js",
-    "test3": "node test/test3.js"
+    "test3": "node test/test3.js",
+    "test4": "node test/test4.js"
   },
   "repository": {
     "type": "git",

+ 0 - 35
schemas/message.ts

@@ -1,35 +0,0 @@
-import mongoose from 'mongoose';
-const { Schema } = mongoose;
-
-const appData = {
-    msgId: {
-        type: String,
-        required: true,
-    },
-    msgLogDateTime: {
-        type: Date,
-        required: true,
-        default: () => Date.now()
-    },
-    msgDateTime: {
-        type: Date,
-        required: true,
-        default: () => Date.now()
-    },
-    msgTag: [String],
-    msgPayload: {
-        type: String,
-        required: true
-    }
-}
-
-const messageSchema = new mongoose.Schema({
-    appLogLocId: {
-        type: String,
-        ref: `appLogLoc`,
-        required: true
-    },
-    appData: appData
-});
-
-module.exports = messageSchema

+ 15 - 8
services/query.service.ts

@@ -1,16 +1,15 @@
-import * as fs from 'fs'
-import {  isObject, get } from 'lodash'
+import { isObject, get } from 'lodash'
 import { Observable, Subject, interval, map, of } from 'rxjs'
 import { DataPrepService } from './dataprep.service'
 import _ = require("lodash")
 export class SearchService {
 
-    private dataPrepService : DataPrepService
-    constructor(){
+    private dataPrepService: DataPrepService
+    constructor() {
         this.dataPrepService = new DataPrepService()
     }
 
-    public callFromOtherClass(){
+    public callFromOtherClass() {
         const t0 = performance.now()
         let i
         for (i = 0; i <= 6000000000; i++) {
@@ -20,10 +19,18 @@ export class SearchService {
         console.log(`Time taken: ${timeTakenInSeconds} seconds to run this function`);
     }
 
-    public query(storageAddress: Storage, ...conditions: Conditions[]): Observable<any> {
+    public query(storage: Storage, ...conditions: Conditions[]): Observable<any> {
         let dataFromStorage: Subject<any> = new Subject()
         let filteredResult: Subject<any> = new Subject()
-        this.dataPrepService.loadObsData(storageAddress, dataFromStorage)
+        // check if storaage is of type ObservableStorage
+        if (typeof storage === "object" && storage !== null && "type" in storage && "ref" in storage) {
+            let obsRef = storage.ref
+            obsRef.subscribe((element) => {
+                dataFromStorage.next(element)
+            })
+        } else {
+            this.dataPrepService.loadObsData(storage, dataFromStorage)
+        }
         this.filterFromObs(dataFromStorage, filteredResult, ...conditions)
         return filteredResult.pipe()
     }
@@ -187,4 +194,4 @@ export interface ObservableStorage {
     ref: Observable<any>
 }
 
-export type Storage = StorageLocation| ObservableStorage
+export type Storage = ObservableStorage | StorageLocation

+ 114 - 0
test/test4.ts

@@ -0,0 +1,114 @@
+/* 
+ */
+import { Observable, Subject, of } from "rxjs"
+import { ObservableStorage, SearchService } from "../services/query.service"
+import { Conditions, Storage } from "../services/query.service"
+import _ = require("lodash")
+import { DataPrepService } from "../services/dataprep.service"
+
+let query = new SearchService()
+let dataPrepService = new DataPrepService()
+
+
+let mongoStorage: Storage = {
+    type: `MongoDB`,
+    url: `mongodb://192.168.100.59:27017/default`
+}
+
+// Array inquiry: should return mutiple data
+let conditions1: Conditions[] = [
+    { 'msgTag': ['Incoming'] }
+]
+
+// Basic inquiry, but with multi search: should return one data
+let conditions2: Conditions[] = [
+    { "msgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
+    { "msgLogDateTime": "2023-01-14T21:50:19.917Z" },
+]
+
+// Value only argument! : should return one data
+let conditions3: Conditions[] = [
+    { "$regex": "cum incidunt maxime voluptatibus" }
+]
+
+// Date Range inquiry: Should return 1 data
+let conditions4: Conditions[] = [
+    {
+        "$dateRange": {
+            "startDate": "2022-04-29T00:00:00.000Z",
+            'endDate': "2022-04-30T00:00:00.000Z",
+            'column': "data.data.appData.msgDateTime"
+        }
+    },
+]
+
+// Multi conditions except for regex search: Should return at least 1 data
+let conditions5: Conditions[] = [
+    {
+        "$dateRange": {
+            "startDate": "2022-04-29T00:00:00.000Z",
+            'endDate': "2022-04-30T00:00:00.000Z",
+            'column': "data.data.appData.msgDateTime"
+        }
+    },
+    { 'msgTag': ['basic'] },
+    { "msgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
+    { "msgLogDateTime": "2023-01-14T21:50:19.917Z" }
+]
+
+// Ultimate search. With all conditions piling at once: Should at least returns 1 data
+let conditions6: Conditions[] = [
+    {
+        "$dateRange": {
+            "startDate": "2022-04-29T00:00:00.000Z",
+            'endDate': "2022-04-30T00:00:00.000Z",
+            'column': "data.data.appData.msgDateTime"
+        }
+    },
+    { "$regex": "maxime voluptatibus ad quasi eveniet" },
+    { 'msgTag': ['basic'] },
+    { "msgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
+]
+
+// should return 1 data
+let conditions7: Conditions[] = [
+    {
+        "$dateRange": {
+            "startDate": "2022-04-29T00:00:00.000Z",
+            'endDate': "2022-04-30T00:00:00.000Z",
+            'column': "data.data.appData.msgDateTime"
+        }
+    },
+    { "$regex": "maxime voluptatibus ad quasi eveniet" },
+    // { 'data.data.appData.msgTag': ['basic'] },
+    { "data.data.appData.msgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
+    { "msgLogDateTime": "2023-01-14T21:50:19.917Z" }
+]
+// should not return anything
+let conditions8: Conditions[] = [
+    {
+        "$dateRange": {
+            "startDate": "2022-04-29T00:00:00.000Z",
+            'endDate': "2022-04-30T00:00:00.000Z",
+            'column': "data.data.appData.msgDateTime"
+        }
+    },
+    { "$regex": "maxime voluptatibus ad quasi eveniet" },
+    { 'msgTag': ['basic'] },
+    { "data.data.appDatamsgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
+    { "header.msgLogDateTime": "2023-01-14T21:50:19.917Z" }
+]
+
+
+let obsStorage : Subject<any> = new Subject()
+dataPrepService.loadObsData(mongoStorage, obsStorage)
+
+
+const testObs: ObservableStorage = {
+    type: "observable",
+    ref: obsStorage
+}
+
+query.query(testObs, ...conditions1).subscribe((element) => {
+    console.log(element.appData.msgId)
+})