import * as fs from 'fs' import { filter, isMatch } from 'lodash' import { Observable, Subject, interval, map, of } from 'rxjs' export class queryService { private dataFromStorage: Subject = new Subject() private filteredResult: Subject = new Subject() public query(storageAddress: Storage, ...conditions: Entries[]) : Observable { this.loadObsData(storageAddress.address) this.filterFromObs(...conditions) return this.filteredResult.pipe() } // Data preparations: Purely Observables private loadObsData(location: string) { let data = fs.readFileSync(location, 'utf-8') let dataJson = JSON.parse(data) let count = 0 const intervalId = setInterval(() => { this.dataFromStorage.next(dataJson[count]); count++; if (count >= 100) { clearInterval(intervalId); this.dataFromStorage.complete(); } }, 1000) } // Search and Filter: Pure Observables. To be moved out to become a separate library again. private filterFromObs(...conditions: Entries[]) { this.dataFromStorage.subscribe({ next: element => { if(isMatch(element, conditions)){ // Logic to check if data meets the conditions, if so, put it into result.next{} this.filteredResult.next(element) } } }) } } // Entries that client will use. Subject to be improved later on export interface Entries { _id?: string, appLogLocId?: string, msgId?: string, msgLogDateTime?: Date | string, msgDateTime?: Date | string, msgTag?: string[], msgPayload?: string } export interface Storage { type: string, address: string }