Skip to content

Commit

Permalink
feat: add examples of ExecutionEngine using decorators and update the…
Browse files Browse the repository at this point in the history
… README
  • Loading branch information
tabkram committed Dec 10, 2023
1 parent 5360093 commit 7547881
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 34 deletions.
33 changes: 5 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ import { ExecutionEngine } from "execution-engine";
const engine = new ExecutionEngine();

// for sync functions:
const result1 = engine.run((param) => `result1 for ${param}`, ['param1']);
const res1 = engine.run((param) => `result1 for ${param}`, ['param1']);

// for async functions:
const result2 = await engine.run(async (param) => `result2 for ${param}`, [result1.outputs]);
const res2 = await engine.run(async (param) => `result2 for ${param}`, [res1.outputs]);

// Retrieve the trace
const trace = engine.getTrace();
Expand Down Expand Up @@ -91,30 +91,7 @@ You can:
- inspect the **trace output** in [examples/usage2.json](examples/usage2.json)
- visualize the **trace graph** using the json-to-graph online tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage2.json)

### Understanding Results 📊

The `result` object contains function output (`outputs`), input parameters (`inputs`), and other attributes related to
the engine. It has the following structure:

```typescript
result = {
// An array containing the input values passed to thunction:
inputs: [
"param1"
],
// The output value returned by the function:
outputs: "result1 for param1",
// The start time of the function execution:
startTime: Date,
// The end time of the function execution:
endTime: Date,
// The duration of the function execution in milliseconds:
duration: number,
// ...other properties depending on the configuration and trace options.
}
```

### Exploring the Trace 🧭
### Understanding the Trace 🧭

The `trace` object is an array containing **nodes** and **edges**. It has the following structure:

Expand All @@ -124,15 +101,15 @@ trace = [
data: {
id: function_uuid1,
label: "function"
//... other properties of the "result1" of the executed function as mentioned above
//... other properties of the result of the executed function as mentioned above
},
group: nodes
},
{
data: {
id: function_uuid2,
label: "function"
//... other properties of the "result2" of the executed function as mentioned above
//... other properties of the result of the executed function as mentioned above
},
group: nodes
},
Expand Down
69 changes: 69 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,79 @@ A TypeScript library for tracing and visualizing code execution workflows

## Installation 📦

Use [npm](https://www.npmjs.com/package/execution-engine) package manager:

```bash
npm install execution-engine
```

Or use the [yarn](https://yarnpkg.com/package?name=execution-engine) package manager:

```bash
yarn add execution-engine
```

## Usage 📚

### Example 1: Basic Usage

```typescript
import { ExecutionEngine } from "execution-engine";

const engine = new ExecutionEngine();

// for sync functions:
const result1 = engine.run((param) => `result1 for ${param}`, ['param1']);

// for async functions:
const result2 = await engine.run(async (param) => `result2 for ${param}`, [result1.outputs]);

// Retrieve the trace
const trace = engine.getTrace();
console.log('Trace:', trace);
```

You can:

- view the **complete code** in [examples/usage.ts](examples/usage.ts)
- inspect the **trace output** in [examples/usage.json](examples/usage.json).
- visualize the **trace graph** using the json-to-graph online
tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage.json)

### Example 2: Usage with Decorators

```typescript
import { engine, run } from "execution-engine";

@engine({ id: "uniqueEngineId" })
class MyClass extends EngineTask {
@run()
myMethod1(param: string) {
return `result1 for ${param}`;
}

@run()
async myMethod2(param: string) {
return `result2 for ${param}`;
}
}

const myInstance = new MyClass();
myInstance.myMethod2("param1");
await myInstance.myMethod2("param2");

// Retrieve the trace
const trace = myInstance.engine.getTrace();
console.log("Trace:", trace);
```

You can:

- view the **complete code** in [examples/usage2.ts](examples/usage2.ts)
- inspect the **trace output** in [examples/usage2.json](examples/usage2.json)
- visualize the **trace graph** using the json-to-graph online
tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage2.json)

## Components 🧩

### ExecutionTimer
Expand Down
6 changes: 3 additions & 3 deletions examples/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ export async function run() {
const engine = new ExecutionEngine();

// for sync functions:
const result1 = engine.run((param) => `result1 for ${param}`, ['param1']);
const res1 = engine.run((param) => `result1 for ${param}`, ['param1']);

// for async functions:
const result2 = await engine.run(async (param) => `result2 for ${param}`, [result1.outputs]);
const res2 = await engine.run(async (param) => `result2 for ${param}`, [res1.outputs]);

// Retrieve the trace
const trace = engine.getTrace();
const jsonString = JSON.stringify(trace, null, 2);
writeTrace(jsonString);
}

run();
run().then();
2 changes: 1 addition & 1 deletion examples/usage2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ export async function generate() {
writeTrace(jsonString);
}

generate()?.then();
generate().then();
2 changes: 0 additions & 2 deletions src/trace/traceableExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ function isAsync(func: Function): boolean {
return func.constructor.name === 'AsyncFunction';
}

export type TraceableRunnerOptions = TraceOptions<Array<any>, unknown> | TraceOptions<Array<any>, unknown>['trace'];

/**
* Represents a class for traceable execution of functions.
*/
Expand Down

0 comments on commit 7547881

Please sign in to comment.