package com.lanyuanxiaoyao.service.web.controller;

import cn.hutool.core.map.MapUtil;
import com.lanyuanxiaoyao.service.configuration.entity.loki.LokiLogLine;
import com.lanyuanxiaoyao.service.forest.service.InfoService;
import com.lanyuanxiaoyao.service.forest.service.loki.LokiHudiService;
import com.lanyuanxiaoyao.service.web.controller.base.AmisCrudResponse;
import com.lanyuanxiaoyao.service.web.controller.base.AmisDetailResponse;
import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse;
import com.lanyuanxiaoyao.service.web.controller.base.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 日志相关
 *
 * @author lanyuanxiaoyao
 * @date 2023-05-25
 */
@RestController
@RequestMapping("log")
public class LogController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(LogController.class);

    private final LokiHudiService lokiHudiService;
    private final InfoService infoService;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    public LogController(LokiHudiService lokiHudiService, InfoService infoService) {
        this.lokiHudiService = lokiHudiService;
        this.infoService = infoService;
    }

    private String preHandle(String line) {
        return line
                .replaceAll("\\n$", "")
                .replaceAll("^\\s*(.+?)\\s*#@# :\\s*(.+?)\\s*$", "$1: $2");
    }

    @GetMapping("query_sync_log")
    public AmisDetailResponse querySyncLog(@RequestParam("flink_job_id") Long flinkJobId) {
        return AmisResponse.responseDetailData(lokiHudiService.queryRange(MapUtil.<String, String>builder()
                        .put("app", "hudi-sync")
                        .put("run_type", "sync")
                        .put("flink_job_id", flinkJobId.toString())
                        .put("limit", "1000")
                        .build())
                .collect(LokiLogLine::getData)
                .collect(this::preHandle)
                .makeString("\n"));
    }

    @GetMapping("query_compaction_log")
    public AmisDetailResponse queryCompactionLog(@RequestParam("flink_job_id") Long flinkJobId, @RequestParam("alias") String alias) {
        return AmisResponse.responseDetailData(lokiHudiService.queryRange(MapUtil.<String, String>builder()
                        .put("app", "hudi-sync")
                        .put("run_type", "compaction")
                        .put("flink_job_id", flinkJobId.toString())
                        .put("alias", alias)
                        .put("limit", "1000")
                        .build())
                .collect(LokiLogLine::getData)
                .collect(this::preHandle)
                .makeString("\n"));
    }

    @GetMapping("query_application_log")
    public AmisDetailResponse queryApplicationLog(@RequestParam("application_id") String applicationId) {
        return AmisResponse.responseDetailData(lokiHudiService.queryRange(MapUtil.<String, String>builder()
                        .put("app_id", applicationId)
                        .put("limit", "1000")
                        .build())
                .collect(LokiLogLine::getData)
                .collect(this::preHandle)
                .makeString("\n"));
    }

    @GetMapping("query_sql_log")
    public AmisCrudResponse querySQLLog() {
        return AmisResponse.responseCrudData(infoService.sqlLogs());
    }
}
