12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import * as fs from 'fs'
- import { filter, isMatch } 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: Entries[]) : Observable<any> {
- 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
- }
|