Browse Source

enhance search utility subject fix

Enzo 1 year ago
parent
commit
cc2a385eec
2 changed files with 18 additions and 14 deletions
  1. 17 14
      services/query.service.ts
  2. 1 0
      test/test1.ts

+ 17 - 14
services/query.service.ts

@@ -1,40 +1,39 @@
 import * as fs from 'fs'
-import { _, isObject } from 'lodash'
+import { _, isObject, get } from 'lodash'
 import { Observable, Subject, interval, map, of } from 'rxjs'
 
 export class queryService {
-    private dataFromStorage: Subject<any> = new Subject()
-    private filteredResult: Subject<any> = new Subject()
 
     public query(storageAddress: Storage, ...conditions: Conditions[]): Observable<any> {
-        this.loadObsData(storageAddress.address)
-        this.filterFromObs(...conditions)
-        return this.filteredResult.pipe()
+        let dataFromStorage: Subject<any> = new Subject()
+        let filteredResult: Subject<any> = new Subject()
+        this.loadObsData(storageAddress.address, dataFromStorage)
+        this.filterFromObs(dataFromStorage, filteredResult, ...conditions)
+        return filteredResult.pipe()
     }
 
     // Data preparations: Purely Observables
-    private loadObsData(location: string) {
+    private loadObsData(location: string, dataFromStorage: Subject<any>) {
         //  Temporary version. More defined design will be implemented to cater for different storage locations
         let data = fs.readFileSync(location, 'utf-8')
         let dataJson = JSON.parse(data)
         let count = 0
         const intervalId = setInterval(() => {
-            this.dataFromStorage.next(dataJson[count]);
+            dataFromStorage.next(dataJson[count]);
             count++;
             if (count >= 100) {
                 clearInterval(intervalId);
-                this.dataFromStorage.complete();
+                dataFromStorage.complete();
             }
         }, 250)
     }
 
     // Search and Filter: Pure Observables. To be moved out to become a separate service again.
-    private filterFromObs(...conditions: Conditions[]) {
-        let searchObj = Object.assign({}, ...conditions)
-        this.dataFromStorage.subscribe({
+    private filterFromObs(dataFromStorage: Subject<any>, filteredResult: Subject<any>, ...conditions: Conditions[]) {
+        dataFromStorage.subscribe({
             next: element => {
                 if (this.filterByKeyValue(element, ...conditions)) {
-                    this.filteredResult.next(element)
+                    filteredResult.next(element)
                 } else {
                     // console.log(`${element.header.messageName} does not match search criteria`)
                 }
@@ -60,7 +59,8 @@ export class queryService {
         return result
     }
 
-    // Logic 2: Success: More superior version than Logic 1 since it can perform flat searches like {messageID : 1234} without specifying its nested properties
+    // Logic 2: Success: More superior version than Logic 1 since it can perform flat searches like {messageID : 1234}
+    //  without specifying its parent property's name. eg: {header.messageID: 1234}
     private filterByKeyValue(data, ...conditions): boolean {
         // Merge all conditions into searchObj
         let searchObj = Object.assign({}, ...conditions)
@@ -137,6 +137,9 @@ export class queryService {
     }
 
     private filterByDateRange(data: any, dateRange: DateRange): boolean {
+        // Lodash implemetation to get the specific property of data
+        let msgDate : string = get(data, 'data.data.appData.msgDateTime')
+        // console.log(msgDate)
         const start = new Date(dateRange.startDate);
         const end = new Date(dateRange.endDate);
         const target = new Date(data.header.dateCreated);

+ 1 - 0
test/test1.ts

@@ -67,6 +67,7 @@ let conditions6: Conditions[] = [
     { "msgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
     { "msgLogDateTime": "2023-01-14T21:50:19.917Z" }
 ]
+query.query(storageAddress, ...conditions4).subscribe((element) => { console.log(`${element.header.messageName} is matched`) })
 query.query(storageAddress, ...conditions6).subscribe((element) => { console.log(`${element.header.messageName} is matched`) })