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

import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import cn.reghao.jutil.jdk.web.log.NginxLog;
import lombok.extern.slf4j.Slf4j;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author reghao
 * @date 2023-11-08 10:08:02
 */
@Slf4j
@Deprecated
public class NgxLogService {
    String matchedMethod = "GET";
    String matchedUrl = "/";
    Map<String, Map<Long, Integer>> map = new HashMap<>();
    Map<Long, Integer> ngxLogMap = new TreeMap<>();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss+08:00").withZone(ZoneId.of("UTC"));
    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.of("UTC"));

    public void processNginxLog0(NginxLog nginxLog) {
        String todayStr = LocalDateTime.now().toLocalDate().format(formatter1);
        String method = nginxLog.getRequestMethod();
        String url = nginxLog.getUrl();
        if (method.equals(matchedMethod) && url.startsWith(matchedUrl)) {
            String date = nginxLog.getTimeIso8601();
            LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
            LocalDate localDate = localDateTime.toLocalDate();
            String localDateStr = localDate.format(formatter1);
            Map<Long, Integer> ngxLogMap = map.computeIfAbsent(localDateStr, k -> new TreeMap<>());

            LocalTime localTime = localDateTime.toLocalTime();
            Long timestampSecond = LocalDateTime.parse(date, formatter).toEpochSecond(ZoneOffset.of("+8"));
            Integer count = ngxLogMap.get(timestampSecond);
            if (count == null) {
                ngxLogMap.put(timestampSecond, 1);
            } else {
                int count1 = ngxLogMap.get(timestampSecond) + 1;
                ngxLogMap.put(timestampSecond, count1);
            }
        }
    }

    /**
     * NginxLog 在前端 echarts 中的可视化
     *
     * @param
     * @return
     * @date 2023-12-01 17:41:07
     */
    class PushTask implements Runnable {
        @Override
        public void run() {
            while (!Thread.interrupted()) {
                Set<String> dateSet = map.keySet();
                try {
                    if (ngxLogMap.size() < 3) {
                        Thread.sleep(10_000);
                        continue;
                    }

                    List<Object> chartData = getChartData();
                    /*TextMessage textMessage = new TextMessage(JsonConverter.objectToJson(chartData));
                    pullSessionMap.values().forEach(webSocketSession -> {
                        try {
                            webSocketSession.sendMessage(textMessage);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });*/
                    Thread.sleep(3_000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        /**
         * x 轴显示时分秒
         * y 轴显示访问次数
         *
         * @param
         * @return
         * @date 2025-12-24 17:48:10
         */
        private List<Object> getChartData() {
            Long timestampSecondCurrent = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
            List<String> xList = new ArrayList<>();
            List<Integer> yList = new ArrayList<>();
            Set<Long> keys = new HashSet<>();
            for (Long timestampSecond : ngxLogMap.keySet()) {
                if (timestampSecond < timestampSecondCurrent) {
                    // x 轴显示时分秒
                    xList.add(DateTimeConverter.format(timestampSecond*1000).split(" ")[1]);
                    // y 轴显示访问次数
                    yList.add(ngxLogMap.get(timestampSecond));
                    keys.add(timestampSecond);
                }
            }

            keys.forEach(ngxLogMap::remove);
            keys.clear();
            List<Object> results = new ArrayList<>();
            results.add(xList.toArray());
            results.add(yList.toArray());
            return results;
        }
    }

    private List<Object> getChartData(Map<Long, Integer> timeMap) {
        Long timestampSecondCurrent = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        List<String> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();
        Set<Long> keys = new HashSet<>();
        for (Long timestampSecond : timeMap.keySet()) {
            if (timestampSecond < timestampSecondCurrent) {
                // x 轴显示时分秒
                xList.add(DateTimeConverter.format(timestampSecond*1000).split(" ")[1]);
                // y 轴显示访问次数
                yList.add(timeMap.get(timestampSecond));
                keys.add(timestampSecond);
            }
        }

        keys.forEach(timeMap::remove);
        keys.clear();
        List<Object> results = new ArrayList<>();
        results.add(xList.toArray());
        results.add(yList.toArray());
        return results;
    }
}
