Skip to content

Commit

Permalink
[Automatic Import] add yarn draw-graphs command (elastic#191813)
Browse files Browse the repository at this point in the history
## Summary

This adds the `yarn draw-graphs` plugin specific command, which is
defined in our plugins `package.json`. The command can be run from the
root of our plugin, and does not require any additional dependencies.

The graphs are added to `docs/imgs` and linked in the README.md of the
plugin.

The purpose of this PR is not to write a better README or provide any
automation of the task itself, that would be separate tasks, but simply
allow the creation of graphs to the readme, which can be useful to debug
changes to nodes and conditional edges.

Examples:

![ecs_graph](https://github.com/user-attachments/assets/ccc403a2-e2b0-4482-9de7-74c016c75c16)

![ecs_subgraph](https://github.com/user-attachments/assets/ad8da90e-191e-4533-b5b2-ede4bd973993)

![log_detection_graph](https://github.com/user-attachments/assets/777c6845-ba52-4a16-a2b1-b13e172a7488)

![related_graph](https://github.com/user-attachments/assets/a5447a5b-2fa7-4b96-920f-3cd9a2c8dbe5)

![categorization_graph](https://github.com/user-attachments/assets/c61463d6-c1d8-4347-bb84-d39e5606417b)

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
P1llus and kibanamachine authored Sep 2, 2024
1 parent a118728 commit 1606db8
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 4 deletions.
24 changes: 24 additions & 0 deletions x-pack/plugins/integration_assistant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ The templates are stored in the `./server/templates` directory and are used to g

One template (pipeline.yml.njk) is used by the ECS Mapping API to generate the boilerplate ingest pipeline structure we want to use for all generated integrations.

#### Flows

Generated by running `yarn draw-graphs` inside the root of the plugin folder.

**First graph:**
**Log Format Detection:**
![!LogFormatDetectionGraph](./docs/img/log_detection_graph.png)

**Second graph, main:**
**ECS Main:**
![ECSMappingGraph](./docs/img/ecs_graph.png)

**Second graph, sub:**
**ECS Sub:**
![ECSMappingSubGraph](./docs/img/ecs_subgraph.png)

**Third graph:**
**Categorization:**
![CategorizationGraph](./docs/img/categorization_graph.png)

**Fourth graph:**
**Related:**
![RelatedGraph](./docs/img/related_graph.png)

## Tests

All mocks/fixtures are placed in the top `./__jest__` directory of the plugin. If many mocks/fixtures are required, try to split them up into separate file(s).
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions x-pack/plugins/integration_assistant/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"author": "Elastic",
"name": "@kbn/integration-assistant-plugin",
"version": "1.0.0",
"private": true,
"license": "Elastic License 2.0",
"scripts": {
"draw-graphs": "node ./scripts/draw_graphs"
}
}
9 changes: 9 additions & 0 deletions x-pack/plugins/integration_assistant/scripts/draw_graphs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

require('../../../../src/setup_node_env');
require('./draw_graphs_script').drawGraphs();
58 changes: 58 additions & 0 deletions x-pack/plugins/integration_assistant/scripts/draw_graphs_script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { IScopedClusterClient } from '@kbn/core-elasticsearch-server';
import {
ActionsClientChatOpenAI,
ActionsClientSimpleChatModel,
} from '@kbn/langchain/server/language_models';
import { ToolingLog } from '@kbn/tooling-log';
import { Graph as RunnableGraph } from '@langchain/core/runnables/graph';
import { FakeLLM } from '@langchain/core/utils/testing';
import fs from 'fs/promises';
import path from 'path';
import { getCategorizationGraph } from '../server/graphs/categorization/graph';
import { getEcsGraph, getEcsSubGraph } from '../server/graphs/ecs/graph';
import { getLogFormatDetectionGraph } from '../server/graphs/log_type_detection/graph';
import { getRelatedGraph } from '../server/graphs/related/graph';

// Some mock elements just to get the graph to compile
const model = new FakeLLM({
response: JSON.stringify({}, null, 2),
}) as unknown as ActionsClientChatOpenAI | ActionsClientSimpleChatModel;
const client = 'test' as unknown as IScopedClusterClient;

const logger = new ToolingLog({
level: 'info',
writeTo: process.stdout,
});
logger.info('Compiling graphs');

async function saveFile(filename: string, buffer: Buffer) {
const outputPath = path.join(__dirname, '../docs/imgs/', filename);
logger.info(`Writing graph to ${outputPath}`);
await fs.writeFile(outputPath, buffer);
}

async function drawGraph(compiledGraph: RunnableGraph, graphName: string) {
const output = await compiledGraph.drawMermaidPng();
const buffer = Buffer.from(await output.arrayBuffer());
await saveFile(`${graphName}.png`, buffer);
}

export async function drawGraphs() {
const relatedGraph = (await getRelatedGraph({ client, model })).getGraph();
const logFormatDetectionGraph = (await getLogFormatDetectionGraph(model)).getGraph();
const categorizationGraph = (await getCategorizationGraph({ client, model })).getGraph();
const ecsSubGraph = (await getEcsSubGraph({ model })).getGraph();
const ecsGraph = (await getEcsGraph({ model })).getGraph();
drawGraph(relatedGraph, 'related_graph');
drawGraph(logFormatDetectionGraph, 'log_detection_graph');
drawGraph(categorizationGraph, 'categorization_graph');
drawGraph(ecsSubGraph, 'ecs_subgraph');
drawGraph(ecsGraph, 'ecs_graph');
}
11 changes: 8 additions & 3 deletions x-pack/plugins/integration_assistant/server/graphs/ecs/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function chainRouter({ state }: EcsBaseNodeParams): string {
}

// This is added as a separate graph to be able to run these steps concurrently from handleCreateMappingChunks
async function getEcsSubGraph({ model }: EcsGraphParams) {
export async function getEcsSubGraph({ model }: EcsGraphParams) {
const workflow = new StateGraph({
channels: graphState,
})
Expand Down Expand Up @@ -99,8 +99,13 @@ export async function getEcsGraph({ model }: EcsGraphParams) {
.addEdge('handleMissingKeys', 'handleValidation')
.addEdge('handleInvalidEcs', 'handleValidation')
.addEdge('handleMergedSubGraphResponse', 'handleValidation')
.addConditionalEdges('modelInput', (state: EcsMappingState) =>
handleCreateMappingChunks({ state })
.addConditionalEdges(
'modelInput',
(state: EcsMappingState) => handleCreateMappingChunks({ state }),
{
modelOutput: 'modelOutput',
subGraph: 'subGraph',
}
)
.addConditionalEdges('handleValidation', (state: EcsMappingState) => chainRouter({ state }), {
duplicateFields: 'handleDuplicates',
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/integration_assistant/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"public/**/*",
"server/**/*.ts",
"common/**/*.ts",
"scripts/**/*.ts",
"__jest__/**/*",
"../../typings/**/*"
],
Expand Down Expand Up @@ -39,6 +40,7 @@
"@kbn/core-http-server",
"@kbn/kibana-utils-plugin",
"@kbn/utils",
"@kbn/zod"
"@kbn/zod",
"@kbn/tooling-log"
]
}

0 comments on commit 1606db8

Please sign in to comment.