package com.lanyuanxiaoyao.service.loki;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author lanyuanxiaoyao
 * @date 2023-05-25
 */
public class LokiQueryParse {
    private static final Logger logger = LoggerFactory.getLogger(LokiQueryParse.class);

    private static String buildLokiQuery(ImmutableMap<String, String> queryMap) {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        String query = queryMap
                .collectValues((key, value) -> StrUtil.format("{}=\"{}\"", key, value))
                .valuesView()
                .makeString(",");
        builder.append(query);
        builder.append("}");
        return builder.toString();
    }

    public static void main(String[] args) throws JsonProcessingException {
//        String query = URLUtil.encodeQuery("{app=\"hudi-sync\",run_type=\"compaction\",level=~\"WARN|ERROR\"}");
//        HttpResponse response = HttpUtil.createGet(StrUtil.format("http://132.122.116.142:33100/loki/api/v1/query_range?query={}&limit=10", query)).execute();
//        // logger.info("{}\n{}\n{}", query, response.getStatus(), response.body());
//        ObjectMapper mapper = new ObjectMapper();
//        mapper.registerModule(new EclipseCollectionsModule());
//        mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
//        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        LokiQueryRangeResponse queryRangeResponse = mapper.readValue(response.body(), LokiQueryRangeResponse.class);
//        logger.info("{}", queryRangeResponse);
        logger.info("{}", buildLokiQuery(Maps.immutable.of("app", "sync", "run_type", "compact")));
    }
}
