package cn.reghao.tnb.search.esapp.log.controller;

import cn.reghao.tnb.common.db.SelectOption;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.search.esapp.log.model.dto.LogQuery;
import cn.reghao.tnb.search.esapp.log.service.NginxLogService;
import cn.reghao.tnb.search.esapp.log.model.dto.DateTimeQuery;
import cn.reghao.tnb.search.esapp.log.model.vo.ChartData;
import cn.reghao.tnb.search.esapp.log.model.vo.ChartMap;
import cn.reghao.tnb.search.esapp.log.model.vo.GroupCount;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2025-12-25 14:09:51
 */
@RestController
@RequestMapping("/api/search1/nginx")
public class NginxLogController {
    private final NginxLogService nginxLogService;

    public NginxLogController(NginxLogService nginxLogService) {
        this.nginxLogService = nginxLogService;
    }

    @Operation(summary = "NginxLog 日期列表", description = "N")
    @GetMapping(value = "/log/date", produces = MediaType.APPLICATION_JSON_VALUE)
    public String nginxLogDate() {
        List<SelectOption> list = nginxLogService.getDateList();
        return WebResult.success(list);
    }

    @Operation(summary = "NginxLog 在指定日期的数量", description = "N")
    @GetMapping(value = "/count", produces = MediaType.APPLICATION_JSON_VALUE)
    public String nginxLogCount(@RequestParam(value = "dateStr") String dateStr) {
        long total = nginxLogService.countNginxLog(dateStr);
        return WebResult.success(total);
    }

    @Operation(summary = "指定日期和 url 的最近一周内 NginxLog 在每天的数量", description = "N")
    @GetMapping(value = "/log/chart5", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getLogChart5(@Validated DateTimeQuery dateTimeQuery) {
        List<Object> chartData = nginxLogService.getChartData5(dateTimeQuery);
        return WebResult.success(chartData);
    }

    @Operation(summary = "指定 url 在某天内每秒的数量", description = "N")
    @GetMapping(value = "/log/chart2", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getLogChart2(@Validated LogQuery logQuery) {
        List<Object> chartData = nginxLogService.getChartData2(logQuery);
        return WebResult.success(chartData);
    }

    @Operation(summary = "对指定字段在某天内进行聚合", description = "N")
    @GetMapping(value = "/log/chart3", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getLogChart3(@Validated LogQuery logQuery) {
        List<GroupCount> chartData = nginxLogService.getChartData3(logQuery);
        return WebResult.success(chartData);
    }

    @Operation(summary = "获取 NginxLog 中耗时 TopN 的数据", description = "N")
    @GetMapping(value = "/log/chart4", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getLogChart4(@Validated LogQuery logQuery) {
        List<ChartData> list = nginxLogService.getTopN(logQuery.getDateStr());
        return WebResult.success(list);
    }

    @Operation(summary = "获取 NginxLog 中根据访问 IP 得到的 echart 地图", description = "N")
    @GetMapping(value = "/log/chart6", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getLogChart6(@Validated LogQuery logQuery) {
        ChartMap chartMap = nginxLogService.getChartMap(logQuery);
        return chartMap != null ? WebResult.success(chartMap) : WebResult.failWithMsg("aggregateField unknown");
    }
}
