Commit 4dadf72e authored by Matija Obreza's avatar Matija Obreza

Merge branch 'ui-212-update-kpi-runs-diff-display'

* ui-212-update-kpi-runs-diff-display:
  Added filtering to kpi calculateRunDiff
parents 4082ec68 00dadbdc
......@@ -18,6 +18,8 @@ package org.genesys2.server.api.v1;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import org.apache.commons.lang3.StringUtils;
......@@ -165,9 +167,9 @@ public class KPIController {
}
@GetMapping(value = "/executions/{name}/diff")
@PostMapping(value = "/executions/{name}/diff")
public SortedMap<Date, List<Observation>> executionRuns(final @PathVariable String name, @RequestParam(value="days", required = false) final Integer days, @RequestParam(value="from", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date from,
@RequestParam(value="to", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date to) {
@RequestParam(value="to", required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date to, @RequestBody(required = false) final Map<String, Set<String>> keys) {
Execution execution = kpiService.getExecution(name);
if (execution == null) {
......@@ -185,7 +187,7 @@ public class KPIController {
from = startDate.getTime();
}
return kpiService.calculateRunDiff(execution, from, to);
return kpiService.calculateRunDiff(execution, from, to, keys);
}
/**
......
......@@ -96,6 +96,6 @@ public interface KPIService {
* @param to the date of the latest run
* @return the sorted map keyed by run date, containing run differences
*/
SortedMap<Date, List<Observation>> calculateRunDiff(Execution execution, Date from, Date to);
SortedMap<Date, List<Observation>> calculateRunDiff(Execution execution, Date from, Date to, Map<String, Set<String>> keys);
}
......@@ -202,7 +202,7 @@ public class KPIServiceImpl implements KPIService {
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'ADMINISTRATION') or hasPermission(#execution, 'READ')")
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'READ')")
public ExecutionRun findLastExecutionRun(Execution execution) {
Page<ExecutionRun> l = executionRunRepository.findLast(execution, new PageRequest(0, 1));
ExecutionRun lastRun = l.getNumberOfElements() == 1 ? l.getContent().get(0) : null;
......@@ -210,6 +210,7 @@ public class KPIServiceImpl implements KPIService {
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'READ')")
public ExecutionRun findExecutionRunByDate(Execution execution, Date date) {
JPAQuery<ExecutionRun> executionQuery = jpaQueryFactory.selectFrom(executionRun);
executionQuery.where(executionRun.execution.eq(execution).and(executionRun.timestamp.before(date)));
......@@ -354,7 +355,7 @@ public class KPIServiceImpl implements KPIService {
}
// readonly mode
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'ADMINISTRATION') or hasPermission(#execution, 'READ')")
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'ADMINISTRATION') or hasPermission(#execution, 'WRITE')")
@Override
public List<Observation> execute(Execution execution) {
List<Observation> results = new ArrayList<Observation>();
......@@ -675,19 +676,72 @@ public class KPIServiceImpl implements KPIService {
return dimension;
}
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'READ')")
@Override
public SortedMap<Date, List<Observation>> calculateRunDiff(Execution execution, Date from, Date to) {
public SortedMap<Date, List<Observation>> calculateRunDiff(Execution execution, Date from, Date to, Map<String, Set<String>> keys) {
if (execution == null) {
throw new InvalidApiUsageException("Execution must be provided.");
}
if (from == null || to == null) {
throw new InvalidApiUsageException("From and to dates must be provided.");
}
}
if (from.after(to)) {
throw new InvalidApiUsageException("From date must be before to date.");
}
LOG.info("Generating run diff from {} to {}", from, to);
if (keys == null || keys.keySet().size() == 0)
return calculateRunDiff(execution, from, to);
SortedMap<Date, List<Observation>> diffs = new TreeMap<>();
Calendar date = Calendar.getInstance();
date.setTime(to);
List<Observation> currentObservations = filterObservations(execution, date.getTime(), keys);
do {
date.add(Calendar.DAY_OF_MONTH, -1);
List<Observation> earlierObservarions = filterObservations(execution, date.getTime(), keys);
List<Observation> diff = Lists.newArrayList();
for (Observation observation : currentObservations) {
Observation earlierObservation = earlierObservarions.stream().filter(o -> o.hasDimensionKeys(observation.getDimensions())).findFirst().orElse(null);
if (earlierObservation == null) {
LOG.debug("No earlier observation for {}", observation.getDimensions());
diff.add(observation);
} else {
LOG.debug("Have earlier observation for {}", observation.getDimensions());
if (observation.getValue() != earlierObservation.getValue()) {
diff.add(new Observation(observation.getDimensions(), observation.getValue() - earlierObservation.getValue()));
}
}
}
for (Observation earlierObservation: earlierObservarions) {
Observation observation = currentObservations.stream().filter(o -> o.hasDimensionKeys(earlierObservation.getDimensions())).findFirst().orElse(null);
if (observation == null) {
LOG.debug("Observation removed for {}", earlierObservation.getDimensions());
diff.add(new Observation(earlierObservation.getDimensions(), -earlierObservation.getValue()));
}
}
if (! diff.isEmpty()) {
diffs.put(currentObservations.get(0).getExecutionRun().getTimestamp(), diff);
}
currentObservations = earlierObservarions;
} while (date.getTime().after(from));
return diffs;
}
private SortedMap<Date, List<Observation>> calculateRunDiff(Execution execution, Date from, Date to) {
SortedMap<Date, List<Observation>> diffs = new TreeMap<>();
Calendar date = Calendar.getInstance();
......
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