KPIController.java

/*
 * Copyright 2018 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.genesys.server.api.v2.impl;

import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import org.genesys.blocks.model.JsonViews;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.Pagination;
import org.genesys.server.api.v2.facade.KPIApiService;
import org.genesys.server.api.v2.model.kpi.DimensionDTO;
import org.genesys.server.api.v2.model.kpi.ExecutionDTO;
import org.genesys.server.api.v2.model.kpi.ExecutionInfo;
import org.genesys.server.api.v2.model.kpi.ExecutionRunDTO;
import org.genesys.server.api.v2.model.kpi.KPIParameterDTO;
import org.genesys.server.api.v2.model.kpi.ObservationDTO;
import org.genesys.server.model.kpi.Dimension;
import org.genesys.server.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.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.CONTROLLER_URL })
@Api(tags = { "kpi" })
public class KPIController extends ApiBaseController {
	public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/kpi";

	@Autowired
	private KPIApiService kpiService;

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

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


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

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

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

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

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

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

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

	@PostMapping(value = "/executions/{name}/purgeRuns")
	public long purgeExecutionRuns(@PathVariable String name) {
		long deleted = kpiService.purgeExecutionRuns(name);
		System.err.println("Done deleting!");
		return deleted;
	}

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

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

	@GetMapping(value = "/executions/{name}/runs")
	public Page<ExecutionRunDTO> executionRuns(final @PathVariable String name, @ParameterObject final Pagination page) {
		return kpiService.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 kpiService.executionRunByDate(name, date);
	}

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

	@DeleteMapping(value = "/executions/{name}/runs/{runId}")
	public ExecutionRunDTO deleteRun(final @PathVariable String name, @PathVariable final long runId) {
		return kpiService.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
		) throws Exception {

		return kpiService.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 kpiService.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 kpiService.getObservationsGroupedByDimension(name, dimensionName, toDate, maxRuns);
	}
}