Commit 88b56904 authored by Matija Obreza's avatar Matija Obreza

KPI Controller v1

parent 8e7c3d51
......@@ -243,11 +243,9 @@ public class KPIController extends ApiBaseController {
public @ResponseBody List<Observation> execute(@PathVariable("executionName") String executionName) {
Execution execution = kpiService.getExecution(executionName);
LOG.info("Running execute on : {}", executionName);
List<Observation> res = kpiService.execute(execution);
LOG.info("Saving results: count={}", res.size());
List<Observation> x = kpiService.save(execution, res);
List<Observation> res = kpiService.executeAndSave(execution).getObservations();
LOG.info("Done saving results.");
return x;
return res;
}
/**
......
/*
* 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.genesys2.server.api.v1;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.JsonViews;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.kpi.Dimension;
import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.ExecutionRun;
import org.genesys2.server.model.kpi.KPIParameter;
import org.genesys2.server.service.KPIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
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.RestController;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
@RestController("kpiApi1")
@RequestMapping(value = { KPIController.CONTROLLER_URL })
@Api(tags = { "kpi" })
public class KPIController {
public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/kpi";
@Autowired
private KPIService kpiService;
/**
* First declare the KPI parameters: e.g. `Accession where `
*
* @param parameter
* @return
*/
@PostMapping(value = "/parameters/save")
public KPIParameter saveParameter(@RequestBody KPIParameter parameter) {
return kpiService.save(parameter);
}
@DeleteMapping(value = "/parameters/{name}")
public KPIParameter deleteParameter(@PathVariable String name) {
return kpiService.delete(kpiService.getParameter(name));
}
@GetMapping(value="/parameters")
public Page<KPIParameter> listParameters(final Pagination page) {
return kpiService.listParameters(page.toPageRequest(100));
}
/**
* 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 Dimension<?> saveDimension(@RequestBody Dimension<?> dimension) {
return kpiService.save(dimension);
}
@DeleteMapping(value = "/dimensions/{name}")
public Dimension<?> deleteDimension(@PathVariable String name) {
return kpiService.delete(kpiService.getDimension(name));
}
@GetMapping(value="/dimensions")
public Page<Dimension<?>> listDimensions(final Pagination page) {
return kpiService.listDimensions(page.toPageRequest(100));
}
/**
* Declare execution with a {@link KPIParameter} and one or more {@link Dimension}
*
* @param execution
* @return
*/
@PostMapping(value = "/executions/save")
public Execution saveExecution(@RequestBody Execution execution) {
return kpiService.save(execution);
}
@DeleteMapping(value = "/executions/{name}")
public Execution deleteExecution(@PathVariable String name) {
return kpiService.save(kpiService.getExecution(name));
}
@GetMapping(value="/executions")
public Page<Execution> listExecution(final Pagination page) {
return kpiService.listExecutions(page.toPageRequest(100));
}
@PostMapping(value = "/executions/{name}/execute")
public ExecutionRun runExecution(@PathVariable String name) {
return kpiService.executeAndSave(kpiService.getExecution(name));
}
/**
* Execution details
*/
@GetMapping(value = "/executions/{name}")
public ExecutionDetails executionDetails(@PathVariable String name) {
Execution execution = kpiService.getExecution(name);
return ExecutionDetails.from(execution, kpiService.listExecutionRuns(execution, new PageRequest(0, 10)));
}
@GetMapping(value = "/executions/{name}/runs")
public List<ExecutionRun> executionRuns(final @PathVariable String name, final Pagination page) {
return kpiService.listExecutionRuns(kpiService.getExecution(name), page.toPageRequest(100));
}
@GetMapping(value = "/executions/{name}/runs/{id}")
public ExecutionRun executionRun(final @PathVariable String name, final long runId) {
ExecutionRun run = kpiService.getExecutionRun(runId);
if (! StringUtils.equals(run.getExecution().getName(), name)) {
throw new NotFoundElement("No run " + runId + " for execution " + name);
}
return run;
}
/**
* Details
*/
public static class ExecutionDetails {
public Execution execution;
public List<ExecutionRun> runs;
public static ExecutionDetails from(Execution execution, List<ExecutionRun> runs) {
ExecutionDetails ed=new ExecutionDetails();
ed.execution=execution;
ed.runs = runs;
return ed;
}
}
}
......@@ -54,9 +54,9 @@ public abstract class Dimension<T> extends AuditedVersionedModel {
*
*/
private static final long serialVersionUID = 1672379271657218936L;
@Column(length = 100, nullable = false)
@Column(length = 100, nullable = false, unique = true)
private String name;
@Column(length = 100, nullable = false)
@Column(length = 300, nullable = false)
private String title;
final public String getName() {
......
......@@ -17,10 +17,12 @@
package org.genesys2.server.model.kpi;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
......@@ -46,6 +48,9 @@ public class ExecutionRun extends BasicModel {
@ManyToOne(cascade = {}, optional = false)
private Execution execution;
@OneToMany(mappedBy = "executionRun", orphanRemoval = true)
private List<Observation> observations;
public Date getTimestamp() {
return timestamp;
}
......@@ -61,4 +66,12 @@ public class ExecutionRun extends BasicModel {
public void setExecution(Execution execution) {
this.execution = execution;
}
public List<Observation> getObservations() {
return observations;
}
public void setObservations(List<Observation> observations) {
this.observations = observations;
}
}
......@@ -21,4 +21,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
public interface DimensionRepository extends JpaRepository<Dimension<?>, Long> {
Dimension<?> findByName(String name);
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
/*
* 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.genesys2.server.service;
......@@ -25,44 +25,54 @@ import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.ExecutionRun;
import org.genesys2.server.model.kpi.KPIParameter;
import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface KPIService {
long getSingleResult(String paQuery, Object... params);
Set<?> getValues(Dimension<?> loadedJpa);
KPIParameter save(KPIParameter parameter);
KPIParameter delete(KPIParameter parameter);
KPIParameter getParameter(String name);
KPIParameter getParameter(long id);
KPIParameter delete(KPIParameter parameter);
List<KPIParameter> listParameters();
Page<KPIParameter> listParameters(Pageable page);
Dimension<?> save(Dimension<?> dimension);
Dimension<?> getDimension(long id);
Dimension<?> getDimension(String name);
Dimension<?> delete(Dimension<?> dimension);
Set<?> getValues(Dimension<?> loadedJpa);
List<Dimension<?>> listDimensions();
Page<Dimension<?>> listDimensions(Pageable page);
Execution save(Execution execution);
Execution getExecution(long id);
Execution getExecution(String executionName);
Execution delete(Execution execution);
List<Observation> execute(Execution execution);
Execution delete(Execution execution);
List<Observation> save(Execution execution, List<Observation> observations);
List<Execution> listExecutions();
List<KPIParameter> listParameters();
Page<Execution> listExecutions(Pageable page);
List<Dimension<?>> listDimensions();
List<Observation> execute(Execution execution);
List<Execution> listExecutions();
ExecutionRun executeAndSave(Execution execution);
List<Observation> listObservations(ExecutionRun executionRun, Map<String, String> dimensionFilters, Pageable page);
......
......@@ -47,6 +47,7 @@ import org.genesys2.server.service.KPIService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
......@@ -101,6 +102,11 @@ public class KPIServiceImpl implements KPIService {
public List<KPIParameter> listParameters() {
return parameterRepository.findAll(new Sort("name"));
}
@Override
public Page<KPIParameter> listParameters(Pageable page) {
return parameterRepository.findAll(page);
}
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#parameter, 'ADMINISTRATION')")
@Override
......@@ -136,10 +142,23 @@ public class KPIServiceImpl implements KPIService {
return dim;
}
@Override
public Dimension<?> getDimension(String name) {
Dimension<?> dim = dimensionRepository.findByName(name);
if (!(dim instanceof JpaDimension))
dim.getValues().size();
return dim;
}
@Override
public List<Dimension<?>> listDimensions() {
return dimensionRepository.findAll(new Sort("name"));
}
@Override
public Page<Dimension<?>> listDimensions(Pageable page) {
return dimensionRepository.findAll(page);
}
@Override
public Execution getExecution(long id) {
......@@ -168,6 +187,11 @@ public class KPIServiceImpl implements KPIService {
public List<Execution> listExecutions() {
return executionRepository.findAll();
}
@Override
public Page<Execution> listExecutions(Pageable page) {
return executionRepository.findAll(page);
}
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'ADMINISTRATION')")
@Override
......@@ -235,9 +259,10 @@ public class KPIServiceImpl implements KPIService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'ADMINISTRATION')")
@Override
@Transactional
public List<Observation> save(Execution execution, List<Observation> observations) {
public ExecutionRun executeAndSave(Execution execution) {
ExecutionRun executionRun = new ExecutionRun();
List<Observation> observations = execute(execution);
executionRun.setExecution(execution);
executionRun.setTimestamp(new Date());
......@@ -291,11 +316,11 @@ public class KPIServiceImpl implements KPIService {
if (observations.size() == 0) {
LOG.warn("No observations to save. Not storing execution run.");
return observations;
return null;
}
executionRunRepository.save(executionRun);
return observationRepository.save(observations);
observationRepository.save(observations);
return executionRunRepository.save(executionRun);
}
// private void removeSameResult(ExecutionRun previousRun, List<Observation>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment