Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
workflow log
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardW98 committed May 23, 2023
1 parent 88c756e commit 519a106
Show file tree
Hide file tree
Showing 22 changed files with 920 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.redhat.parodos.workflow.enums;

import lombok.Getter;

public enum LogType {

TEXT(""), INFO("\u001B[32m"), WARNING("\u001B[33m"), ERROR("\u001B[34m"), END("\u001B[39m");

// Ansi code
@Getter
private final String code;

LogType(String code) {
this.code = code;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.redhat.parodos.workflow.log;

import java.util.UUID;

public interface WorkFlowLogService {

String getLog(UUID workflowExecutionId, String taskName);

void writeLog(UUID workflowExecutionId, String taskName, WorkFlowTaskLog log);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.redhat.parodos.workflow.log;

import com.redhat.parodos.workflow.enums.LogType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WorkFlowTaskLog {

private String logText;

private LogType logType = LogType.TEXT;

@Override
public String toString() {
return logType.getCode() + logText + (logType.getCode().isEmpty() ? "" : LogType.END.getCode());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
import java.util.Map;
import java.util.UUID;

import com.redhat.parodos.workflow.enums.LogType;
import com.redhat.parodos.workflow.exception.MissingParameterException;
import com.redhat.parodos.workflow.log.WorkFlowLogService;
import com.redhat.parodos.workflow.log.WorkFlowTaskLog;
import com.redhat.parodos.workflow.utils.WorkContextUtils;
import com.redhat.parodos.workflows.work.WorkContext;
import com.redhat.parodos.workflows.workflow.WorkFlow;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Base Class for a WorkFlowTask.
Expand All @@ -44,6 +48,9 @@ public abstract class BaseWorkFlowTask implements WorkFlowTask, BeanNameAware {
@Getter
private String name;

@Autowired
private WorkFlowLogService workFlowLogService;

@Override
public void setBeanName(String name) {
this.name = name;
Expand Down Expand Up @@ -108,4 +115,25 @@ public String getOptionalParameterValue(WorkContext workContext, String paramete
.map(Map.Entry::getValue).findFirst().orElse(defaultValue);
}

private void addTaskLogByLogType(WorkContext workContext, String logText, LogType logType) {
workFlowLogService.writeLog(getMainExecutionId(workContext), name,
WorkFlowTaskLog.builder().logText(logText).logType(logType).build());
}

public void addTaskLog(WorkContext workContext, String logText) {
addTaskLogByLogType(workContext, logText, LogType.TEXT);
}

public void addInfoTaskLog(WorkContext workContext, String logText) {
addTaskLogByLogType(workContext, logText, LogType.INFO);
}

public void addErrorTaskLog(WorkContext workContext, String logText) {
addTaskLogByLogType(workContext, logText, LogType.ERROR);
}

public void addWarningTaskLog(WorkContext workContext, String logText) {
addTaskLogByLogType(workContext, logText, LogType.WARNING);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public JiraTicketCreationWorkFlowTask(String jiraServiceBaseUrl, String jiraUser
*/
public WorkReport execute(WorkContext workContext) {
log.info("Start jiraTicketCreationWorkFlowTask...");
addTaskLog(workContext, "Start jiraTicketCreationWorkFlowTask...");
try {
String urlString = jiraServiceBaseUrl + "/rest/servicedeskapi/request";
String serviceDeskId = "1";
Expand All @@ -95,12 +96,14 @@ public WorkReport execute(WorkContext workContext) {
addParameter(workContext, ISSUE_KEY, Objects.requireNonNull(response.getBody()).getIssueKey());
addParameter(workContext, ISSUE_LINK,
Objects.requireNonNull(response.getBody()).getLinks().get(WEB_LINK));
addInfoTaskLog(workContext, "jiraTicketCreationWorkFlowTask is completed");
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}
log.error("Call to the API was not successful. Response: {}", response.getStatusCode());
}
catch (Exception e) {
log.error("There was an issue with the REST call: {}", e.getMessage());
addErrorTaskLog(workContext, "jiraTicketCreationWorkFlowTask is Failed");
}
return new DefaultWorkReport(WorkStatus.FAILED, workContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -62,6 +63,9 @@ public class JiraTicketCreationWorkFlowTaskTest extends BaseInfrastructureWorkFl
@Before
public void setUp() {
this.jiraTicketCreationWorkFlowTask = spy((JiraTicketCreationWorkFlowTask) getConcretePersonImplementation());
Mockito.doNothing().when(jiraTicketCreationWorkFlowTask).addInfoTaskLog(any(), any());
Mockito.doNothing().when(jiraTicketCreationWorkFlowTask).addErrorTaskLog(any(), any());
Mockito.doNothing().when(jiraTicketCreationWorkFlowTask).addTaskLog(any(), any());
}

@Override
Expand Down Expand Up @@ -101,15 +105,19 @@ public void executeSuccess() {
public void executeFail() {
// given
WorkContext workContext = mock(WorkContext.class);

try (MockedStatic<RestUtils> restUtilsMockedStatic = mockStatic(RestUtils.class)) {
restUtilsMockedStatic.when(
() -> RestUtils.executePost(any(String.class), any(), any(String.class), any(String.class), any()))
.thenReturn(new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR));
// when
WorkReport workReport = jiraTicketCreationWorkFlowTask.execute(workContext);
// then
assertEquals(WorkStatus.FAILED, workReport.getStatus());
try (MockedStatic<WorkContextUtils> workContextUtilsMockedStatic = mockStatic(WorkContextUtils.class)) {
workContextUtilsMockedStatic.when(() -> WorkContextUtils.getMainExecutionId(any(WorkContext.class)))
.thenReturn(UUID.randomUUID());

// when
WorkReport workReport = jiraTicketCreationWorkFlowTask.execute(workContext);
// then
assertEquals(WorkStatus.FAILED, workReport.getStatus());
}
}
}

Expand Down
1 change: 1 addition & 0 deletions workflow-service-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Class | Method | HTTP request | Description
*ProjectApi* | [**removeUsersFromProject**](docs/ProjectApi.md#removeUsersFromProject) | **DELETE** /api/v1/projects/{id}/users | Remove users from project
*ProjectApi* | [**updateUserRolesToProject**](docs/ProjectApi.md#updateUserRolesToProject) | **POST** /api/v1/projects/{id}/users | Update user roles in project
*WorkflowApi* | [**execute**](docs/WorkflowApi.md#execute) | **POST** /api/v1/workflows | Executes a workflow
*WorkflowApi* | [**getLog**](docs/WorkflowApi.md#getLog) | **GET** /api/v1/workflows/{workFlowExecutionId}/{taskName}/log | Returns workflow execution log
*WorkflowApi* | [**getStatus**](docs/WorkflowApi.md#getStatus) | **GET** /api/v1/workflows/{workFlowExecutionId}/status | Returns a workflow status
*WorkflowApi* | [**getStatusByProjectId**](docs/WorkflowApi.md#getStatusByProjectId) | **GET** /api/v1/workflows | Returns workflows by project id
*WorkflowApi* | [**getWorkflowParameters**](docs/WorkflowApi.md#getWorkflowParameters) | **GET** /api/v1/workflows/{workFlowExecutionId}/context | Returns workflow context parameters
Expand Down
52 changes: 52 additions & 0 deletions workflow-service-sdk/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,58 @@ paths:
tags:
- Workflow
x-accepts: application/json
/api/v1/workflows/{workFlowExecutionId}/{taskName}/log:
get:
operationId: getLog
parameters:
- explode: false
in: path
name: workFlowExecutionId
required: true
schema:
format: uuid
type: string
style: simple
- explode: false
in: path
name: taskName
required: true
schema:
type: string
style: simple
responses:
"200":
content:
text/plain:
schema:
type: string
description: Succeeded
"400":
content:
'*/*':
schema:
$ref: '#/components/schemas/ErrorMessageDTO'
description: Bad Request
"401":
description: Unauthorized
"403":
description: Forbidden
"404":
content:
'*/*':
schema:
$ref: '#/components/schemas/ErrorMessageDTO'
description: Not Found
"409":
content:
'*/*':
schema:
$ref: '#/components/schemas/ErrorMessageDTO'
description: Conflict
summary: Returns workflow execution log
tags:
- Workflow
x-accepts: "*/*,text/plain"
components:
schemas:
ArgumentRequestDTO:
Expand Down
68 changes: 68 additions & 0 deletions workflow-service-sdk/docs/WorkflowApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All URIs are relative to *http://localhost:8080*
| Method | HTTP request | Description |
|------------- | ------------- | -------------|
| [**execute**](WorkflowApi.md#execute) | **POST** /api/v1/workflows | Executes a workflow |
| [**getLog**](WorkflowApi.md#getLog) | **GET** /api/v1/workflows/{workFlowExecutionId}/{taskName}/log | Returns workflow execution log |
| [**getStatus**](WorkflowApi.md#getStatus) | **GET** /api/v1/workflows/{workFlowExecutionId}/status | Returns a workflow status |
| [**getStatusByProjectId**](WorkflowApi.md#getStatusByProjectId) | **GET** /api/v1/workflows | Returns workflows by project id |
| [**getWorkflowParameters**](WorkflowApi.md#getWorkflowParameters) | **GET** /api/v1/workflows/{workFlowExecutionId}/context | Returns workflow context parameters |
Expand Down Expand Up @@ -76,6 +77,73 @@ No authorization required
| **404** | Not Found | - |
| **409** | Conflict | - |

<a name="getLog"></a>
# **getLog**
> String getLog(workFlowExecutionId, taskName)
Returns workflow execution log

### Example
```java
// Import classes:
import com.redhat.parodos.sdk.invoker.ApiClient;
import com.redhat.parodos.sdk.invoker.ApiException;
import com.redhat.parodos.sdk.invoker.Configuration;
import com.redhat.parodos.sdk.invoker.models.*;
import com.redhat.parodos.sdk.api.WorkflowApi;

public class Example {
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("http://localhost:8080");

WorkflowApi apiInstance = new WorkflowApi(defaultClient);
UUID workFlowExecutionId = UUID.randomUUID(); // UUID |
String taskName = "taskName_example"; // String |
try {
String result = apiInstance.getLog(workFlowExecutionId, taskName);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling WorkflowApi#getLog");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}
}
```

### Parameters

| Name | Type | Description | Notes |
|------------- | ------------- | ------------- | -------------|
| **workFlowExecutionId** | **UUID**| | |
| **taskName** | **String**| | |

### Return type

**String**

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: text/plain, */*

### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
| **200** | Succeeded | - |
| **400** | Bad Request | - |
| **401** | Unauthorized | - |
| **403** | Forbidden | - |
| **404** | Not Found | - |
| **409** | Conflict | - |

<a name="getStatus"></a>
# **getStatus**
> WorkFlowStatusResponseDTO getStatus(workFlowExecutionId)
Expand Down
Loading

0 comments on commit 519a106

Please sign in to comment.