Skip to content

Commit

Permalink
feat: add more examples to explain ExecutionEngine usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tabkram committed Nov 18, 2023
1 parent d1cab65 commit 28feb55
Show file tree
Hide file tree
Showing 5 changed files with 638 additions and 0 deletions.
62 changes: 62 additions & 0 deletions README.md
Expand Up @@ -16,6 +16,7 @@ npm install execution-engine

## Usage 📚

- example:
```typescript
import { ExecutionEngine } from 'execution-engine';

Expand All @@ -35,6 +36,67 @@ const trace = engine.getTrace();
console.log('Trace:', trace);
```

- The `result` object has the following structure:

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

- The `trace` object has the following structure:

```typescript
trace = [
{
data: {
id: function_uuid1,
label: functionLabel1
//... other properties of the "result1" of the executed function as mentioned above
},
group: nodes
},
{
data: {
id: function_uuid2,
label: functionLabel2
//... other properties of the "result2" of the executed function as mentioned above
},
group: nodes
},
{
data: {
id: function_uuid1 -> function_uuid2,
source: function_uuid1,
target: function_uuid2,
parallel: false
},
group: edges
}
];
```

- The trace object structure is a JSON array object that can be visualized in the:
- [json-to-graph online demo](https://tabkram.github.io/json-to-graph/)

## Examples 📘
For additional usage examples, please explore the **[/examples](https://github.com/tabkram/execution-engine/tree/main/examples)** directory in this repository.

You'll find a variety of scenarios showcasing the capabilities of Execution Engine.

# Contributing 🤝
If you find any issues or have suggestions for improvement, feel free to open an issue or submit a pull request. Contributions are welcome!

Expand Down
46 changes: 46 additions & 0 deletions examples/authentication.json
@@ -0,0 +1,46 @@
[
{
"data": {
"inputs": [
"john_doe",
"secure_password"
],
"outputs": "User john_doe successfully logged in",
"startTime": "2023-11-18T19:22:28.044Z",
"endTime": "2023-11-18T19:22:28.044Z",
"duration": 0,
"id": "loginUser_1700335348044_cfccf8cb-5dc5-4857-8891-4afc79be398f",
"label": "loginUser",
"parallel": false,
"abstract": false,
"createTime": "2023-11-18T19:22:28.044Z"
},
"group": "nodes"
},
{
"data": {
"inputs": [
"john_doe"
],
"outputs": "User Information for john_doe: Full Name - John Doe, Email - john.doe@example.com, Role - User",
"startTime": "2023-11-18T19:22:28.044Z",
"endTime": "2023-11-18T19:22:28.044Z",
"duration": 0,
"id": "getUserInformation_1700335348044_d92efa30-7bd2-4eea-910b-df040bc5fcf2",
"label": "getUserInformation",
"parallel": false,
"abstract": false,
"createTime": "2023-11-18T19:22:28.044Z"
},
"group": "nodes"
},
{
"data": {
"id": "loginUser_1700335348044_cfccf8cb-5dc5-4857-8891-4afc79be398f->getUserInformation_1700335348044_d92efa30-7bd2-4eea-910b-df040bc5fcf2",
"source": "loginUser_1700335348044_cfccf8cb-5dc5-4857-8891-4afc79be398f",
"target": "getUserInformation_1700335348044_d92efa30-7bd2-4eea-910b-df040bc5fcf2",
"parallel": false
},
"group": "edges"
}
]
53 changes: 53 additions & 0 deletions examples/authentication.ts
@@ -0,0 +1,53 @@
import { ExecutionEngine } from "../src";
import * as fs from "fs";

function registerUser(username: string, password: string) {
if (username && password) {
return Promise.resolve(`User ${username} successfully registered`);
} else {
Promise.reject("Invalid registration information");
}
}

function loginUser(username: string, password: string) {
if (username && password) {
return `User ${username} successfully logged in`;
} else {
throw new Error("Invalid login credentials");
}
}

function getUserInformation(username: string) {
const userInfo = {
fullName: "John Doe",
email: "john.doe@example.com",
role: "User",
};
return `User Information for ${username}: Full Name - ${userInfo.fullName}, Email - ${userInfo.email}, Role - ${userInfo.role}`;
}

// Sequential consecutive calls for user registration, login, and retrieving user information
const newUser = {
username: "john_doe",
password: "secure_password",
};
const executionEngine = new ExecutionEngine();
executionEngine
.run(registerUser, [newUser.username, newUser.password])
.then((result) => result);
executionEngine.run(loginUser, [newUser.username, newUser.password]);
executionEngine.run(getUserInformation, [newUser.username]);

// Retrieve the trace
const finalTrace = executionEngine.getTrace();
const jsonString = JSON.stringify(finalTrace, null, 2);

// Write the JSON string to the file
const filePath = process?.argv?.[1]?.replace(".ts", ".json");
fs.writeFile(filePath, jsonString, "utf-8", (err) => {
if (err) {
console.error("Error writing JSON to file:", err);
} else {
console.log(`JSON data written to ${filePath}`);
}
});

0 comments on commit 28feb55

Please sign in to comment.