Commit 9026d614 authored by Matija Obreza's avatar Matija Obreza

List observations

parent 032dc226
......@@ -16,6 +16,8 @@
package org.genesys2.server.persistence.domain.kpi;
import java.util.List;
import org.genesys2.server.model.kpi.DimensionKey;
import org.springframework.data.jpa.repository.JpaRepository;
......@@ -23,4 +25,6 @@ public interface DimensionKeyRepository extends JpaRepository<DimensionKey, Long
DimensionKey findByNameAndValue(String name, String value);
List<DimensionKey> findByName(String name);
}
package org.genesys2.server.persistence.domain.kpi;
import java.util.List;
import java.util.Set;
import org.genesys2.server.model.kpi.DimensionKey;
import org.genesys2.server.model.kpi.KPIParameter;
import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Pageable;
public interface ObservationCustomRepository {
List<Observation> findObservations(KPIParameter parameter, Set<DimensionKey> dks, Pageable page);
}
......@@ -16,9 +16,21 @@
package org.genesys2.server.persistence.domain.kpi;
import java.util.List;
import java.util.Set;
import org.genesys2.server.model.kpi.DimensionKey;
import org.genesys2.server.model.kpi.KPIParameter;
import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface ObservationRepository extends JpaRepository<Observation, Long>, ObservationCustomRepository {
List<Observation> findByParameter(KPIParameter parameter, Pageable page);
public interface ObservationRepository extends JpaRepository<Observation, Long> {
@Query("select o from Observation o where o.parameter=?1 and ?2 member of o.dimensions order by o.timestamp desc")
List<Observation> findObservationsByDimensionKey(KPIParameter parameter, DimensionKey next, Pageable page);
}
package org.genesys2.server.persistence.domain.kpi;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
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.KPIParameter;
import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
@Repository
public class ObservationRepositoryCustomImpl implements ObservationCustomRepository {
public static final Log LOG = LogFactory.getLog(ObservationRepositoryCustomImpl.class);
@PersistenceContext
private EntityManager entityManager;
@SuppressWarnings("unchecked")
@Override
public List<Observation> findObservations(KPIParameter parameter, Set<DimensionKey> dks, Pageable page) {
LOG.info("Doing magic!");
StringBuilder where = new StringBuilder();
int i = 2;
for (DimensionKey dk : dks) {
i++;
where.append("and ?").append(i).append(" member of o.dimensions ");
}
Query q = entityManager.createQuery("select o from Observation o where o.parameter=?1 and o.dimensionCount=?2 " + where.toString() + " order by o.timestamp desc");
q.setParameter(1, parameter);
q.setParameter(2, dks.size());
i = 2;
for (DimensionKey dk : dks) {
i++;
q.setParameter(i, dk);
}
q.setFirstResult(page.getOffset());
q.setMaxResults(page.getPageSize());
return q.getResultList();
}
}
package org.genesys2.server.service;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.genesys2.server.model.kpi.Dimension;
import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.KPIParameter;
import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Pageable;
public interface KPIService {
......@@ -46,4 +48,6 @@ public interface KPIService {
List<Execution> listExecutions();
List<Observation> listObservations(KPIParameter parameter, Map<String, String> dimensionFilters, Pageable page);
}
......@@ -3,6 +3,7 @@ package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
......@@ -25,6 +26,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.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
......@@ -249,4 +251,40 @@ public class KPIServiceImpl implements KPIService {
LOG.info("OBSERVATION: " + observation);
results.add(observation);
}
@Override
public List<Observation> listObservations(KPIParameter parameter, Map<String, String> dimensionFilters, Pageable page) {
List<Observation> res = null;
if (dimensionFilters == null || dimensionFilters.isEmpty()) {
LOG.debug("Dimension filters not provided");
res = observationRepository.findByParameter(parameter, page);
} else {
Set<DimensionKey> dks = getDimensionKeys(dimensionFilters);
LOG.info("Got " + dks.size() + " dimension keys.");
res = observationRepository.findObservations(parameter, dks, page);
}
// Load lazy
for (Observation o : res) {
o.getDimensions().size();
}
return res;
}
private Set<DimensionKey> getDimensionKeys(Map<String, String> dimensionFilters) {
// TODO needs .equals()?
Set<DimensionKey> dks = new HashSet<DimensionKey>();
for (String name : dimensionFilters.keySet()) {
String value = dimensionFilters.get(name);
if (value == null) {
continue;
} else {
// Get one
DimensionKey dk = dimensionKeyRepository.findByNameAndValue(name, value);
if (dk != null)
dks.add(dk);
}
}
return dks;
}
}
......@@ -37,6 +37,7 @@ import org.genesys2.server.service.KPIService;
import org.genesys2.server.servlet.controller.rest.model.ExecutionDimensionJson;
import org.genesys2.server.servlet.controller.rest.model.ExecutionJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
......@@ -44,6 +45,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
......@@ -218,6 +220,21 @@ public class KPIController extends RestController {
return ExecutionJson.from(kpiService.delete(kpiService.getExecution(id)));
}
/**
* List observations
*
* @return
*
* @return
* @throws ValidationException
*/
@RequestMapping(value = "/observation/{parameterName}/", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
List<Observation> listObservations(@PathVariable("parameterName") String parameterName,
@RequestParam(value = "page", required = false, defaultValue = "1") int page, @RequestBody(required = false) Map<String, String> dimensionFilters) {
return kpiService.listObservations(kpiService.getParameter(parameterName), dimensionFilters, new PageRequest(page - 1, 50));
}
/**
* Update {@link BooleanDimension}
*
......
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