|
@@ -1,14 +1,16 @@
|
|
import { map, Observable, of, Subject } from "rxjs";
|
|
import { map, Observable, of, Subject } from "rxjs";
|
|
import { ErrorTrigger, MessageAuditorServiceInterface, MessageSynchronisationServiceSetting } from "../type/datatype";
|
|
import { ErrorTrigger, MessageAuditorServiceInterface, MessageSynchronisationServiceSetting } from "../type/datatype";
|
|
import { LoggingService } from "../dependencies/log/interface/export";
|
|
import { LoggingService } from "../dependencies/log/interface/export";
|
|
-import { BaseMessage } from "../dependencies/msgutil/interface/export";
|
|
|
|
import { MessageLog } from "../dependencies/log/type/datatype";
|
|
import { MessageLog } from "../dependencies/log/type/datatype";
|
|
|
|
+import { _ } from 'lodash'
|
|
|
|
+import { BaseMessage, RequestMessage, ResponseMessage } from "../dependencies/msgutil/interface/export";
|
|
|
|
|
|
export class MessageAuditorService implements MessageAuditorServiceInterface {
|
|
export class MessageAuditorService implements MessageAuditorServiceInterface {
|
|
private settings: MessageSynchronisationServiceSetting
|
|
private settings: MessageSynchronisationServiceSetting
|
|
private sourceSrc: LoggingService = new LoggingService()
|
|
private sourceSrc: LoggingService = new LoggingService()
|
|
private targetSrc: LoggingService = new LoggingService()
|
|
private targetSrc: LoggingService = new LoggingService()
|
|
private missingMessageSubject: Subject<MessageLog> = new Subject()
|
|
private missingMessageSubject: Subject<MessageLog> = new Subject()
|
|
|
|
+ private filter: any
|
|
|
|
|
|
/* Set up the targets or points of synchronization. This is where it will register the 2 different location of
|
|
/* Set up the targets or points of synchronization. This is where it will register the 2 different location of
|
|
the data to be synchronized */
|
|
the data to be synchronized */
|
|
@@ -26,6 +28,11 @@ export class MessageAuditorService implements MessageAuditorServiceInterface {
|
|
obsTrigger.subscribe({
|
|
obsTrigger.subscribe({
|
|
next: obsTrigger => {
|
|
next: obsTrigger => {
|
|
console.log(obsTrigger.message)// just checking the message
|
|
console.log(obsTrigger.message)// just checking the message
|
|
|
|
+ if (!this.filter) {
|
|
|
|
+ console.log(`No filter applies`)
|
|
|
|
+ } else {
|
|
|
|
+ console.log(`Synchronizating with filters: ${Object.keys(this.filter)}: ${Object.values(this.filter)}`)
|
|
|
|
+ }
|
|
let missingMsg: Observable<MessageLog> = this.synchronize()
|
|
let missingMsg: Observable<MessageLog> = this.synchronize()
|
|
missingMsg.subscribe({
|
|
missingMsg.subscribe({
|
|
next: element => {
|
|
next: element => {
|
|
@@ -38,6 +45,62 @@ export class MessageAuditorService implements MessageAuditorServiceInterface {
|
|
return this.missingMessageSubject
|
|
return this.missingMessageSubject
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public setFilter(filters: any) {
|
|
|
|
+ this.filter = filters
|
|
|
|
+ console.log(`Integrating filters: ${Object.keys(this.filter)} in AuditMessage service`)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /* ________________ Private Functions _________________ */
|
|
|
|
+ // Filtering functions to filter out messages
|
|
|
|
+ private filterData(filters: any, message: MessageLog): boolean {
|
|
|
|
+ let response: boolean = true //Just using this like a statemanagement
|
|
|
|
+ let payload: BaseMessage = JSON.parse(message.appData.msgPayload as string) // Extract the payload from the messageLog first
|
|
|
|
+ // Making a separate function to cater to different multi filter conditions are coded below
|
|
|
|
+ function checkValues(filter): boolean { //FYI, all parameters are string
|
|
|
|
+ let key = Object.keys(filter)
|
|
|
|
+ let value = Object.values(filter)
|
|
|
|
+ let res = _.get(payload, key[0])
|
|
|
|
+ // Check first if the payload has the filtering properties/path
|
|
|
|
+ if (_.has(payload, key[0])) {
|
|
|
|
+ // check if value is equal to fitler's
|
|
|
|
+ if (value == res) {
|
|
|
|
+ return true
|
|
|
|
+ } else {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ console.log(`${key} does not exists in payload`)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (filters) { // if filters is not null
|
|
|
|
+ if (Object.keys(filters).length > 1) {
|
|
|
|
+ let totalCount = Object.keys(filters).length
|
|
|
|
+ let matchedCount = 0
|
|
|
|
+ Object.entries(filters).forEach(([key, value]) => {
|
|
|
|
+ let filter = { [key]: value }
|
|
|
|
+ // console.log(filter)
|
|
|
|
+ if (checkValues(filter) == true) matchedCount++
|
|
|
|
+ })
|
|
|
|
+ if (totalCount == matchedCount) {
|
|
|
|
+ response = true
|
|
|
|
+ } else {
|
|
|
|
+ response = false
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (checkValues(filters) == true) {
|
|
|
|
+ response = true
|
|
|
|
+ } else {
|
|
|
|
+ response = false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ response = true
|
|
|
|
+ }
|
|
|
|
+ return response
|
|
|
|
+ }
|
|
|
|
+
|
|
/* This is where the 'synching' operation takes place. */
|
|
/* This is where the 'synching' operation takes place. */
|
|
private synchronize(): Subject<MessageLog> {
|
|
private synchronize(): Subject<MessageLog> {
|
|
let subjectOutput: Subject<MessageLog> = new Subject()
|
|
let subjectOutput: Subject<MessageLog> = new Subject()
|
|
@@ -68,18 +131,24 @@ export class MessageAuditorService implements MessageAuditorServiceInterface {
|
|
arr1: [],
|
|
arr1: [],
|
|
arr2: []
|
|
arr2: []
|
|
}
|
|
}
|
|
- let set1: MessageLog[]
|
|
|
|
- let set2: MessageLog[]
|
|
|
|
|
|
+ let set1: MessageLog[] = []
|
|
|
|
+ let set2: MessageLog[] = []
|
|
|
|
|
|
// Initiate the source to find the location of the targeted data to be synched.
|
|
// Initiate the source to find the location of the targeted data to be synched.
|
|
this.sourceSrc.init(this.settings.incomingSource).then(() => {
|
|
this.sourceSrc.init(this.settings.incomingSource).then(() => {
|
|
this.targetSrc.init(this.settings.target).then(() => {
|
|
this.targetSrc.init(this.settings.target).then(() => {
|
|
// Filter also carries out the query aspect of the operation, allowing it to acquire all the relevant data.
|
|
// Filter also carries out the query aspect of the operation, allowing it to acquire all the relevant data.
|
|
this.sourceSrc.filter({ msgTag: this.settings.incomingSource.tags[0] }).then((data: MessageLog[]) => {
|
|
this.sourceSrc.filter({ msgTag: this.settings.incomingSource.tags[0] }).then((data: MessageLog[]) => {
|
|
- set1 = data
|
|
|
|
|
|
+ data.forEach((message: MessageLog) => {
|
|
|
|
+ if (this.filterData(this.filter, message)) set1.push(message)
|
|
|
|
+ })
|
|
|
|
+ }).catch((err) => {
|
|
|
|
+ console.error(err.message)
|
|
}).then(() => {
|
|
}).then(() => {
|
|
this.targetSrc.filter({ msgTag: this.settings.target.tags[0] }).then((data: MessageLog[]) => {
|
|
this.targetSrc.filter({ msgTag: this.settings.target.tags[0] }).then((data: MessageLog[]) => {
|
|
- set2 = data
|
|
|
|
|
|
+ data.forEach(message => {
|
|
|
|
+ if (this.filterData(this.filter, message)) set2.push(message)
|
|
|
|
+ })
|
|
allSets.arr1 = set1
|
|
allSets.arr1 = set1
|
|
allSets.arr2 = set2
|
|
allSets.arr2 = set2
|
|
resolve(allSets)
|
|
resolve(allSets)
|
|
@@ -92,7 +161,7 @@ export class MessageAuditorService implements MessageAuditorServiceInterface {
|
|
}
|
|
}
|
|
|
|
|
|
// compare results and return differences
|
|
// compare results and return differences
|
|
- private async checkArrayDifferences(args: { arr1?: any[], arr2?: any[] }): Promise<MessageLog[]> {
|
|
|
|
|
|
+ private async checkArrayDifferences(args: { arr1: MessageLog[], arr2: MessageLog[] }): Promise<MessageLog[]> {
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
let missingMsg: MessageLog[] = []
|
|
let missingMsg: MessageLog[] = []
|
|
args.arr1.forEach((msgElement: MessageLog) => {
|
|
args.arr1.forEach((msgElement: MessageLog) => {
|