package com.datacenter.controller;

import com.datacenter.entity.StatDailyAggregate;
import com.datacenter.entity.resp.MessageResult;
import com.datacenter.service.StatDailyAggregateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/stats")
public class StatsController {

    @Autowired
    private StatDailyAggregateService statDailyAggregateService;

    @GetMapping("/ping")
    public MessageResult ping() {
        Map<String, Object> body = new HashMap<>(2);
        body.put("service", "datacenter");
        body.put("role", "statistics");
        return MessageResult.success("SUCCESS", body);
    }

    @GetMapping("/daily/list")
    public MessageResult dailyList(
            @RequestParam String metricKey,
            @RequestParam(defaultValue = "30") int limitDays) {
        List<StatDailyAggregate> list = statDailyAggregateService.listRecentByMetricKey(metricKey, limitDays);
        return MessageResult.success("SUCCESS", list);
    }
}
