|
@@ -1,40 +1,39 @@
|
|
import * as fs from 'fs'
|
|
import * as fs from 'fs'
|
|
-import { _, isObject } from 'lodash'
|
|
|
|
|
|
+import { _, isObject, get } from 'lodash'
|
|
import { Observable, Subject, interval, map, of } from 'rxjs'
|
|
import { Observable, Subject, interval, map, of } from 'rxjs'
|
|
|
|
|
|
export class queryService {
|
|
export class queryService {
|
|
- private dataFromStorage: Subject<any> = new Subject()
|
|
|
|
- private filteredResult: Subject<any> = new Subject()
|
|
|
|
|
|
|
|
public query(storageAddress: Storage, ...conditions: Conditions[]): Observable<any> {
|
|
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
|
|
// 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
|
|
// Temporary version. More defined design will be implemented to cater for different storage locations
|
|
let data = fs.readFileSync(location, 'utf-8')
|
|
let data = fs.readFileSync(location, 'utf-8')
|
|
let dataJson = JSON.parse(data)
|
|
let dataJson = JSON.parse(data)
|
|
let count = 0
|
|
let count = 0
|
|
const intervalId = setInterval(() => {
|
|
const intervalId = setInterval(() => {
|
|
- this.dataFromStorage.next(dataJson[count]);
|
|
|
|
|
|
+ dataFromStorage.next(dataJson[count]);
|
|
count++;
|
|
count++;
|
|
if (count >= 100) {
|
|
if (count >= 100) {
|
|
clearInterval(intervalId);
|
|
clearInterval(intervalId);
|
|
- this.dataFromStorage.complete();
|
|
|
|
|
|
+ dataFromStorage.complete();
|
|
}
|
|
}
|
|
}, 250)
|
|
}, 250)
|
|
}
|
|
}
|
|
|
|
|
|
// Search and Filter: Pure Observables. To be moved out to become a separate service again.
|
|
// 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 => {
|
|
next: element => {
|
|
if (this.filterByKeyValue(element, ...conditions)) {
|
|
if (this.filterByKeyValue(element, ...conditions)) {
|
|
- this.filteredResult.next(element)
|
|
|
|
|
|
+ filteredResult.next(element)
|
|
} else {
|
|
} else {
|
|
// console.log(`${element.header.messageName} does not match search criteria`)
|
|
// console.log(`${element.header.messageName} does not match search criteria`)
|
|
}
|
|
}
|
|
@@ -60,7 +59,8 @@ export class queryService {
|
|
return result
|
|
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 {
|
|
private filterByKeyValue(data, ...conditions): boolean {
|
|
// Merge all conditions into searchObj
|
|
// Merge all conditions into searchObj
|
|
let searchObj = Object.assign({}, ...conditions)
|
|
let searchObj = Object.assign({}, ...conditions)
|
|
@@ -137,6 +137,9 @@ export class queryService {
|
|
}
|
|
}
|
|
|
|
|
|
private filterByDateRange(data: any, dateRange: DateRange): boolean {
|
|
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 start = new Date(dateRange.startDate);
|
|
const end = new Date(dateRange.endDate);
|
|
const end = new Date(dateRange.endDate);
|
|
const target = new Date(data.header.dateCreated);
|
|
const target = new Date(data.header.dateCreated);
|