Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(orchestrator): use v2 endpoints to retrieve instances #1956

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getWorkflowCategory,
parseWorkflowVariables,
ProcessInstance,
ProcessInstanceVariables,
WorkflowDefinition,
WorkflowInfo,
} from '@janus-idp/backstage-plugin-orchestrator-common';
Expand Down Expand Up @@ -282,7 +281,7 @@ export class DataIndexService {

public async fetchInstanceVariables(
instanceId: string,
): Promise<ProcessInstanceVariables | undefined> {
): Promise<object | undefined> {
const graphQlQuery = `{ ProcessInstances (where: { id: {equal: "${instanceId}" } } ) { variables } }`;

const result = await this.client.query(graphQlQuery, {});
Expand All @@ -304,7 +303,7 @@ export class DataIndexService {
return undefined;
}

return parseWorkflowVariables(processInstances[0].variables);
return parseWorkflowVariables(processInstances[0].variables as object);
}

public async fetchDefinitionIdByInstanceId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
isComposedSchema,
isJsonObjectSchema,
JsonObjectSchema,
ProcessInstanceVariables,
WorkflowDefinition,
WorkflowInputSchemaResponse,
WorkflowInputSchemaStep,
Expand Down Expand Up @@ -96,8 +95,8 @@ export class DataInputSchemaService {
public getWorkflowInputSchemaResponse(
definition: WorkflowDefinition,
inputSchema: JSONSchema7,
instanceVariables?: ProcessInstanceVariables,
assessmentInstanceVariables?: ProcessInstanceVariables,
instanceVariables?: object,
assessmentInstanceVariables?: object,
): WorkflowInputSchemaResponse {
const instanceWorkflowData = this.extractWorkflowData(instanceVariables);
const assessmentInstanceWorkflowData = this.extractWorkflowData(
Expand Down Expand Up @@ -159,10 +158,10 @@ export class DataInputSchemaService {
return res;
}

private extractWorkflowData(
variables?: ProcessInstanceVariables,
): JsonObject | undefined {
return variables ? (variables[WORKFLOW_DATA_KEY] as JsonObject) : undefined;
private extractWorkflowData(variables?: object): JsonObject | undefined {
return variables && WORKFLOW_DATA_KEY in variables
? (variables[WORKFLOW_DATA_KEY] as JsonObject)
: undefined;
}

private extractObjectKeys(obj: JsonObject | undefined): string[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ProcessInstance,
ProcessInstanceVariables,
WorkflowDefinition,
WorkflowExecutionResponse,
WorkflowInfo,
Expand Down Expand Up @@ -319,7 +318,7 @@ describe('OrchestratorService', () => {
});

describe('fetchInstanceVariables', () => {
const variables: ProcessInstanceVariables = { foo: 'bar' };
const variables: object = { foo: 'bar' };

beforeEach(() => {
jest.clearAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class OrchestratorService {
public async fetchInstanceVariables(args: {
instanceId: string;
cacheHandler?: CacheHandler;
}): Promise<ProcessInstanceVariables | undefined> {
}): Promise<object | undefined> {
const { instanceId, cacheHandler } = args;
const definitionId =
await this.dataIndexService.fetchDefinitionIdByInstanceId(instanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ describe('scenarios to verify mapToProcessInstanceDTO', () => {
// @ts-ignore
processInstanceV1?.variables?.workflowdata,
);
expect(result.workflow).toEqual(
processInstanceV1.processName ?? processInstanceV1.processId,
);
});
it('correctly maps ProcessInstanceDTO', () => {
// Arrange
Expand Down Expand Up @@ -143,9 +140,6 @@ describe('scenarios to verify mapToProcessInstanceDTO', () => {
// @ts-ignore
processIntanceV1?.variables?.workflowdata,
);
expect(result.workflow).toEqual(
processIntanceV1.processName ?? processIntanceV1.processId,
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
extractWorkflowFormat,
fromWorkflowSource,
getWorkflowCategory,
NodeInstance,
NodeInstanceDTO,
ProcessInstance,
ProcessInstanceDTO,
ProcessInstanceState,
Expand Down Expand Up @@ -75,7 +77,7 @@ export function getProcessInstancesDTOFromString(
): ProcessInstanceStatusDTO {
switch (state) {
case ProcessInstanceState.Active.valueOf():
return 'Running';
return 'Active';
case ProcessInstanceState.Error.valueOf():
return 'Error';
case ProcessInstanceState.Completed.valueOf():
Expand Down Expand Up @@ -110,20 +112,22 @@ export function mapToProcessInstanceDTO(
}

return {
...processInstance,
category: mapWorkflowCategoryDTO(processInstance.category),
description: processInstance.description,
id: processInstance.id,
name: processInstance.processName,
duration: duration,
// @ts-ignore
workflowdata: variables?.workflowdata,
start: processInstance.start,
end: processInstance.end,
duration: duration,
status: getProcessInstancesDTOFromString(processInstance.state),
workflow: processInstance.processName ?? processInstance.processId,
nodes: processInstance.nodes.map(mapToNodeInstanceDTO),
};
}

export function mapToNodeInstanceDTO(
nodeInstance: NodeInstance,
): NodeInstanceDTO {
return { ...nodeInstance, __typename: 'NodeInstance' };
}

export function mapToExecuteWorkflowResponseDTO(
workflowId: string,
workflowExecutionResponse: WorkflowExecutionResponse,
Expand Down
35 changes: 0 additions & 35 deletions plugins/orchestrator-backend/src/service/api/v1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import express from 'express';

import {
AssessedProcessInstance,
ProcessInstance,
ProcessInstanceVariables,
WorkflowDefinition,
WorkflowExecutionResponse,
Expand Down Expand Up @@ -45,39 +43,6 @@ export class V1 {
return source;
}

public async getInstances(): Promise<ProcessInstance[]> {
return await this.orchestratorService.fetchInstances({});
}

public async getInstanceById(
instanceId: string,
includeAssessment: boolean = false,
): Promise<AssessedProcessInstance> {
const instance = await this.orchestratorService.fetchInstance({
instanceId,
cacheHandler: 'throw',
});

if (!instance) {
throw new Error(`Couldn't fetch process instance ${instanceId}`);
}

let assessedByInstance: ProcessInstance | undefined;

if (includeAssessment && instance.businessKey) {
assessedByInstance = await this.orchestratorService.fetchInstance({
instanceId: instance.businessKey,
cacheHandler: 'throw',
});
}

const response: AssessedProcessInstance = {
instance,
assessedBy: assessedByInstance,
};
return response;
}

public async executeWorkflow(
inputData: ProcessInstanceVariables,
definitionId: string,
Expand Down
23 changes: 16 additions & 7 deletions plugins/orchestrator-backend/src/service/api/v2.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ParsedRequest } from 'openapi-backend';

import {
AssessedProcessInstance,
AssessedProcessInstanceDTO,
ExecuteWorkflowRequestDTO,
ExecuteWorkflowResponseDTO,
ProcessInstance,
ProcessInstanceListResultDTO,
ProcessInstanceState,
WorkflowDTO,
Expand Down Expand Up @@ -98,19 +98,28 @@ export class V2 {
instanceId: string,
includeAssessment: boolean = false,
): Promise<AssessedProcessInstanceDTO> {
const instance: AssessedProcessInstance = await this.v1.getInstanceById(
const instance = await this.orchestratorService.fetchInstance({
instanceId,
includeAssessment,
);
cacheHandler: 'throw',
});

if (!instance) {
throw new Error(`Couldn't fetch process instance ${instanceId}`);
}

let assessedByInstance: ProcessInstance | undefined;

if (includeAssessment && instance.businessKey) {
assessedByInstance = await this.orchestratorService.fetchInstance({
instanceId: instance.businessKey,
cacheHandler: 'throw',
});
}

return {
instance: mapToProcessInstanceDTO(instance.instance),
assessedBy: instance.assessedBy
? mapToProcessInstanceDTO(instance.assessedBy)
instance: mapToProcessInstanceDTO(instance),
assessedBy: assessedByInstance
? mapToProcessInstanceDTO(assessedByInstance)
: undefined,
};
}
Expand Down
77 changes: 0 additions & 77 deletions plugins/orchestrator-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,40 +802,6 @@ function setupInternalRoutes(
},
);

// v1
router.get('/instances', async (req, res) => {
const endpointName = 'Instances';
const endpoint = '/v1/instances';

auditLogger.auditLog({
eventName: endpointName,
stage: 'start',
status: 'succeeded',
level: 'debug',
request: req,
message: `Received request to '${endpoint}' endpoint`,
});

const decision = await authorize(
req,
orchestratorWorkflowInstancesReadPermission,
permissions,
httpAuth,
);
if (decision.result === AuthorizeResult.DENY) {
manageDenyAuthorization(endpointName, endpoint, req);
}
await routerApi.v1
.getInstances()
.then(result => res.status(200).json(result))
.catch(error => {
auditLogRequestError(error, endpointName, endpoint, req);
res
.status(500)
.json({ message: error.message || INTERNAL_SERVER_ERROR_MESSAGE });
});
});

// v2
routerApi.openApiBackend.register(
'getInstances',
Expand Down Expand Up @@ -868,49 +834,6 @@ function setupInternalRoutes(
},
);

// v1
router.get('/instances/:instanceId', async (req, res) => {
const {
params: { instanceId },
} = req;
const endpointName = 'InstancesInstanceId';
const endpoint = `/v1/instances/${instanceId}`;

auditLogger.auditLog({
eventName: endpointName,
stage: 'start',
status: 'succeeded',
level: 'debug',
request: req,
message: `Received request to '${endpoint}' endpoint`,
});

const decision = await authorize(
req,
orchestratorWorkflowInstanceReadPermission,
permissions,
httpAuth,
);
if (decision.result === AuthorizeResult.DENY) {
manageDenyAuthorization(endpointName, endpoint, req);
}

const includeAssessment = routerApi.v1.extractQueryParam(
req,
QUERY_PARAM_INCLUDE_ASSESSMENT,
);

await routerApi.v1
.getInstanceById(instanceId, !!includeAssessment)
.then(result => res.status(200).json(result))
.catch(error => {
auditLogRequestError(error, endpointName, endpoint, req);
res
.status(500)
.json({ message: error.message || INTERNAL_SERVER_ERROR_MESSAGE });
});
});

// v2
routerApi.openApiBackend.register(
'getInstanceById',
Expand Down
2 changes: 1 addition & 1 deletion plugins/orchestrator-common/src/generated/.METADATA.sha1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2303a93f0cf2523f926e28bec31e21619f04e03b
a6f1ec3718385945b49150694189881ac506b364
Loading