package com.lanyuanxiaoyao.service.monitor.controller;

import com.lanyuanxiaoyao.service.configuration.entity.monitor.MetricsProgress;
import com.lanyuanxiaoyao.service.monitor.metric.Metrics;
import java.util.Map;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 操作进度
 *
 * @author lanyuanxiaoyao
 * @date 2024-10-14
 */
@RestController
@RequestMapping("metrics_control")
public class MetricsController {
    private static final Logger logger = LoggerFactory.getLogger(MetricsController.class);

    private final ApplicationContext context;

    public MetricsController(ApplicationContext context) {
        this.context = context;
    }

    @GetMapping("progress")
    public ImmutableList<MetricsProgress> progress() {
        Map<String, Metrics> metricsMap = context.getBeansOfType(Metrics.class);
        return Lists.immutable.ofAll(metricsMap.entrySet())
                .toImmutableSortedList(Map.Entry.comparingByKey())
                .collect(Map.Entry::getValue)
                .collect(metrics -> new MetricsProgress(metrics.name(), metrics.running(), metrics.progress()));
    }
}
