Commit 4eab729c authored by Matija Obreza's avatar Matija Obreza
Browse files

Observations by ExecutionRun

parent 88594865
......@@ -31,6 +31,8 @@ import javax.persistence.Table;
import org.genesys2.server.model.BusinessModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Holds results of {@link Execution} run.
*/
......@@ -41,6 +43,7 @@ public class Observation extends BusinessModel {
@Column(name = "`value`")
private double value;
@JsonIgnore
@ManyToOne(cascade = {}, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "executionRunId")
private ExecutionRun executionRun;
......
......@@ -22,9 +22,13 @@ import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.ExecutionRun;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface ExecutionRunRepository extends JpaRepository<ExecutionRun, Long> {
List<ExecutionRun> findByExecution(Execution execution, Pageable pageable);
@Query("select er from ExecutionRun er where er.execution=?1 order by er.timestamp desc")
List<ExecutionRun> findLast(Execution execution, Pageable pageable);
}
......@@ -21,12 +21,13 @@ import java.util.Set;
import org.genesys2.server.model.kpi.DimensionKey;
import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.ExecutionRun;
import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Pageable;
public interface ObservationCustomRepository {
List<Observation> findObservations(Execution execution, Set<DimensionKey> dks, Pageable page);
List<Observation> findObservations(ExecutionRun executionRun, Set<DimensionKey> dks, Pageable page);
Observation findLastObservation(Execution execution, Set<DimensionKey> dks);
......
......@@ -20,6 +20,7 @@ import java.util.List;
import org.genesys2.server.model.kpi.DimensionKey;
import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.ExecutionRun;
import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
......@@ -28,8 +29,8 @@ import org.springframework.data.jpa.repository.Query;
public interface ObservationRepository extends JpaRepository<Observation, Long>, ObservationCustomRepository {
@Query("select o from Observation o where o.executionRun.execution=?1")
List<Observation> findByExecution(Execution execution, Pageable page);
@Query("select o from Observation o where o.executionRun=?1")
List<Observation> findByExecutionRun(ExecutionRun executionRun, Pageable page);
@Query("select o from Observation o where o.executionRun.execution=?1 and ?2 member of o.dimensions order by o.executionRun.timestamp desc")
List<Observation> listObservationsByDimensionKey(Execution execution, DimensionKey next, Pageable page);
......
......@@ -27,6 +27,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.kpi.DimensionKey;
import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.ExecutionRun;
import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
......@@ -40,16 +41,16 @@ public class ObservationRepositoryCustomImpl implements ObservationCustomReposit
@SuppressWarnings("unchecked")
@Override
public List<Observation> findObservations(Execution execution, Set<DimensionKey> dks, Pageable page) {
public List<Observation> findObservations(ExecutionRun executionRun, Set<DimensionKey> dks, Pageable page) {
StringBuilder where = new StringBuilder();
final int OFFSET = 3;
for (int i = 0; i < dks.size(); i++) {
where.append("and ?").append(i + OFFSET).append(" member of o.dimensions ");
}
Query q = entityManager.createQuery("select o from Observation o where o.execution=?1 and o.dimensionCount=?2 " + where.toString()
Query q = entityManager.createQuery("select o from Observation o where o.executionRun=?1 and o.dimensionCount=?2 " + where.toString()
+ " order by o.timestamp desc");
q.setParameter(1, execution);
q.setParameter(1, executionRun);
q.setParameter(2, dks.size());
int i = OFFSET;
......@@ -73,7 +74,8 @@ public class ObservationRepositoryCustomImpl implements ObservationCustomReposit
}
Query q = entityManager.createQuery("select o from Observation o where o.execution=?1 and o.dimensionCount=?2 " + where.toString()
+ " order by o.timestamp desc limit 1", Observation.class);
+ " order by o.timestamp desc", Observation.class);
q.setMaxResults(1);
q.setParameter(1, execution);
q.setParameter(2, dks.size());
......
......@@ -22,6 +22,7 @@ import java.util.Set;
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.model.kpi.Observation;
import org.springframework.data.domain.Pageable;
......@@ -63,9 +64,11 @@ public interface KPIService {
List<Execution> listExecutions();
List<Observation> listObservations(Execution execution, Map<String, String> dimensionFilters, Pageable page);
void deleteObservations(Execution execution);
List<Observation> listObservations(ExecutionRun executionRun, Map<String, String> dimensionFilters, Pageable page);
ExecutionRun getLastExecutionRun(Execution execution);
}
......@@ -45,6 +45,7 @@ import org.genesys2.server.persistence.domain.kpi.KPIParameterRepository;
import org.genesys2.server.persistence.domain.kpi.ObservationRepository;
import org.genesys2.server.service.KPIService;
import org.springframework.beans.factory.annotation.Autowired;
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;
......@@ -149,6 +150,12 @@ public class KPIServiceImpl implements KPIService {
return executionRepository.findByName(executionName);
}
@Override
public ExecutionRun getLastExecutionRun(Execution execution) {
List<ExecutionRun> l = executionRunRepository.findLast(execution, new PageRequest(0, 1));
return l.size() == 1 ? l.get(0) : null;
}
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'ADMINISTRATION')")
@Override
@Transactional
......@@ -227,7 +234,7 @@ public class KPIServiceImpl implements KPIService {
@Override
@Transactional
public List<Observation> save(Execution execution, List<Observation> observations) {
ExecutionRun executionRun=new ExecutionRun();
ExecutionRun executionRun = new ExecutionRun();
executionRun.setExecution(execution);
executionRun.setTimestamp(new Date());
executionRunRepository.save(executionRun);
......@@ -303,15 +310,15 @@ public class KPIServiceImpl implements KPIService {
}
@Override
public List<Observation> listObservations(Execution execution, Map<String, String> dimensionFilters, Pageable page) {
public List<Observation> listObservations(ExecutionRun executionRun, Map<String, String> dimensionFilters, Pageable page) {
List<Observation> res = null;
if (dimensionFilters == null || dimensionFilters.isEmpty()) {
LOG.debug("Dimension filters not provided");
res = observationRepository.findByExecution(execution, page);
res = observationRepository.findByExecutionRun(executionRun, page);
} else {
Set<DimensionKey> dks = getDimensionKeys(dimensionFilters);
LOG.debug("Got " + dks.size() + " dimension keys.");
res = observationRepository.findObservations(execution, dks, page);
res = observationRepository.findObservations(executionRun, dks, page);
}
// Load lazy
......
......@@ -232,7 +232,8 @@ public class KPIController extends RestController {
public @ResponseBody
List<Observation> listObservations(@PathVariable("executionName") String executionName,
@RequestParam(value = "page", required = false, defaultValue = "1") int page, @RequestBody(required = false) Map<String, String> dimensionFilters) {
return kpiService.listObservations(kpiService.getExecution(executionName), dimensionFilters, new PageRequest(page - 1, 50));
return kpiService.listObservations(kpiService.getLastExecutionRun(kpiService.getExecution(executionName)), dimensionFilters, new PageRequest(page - 1,
50));
}
/**
......
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