Commit af9ff4e7 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'ui-102-admin-kpi-lastRun' into 'master'

Updates to KPIServiceImpl

See merge request genesys-pgr/genesys-server!274
parents dba87907 6eecba53
......@@ -17,6 +17,7 @@ package org.genesys2.server.api.v1;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonView;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.JsonViews;
import org.genesys2.server.api.ApiBaseController;
......@@ -38,8 +39,6 @@ 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")
......@@ -72,6 +71,12 @@ public class KPIController {
return kpiService.listParameters(page.toPageRequest(100));
}
@GetMapping(value="/dimensions/{name}")
public Dimension<?> 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
......@@ -88,7 +93,8 @@ public class KPIController {
public Dimension<?> deleteDimension(@PathVariable String name) {
return kpiService.delete(kpiService.getDimension(name));
}
@JsonView(JsonViews.Minimal.class)
@GetMapping(value="/dimensions")
public Page<Dimension<?>> listDimensions(final Pagination page) {
return kpiService.listDimensions(page.toPageRequest(100));
......@@ -107,9 +113,10 @@ public class KPIController {
@DeleteMapping(value = "/executions/{name}")
public Execution deleteExecution(@PathVariable String name) {
return kpiService.save(kpiService.getExecution(name));
return kpiService.delete(kpiService.getExecution(name));
}
@JsonView(JsonViews.Minimal.class)
@GetMapping(value="/executions")
public Page<Execution> listExecution(final Pagination page) {
return kpiService.listExecutions(page.toPageRequest(100));
......@@ -123,10 +130,11 @@ public class KPIController {
/**
* Execution details
*/
@JsonView(JsonViews.Minimal.class)
@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)));
return ExecutionDetails.from(execution, kpiService.findLastExecutionRun(execution), kpiService.listExecutionRuns(execution, new PageRequest(0, 10)));
}
@GetMapping(value = "/executions/{name}/runs")
......@@ -134,8 +142,8 @@ public class KPIController {
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) {
@GetMapping(value = "/executions/{name}/runs/{runId}")
public ExecutionRun executionRun(final @PathVariable String name, @PathVariable final long runId) {
ExecutionRun run = kpiService.getExecutionRun(runId);
if (! StringUtils.equals(run.getExecution().getName(), name)) {
throw new NotFoundElement("No run " + runId + " for execution " + name);
......@@ -148,11 +156,13 @@ public class KPIController {
*/
public static class ExecutionDetails {
public Execution execution;
public ExecutionRun lastRun;
public List<ExecutionRun> runs;
public static ExecutionDetails from(Execution execution, List<ExecutionRun> runs) {
public static ExecutionDetails from(Execution execution, ExecutionRun lastRun, List<ExecutionRun> runs) {
ExecutionDetails ed=new ExecutionDetails();
ed.execution=execution;
ed.lastRun = lastRun;
ed.runs = runs;
return ed;
}
......
......@@ -28,7 +28,9 @@ import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonView;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.JsonViews;
import org.genesys2.server.model.genesys.Parameter;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
......@@ -49,7 +51,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "className", discriminatorType = DiscriminatorType.STRING, length = 100)
@Table(name = "kpidimension")
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "_fullClass")
public abstract class Dimension<T> extends AuditedVersionedModel {
/**
......@@ -83,6 +85,7 @@ public abstract class Dimension<T> extends AuditedVersionedModel {
return title;
}
@JsonView(JsonViews.Public.class)
abstract public Set<T> getValues();
@Override
......
......@@ -49,6 +49,14 @@ public class Execution extends AuditedVersionedModel {
*/
private static final long serialVersionUID = 1102563708369373562L;
public List<ExecutionRun> getRuns() {
return runs;
}
public void setRuns(List<ExecutionRun> runs) {
this.runs = runs;
}
public static enum ExecutionType {
COUNT, SUM, AVERAGE
}
......@@ -193,7 +201,7 @@ public class Execution extends AuditedVersionedModel {
if (where.length() > 0 || parameter.getCondition() != null) {
sb.append(" where ");
if (parameter.getCondition() != null) {
sb.append(parameter.getCondition());
sb.append(alias).append(".").append(parameter.getCondition());
sb.append(" and ");
}
sb.append(where);
......
......@@ -37,6 +37,7 @@ import javax.persistence.Table;
import org.genesys.blocks.model.EntityId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
/**
* Holds results of {@link Execution} run.
......@@ -67,6 +68,7 @@ public class Observation implements EntityId, Serializable {
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "kpiobservationdimension", joinColumns = @JoinColumn(name = "observationId"), inverseJoinColumns = @JoinColumn(name = "dimensionKeyId"))
@JsonIgnoreProperties({ "id" })
private List<DimensionKey> dimensions = new ArrayList<DimensionKey>();
@Column
......
......@@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
......@@ -51,7 +52,6 @@ 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;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -120,7 +120,7 @@ public class KPIServiceImpl implements KPIService {
@Validated
public <T extends Dimension<?>> T save(@Valid T dimension) {
LOG.debug("Persising dimension {}", dimension);
return dimensionRepository.save(dimension);
return lazyLoad(dimensionRepository.save(dimension));
}
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dimension, 'ADMINISTRATION')")
......@@ -137,7 +137,7 @@ public class KPIServiceImpl implements KPIService {
Dimension<?> dim = dimensionRepository.findOne(id);
if (!(dim instanceof JpaDimension))
dim.getValues().size();
return dim;
return lazyLoad(dim);
}
@Override
......@@ -145,7 +145,7 @@ public class KPIServiceImpl implements KPIService {
Dimension<?> dim = dimensionRepository.findByName(name);
if (!(dim instanceof JpaDimension))
dim.getValues().size();
return dim;
return lazyLoad(dim);
}
@Override
......@@ -155,18 +155,19 @@ public class KPIServiceImpl implements KPIService {
@Override
public Execution getExecution(long id) {
return executionRepository.findOne(id);
return lazyLoad(executionRepository.findOne(id));
}
@Override
public Execution getExecution(String executionName) {
return executionRepository.findByName(executionName);
return lazyLoad(executionRepository.findByName(executionName));
}
@Override
public ExecutionRun findLastExecutionRun(Execution execution) {
List<ExecutionRun> l = executionRunRepository.findLast(execution, new PageRequest(0, 1));
return l.size() == 1 ? l.get(0) : null;
ExecutionRun lastRun = l.size() == 1 ? l.get(0) : null;
return lazyLoad(lastRun);
}
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'ADMINISTRATION')")
......@@ -174,7 +175,13 @@ public class KPIServiceImpl implements KPIService {
@Transactional
@Validated
public Execution save(@Valid Execution execution) {
return executionRepository.save(execution);
Execution target = executionRepository.findByName(execution.getName());
if (target == null) {
target = execution;
} else {
copyValues(target, execution);
}
return lazyLoad(executionRepository.save(target));
}
@Override
......@@ -186,10 +193,11 @@ public class KPIServiceImpl implements KPIService {
@Override
@Transactional
public Execution delete(Execution execution) {
// Exception thrown if there are Observations (which is okay).
// // Exception thrown if there are Observations (which is okay).
// execution = executionRepository.findByName(execution.getName());
executionRunRepository.deleteByExecution(execution);
executionRepository.delete(execution);
execution.setId(null);
// execution.setId(null);
return execution;
}
......@@ -499,6 +507,46 @@ public class KPIServiceImpl implements KPIService {
@Override
public ExecutionRun getExecutionRun(long runId) {
return executionRunRepository.findOne(runId);
return lazyLoad(executionRunRepository.findOne(runId));
}
private void copyValues(Execution target, Execution source) {
if (source.getExecutionDimensions() != null) {
target.getExecutionDimensions().clear();
target.getExecutionDimensions().addAll(source.getExecutionDimensions().stream().filter(executionDimension -> !target.getExecutionDimensions().contains(executionDimension)).collect(Collectors.toList()));
}
target.setProperty(source.getProperty());
target.setParameter(source.getParameter());
target.setTitle(source.getTitle());
}
private ExecutionRun lazyLoad(ExecutionRun run) {
if (run != null) {
run.getObservations().forEach(obs -> {
obs.getDimensions().size();
});
}
return run;
}
private Execution lazyLoad(Execution execution) {
if (execution != null) {
if (execution.getExecutionDimensions() != null) {
execution.getExecutionDimensions().forEach((ed) -> {
lazyLoad(ed.getDimension());
});
}
if (execution.getRuns() != null) {
execution.getRuns().size();
}
}
return execution;
}
private <T extends Dimension<?>> T lazyLoad(T dimension) {
if (!(dimension instanceof JpaDimension) && dimension.getValues() != null) {
dimension.getValues().size();
}
return dimension;
}
}
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