Commit fc0029ee authored by Matija Obreza's avatar Matija Obreza
Browse files

Return nice observations JSON, list names of available names of executions at /kpi/observations

parent 2bd61ccd
......@@ -16,17 +16,26 @@
package org.genesys2.server.api.v1;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import io.swagger.annotations.Api;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.Observation;
import org.genesys2.server.service.KPIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
......@@ -34,6 +43,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.Api;
/**
* @author Maxym Borodenko
*/
......@@ -45,12 +60,37 @@ public class KPIReadController {
@Autowired
private KPIService kpiService;
@Autowired
private ObjectMapper objectMapper;
@GetMapping(value="/observations")
public Page<String> listExecutions(final Pagination page) {
Pageable pagination = page.toPageRequest(100, Sort.Direction.ASC, "name");
Page<Execution> executions = kpiService.listExecutions(pagination);
return new PageImpl<String>(executions.getContent().stream().filter(exec -> exec != null).map(exec -> exec.getName()).collect(Collectors.toList()), pagination, executions.getTotalElements());
}
@PostMapping(value="/observations/{executionName}")
public List<Observation> observations(@PathVariable final String executionName,
@RequestParam(value="date", required = true) @DateTimeFormat(pattern="MM-dd-yyyy") final Date date,
public ArrayNode observations(@PathVariable final String executionName,
@RequestParam(value="date", required = true) @DateTimeFormat(pattern="yyyyMMdd") final Date date,
@RequestBody(required = false) final Map<String, Set<String>> keys) {
return kpiService.filterObservations(executionName, date, keys);
ArrayNode l=objectMapper.createArrayNode();
kpiService.filterObservations(executionName, date, keys).forEach(observation -> {
l.add(toMap(observation));
});;
return l;
}
private ObjectNode toMap(Observation observation) {
ObjectNode m=objectMapper.createObjectNode();
m.put("value", observation.getValue());
m.put("stdDev", observation.getStdDev());
ObjectNode dims = m.putObject("dimensions");
observation.getDimensions().forEach(dim -> {
dims.put(dim.getName(), dim.getValue());
});
return m;
}
}
......@@ -16,8 +16,8 @@
package org.genesys2.server.service.impl;
import static org.genesys2.server.model.kpi.QExecutionRun.executionRun;
import static org.genesys2.server.model.kpi.QDimensionKey.dimensionKey;
import static org.genesys2.server.model.kpi.QExecutionRun.executionRun;
import static org.genesys2.server.model.kpi.QObservation.observation;
import java.util.ArrayList;
......@@ -36,8 +36,6 @@ import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.validation.Valid;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -67,6 +65,9 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
@Service
@Transactional(readOnly = true)
@Validated
......@@ -535,22 +536,16 @@ public class KPIServiceImpl implements KPIService {
return Collections.emptyList();
}
List<Observation> observationList;
JPAQuery<Observation> observationQuery = jpaQueryFactory.selectFrom(observation).distinct();
if (!MapUtils.isEmpty(keys)) {
JPAQuery<Observation> observationQuery = jpaQueryFactory.selectFrom(observation);
for (String name: keys.keySet()) {
observationQuery = observationQuery.innerJoin(observation.dimensions, dimensionKey).on(dimensionKey.name.eq(name).and(dimensionKey.value.in(keys.get(name))));
for (String name : keys.keySet()) {
observationQuery = observationQuery.join(observation.dimensions, dimensionKey).on(dimensionKey.name.eq(name).and(dimensionKey.value.in(keys.get(name))));
}
observationQuery.where(observation.executionRun.id.eq(executionRun.getId()));
observationList = observationQuery.fetch();
return observationList;
}
observationList = executionRun.getObservations();
if (observationList != null) {
observationList.size();
}
return observationList;
observationQuery.where(observation.executionRun.id.eq(executionRun.getId()));
List<Observation> observations = observationQuery.fetch();
observations.forEach(observation -> observation.getDimensions().size());
return observations;
}
private Set<DimensionKey> getDimensionKeys(Map<String, String> dimensionFilters) {
......
package org.genesys.test.server.api.v1;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
......@@ -16,10 +13,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.RandomUtils;
import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.api.v1.KPIController;
......@@ -39,9 +32,13 @@ import org.genesys2.server.service.KPIService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Sets;
/**
* @author Maxym Borodenko
*/
......@@ -107,13 +104,25 @@ public class KPIReadControllerTest extends AbstractApiTest {
instituteRepository.deleteAll();
super.cleanup();
}
@Test
public void testList() throws Exception {
ExecutionRun executionRun = buildAndSave("institute.count");
assertThat(executionRun.getExecution().getName(), is("institute.count"));
mockMvc.perform(get(KPIController.CONTROLLER_URL.concat("/observations")))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.content", hasSize(1)));
}
@Test
public void testFilterObservations() throws Exception {
ExecutionRun executionRun = buildAndSave("institute.count");
assertThat(executionRun.getObservations().size(), is(10));
LocalDateTime ldt = LocalDateTime.now().plusMonths(1);
String searchDate = DateTimeFormatter.ofPattern("MM-dd-yyyy").format(ldt);
String searchDate = DateTimeFormatter.ofPattern("yyyyMMdd").format(ldt);
Map<String, Set<String>> filter = new HashMap<>();
filter.put("allowMaterialRequests", Sets.newHashSet("true"));
......@@ -123,7 +132,7 @@ public class KPIReadControllerTest extends AbstractApiTest {
.param("date", searchDate)
.content(verboseMapper.writeValueAsString(filter))
.contentType(MediaType.APPLICATION_JSON_UTF8))
// .andDo(MockMvcResultHandlers.print())
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(1)));
......@@ -133,12 +142,12 @@ public class KPIReadControllerTest extends AbstractApiTest {
public void testEmptyListIfNotFound() throws Exception {
ExecutionRun executionRun = buildAndSave("institute.count");
LocalDateTime ldt = LocalDateTime.now().plusMonths(-10);
String searchDate = DateTimeFormatter.ofPattern("MM-dd-yyyy").format(ldt);
String searchDate = DateTimeFormatter.ofPattern("yyyyMMdd").format(ldt);
mockMvc.perform(post(KPIController.CONTROLLER_URL.concat("/observations/{executionName}"), executionRun.getExecution().getName())
.param("date", searchDate)
.contentType(MediaType.APPLICATION_JSON_UTF8))
// .andDo(MockMvcResultHandlers.print())
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(0)));
......
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