Skip to content

Commit

Permalink
Merge pull request #18 from tabkram/engine-id-as-execution-id
Browse files Browse the repository at this point in the history
Engine id as executionId
  • Loading branch information
tabkram committed Dec 15, 2023
2 parents 45abede + 73f539a commit 726db14
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/engine/executionEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export class ExecutionEngine<
return this;
}

/**
* Get the current options of the Execution Engine.
*
* @returns {Object} An object containing the execution date and ID.
* @public
*/
getOptions(): { executionDate: Date; executionId: string } {
return { executionDate: this.executionDate, executionId: this.executionId };
}

setContext(value: CXT) {
this.context = this.deepClone(value);
return this;
Expand Down
14 changes: 8 additions & 6 deletions src/engine/executionEngineDecorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ describe('decorators', () => {
});

it('should apply the run decorator to a method', async () => {
const id = 'testId';

@engine({ id })
class TestClass {
@engine()
class TestClass extends EngineTask {
@run()
async testMethod() {
return 'Test Result';
Expand All @@ -26,7 +24,7 @@ describe('decorators', () => {

const instance = new TestClass();
const result = await instance.testMethod();

expect(instance.engine.getOptions().executionId).toEqual(expect.stringMatching(/^exec.*$/));
expect(result).toBe('Test Result');
});

Expand Down Expand Up @@ -90,6 +88,7 @@ describe('decorators', () => {
}
);
const greetingTrace = instance.engine.getTrace();
expect(instance.engine.getOptions().executionId).toEqual('greetingId');
expect(greetingTrace?.length).toEqual(1);
expect(greetingTrace[0]).toEqual({
data: {
Expand Down Expand Up @@ -139,7 +138,10 @@ describe('decorators', () => {
class MyVigilanceTask extends EngineTask {
@run({
trace: { id: 'decideIfIShouldGoOut_custom_id', narratives: ['Narrative 0 GoOut'] },
config: { parallel: true, traceExecution: { inputs: true, outputs: true, narratives: ['Narrative 1 GoOut', 'Narrative 2 GoOut'] } }
config: {
parallel: true,
traceExecution: { inputs: true, outputs: true, narratives: ['Narrative 1 GoOut', 'Narrative 2 GoOut'] }
}
})
async decideIfIShouldGoOut(city: string) {
const temperature = await new MyWeatherTask().fetchCurrentTemperature(city);
Expand Down
11 changes: 7 additions & 4 deletions src/engine/executionEngineDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export abstract class EngineTask {
* A class decorator that enhances a class with execution engine capabilities.
* @param options - Configuration options for the execution engine.
*/
export function engine(options: { id: string }): ClassDecorator {
export function engine(options?: { id: string }): ClassDecorator {
/**
* The actual decorator function.
* @param target - The target class.
Expand All @@ -38,10 +38,13 @@ export function engine(options: { id: string }): ClassDecorator {
*/
const newConstructor: any = function (...args: any[]) {
const instance = new originalConstructor(...args);
if (!executionEngines[options.id]) {
executionEngines[options.id] = new ExecutionEngine();
let executionId = options?.id;
if (!executionId || !executionEngines[executionId]) {
const engineInstance = new ExecutionEngine({ executionId: options?.id });
executionId = engineInstance.getOptions().executionId;
executionEngines[executionId] = engineInstance;
}
instance.engine = executionEngines[options.id];
instance.engine = executionEngines[executionId];
return instance;
};

Expand Down

1 comment on commit 726db14

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 92.34% 241/261
🟢 Branches 86.21% 175/203
🟢 Functions 91.78% 67/73
🟢 Lines 93.06% 228/245

Test suite run success

33 tests passing in 5 suites.

Report generated by 🧪jest coverage report action from 726db14

Please sign in to comment.