Jelajahi Sumber

upgraded to include array search

Enzo 1 tahun lalu
induk
melakukan
0c2276bcfc
4 mengubah file dengan 389 tambahan dan 320 penghapusan
  1. 2 1
      generate_data.ts
  2. 365 309
      payload.json
  3. 13 6
      services/query.service.ts
  4. 9 4
      test/test1.ts

+ 2 - 1
generate_data.ts

@@ -2,6 +2,7 @@ import * as fs from "fs"
 import { faker } from '@faker-js/faker';
 
 let GigaPayload: any[] = []
+let tags = ['free', 'basic', 'business', 'enterprise', 'rich', 'super-rich', 'mega-rich', 'empire'];
 
 export function createMessage(): any {
   return {
@@ -44,7 +45,7 @@ export function createMessage(): any {
           msgId: faker.datatype.uuid(),
           msgLogDateTime: faker.date.past(),
           msgDateTime: faker.date.past(),
-          msgTag: faker.helpers.arrayElement(['free', 'basic', 'business', 'enterprise', 'rich', 'super-rich', 'mega-rich', 'empire']),
+          msgTag: faker.helpers.arrayElements(tags, 3),
           msgPayload: faker.lorem.sentences()
         }
       }

File diff ditekan karena terlalu besar
+ 365 - 309
payload.json


+ 13 - 6
services/query.service.ts

@@ -6,7 +6,7 @@ export class queryService {
     private dataFromStorage: Subject<any> = new Subject()
     private filteredResult: Subject<any> = new Subject()
 
-    public query(storageAddress: Storage, ...conditions: Entries[]): Observable<any> {
+    public query(storageAddress: Storage, ...conditions: Conditions[]): Observable<any> {
         this.loadObsData(storageAddress.address)
         this.filterFromObs(...conditions)
         return this.filteredResult.pipe()
@@ -29,14 +29,14 @@ export class queryService {
     }
 
     // Search and Filter: Pure Observables. To be moved out to become a separate service again.
-    private filterFromObs(...conditions: Entries[]) {
+    private filterFromObs(...conditions: Conditions[]) {
         this.dataFromStorage.subscribe({
             next: element => {
                 // Logic to check if data meets the conditions, if so, put it into result.next{}
                 if (this.hasKeyAndValue(element, ...conditions)) {
                     this.filteredResult.next(element)
                 } else {
-                    console.log(`${element.header.messageID} does not match search criteria`)
+                    // console.log(`${element.header.messageName} does not match search criteria`)
                 }
             }
         })
@@ -75,7 +75,12 @@ export class queryService {
                 let objectValue = _.get(object, lodashPath);
                 let searchValue = searchObj[key];
 
-                if (typeof searchValue === 'object' && typeof objectValue === 'object') {
+                if (Array.isArray(searchValue) && key === 'msgTag') {
+                    // Check if any of the search values are included in the object value
+                    return searchValue.some((value) => {
+                        return Array.isArray(objectValue) ? objectValue.includes(value) : objectValue === value;
+                    });
+                } else if (typeof searchValue === 'object' && typeof objectValue === 'object') {
                     return isMatchingObject(objectValue);
                 } else {
                     return objectValue === searchValue;
@@ -83,6 +88,8 @@ export class queryService {
             });
         };
 
+
+
         let isObjectMatching = (object) => {
             if (typeof object !== 'object') {
                 return false;
@@ -112,7 +119,7 @@ export class queryService {
 
 
 // Entries that client will use. Subject to be improved later on
-export interface Entries {
+export interface Conditions {
     _id?: string,
     appLogLocId?: string,
     msgId?: string,
@@ -120,7 +127,7 @@ export interface Entries {
     msgDateTime?: Date | string,
     msgTag?: string[],
     msgPayload?: string,
-    messageID: string
+    messageID?: string
 }
 
 export interface Storage {

+ 9 - 4
test/test1.ts

@@ -1,6 +1,6 @@
 import { Observable } from "rxjs"
 import { queryService } from "../services/query.service"
-import { Entries, Storage } from "../services/query.service"
+import { Conditions, Storage } from "../services/query.service"
 import { _, isObject } from 'lodash'
 
 let query = new queryService()
@@ -10,12 +10,17 @@ let storageAddress: Storage = {
     address: "payload.json"
 }
 
-let conditions: any[] = [
-    { "msgDateTime": "2023-01-25T02:54:01.434Z" },
-    { "msgTag": "enterprise" }
+let conditions: Conditions[] = [
+    { 'msgTag': ['free'] }
+]
+let conditions2: Conditions[] = [
+    { "msgId": "4f710c4b-a258-4c7e-a4b6-6095bb7028e9" },
+    { "msgLogDateTime": "2023-01-14T21:50:19.917Z" },
 ]
 query.query(storageAddress, ...conditions).subscribe((element) => { console.log(`${element.header.messageName} is matched`) })
 
+// query.query(storageAddress, ...conditions2).subscribe((element) => { console.log(`${element.header.messageName} is matched`) })
+
 // the key is to do it in one line. Client just pass 2 arguments, one is the location of the data, which could be file, sql or mongodb, and also
 // pass in the conditions of their search enquiries. We will aslo have to cater to different file storage location to determine how to prep the
 // data to be filtered

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini