package com.purui.prims;

import com.purui.prims.common.pojo.PersistenceConf;
import com.purui.prims.dscheduler.model.DSTaskRequest;
import com.purui.prims.dscheduler.model.TaskInstance;
import com.purui.prims.dscheduler.model.TaskLog;
import com.purui.prims.dscheduler.service.ProjectService;
import com.purui.prims.headless.server.service.SchedulerService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * DolphinScheduler测试
 *
 * @author charlie
 * @date 2024/7/9
 */
public class SchedulerXTest extends BaseTest {

    @Resource
    ProjectService projectService;

    @Resource
    SchedulerService schedulerService;

    @Test
    public void testCreateProject() {
        projectService.getProject();
    }

    @Test
    public void testCreateTask() {
        Long code = schedulerService.getProjectCode();
        String sql = "select * from test";
        String taskName = "test";
        // 根据不同的调度周期区分工作流
        String processName = "process_index_day";
        List<DSTaskRequest> taskRequests = new ArrayList<>();
        // schedulerService.pushAddTask(processName, request);

        DSTaskRequest request = new DSTaskRequest().create(taskName, sql, null, null);
        request.setProjectCode(code);
        request.setScheduleCycle("day");
        request.setTaskId("1");
        taskRequests.add(request);

        taskName = "next-test";
        DSTaskRequest request2 = new DSTaskRequest().create(taskName, sql, null, null);
        request2.setProjectCode(code);

        request2.setScheduleCycle("day");
        request2.setTaskId("2");
        taskRequests.add(request2);


        taskName = "next-new";
        DSTaskRequest request3 = new DSTaskRequest().create(taskName, sql, null, null);
        request3.setProjectCode(code);
        request3.setScheduleCycle("day");
        request3.setTaskId("3");
        taskRequests.add(request3);

        PersistenceConf conf = new PersistenceConf();
        conf.setCron("0 20 0 * * ?");

        schedulerService.pushWorkflow(processName, taskRequests, conf);

    }

    @Test
    public void testPublishWorkflow() {
        String flowName = "process_index_day";
    }

    @Test
    void queryTaskLog() {
        List<TaskInstance> taskInstances = schedulerService.getTaskInstances("index_metrics_day_16", "", "");
        if (taskInstances.size() > 0) {
            TaskInstance taskInstance = taskInstances.get(0);
            TaskLog log = schedulerService.queryTaskLog(taskInstance.getId());
            Assert.assertNotNull(log);
        }
    }


}
