message-auditor.service.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { map, Observable, of, Subject } from "rxjs";
  2. import { ErrorTrigger, MessageAuditorServiceInterface, MessageSynchronisationServiceSetting } from "../type/datatype";
  3. import { MessageLog } from "../dependencies/log/type/datatype";
  4. import * as _ from 'lodash'
  5. import { LoggingService } from "../dependencies/log/interface/export";
  6. import { BaseMessage } from "../dependencies/log/dependencies/msgutil/interface/export";
  7. require('dotenv').config();
  8. export class MessageAuditorService implements MessageAuditorServiceInterface {
  9. private settings: MessageSynchronisationServiceSetting
  10. private sourceSrc: LoggingService = new LoggingService()
  11. private targetSrc: LoggingService = new LoggingService()
  12. private missingMessageSubject: Subject<MessageLog> = new Subject()
  13. /* Set up the targets or points of synchronization. This is where it will register the 2 different location of
  14. the data to be synchronized */
  15. public init(settings: MessageSynchronisationServiceSetting): void {
  16. this.settings = settings;
  17. if (settings.filters) {
  18. console.log(`Integrating filters: ${Object.keys(this.settings.filters)} in AuditMessage service`)
  19. }
  20. }
  21. /* This is the main interface of the message sync service. The argument will take in an observable stream of
  22. error notifications, prompting it to perform the subscription of the targeted sources and it's corresponding
  23. target. Essentially, this does not synchronize, but rather it checks against the two sources and compare
  24. and return the missing data, which will then be passed into the targeted subject stream as specified by the
  25. respective client. They can choose how they want to handle the missing messages returned. */
  26. public subscribe(obsTrigger: Observable<ErrorTrigger>): Observable<MessageLog> {
  27. // Subsribe to the errorTrigger obs to listen to any notification.
  28. obsTrigger.subscribe({
  29. next: obsTrigger => {
  30. console.log(obsTrigger.message)// just checking the message
  31. if (!this.settings.filters) {
  32. console.log(`No filters applies`)
  33. } else {
  34. console.log(`Synchronizating with filters: '${Object.keys(this.settings.filters)}': '${Object.values(this.settings.filters)}'`)
  35. }
  36. let missingMsg: Observable<MessageLog> = this.synchronize()
  37. missingMsg.subscribe({
  38. next: element => {
  39. this.missingMessageSubject.next(element)
  40. console.log(`AuditService: Returning missing messages ${element.appData.msgId} ....`)
  41. }
  42. })
  43. }
  44. })
  45. return this.missingMessageSubject
  46. }
  47. /* ________________ Private Functions _________________ */
  48. // Filtering functions to filters out messages
  49. private filterData(filters: any, message: MessageLog): boolean {
  50. let response: boolean = true //Just using this like a statemanagement
  51. let payload: BaseMessage = JSON.parse(message.appData.msgPayload as string) // Extract the payload from the messageLog first
  52. this.checkIfIsInPayloadDataFormat(payload) // Convert stringified nested payload if there's any
  53. // Making a separate function to cater to different multi filters conditions are coded below
  54. if (filters) { // if filters is not null
  55. if (Object.keys(filters).length > 1) {
  56. let totalCount = Object.keys(filters).length
  57. let matchedCount = 0
  58. Object.entries(filters).forEach(([key, value]) => {
  59. let filters = { [key]: value }
  60. // console.log(filters)
  61. if (this.checkValues(payload, filters) == true) matchedCount++
  62. })
  63. if (totalCount == matchedCount) { // check if all the criterias are met
  64. response = true
  65. } else {
  66. response = false
  67. }
  68. } else {
  69. if (this.checkValues(payload, filters) == true) {
  70. response = true
  71. } else {
  72. response = false
  73. }
  74. }
  75. } else { // if not filters is provided. Then the just let response be true so that the data can be further processed
  76. response = true
  77. }
  78. return response
  79. }
  80. /* This is where the 'synching' operation takes place. */
  81. private synchronize(): Subject<MessageLog> {
  82. let subjectOutput: Subject<MessageLog> = new Subject()
  83. // Acquire the data from both location and return them as an array respectively.
  84. this.acquireData().then((data: { arr1: MessageLog[], arr2: MessageLog[] }) => {
  85. // In the case where there are differences in the array length, then extensive comparison
  86. // will be carried out to filters out the differences. Differences are the missing data.
  87. if(process.env.CheckAudit)
  88. {
  89. console.log("Record set 1: ", _.keys(_.countBy(data.arr1,function(data:MessageLog){return data.appData['msgId']})).length);
  90. console.log("Record set 2: ", _.keys(_.countBy(data.arr2,function(data:MessageLog){return data.appData['msgId']})).length);
  91. }
  92. this.checkArrayDifferences(data).then((data: MessageLog[]) => {
  93. if(process.env.CheckAudit)
  94. {
  95. console.log("Difference: ",data.length);
  96. }
  97. data.forEach(msgElement => {
  98. let refined = JSON.parse(JSON.stringify(msgElement))
  99. // Once the missing data has been weeded out, it is then passed into the Subject
  100. // to be returned for the subscribe method.`
  101. subjectOutput.next(refined)
  102. })
  103. })
  104. }).catch((e) => console.error(e))
  105. return subjectOutput
  106. }
  107. /* This is where the targeted data is queried. The process is pretty straightforward. */
  108. private async acquireData(): Promise<any> {
  109. const promiseQuery: Promise<any> = new Promise((resolve, reject) => {
  110. // declare what to expect.
  111. let allSets: { arr1: MessageLog[], arr2: MessageLog[] } = {
  112. arr1: [],
  113. arr2: []
  114. }
  115. let set1: MessageLog[] = []
  116. let set2: MessageLog[] = []
  117. // Initiate the source to find the location of the targeted data to be synched.
  118. this.sourceSrc.init(this.settings.incomingSource).then(() => {
  119. this.targetSrc.init(this.settings.target).then(() => {
  120. // Filter also carries out the query aspect of the operation, allowing it to acquire all the relevant data.
  121. this.sourceSrc.filter({ msgTag: this.settings.incomingSource.tags[0] }).then((data: MessageLog[]) => {
  122. data.forEach((message: MessageLog) => {
  123. if (this.filterData(this.settings.filters, message)) set1.push(message)
  124. })
  125. }).catch((err) => {
  126. console.error(err.message)
  127. }).then(() => {
  128. this.targetSrc.filter({ msgTag: this.settings.target.tags[0] }).then((data: MessageLog[]) => {
  129. data.forEach(message => {
  130. if (this.filterData(this.settings.filters, message)) set2.push(message)
  131. })
  132. allSets.arr1 = set1
  133. allSets.arr2 = set2
  134. resolve(allSets)
  135. })
  136. })
  137. })
  138. })
  139. })
  140. return promiseQuery
  141. }
  142. // compare results and return differences
  143. private async checkArrayDifferences(args: { arr1: MessageLog[], arr2: MessageLog[] }): Promise<MessageLog[]> {
  144. return new Promise((resolve, reject) => {
  145. let missingMsg: MessageLog[] = []
  146. args.arr1.forEach((msgElement: MessageLog) => {
  147. // In this case, we are just checking if the msgId matches within the given the array.
  148. // Just to save time, there's no need to check the entire message structure unless
  149. // the circumstances necessitates it.
  150. if (args.arr2.some(obj => obj.appData.msgId === msgElement.appData.msgId)) {
  151. console.log(`Item Found!`)
  152. } else {
  153. console.log(`This ${msgElement.appData.msgId} is missing`)
  154. missingMsg.push(msgElement)
  155. resolve(missingMsg)
  156. }
  157. })
  158. })
  159. }
  160. // To be used by the filterData function to check between payload values and filter conditions
  161. private checkValues(payload, filters): boolean { //FYI, all parameters are string
  162. let key = Object.keys(filters)
  163. // console.log(Object.values(filters))
  164. let value = Object.values(filters)[0]
  165. let res = _.get(payload, key[0])
  166. // Check first if the payload has the filtering properties/path
  167. if (_.has(payload, key[0])) {
  168. let strarray: string[]
  169. // check array
  170. if (Array.isArray(value)) {
  171. strarray = value as string[]
  172. }
  173. else {
  174. strarray = [value as string]
  175. }
  176. // compare array with that string
  177. if (strarray.includes(res)) {
  178. return true
  179. } else {
  180. return false
  181. }
  182. } else {
  183. console.log(`${key} does not exists in payload`)
  184. return false
  185. }
  186. }
  187. // Check in the case of notification messages, for the nested data properties
  188. // Notification message may have multiple nested data properties that maybe in string format
  189. private checkIfIsInPayloadDataFormat(payload: BaseMessage | any) {
  190. let parsedData: any
  191. if(payload.data.data){
  192. if (payload.data.data.data && typeof payload.data.data.data === 'string') {
  193. parsedData = JSON.parse(payload.data.data.data)
  194. // console.log(parsedData)
  195. payload.data.data.data = parsedData
  196. return payload
  197. } else {
  198. return payload
  199. }
  200. } else {
  201. return payload
  202. }
  203. }
  204. }