package com.lanyuanxiaoyao.service.loki;

import com.lanyuanxiaoyao.service.configuration.entity.loki.LokiLogLine;
import com.lanyuanxiaoyao.service.loki.service.LokiQueryService;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import java.util.Map;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.retry.annotation.EnableRetry;
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
 */
@EnableDiscoveryClient
@SpringBootApplication(
        scanBasePackages = {"com.lanyuanxiaoyao.service"},
        exclude = {GsonAutoConfiguration.class}
)
@EnableConfigurationProperties
@EnableEncryptableProperties
@EnableRetry
@RequestMapping("loki")
@RestController
public class LokiQueryApplication {
    private static final Logger logger = LoggerFactory.getLogger(LokiQueryApplication.class);

    public LokiQueryApplication(LokiQueryService lokiQueryService) {
        this.lokiQueryService = lokiQueryService;
    }

    public static void main(String[] args) {
        SpringApplication.run(LokiQueryApplication.class, args);
    }

    private final LokiQueryService lokiQueryService;

    @GetMapping("query_range")
    public ImmutableList<LokiLogLine> queryRange(@RequestParam Map<String, String> queryMap, @RequestParam(value = "limit", defaultValue = "200") Integer limit) throws Exception {
        return lokiQueryService.queryRange(Maps.immutable.ofAll(queryMap), limit);
    }
}
