package com.jay.monitor.data.server.report;

import com.jay.monitor.data.server.costs.MdConstants;
import com.jay.monitor.data.server.models.dto.report.SlowGroupModel;

import java.util.List;

/**
 * @Module 报表模块
 * @Description 列表排行执行器
 * @Author liukaixiong
 * @Date 2020/12/14 10:58
 */
public interface TopSlowTimeListReport<T> extends NameReport {

    /**
     * 报表详情
     *
     * @param reportDate
     * @return
     */
    public List<T> getListData(String reportDate);

    @Override
    default int getOrder() {
        return MdConstants.initSortCount * 2;
    }

    /**
     * 默认的格式化数据
     *
     * @param reportDate
     * @return
     */
    @Override
    default String formatReport(String reportDate) {
        StringBuffer sb = new StringBuffer("#### " + reportName());
        sb.append(MdConstants.newLine);

        List listData = getListData(reportDate);

        boolean isStart = true;

        for (int j = 0; j < listData.size(); j++) {
            Object listDatum = listData.get(j);
            if (listDatum instanceof SlowGroupModel) {

                if (isStart) {
                    sb.append("| 序号  | URL        | 访问量   | 平均值  | 最大值  |");
                    sb.append(MdConstants.newLine);
                    sb.append("| ---- | ---------- | :--------: | :------: | :------: | ");
                    sb.append(MdConstants.newLine);
                    isStart = false;
                }

                SlowGroupModel data = (SlowGroupModel) listDatum;
                sb.append("|" + (j + 1));
                sb.append("|" + data.getKey());
                sb.append("|" + data.getCountValue());
                sb.append("|" + data.getAvgValue());
                sb.append("|" + data.getMaxValue() + "|");
            }
            sb.append(MdConstants.newLine);
        }
        return sb.toString();
    }
}
