Commit cfa17684 authored by Nikolaos Kyriakoulis's avatar Nikolaos Kyriakoulis
Browse files

Use metrics models for API responses

parent b4e7250e
Loading
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.etsi.osl.tmf.metrics.ResourcesGroupByState;
import org.etsi.osl.tmf.metrics.TotalResources;
import org.etsi.osl.tmf.ri639.model.ResourceStatusType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -29,7 +31,7 @@ public interface MetricsApi {
            @ApiResponse(responseCode = "500", description = "Internal Server Error")
    })
    @RequestMapping(value = "/metrics/totalResources", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    ResponseEntity<Map<String, Integer>> getTotalResources(
    ResponseEntity<TotalResources> getTotalResources(
            @Valid @RequestParam(value = "state", required = false) ResourceStatusType state
    );

@@ -40,7 +42,7 @@ public interface MetricsApi {
            @ApiResponse(responseCode = "500", description = "Internal Server Error")
    })
    @RequestMapping(value = "/metrics/resourcesGroupByState", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    ResponseEntity<Map<String, Object>> getResourcesGroupedByState(
    ResponseEntity<ResourcesGroupByState> getResourcesGroupedByState(
            @Valid @RequestParam(value = "starttime", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) OffsetDateTime starttime,
            @Valid @RequestParam(value = "endtime", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) OffsetDateTime endtime
    );
+12 −21
Original line number Diff line number Diff line
package org.etsi.osl.tmf.metrics.api;

import org.etsi.osl.tmf.metrics.*;
import org.etsi.osl.tmf.metrics.reposervices.MetricsRepoService;
import org.etsi.osl.tmf.ri639.model.ResourceStatusType;
import org.slf4j.Logger;
@@ -10,7 +11,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -27,11 +27,10 @@ public class MetricsApiController implements MetricsApi {
    }

    @Override
    public ResponseEntity<Map<String, Integer>> getTotalResources(ResourceStatusType state) {
    public ResponseEntity<TotalResources> getTotalResources(ResourceStatusType state) {
        try {
            int totalResources = resourceMetricsRepoService.countTotalResources(state);
            Map<String, Integer> response = new HashMap<>();
            response.put("totalResources", totalResources);
            TotalResources response = new TotalResources(totalResources);
            return new ResponseEntity<>(response, HttpStatus.OK);
        } catch (Exception e) {
            log.error("Couldn't retrieve total resources. ", e);
@@ -40,7 +39,7 @@ public class MetricsApiController implements MetricsApi {
    }

    @Override
    public ResponseEntity<Map<String, Object>> getResourcesGroupedByState(OffsetDateTime starttime, OffsetDateTime endtime) {
    public ResponseEntity<ResourcesGroupByState> getResourcesGroupedByState(OffsetDateTime starttime, OffsetDateTime endtime) {
        try {
            Map<String, Integer> resourcesByState = resourceMetricsRepoService.getResourcesGroupedByState(starttime, endtime);

@@ -55,25 +54,17 @@ public class MetricsApiController implements MetricsApi {
                fullStateMap.put(key.toUpperCase(), value); // normalize case just in case
            });

            // Build groupByState list
            List<Map<String, Object>> groupByStateList = fullStateMap.entrySet().stream()
                    .map(entry -> {
                        Map<String, Object> map = new HashMap<>();
                        map.put("key", entry.getKey());
                        map.put("count", entry.getValue());
                        return map;
                    })
            // Create aggregation items
            List<GroupByItem> groupByStateList = fullStateMap.entrySet().stream()
                    .map(entry -> new GroupByItem(entry.getKey(), entry.getValue()))
                    .toList();

            // Build response structure using models
            GroupByStateAggregations aggregations = new GroupByStateAggregations(groupByStateList);
            int total = fullStateMap.values().stream().mapToInt(Integer::intValue).sum();
            Resources services = new Resources(total, aggregations);
            ResourcesGroupByState response = new ResourcesGroupByState(services);

            // Wrap in response structure
            Map<String, Object> aggregations = Map.of("groupByState", groupByStateList);
            Map<String, Object> services = Map.of(
                    "total", fullStateMap.values().stream().mapToInt(Integer::intValue).sum(),
                    "aggregations", aggregations
            );

            Map<String, Object> response = Map.of("resources", services);
            return new ResponseEntity<>(response, HttpStatus.OK);

        } catch (Exception e) {