KPIController.java

/*
 * Copyright 2025 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gringlobal.api.v2.impl;

import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.genesys.blocks.model.JsonViews;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.Pagination;
import org.gringlobal.api.model.kpi.DimensionDTO;
import org.gringlobal.api.model.kpi.ExecutionDTO;
import org.gringlobal.api.model.kpi.ExecutionRunDTO;
import org.gringlobal.api.model.kpi.KPIParameterDTO;
import org.gringlobal.api.model.kpi.ObservationDTO;
import org.gringlobal.api.v2.facade.KPIApiService;
import org.gringlobal.model.kpi.Dimension;
import org.gringlobal.model.kpi.KPIParameter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

@RestController("kpiApi2")
@RequestMapping(value = { KPIController.API_URL })
@PreAuthorize("isAuthenticated()")
@Tag(name = "KPI")
public class KPIController extends ApiBaseController {

	/** The Constant API_URL. */
	public static final String API_URL = ApiBaseController.APIv2_BASE + "/kpi";

	@Autowired
	private KPIApiService kpiApiService;

	/**
	 * First declare the KPI parameters: e.g. `Accession where `
	 *
	 * @param parameter the parameter
	 * @return
	 */
	@PostMapping(value = "/parameters/save")
	public KPIParameterDTO saveParameter(@RequestBody KPIParameterDTO parameter) {
		return kpiApiService.getParameter(kpiApiService.save(parameter).getId());
	}

	@DeleteMapping(value = "/parameters/{name:.+}")
	public KPIParameterDTO deleteParameter(@PathVariable String name) {
		return kpiApiService.deleteParameter(name);
	}

	@GetMapping(value="/parameters")
	public Page<KPIParameterDTO> listParameters(@ParameterObject final Pagination page) {
		return kpiApiService.listParameters(page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}


	@GetMapping(value="/dimensions/{name:.+}")
	public DimensionDTO getDimension(@PathVariable String name) {
		return kpiApiService.getDimension(name);
	}

	/**
	 * Then declare the KPI dimensions: e.g. `FaoInstitute#code where accessionCount gt 0`
	 * Dimensions provide query parameters to KPI execution
	 *
	 * @param dimension the parameter
	 * @return
	 */
	@PostMapping(value = "/dimensions/save")
	public DimensionDTO saveDimension(@RequestBody DimensionDTO dimension) {
		return kpiApiService.getDimension(kpiApiService.save(dimension).getId());
	}

	@DeleteMapping(value = "/dimensions/{name:.+}")
	public DimensionDTO deleteDimension(@PathVariable String name) {
		return kpiApiService.deleteDimension(name);
	}

	@JsonView(JsonViews.Minimal.class)
	@GetMapping(value="/dimensions")
	public Page<DimensionDTO> listDimensions(@ParameterObject final Pagination page) {
		return kpiApiService.listDimensions(page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	/**
	 * Declare execution with a {@link KPIParameter} and one or more {@link Dimension}
	 *
	 * @param execution the execution
	 * @return
	 */
	@PostMapping(value = "/executions/save")
	public ExecutionDTO saveExecution(@RequestBody ExecutionDTO execution) {
		return kpiApiService.getExecution(kpiApiService.save(execution).getId());
	}

	@DeleteMapping(value = "/executions/{name:.+}")
	public ExecutionDTO deleteExecution(@PathVariable String name) {
		return kpiApiService.deleteExecution(name);
	}

	@JsonView(JsonViews.Minimal.class)
	@GetMapping(value="/executions")
	public Page<ExecutionDTO> listExecution(@ParameterObject final Pagination page) {
		return kpiApiService.listExecutions(page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	@PostMapping(value = "/executions/{name:.+}/execute")
	public ExecutionRunDTO runExecution(@PathVariable String name) {
		return kpiApiService.runExecution(name);
	}

	@PostMapping(value = "/executions/{name:.+}/purgeRuns")
	public long purgeExecutionRuns(@PathVariable String name) {
		return kpiApiService.purgeExecutionRuns(name);
	}

	/**
	 * Execution details
	 */
	@JsonView(JsonViews.Minimal.class)
	@GetMapping(value = "/executions/{name:.+}")
	public KPIApiService.ExecutionDetailsDTO executionDetails(@PathVariable String name) {
		return kpiApiService.executionDetails(name);
	}

	/**
	 * Execution info
	 */
	@JsonView(JsonViews.Minimal.class)
	@GetMapping(value = "/executions/{name:.+}/info")
	public ExecutionDTO executionInfo(@PathVariable String name) {
		return kpiApiService.getExecution(name);
	}

	@GetMapping(value = "/executions/{name:.+}/runs")
	public Page<ExecutionRunDTO> executionRuns(final @PathVariable String name, @ParameterObject final Pagination page) {
		return kpiApiService.executionRuns(name, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	@GetMapping(value = "/executions/{name:.+}/run")
	public ExecutionRunDTO executionRunByDate(final @PathVariable String name,
			@RequestParam(value="date", required = true) @DateTimeFormat(pattern="yyyy-MM-dd") final LocalDate date) {
		return kpiApiService.executionRunByDate(name, date);
	}

	@GetMapping(value = "/executions/{name:.+}/runs/{runId}")
	public ExecutionRunDTO executionRun(final @PathVariable String name, @PathVariable final long runId) {
		return kpiApiService.executionRun(name, runId);
	}

	@DeleteMapping(value = "/executions/{name:.+}/runs/{runId}")
	public ExecutionRunDTO deleteExecutionRun(final @PathVariable String name, final @PathVariable long runId) {
		return kpiApiService.deleteExecutionRun(name, runId);
	}

	@PostMapping(value = "/executions/{name:.+}/diff")
	public SortedMap<LocalDate, List<ObservationDTO>> executionRuns(
			final @PathVariable String name,
			@RequestParam(value="days", required = false) final Integer days,
			@RequestParam(value="from", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate from,
			@RequestParam(value="to", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate to,
			@RequestBody(required = false) final Map<String, Set<String>> keys
		) {

		return kpiApiService.executionRuns(name, days, from, to, keys);
	}

	@PostMapping(value = "/executions/{name}/runs")
	public SortedMap<LocalDate, List<ObservationDTO>> executionRunsObservations(
		final @PathVariable String name, final @RequestBody KPIApiService.ExecutionRunsRequestDTO runsRequest) {

		return kpiApiService.executionRunsObservations(name, runsRequest);
	}

	@GetMapping(value = "/executions/{name}/runs/group")
	public List<KPIApiService.GroupedRunObservationsDTO> getObservationsGroupedByDimension(@PathVariable String name, 
		@RequestParam(value="dimensionName") String dimensionName,
		@RequestParam(value="toDate", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate toDate,
		@RequestParam(value="maxRuns", required = false) Integer maxRuns) {
		
		return kpiApiService.getObservationsGroupedByDimension(name, dimensionName, toDate, maxRuns);
	}
}