package com.sankuai.inf.leaf.server.controller;

import com.sankuai.inf.leaf.segment.SegmentIDGenImpl;
import com.sankuai.inf.leaf.server.model.SegmentBufferView;
import com.sankuai.inf.leaf.segment.model.LeafAlloc;
import com.sankuai.inf.leaf.segment.model.SegmentBuffer;
import com.sankuai.inf.leaf.server.service.SegmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LeafMonitorController {
    private Logger logger = LoggerFactory.getLogger(LeafMonitorController.class);
    @Autowired
    SegmentService segmentService;

    @RequestMapping(value = "cache")
    public String getCache(Model model) {
        Map<String, SegmentBufferView> data = new HashMap<>();
        SegmentIDGenImpl segmentIDGen = segmentService.getIdGen();
        if (segmentIDGen == null) {
            throw new IllegalArgumentException("You should config leaf.segment.enable=true first");
        }
        Map<String, SegmentBuffer> cache = segmentIDGen.getCache();
        for (Map.Entry<String, SegmentBuffer> entry : cache.entrySet()) {
            SegmentBufferView sv = new SegmentBufferView();
            SegmentBuffer buffer = entry.getValue();
            sv.setInitOk(buffer.isInitOk());
            sv.setKey(buffer.getKey());
            sv.setPos(buffer.getCurrentPos());
            sv.setNextReady(buffer.isNextReady());
            sv.setMax0(buffer.getSegments()[0].getMax());
            sv.setValue0(buffer.getSegments()[0].getValue().get());
            sv.setStep0(buffer.getSegments()[0].getStep());

            sv.setMax1(buffer.getSegments()[1].getMax());
            sv.setValue1(buffer.getSegments()[1].getValue().get());
            sv.setStep1(buffer.getSegments()[1].getStep());

            data.put(entry.getKey(), sv);

        }
        logger.info("Cache info {}", data);
        model.addAttribute("data", data);
        return "segment";
    }

    @RequestMapping(value = "db")
    public String getDb(Model model) {
        SegmentIDGenImpl segmentIDGen = segmentService.getIdGen();
        if (segmentIDGen == null) {
            throw new IllegalArgumentException("You should config leaf.segment.enable=true first");
        }
        List<LeafAlloc> items = segmentIDGen.getAllLeafAllocs();
        logger.info("DB info {}", items);
        model.addAttribute("items", items);
        return "db";
    }

}

@@ -590,17 +590,257 @@
 uest
-Mapping;%0A
+Body;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestParam;%0Aimport org.springframework.web.bind.annotation.ResponseBody;%0A%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date;
 %0Aimp
@@ -2894,10 +2894,1207 @@
 %0A    %7D%0A%0A
+    /**%0A     * the output is like this:%0A     * %7B%0A     *   %22timestamp%22: %221567733700834(2019-09-06 09:35:00.834)%22,%0A     *   %22sequenceId%22: %223448%22,%0A     *   %22workerId%22: %2239%22%0A     * %7D%0A     */%0A    @RequestMapping(value = %22decodeSnowflakeId%22)%0A    @ResponseBody%0A    public Map%3CString, String%3E decodeSnowflakeId(@RequestParam(%22snowflakeId%22) String snowflakeIdStr) %7B%0A        Map%3CString, String%3E map = new HashMap%3C%3E();%0A        try %7B%0A            long snowflakeId = Long.parseLong(snowflakeIdStr);%0A%0A            long originTimestamp = (snowflakeId %3E%3E 22) + 1288834974657L;%0A            Date date = new Date(originTimestamp);%0A            SimpleDateFormat sdf = new SimpleDateFormat(%22yyyy-MM-dd HH:mm:ss.SSS%22);%0A            map.put(%22timestamp%22, String.valueOf(originTimestamp) + %22(%22 + sdf.format(date) + %22)%22);%0A%0A            long workerId = (snowflakeId %3E%3E 12) %5E (snowflakeId %3E%3E 22 %3C%3C 10);%0A            map.put(%22workerId%22, String.valueOf(workerId));%0A%0A            long sequence = snowflakeId %5E (snowflakeId %3E%3E 12 %3C%3C 12);%0A            map.put(%22sequenceId%22, String.valueOf(sequence));%0A        %7D catch (NumberFormatException e) %7B%0A            map.put(%22errorMsg%22, %22snowflake Id%E5%8F%8D%E8%A7%A3%E6%9E%90%E5%8F%91%E7%94%9F%E5%BC%82%E5%B8%B8!%22);%0A        %7D%0A        return map;%0A    %7D%0A
 %7D%0A

decode snowflake id