diff --git a/src/readers/appearanceCheck.ts b/src/readers/appearanceCheck.ts new file mode 100644 index 0000000000000000000000000000000000000000..9a280b47fa20045eb0be062da2dc6ac8f8a2e71b --- /dev/null +++ b/src/readers/appearanceCheck.ts @@ -0,0 +1,54 @@ +import type { InsightModel } from 'ts-common'; +import type { GraphQueryResultMetaFromBackend } from 'ts-common/src/model/webSocket/graphResult'; +import type { SaveState } from 'ts-common'; + +export type AppearanceMap = { + [key: string]: { count: number; queries: string[] }; +}; + +export function appearanceCheck( + result: GraphQueryResultMetaFromBackend, + insight: InsightModel, + ss: SaveState, + queryIndex: number, + appeareanceResult: { + nodeAppearance: AppearanceMap; + edgeAppearances: AppearanceMap; + }, +) { + const nameQuery = ss.queryStates.openQueryArray[queryIndex].name; + + const queryNodesID = result.nodes + .filter(node => !insight.entitiesAppearances?.nodeLabel || node.label === insight.entitiesAppearances.nodeLabel) + .map(node => node._id); + + const queryEdgesID = result.edges + .filter(edge => !insight.entitiesAppearances?.edgeLabel || edge.label === insight.entitiesAppearances.edgeLabel) + .map(edge => edge._id); + + trackEntityAppearances( + queryNodesID, + appeareanceResult.nodeAppearance, + nameQuery ? nameQuery : ss.queryStates.openQueryArray[queryIndex].id?.toString() || 'default-id', + ); + + trackEntityAppearances( + queryEdgesID, + appeareanceResult.edgeAppearances, + nameQuery ? nameQuery : ss.queryStates.openQueryArray[queryIndex].id?.toString() || 'default-id', + ); +} + +export const trackEntityAppearances = ( + entities: string[], + appearances: AppearanceMap, + queryId: string, // Changed to string +) => { + for (const entity of entities) { + if (!appearances[entity]) { + appearances[entity] = { count: 0, queries: [] }; + } + appearances[entity].count++; + appearances[entity].queries.push(queryId); + } +}; diff --git a/src/readers/insightProcessor.ts b/src/readers/insightProcessor.ts index 50bd7befe13b3715439cc73bb10ee53710800bb9..5465adc92b88731565d04ca6561f4109c8bad93e 100644 --- a/src/readers/insightProcessor.ts +++ b/src/readers/insightProcessor.ts @@ -9,6 +9,7 @@ import { query2Cypher } from '../utils/cypher/converter'; import { queryService } from './queryService'; import { statCheck } from './statCheck'; import { diffCheck } from './diffCheck'; +import { type AppearanceMap, appearanceCheck } from './appearanceCheck'; import { VariableNode } from '../utils/lexical'; import { populateTemplate } from '../utils/insights'; import { RabbitMqBroker } from 'ts-common/rabbitMq'; @@ -87,6 +88,9 @@ export const insightProcessor = async () => { const queries = ss.queryStates.openQueryArray; const visualizations = ss.visualizations.openVisualizationArray; + const nodeAppearances: AppearanceMap = {}; + const edgeAppearances: AppearanceMap = {}; + for (const queryIndex in queries) { const visualQuery = ss.queryStates.openQueryArray[queryIndex].graph; const queryBuilderSettings = ss.queryStates.openQueryArray[queryIndex].settings; @@ -104,6 +108,9 @@ export const insightProcessor = async () => { insight = await diffCheck(insight, ss, result); } else if (insight.alarmMode === 'conditional' && insight.conditionsCheck && insight.conditionsCheck.length > 0) { insight = statCheck(insight, result); + } else if (insight.alarmMode === 'entitiesAppearances') { + appearanceCheck(result, insight, ss, Number(queryIndex), { nodeAppearance: nodeAppearances, edgeAppearances: edgeAppearances }); + log.info('LogicSet resultSet:', nodeAppearances); } if (insight.userId == null) return; // fixes ts but never is the case