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

fix: Change the field of task table #1173

Merged
merged 3 commits into from
Sep 9, 2022
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
1 change: 1 addition & 0 deletions console/src/domain/job/schemas/task.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum TaskStatusType {

export interface ITaskSchema extends IResourceSchema {
uuid: string
resourcePool: string
agent: IAgentSchema
taskStatus: TaskStatusType
}
Expand Down
5 changes: 2 additions & 3 deletions console/src/pages/Job/TaskListCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ export default function TaskListCard({ header, onAction }: ITaskListCardProps) {
{header}
<Table
isLoading={tasksInfo.isLoading}
columns={[t('Task ID'), t('IP'), t('Version'), t('Started'), t('Status'), t('Action')]}
columns={[t('Task ID'), t('Resource Pool'), t('Started'), t('Status'), t('Action')]}
data={
tasksInfo.data?.list.map((task) => {
return [
task.uuid,
task.agent?.ip,
task.agent?.version,
task.resourcePool,
task.createdTime && formatTimestampDateTime(task.createdTime),
task.taskStatus,
<StyledLink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@

package ai.starwhale.mlops.api.protocol.task;

import ai.starwhale.mlops.domain.system.resourcepool.bo.ResourcePool;
import ai.starwhale.mlops.domain.task.status.TaskStatus;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serializable;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.validation.annotation.Validated;

@Data
@Builder
@EqualsAndHashCode
@Schema(description = "Task object", title = "Task")
@Validated
public class TaskVo implements Serializable {
Expand All @@ -42,7 +45,10 @@ public class TaskVo implements Serializable {
@JsonProperty("taskStatus")
private TaskStatus taskStatus;

@JsonProperty("resourcePool")
private String resourcePool;

public static TaskVo empty() {
return new TaskVo("", "", -1L, TaskStatus.CREATED);
return new TaskVo("", "", -1L, TaskStatus.CREATED, ResourcePool.DEFAULT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import ai.starwhale.mlops.domain.system.po.ResourcePoolEntity;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface ResourcePoolMapper {

List<ResourcePoolEntity> listResourcePools();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
import ai.starwhale.mlops.common.PageParams;
import ai.starwhale.mlops.common.util.PageUtil;
import ai.starwhale.mlops.domain.job.JobManager;
import ai.starwhale.mlops.domain.job.bo.Job;
import ai.starwhale.mlops.domain.job.po.JobEntity;
import ai.starwhale.mlops.domain.system.mapper.ResourcePoolMapper;
import ai.starwhale.mlops.domain.system.po.ResourcePoolEntity;
import ai.starwhale.mlops.domain.system.resourcepool.bo.ResourcePool;
import ai.starwhale.mlops.domain.task.converter.TaskConvertor;
import ai.starwhale.mlops.domain.task.mapper.TaskMapper;
import ai.starwhale.mlops.domain.task.po.TaskEntity;
Expand All @@ -33,32 +38,48 @@
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class TaskService {

@Resource
private TaskConvertor taskConvertor;
private final TaskConvertor taskConvertor;

@Resource
private TaskMapper taskMapper;
private final TaskMapper taskMapper;

@Resource
private StorageAccessService storageAccessService;
private final StorageAccessService storageAccessService;

@Resource
private JobManager jobManager;
private final JobManager jobManager;

private final ResourcePoolMapper resourcePoolMapper;

public TaskService(TaskConvertor taskConvertor, TaskMapper taskMapper,
StorageAccessService storageAccessService, JobManager jobManager,
ResourcePoolMapper resourcePoolMapper) {
this.taskConvertor = taskConvertor;
this.taskMapper = taskMapper;
this.storageAccessService = storageAccessService;
this.jobManager = jobManager;
this.resourcePoolMapper = resourcePoolMapper;
}

public PageInfo<TaskVo> listTasks(String jobUrl, PageParams pageParams) {
PageHelper.startPage(pageParams.getPageNum(), pageParams.getPageSize());
Long jobId = jobManager.getJobId(jobUrl);
List<TaskEntity> tasks = taskMapper.listTasks(jobId);

return PageUtil.toPageInfo(tasks, taskConvertor::convert);
JobEntity job = jobManager.findJob(Job.builder().id(jobId).build());
Long resourcePoolId = job.getResourcePoolId();
String label = ResourcePool.DEFAULT;
if (null != resourcePoolId) {
ResourcePoolEntity resourcePool = resourcePoolMapper.findById(resourcePoolId);
label = resourcePool.getLabel();
}
final String resourcePool = label;
List<TaskVo> tasks = taskMapper.listTasks(jobId).stream().map(taskConvertor::convert)
.peek(taskVo -> taskVo.setResourcePool(resourcePool)).collect(
Collectors.toList());
return PageInfo.of(tasks);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@
import ai.starwhale.mlops.domain.task.po.TaskEntity;
import ai.starwhale.mlops.exception.ConvertException;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class TaskConvertor implements Convertor<TaskEntity, TaskVo> {

@Resource
private IdConvertor idConvertor;
private final IdConvertor idConvertor;

@Resource
private LocalDateTimeConvertor localDateTimeConvertor;
private final LocalDateTimeConvertor localDateTimeConvertor;

public TaskConvertor(IdConvertor idConvertor,
LocalDateTimeConvertor localDateTimeConvertor) {
this.idConvertor = idConvertor;
this.localDateTimeConvertor = localDateTimeConvertor;
}

@Override
public TaskVo convert(TaskEntity entity) throws ConvertException {
Expand All @@ -44,7 +47,7 @@ public TaskVo convert(TaskEntity entity) throws ConvertException {
.id(idConvertor.convert(entity.getId()))
.uuid(entity.getTaskUuid())
.taskStatus(entity.getTaskStatus())
.createdTime(localDateTimeConvertor.convert(entity.getCreatedTime()))
.createdTime(localDateTimeConvertor.convert(entity.getStartedTime()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import ai.starwhale.mlops.domain.task.status.TaskStatus;
import java.time.LocalDateTime;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface TaskMapper {

List<TaskEntity> listTasks(@Param("jobId") Long jobId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2022 Starwhale, Inc. All Rights Reserved.
*
* 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
*
* http://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 ai.starwhale.mlops.domain.task;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import ai.starwhale.mlops.api.protocol.task.TaskVo;
import ai.starwhale.mlops.common.IdConvertor;
import ai.starwhale.mlops.common.LocalDateTimeConvertor;
import ai.starwhale.mlops.common.PageParams;
import ai.starwhale.mlops.domain.job.JobManager;
import ai.starwhale.mlops.domain.job.po.JobEntity;
import ai.starwhale.mlops.domain.system.mapper.ResourcePoolMapper;
import ai.starwhale.mlops.domain.system.po.ResourcePoolEntity;
import ai.starwhale.mlops.domain.system.resourcepool.bo.ResourcePool;
import ai.starwhale.mlops.domain.task.converter.TaskConvertor;
import ai.starwhale.mlops.domain.task.mapper.TaskMapper;
import ai.starwhale.mlops.domain.task.po.TaskEntity;
import ai.starwhale.mlops.domain.task.status.TaskStatus;
import ai.starwhale.mlops.storage.StorageAccessService;
import com.github.pagehelper.PageInfo;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestTaskService {

TaskService taskService;
TaskConvertor taskConvertor;

TaskMapper taskMapper;

StorageAccessService storageAccessService;

JobManager jobManager;

ResourcePoolMapper resourcePoolMapper;

LocalDateTimeConvertor localDateTimeConvertor = new LocalDateTimeConvertor();

@BeforeEach
public void setup() {
taskConvertor = new TaskConvertor(new IdConvertor(), localDateTimeConvertor);
taskMapper = mock(TaskMapper.class);
storageAccessService = mock(StorageAccessService.class);
jobManager = mock(JobManager.class);
resourcePoolMapper = mock(ResourcePoolMapper.class);
taskService = new TaskService(taskConvertor, taskMapper, storageAccessService, jobManager,
resourcePoolMapper);
}

@Test
public void testListTaskWithResourcePool() {
when(jobManager.getJobId(anyString())).thenReturn(1L);
when(jobManager.findJob(any())).thenReturn(JobEntity.builder().resourcePoolId(1L).build());
when(resourcePoolMapper.findById(1L)).thenReturn(ResourcePoolEntity.builder().id(1L).label("LABEL").build());
LocalDateTime startedTime = LocalDateTime.of(2022, 9, 9, 9, 9);
when(taskMapper.listTasks(1L)).thenReturn(
List.of(TaskEntity.builder().id(1L).startedTime(startedTime).taskUuid("uuid1")
.taskStatus(
TaskStatus.RUNNING).build(),
TaskEntity.builder().id(2L).startedTime(startedTime).taskUuid("uuid2")
.taskStatus(
TaskStatus.SUCCESS).build()));
PageInfo<TaskVo> taskVoPageInfo = taskService.listTasks("",
PageParams.builder().pageNum(0).pageSize(3).build());
Assertions.assertEquals(1, taskVoPageInfo.getPages());
Assertions.assertEquals(2, taskVoPageInfo.getSize());
Assertions.assertEquals(2, taskVoPageInfo.getList().size());
assertThat(taskVoPageInfo.getList(), containsInAnyOrder(
TaskVo.builder().id("1").createdTime(localDateTimeConvertor.convert(startedTime)).uuid("uuid1")
.taskStatus(TaskStatus.RUNNING).resourcePool("LABEL").build(),
TaskVo.builder().id("2").createdTime(localDateTimeConvertor.convert(startedTime)).uuid("uuid2")
.taskStatus(TaskStatus.SUCCESS).resourcePool("LABEL").build()));


}

@Test
public void testListTaskWithoutResourcePool() {
when(jobManager.getJobId(anyString())).thenReturn(1L);
when(jobManager.findJob(any())).thenReturn(JobEntity.builder().build());
LocalDateTime startedTime = LocalDateTime.of(2022, 9, 9, 9, 9);
when(taskMapper.listTasks(1L)).thenReturn(
List.of(TaskEntity.builder().id(1L).startedTime(startedTime).taskUuid("uuid1")
.taskStatus(
TaskStatus.RUNNING).build(),
TaskEntity.builder().id(2L).startedTime(startedTime).taskUuid("uuid2")
.taskStatus(
TaskStatus.SUCCESS).build()));
PageInfo<TaskVo> taskVoPageInfo = taskService.listTasks("",
PageParams.builder().pageNum(0).pageSize(3).build());
Assertions.assertEquals(1, taskVoPageInfo.getPages());
Assertions.assertEquals(2, taskVoPageInfo.getSize());
Assertions.assertEquals(2, taskVoPageInfo.getList().size());
assertThat(taskVoPageInfo.getList(), containsInAnyOrder(
TaskVo.builder().id("1").createdTime(localDateTimeConvertor.convert(startedTime)).uuid("uuid1")
.taskStatus(TaskStatus.RUNNING).resourcePool(ResourcePool.DEFAULT).build(),
TaskVo.builder().id("2").createdTime(localDateTimeConvertor.convert(startedTime)).uuid("uuid2")
.taskStatus(TaskStatus.SUCCESS).resourcePool(ResourcePool.DEFAULT).build()));


}

}