Commit 3e0bea73 authored by Matija Obreza's avatar Matija Obreza
Browse files

Updated KPI Execution

- getExecution doesn't lazy load, use loadExecution
- filterObservations method receives Execution
parent 8b5f7fed
......@@ -204,7 +204,7 @@ public class KPIController extends ApiBaseController {
*/
@RequestMapping(value = "/execution/{executionName:.+}", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody ExecutionJson getExecution(@PathVariable("executionName") String executionName) {
Execution execution = kpiService.getExecution(executionName);
Execution execution = kpiService.loadExecution(executionName);
return ExecutionJson.from(execution);
}
......
......@@ -133,7 +133,7 @@ public class KPIController {
@JsonView(JsonViews.Minimal.class)
@GetMapping(value = "/executions/{name}")
public ExecutionDetails executionDetails(@PathVariable String name) {
Execution execution = kpiService.getExecution(name);
Execution execution = kpiService.loadExecution(name);
return ExecutionDetails.from(execution, kpiService.findLastExecutionRun(execution), kpiService.listExecutionRuns(execution, new PageRequest(0, 10)));
}
......
......@@ -73,8 +73,9 @@ public class KPIReadController {
@RequestParam(value="date", required = true) @DateTimeFormat(pattern="yyyyMMdd") final Date date,
@RequestBody(required = false) final Map<String, Set<String>> keys) {
ArrayNode l=objectMapper.createArrayNode();
kpiService.filterObservations(executionName, date, keys).forEach(observation -> {
ArrayNode l = objectMapper.createArrayNode();
Execution execution = kpiService.getExecution(executionName);
kpiService.filterObservations(execution, date, keys).forEach(observation -> {
l.add(toMap(observation));
});
return l;
......
......@@ -70,7 +70,7 @@ public class ExecutionGroup implements SelfCleaning, Serializable {
/** The alias. */
@Size(max = 100)
@Pattern(regexp = "[_a-z][a-zA-Z0-9_]+")
@Pattern(regexp = "[_a-z][a-zA-Z0-9_\\.]+")
@Column(length = 100)
private String alias;
......
......@@ -34,7 +34,7 @@ public class KPIController {
@RequestMapping(value = "/exec/{executionName:.+}", method = RequestMethod.GET)
public String execution(ModelMap model, @PathVariable("executionName") String executionName,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
Execution execution = kpiService.getExecution(executionName);
Execution execution = kpiService.loadExecution(executionName);
model.addAttribute("execution", execution);
model.addAttribute("executionRuns", kpiService.listExecutionRuns(execution, new PageRequest(page - 1, pageSize)));
return "/admin/kpi/execution";
......@@ -44,7 +44,7 @@ public class KPIController {
@RequestMapping(value = "/exec/{executionName:.+}", method = RequestMethod.GET, params={"dk"})
public String execution(ModelMap model, @PathVariable("executionName") String executionName,
@RequestParam(value = "dk", required = true) long dimensionKeyId) {
Execution execution = kpiService.getExecution(executionName);
Execution execution = kpiService.loadExecution(executionName);
model.addAttribute("execution", execution);
model.addAttribute("observations", kpiService.listObservations(execution, dimensionKeyId, new PageRequest(0, pageSize)));
return "/admin/kpi/execution";
......@@ -53,7 +53,7 @@ public class KPIController {
@RequestMapping(value = "/exec/{executionName}/run/{runId}", method = RequestMethod.GET)
public String executionRun(ModelMap model, @PathVariable("executionName") String executionName, @PathVariable("runId") long runId,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
Execution execution = kpiService.getExecution(executionName);
Execution execution = kpiService.loadExecution(executionName);
ExecutionRun executionRun = kpiService.getExecutionRun(runId);
model.addAttribute("execution", execution);
model.addAttribute("executionRun", executionRun);
......
......@@ -53,8 +53,9 @@ public interface KPIService {
Execution save(@Valid Execution execution);
Execution getExecution(long id);
Execution loadExecution(String executionName);
Execution getExecution(long id);
Execution getExecution(String executionName);
Execution delete(Execution execution);
......@@ -80,7 +81,7 @@ public interface KPIService {
Page<Observation> listObservations(Execution execution, long dimensionKeyId, Pageable pageable);
List<Observation> filterObservations(String executionName, Date date, Map<String, Set<String>> keys);
List<Observation> filterObservations(Execution execution, Date date, Map<String, Set<String>> keys);
}
......@@ -28,7 +28,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -181,13 +180,19 @@ public class KPIServiceImpl implements KPIService {
@PostAuthorize("hasRole('ADMINISTRATOR') || returnObject==null || hasPermission(returnObject, 'READ')")
@Override
public Execution getExecution(long id) {
return lazyLoad(executionRepository.findOne(id));
return executionRepository.findOne(id);
}
@PostAuthorize("hasRole('ADMINISTRATOR') || returnObject==null || hasPermission(returnObject, 'READ')")
@Override
public Execution loadExecution(String executionName) {
return lazyLoad(getExecution(executionName));
}
@PostAuthorize("hasRole('ADMINISTRATOR') || returnObject==null || hasPermission(returnObject, 'READ')")
@Override
public Execution getExecution(String executionName) {
return lazyLoad(executionRepository.findByName(executionName));
return executionRepository.findByName(executionName);
}
@Override
......@@ -551,17 +556,14 @@ public class KPIServiceImpl implements KPIService {
}
@Override
public List<Observation> filterObservations(final String executionName, final Date date, final Map<String, Set<String>> keys) {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'READ')")
public List<Observation> filterObservations(final Execution execution, final Date date, final Map<String, Set<String>> keys) {
JPAQuery<ExecutionRun> executionQuery = jpaQueryFactory.selectFrom(executionRun);
executionQuery.where(executionRun.execution.name.eq(executionName).and(executionRun.timestamp.before(date)));
executionQuery.where(executionRun.execution.eq(execution).and(executionRun.timestamp.before(date)));
executionQuery.orderBy(executionRun.timestamp.desc());
ExecutionRun executionRun = executionQuery.fetchFirst();
boolean withoutRights = false;
if (!securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
Set<Long> executionIds = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Execution.class, BasePermission.READ));
withoutRights = executionIds.stream().noneMatch(id -> Objects.equals(executionRun.getExecution().getId(), id));
}
if (executionRun == null || withoutRights) {
if (executionRun == null) {
return Collections.emptyList();
}
......@@ -578,7 +580,6 @@ public class KPIServiceImpl implements KPIService {
}
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);
......
......@@ -16,13 +16,10 @@
package org.genesys.test.server.services;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.util.Calendar;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -297,7 +294,7 @@ public class KPIServiceTest extends AbstractServicesTest {
public void testExecutionCountByBoolean1() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
Execution exec1 = new Execution();
exec1.setName("institute.count");
exec1.setName(EXECUTION_NAME);
exec1.setTitle("Institute count");
exec1.setParameter(param1);
BooleanDimension dim1 = kpiService.save(createBoolDimension("yesno", "Boolean yes and no dimension"));
......@@ -305,7 +302,7 @@ public class KPIServiceTest extends AbstractServicesTest {
Execution loaded = kpiService.save(exec1);
assertThat(loaded, notNullValue());
assertThat(loaded.getId(), notNullValue());
assertThat(loaded.getName(), is("institute.count"));
assertThat(loaded.getName(), is(EXECUTION_NAME));
assertThat(loaded.getTitle(), is("Institute count"));
// defaults
assertThat(loaded.getProperty(), is("id"));
......@@ -332,7 +329,7 @@ public class KPIServiceTest extends AbstractServicesTest {
public void testExecutionAverageByBoolean1() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
Execution exec1 = new Execution();
exec1.setName("institute.count");
exec1.setName(EXECUTION_NAME);
exec1.setTitle("Institute count");
exec1.setParameter(param1);
exec1.setType(ExecutionType.AVERAGE);
......@@ -365,7 +362,7 @@ public class KPIServiceTest extends AbstractServicesTest {
public void testFilterObservations() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
Execution exec1 = new Execution();
exec1.setName("institute.count");
exec1.setName(EXECUTION_NAME);
exec1.setTitle("Institute count");
exec1.setParameter(param1);
exec1.setType(ExecutionType.AVERAGE);
......@@ -406,7 +403,7 @@ public class KPIServiceTest extends AbstractServicesTest {
Map<String, Set<String>> map = new HashMap<>();
map.put("allowMaterialRequests", Sets.newHashSet("true"));
map.put("code", Sets.newHashSet("INS1"));
List<Observation> observationList = kpiService.filterObservations(EXECUTION_NAME, searchDate.getTime(), map);
List<Observation> observationList = kpiService.filterObservations(loaded, searchDate.getTime(), map);
assertThat(observationList.size(), is(1));
assertTrue(run1.getObservations().stream().anyMatch(obs -> obs.getId().equals(observationList.get(0).getId())));
......@@ -418,7 +415,7 @@ public class KPIServiceTest extends AbstractServicesTest {
public void testExecutionAverageByBooleanAndJpa() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
Execution exec1 = new Execution();
exec1.setName("institute.count");
exec1.setName(EXECUTION_NAME);
exec1.setTitle("Institute count");
exec1.setParameter(param1);
exec1.setType(ExecutionType.AVERAGE);
......@@ -460,7 +457,7 @@ public class KPIServiceTest extends AbstractServicesTest {
public void testExecutionLinkedCollection() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
Execution exec1 = new Execution();
exec1.setName("institute.count");
exec1.setName(EXECUTION_NAME);
exec1.setTitle("Institute count");
exec1.setParameter(param1);
exec1.setLink("networks"); // @OneToMany in FaoInstitute
......@@ -468,7 +465,7 @@ public class KPIServiceTest extends AbstractServicesTest {
Execution loaded = kpiService.save(exec1);
assertThat(loaded, notNullValue());
assertThat(loaded.getId(), notNullValue());
assertThat(loaded.getName(), is("institute.count"));
assertThat(loaded.getName(), is(EXECUTION_NAME));
assertThat(loaded.getTitle(), is("Institute count"));
// defaults
assertThat(loaded.getProperty(), is("slug"));
......@@ -491,7 +488,7 @@ public class KPIServiceTest extends AbstractServicesTest {
public void testExecutionLinkedCollectionCountByBoolean1() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
Execution exec1 = new Execution();
exec1.setName("institute.count");
exec1.setName(EXECUTION_NAME);
exec1.setTitle("Institute count");
exec1.setParameter(param1);
exec1.setLink("networks"); // @OneToMany in FaoInstitute
......@@ -501,7 +498,7 @@ public class KPIServiceTest extends AbstractServicesTest {
Execution loaded = kpiService.save(exec1);
assertThat(loaded, notNullValue());
assertThat(loaded.getId(), notNullValue());
assertThat(loaded.getName(), is("institute.count"));
assertThat(loaded.getName(), is(EXECUTION_NAME));
assertThat(loaded.getTitle(), is("Institute count"));
// defaults
assertThat(loaded.getProperty(), is("slug"));
......@@ -530,7 +527,7 @@ public class KPIServiceTest extends AbstractServicesTest {
public void testExecutionGroupingComplex() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
Execution exec1 = new Execution();
exec1.setName("institute.count");
exec1.setName(EXECUTION_NAME);
exec1.setTitle("Institute count");
exec1.setParameter(param1);
exec1.setProperty("id");
......@@ -543,7 +540,7 @@ public class KPIServiceTest extends AbstractServicesTest {
Execution loaded = kpiService.save(exec1);
assertThat(loaded, notNullValue());
assertThat(loaded.getId(), notNullValue());
assertThat(loaded.getName(), is("institute.count"));
assertThat(loaded.getName(), is(EXECUTION_NAME));
assertThat(loaded.getTitle(), is("Institute count"));
// defaults
assertThat(loaded.getProperty(), is("slug"));
......@@ -583,7 +580,7 @@ public class KPIServiceTest extends AbstractServicesTest {
public void testExecutionGroup1() {
KPIParameter param1 = kpiService.save(createParameter("institute", FaoInstitute.class, "Institute"));
Execution exec1 = new Execution();
exec1.setName("institute.count");
exec1.setName(EXECUTION_NAME);
exec1.setTitle("Institute count");
exec1.setParameter(param1);
exec1.setProperty("id");
......@@ -592,7 +589,7 @@ public class KPIServiceTest extends AbstractServicesTest {
Execution loaded = kpiService.save(exec1);
assertThat(loaded, notNullValue());
assertThat(loaded.getId(), notNullValue());
assertThat(loaded.getName(), is("institute.count"));
assertThat(loaded.getName(), is(EXECUTION_NAME));
assertThat(loaded.getTitle(), is("Institute count"));
// defaults
assertThat(loaded.getProperty(), is("id"));
......
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