package com.ruoyi.platform.controller.jupyter;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.DevEnvironment;
import com.ruoyi.platform.service.JupyterService;
import com.ruoyi.platform.service.NewDatasetService;
import com.ruoyi.platform.vo.PodStatusVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/jupyter")
@Api("jupyter service")
public class JupyterController extends BaseController {
    @Resource
    private JupyterService jupyterService;
    @Resource
    private NewDatasetService newDatasetService;

    @GetMapping(value = "/getURL")
    @ApiOperation("得到访问地址")
    public GenericsAjaxResult<String> getURL() throws IOException {
        return genericsSuccess(jupyterService.getJupyterServiceUrl());
    }


    /**
     * 启动jupyter容器接口
     *
     * @param id 开发环境配置id
     * @return url
     */
    @PostMapping("/run/{id}")
    @ApiOperation("根据开发环境id启动jupyter pod")
    @ApiResponse
    public GenericsAjaxResult<String> runJupyter(@PathVariable("id") Integer id) throws Exception {
        return genericsSuccess(this.jupyterService.runJupyterService(id));
    }


    /**
     * 停止jupyter容器接口
     *
     * @param id 开发环境配置id
     * @return 操作结果
     */
    @DeleteMapping("/stop/{id}")
    @ApiOperation("根据开发环境id停止jupyter pod")
    @ApiResponse
    public GenericsAjaxResult<String> stopJupyter(@PathVariable("id") Integer id) throws Exception {
        return genericsSuccess(this.jupyterService.stopJupyterService(id));
    }

    @PostMapping("/getStatus")
    @ApiOperation("查询jupyter pod状态")
    @ApiResponse
    public GenericsAjaxResult<PodStatusVo> getStatus(DevEnvironment devEnvironment) throws Exception {
        return genericsSuccess(this.jupyterService.getJupyterStatus(devEnvironment));
    }


    @GetMapping(value = "/upload")
    public AjaxResult upload() throws Exception {
        File file = new File("D://nexus-deploy.yaml");
        InputStream inputStream = new FileInputStream(file);
        jupyterService.upload(inputStream);
        return AjaxResult.success();
    }

    @GetMapping(value = "/mlflow")
    public AjaxResult mlflow() throws Exception {
        jupyterService.mlflow();
        return AjaxResult.success();
    }

}
