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

FLPATH-377: workflow log #368

Merged
merged 1 commit into from
May 26, 2023
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
6 changes: 5 additions & 1 deletion parodos-model-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
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 WorkFlowLogLevel {

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

// Ansi code
@Getter
private final String code;

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
import java.util.UUID;

import com.redhat.parodos.workflow.exception.MissingParameterException;
import com.redhat.parodos.workflow.task.log.WorkFlowTaskLogger;
import com.redhat.parodos.workflow.task.log.service.WorkFlowLogService;
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.slf4j.LoggerFactory;

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

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

@Autowired
private WorkFlowLogService workFlowLogService;

private WorkContext workContext;

protected WorkFlowTaskLogger taskLogger;

@Override
public void setBeanName(String name) {
this.name = name;
}

@Override
public void preExecute(WorkContext workContext) {
this.workContext = workContext;
taskLogger = new WorkFlowTaskLogger(getMainExecutionId(), name, workFlowLogService,
LoggerFactory.getLogger(this.getClass()));
}

// WorkFlowChecker check a process that has been initiated by a WorkFlow to see if its
// been completed
private List<WorkFlow> workFlowCheckers;
Expand All @@ -65,11 +83,11 @@ public UUID getProjectId(WorkContext workContext) {
return WorkContextUtils.getProjectId(workContext);
}

public UUID getMainExecutionId(WorkContext workContext) {
public UUID getMainExecutionId() {
return WorkContextUtils.getMainExecutionId(workContext);
}

public void addParameter(WorkContext workContext, String key, String value) {
public void addParameter(String key, String value) {
WorkContextUtils.addParameter(workContext, key, value);
}

Expand All @@ -79,13 +97,11 @@ public Map<String, String> getAllParameters(WorkContext workContext) {

/**
* Get Parameters specific to this WorkFlowTask, this is a required parameter
* @param workContext
* @param parameterName
* @return String value for the Parameter name
* @throws MissingParameterException
*/
public String getRequiredParameterValue(WorkContext workContext, String parameterName)
throws MissingParameterException {
public String getRequiredParameterValue(String parameterName) throws MissingParameterException {
Map<String, String> parameters = getAllParameters(workContext);
return parameters.entrySet().stream().filter(entry -> parameterName.equals(entry.getKey()))
.map(Map.Entry::getValue).findFirst().orElseThrow(() -> {
Expand All @@ -96,16 +112,15 @@ public String getRequiredParameterValue(WorkContext workContext, String paramete

/**
* Gets an optional parameter. Returns the defaultValue if not found
* @param workContext
* @param parameterName
* @param defaultValue
* @return
* @throws MissingParameterException
*/
public String getOptionalParameterValue(WorkContext workContext, String parameterName, String defaultValue) {
public String getOptionalParameterValue(String parameterName, String defaultValue) {
Map<String, String> parameters = getAllParameters(workContext);
return parameters.entrySet().stream().filter(entry -> parameterName.equals(entry.getKey()))
.map(Map.Entry::getValue).findFirst().orElse(defaultValue);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.redhat.parodos.workflow.parameter.WorkParameter;
import com.redhat.parodos.workflow.task.enums.WorkFlowTaskOutput;
import com.redhat.parodos.workflows.work.Work;
import com.redhat.parodos.workflows.work.WorkContext;
import lombok.NonNull;

/**
Expand All @@ -33,6 +34,12 @@
*/
public interface WorkFlowTask extends Work {

/**
* method runs before @see execute()
* @param workContext
*/
void preExecute(WorkContext workContext);

/**
* Parameters required for the Task to execute. These are generally obtained from
* the @see WorkContext. The @see BaseWorkFlowTask has a method to simplify getting
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.redhat.parodos.workflow.task.log;

import java.util.UUID;

import com.redhat.parodos.workflow.enums.WorkFlowLogLevel;
import com.redhat.parodos.workflow.task.log.dto.WorkFlowTaskLog;
import com.redhat.parodos.workflow.task.log.service.WorkFlowLogService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.helpers.MessageFormatter;

@AllArgsConstructor
public class WorkFlowTaskLogger {

private UUID mainWorkFlowId;

private String taskName;

private WorkFlowLogService workFlowLogService;

private Logger log;

public void logInfo(String logText, String... value) {
logWithLogLevel(WorkFlowLogLevel.INFO, logText, value);
}

public void logInfoWithSlf4j(String logText, String... value) {
logInfo(logText, value);
log.info(logText, (Object[]) value);
}

public void logError(String logText, String... value) {
logWithLogLevel(WorkFlowLogLevel.ERROR, logText, value);
}

public void logErrorWithSlf4j(String logText, String... value) {
logError(logText, value);
log.error(logText, (Object[]) value);
}

public void logWarn(String logText, String... value) {
logWithLogLevel(WorkFlowLogLevel.WARNING, logText, value);
}

public void logWarnWithSlf4j(String logText, String... value) {
logWarn(logText, value);
log.warn(logText, (Object[]) value);
}

private void logWithLogLevel(WorkFlowLogLevel workFlowLoglevel, String logText, String... value) {
String formattedLog = value.length > 0 ? MessageFormatter.arrayFormat(logText, value).getMessage() : logText;
workFlowLogService.writeLog(mainWorkFlowId, taskName,
WorkFlowTaskLog.builder().logText(formattedLog).workFlowLoglevel(workFlowLoglevel).build());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022 Red Hat Developer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.redhat.parodos.workflow.task.log.dto;

import java.time.Instant;

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

/**
* dto to represent workFlow tasks' log objects
*
* @author Richard Wang (Github: richardW98)
*/
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WorkFlowTaskLog {

private String logText;

private WorkFlowLogLevel workFlowLoglevel = WorkFlowLogLevel.INFO;

@Override
public String toString() {
return Instant.now().toString() + " " + workFlowLoglevel.getCode() + workFlowLoglevel.name() + "\u001B[39m "
+ logText;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022 Red Hat Developer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.redhat.parodos.workflow.task.log.service;

import java.util.UUID;

import com.redhat.parodos.workflow.task.log.dto.WorkFlowTaskLog;

/**
* Service interface for recording workFlow tasks' log
*
* @author Richard Wang (Github: richardW98)
*/
public interface WorkFlowLogService {

/**
* contract to get log for a task execution
* @param workFlowExecutionId main WorkFlow Execution's ID
* @param taskName task name for the log
* @return log of the task execution
*/
String getLog(UUID workFlowExecutionId, String taskName);

/**
* contract to add log to a task execution
* @param workFlowExecutionId main WorkFlow Execution's ID
* @param taskName task name for the log
* @param log log dto object of the task execution
*/
void writeLog(UUID workFlowExecutionId, String taskName, WorkFlowTaskLog log);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.redhat.parodos.workflow.context.WorkContextDelegate;
import com.redhat.parodos.workflow.exception.MissingParameterException;
Expand Down Expand Up @@ -85,13 +86,17 @@ public void checkTask() throws MissingParameterException {
public void verifyParameter() throws MissingParameterException {
TestTask task = new TestTask();
WorkContext context = new WorkContext();
WorkContextDelegate.write(context, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION,
WorkContextDelegate.Resource.ID, UUID.randomUUID());

task.preExecute(context);
Map<String, String> map = Map.of("username", "test");

WorkContextDelegate.write(context, WorkContextDelegate.ProcessType.WORKFLOW_TASK_EXECUTION, "Test",
WorkContextDelegate.Resource.ARGUMENTS, map);

task.setBeanName("Test");
assertEquals("test", task.getRequiredParameterValue(context, "username"));
assertEquals("test", task.getRequiredParameterValue("username"));
}

@Test(expected = MissingParameterException.class)
Expand All @@ -100,9 +105,16 @@ public void noParameters() throws MissingParameterException {
WorkContextDelegate.write(context, WorkContextDelegate.ProcessType.WORKFLOW_TASK_EXECUTION, "Test",
WorkContextDelegate.Resource.ARGUMENTS, new HashMap<String, String>());

WorkContextDelegate.write(context, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, "Test",
WorkContextDelegate.Resource.ID, UUID.randomUUID());

WorkContextDelegate.write(context, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION,
WorkContextDelegate.Resource.ID, UUID.randomUUID());

BaseWorkFlowTask flowTask = new TestTask();
flowTask.preExecute(context);
flowTask.setBeanName("Test");
assertEquals("Test", flowTask.getRequiredParameterValue(context, "username"));
assertEquals("Test", flowTask.getRequiredParameterValue("username"));
}

@Test
Expand Down
Loading