Commit de04b3d1 authored by OlegPahhomov's avatar OlegPahhomov

Merge branch '0.22.x' into 'master'

SA0140-575 | widget update problems

See merge request statistics-estonia/juhtimislauad/dashboard-backend!4
parents 8f20f8b2 93ff904f
......@@ -5,30 +5,28 @@ import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.MapUtils;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static ee.stat.dashboard.util.StatListUtil.firstOrNull;
@Getter
@Setter
public class DataSet {
private String action;
private HashMap<String, List<Object>> observations;
private HashMap<String, List<String>> observations;
@JsonIgnore
public Number getObservation(String key) {
public BigDecimal getObservation(String key) {
if (MapUtils.isEmpty(observations)){
return null;
}
List<Object> objects = observations.get(key);
if (isNotEmpty(objects)){
return (Number) objects.get(0);
}
return null;
String s = firstOrNull(observations.get(key));
return s != null ? new BigDecimal(s) : null;
}
@Override
......
......@@ -32,6 +32,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
......@@ -139,8 +140,8 @@ public class StatDbUpdater {
}
boolean responsesAreSame(ProcessLog processLog, Widget widget, StatResponse response, StatResponse newStatResponse, String cube) {
TreeMap<String, List<Object>> oldTreemap = new TreeMap<>(response.getDataSet().getObservations());
TreeMap<String, List<Object>> newTreemap = new TreeMap<>(newStatResponse.getDataSet().getObservations());
TreeMap<String, List<String>> oldTreemap = new TreeMap<>(response.getDataSet().getObservations());
TreeMap<String, List<String>> newTreemap = new TreeMap<>(newStatResponse.getDataSet().getObservations());
boolean dataIsSame = Objects.equals(newTreemap, oldTreemap);
if (!dataIsSame) {
log.log(processLog, widget, INFO, observationsHaveChanged(oldTreemap, newTreemap), cube);
......@@ -193,7 +194,7 @@ public class StatDbUpdater {
);
}
private String observationsHaveChanged(TreeMap<String, List<Object>> response, TreeMap<String, List<Object>> newStatResponse) {
private String observationsHaveChanged(TreeMap<String, List<String>> response, TreeMap<String, List<String>> newStatResponse) {
return format("Stat observations have changed! " +
"\n Before: %s " +
"\n After: %s",
......
......@@ -13,11 +13,11 @@ import ee.stat.dashboard.service.element.ElementServiceCache;
import ee.stat.dashboard.service.widget.coordinates.converters.MonthConverter;
import ee.stat.dashboard.service.widget.coordinates.converters.QuarterConverter;
import ee.stat.dashboard.service.widget.coordinates.converters.SdmxDateConverter;
import ee.stat.dashboard.service.widget.coordinates.converters.ValueConverter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
......@@ -28,9 +28,11 @@ import static ee.stat.dashboard.service.widget.coordinates.CoordinateUtil.setDim
import static ee.stat.dashboard.service.widget.widget.FilterUtil.getRegion;
import static ee.stat.dashboard.service.widget.widget.FilterUtil.getTimePart;
import static ee.stat.dashboard.service.widget.widget.FilterUtil.timeFilterOrThrow;
import static ee.stat.dashboard.util.StatListUtil.firstOrNull;
import static ee.stat.dashboard.util.StatStringUtil.cleanUp;
import static java.lang.String.format;
import static java.util.Optional.*;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static java.util.stream.Collectors.toList;
@Slf4j
......@@ -48,7 +50,7 @@ public class SdmxToCoordinates {
List<Dimension> observations = apiResponse.getStructure().getDimensions().getObservations();
List<DataPoint> dataPoints = new ArrayList<>();
for (Map.Entry<String, List<Object>> keyValue : apiResponse.getDataSet().getObservations().entrySet()) {
for (Map.Entry<String, List<String>> keyValue : apiResponse.getDataSet().getObservations().entrySet()) {
Optional<DataPoint> dataPointOp = mapToPoint(keyValue, observations, timeFilter, timePart, lang);
if (dataPointOp.isPresent()) {
DataPoint dataPoint = dataPointOp.get();
......@@ -80,7 +82,7 @@ public class SdmxToCoordinates {
return !county.equalsIgnoreCase(dim) || ehakNames.contains(val.toLowerCase());
}
private Optional<DataPoint> mapToPoint(Map.Entry<String, List<Object>> keyValue, List<Dimension> observations, Filter timeFilter, Optional<Filter> timePartFilterOp, Language lang) throws RawDataException {
private Optional<DataPoint> mapToPoint(Map.Entry<String, List<String>> keyValue, List<Dimension> observations, Filter timeFilter, Optional<Filter> timePartFilterOp, Language lang) throws RawDataException {
String code = keyValue.getKey();
List<Integer> indexes = Arrays.stream(code.split(":")).map(Integer::valueOf).collect(toList());
DataPoint dataPoint = new DataPoint();
......@@ -111,7 +113,8 @@ public class SdmxToCoordinates {
}
}
}
dataPoint.setValue(ValueConverter.getValue(keyValue.getValue().get(0)));
String value = firstOrNull(keyValue.getValue());
dataPoint.setValue(value != null ? new BigDecimal(value) : null);
dataPoint.setSource(Source.STAT_DB);
return of(dataPoint);
}
......
......@@ -206,10 +206,7 @@ public class AssertUtil {
if (s == null) {
return null;
}
if (s.contains(".")) {
return new Double(s);
}
return Integer.valueOf(s);
return new BigDecimal(s);
}
public static <T> void list_size(List<T> list, int i) {
......
......@@ -10,10 +10,10 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import static java.util.Arrays.*;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
......@@ -28,31 +28,31 @@ public class StatDbUpdaterTest {
assertTrue(statDbUpdater.responsesAreSame(null, null, response(existingData()), response(newData()), null));
}
private HashMap<String, List<Object>> newData() {
HashMap<String, List<Object>> newData = new HashMap<>();
newData.put("0:0:2:", Arrays.asList(3253.6));
newData.put("0:0:1:", Arrays.asList(2704.4));
newData.put("0:0:0:", Arrays.asList(2572.3));
private HashMap<String, List<String>> newData() {
HashMap<String, List<String>> newData = new HashMap<>();
newData.put("0:0:2:", asList("3253.6"));
newData.put("0:0:1:", asList("2704.4"));
newData.put("0:0:0:", asList("2572.3"));
return newData;
}
private HashMap<String, List<Object>> existingData() {
HashMap<String, List<Object>> existing = new HashMap<>();
existing.put("0:0:0:", Arrays.asList(2572.3));
existing.put("0:0:1:", Arrays.asList(2704.4));
existing.put("0:0:2:", Arrays.asList(3253.6));
private HashMap<String, List<String>> existingData() {
HashMap<String, List<String>> existing = new HashMap<>();
existing.put("0:0:0:", asList("2572.3"));
existing.put("0:0:1:", asList("2704.4"));
existing.put("0:0:2:", asList("3253.6"));
return existing;
}
private StatResponse response(HashMap<String, List<Object>> data) {
private StatResponse response(HashMap<String, List<String>> data) {
DataSet newDataSet = dataset(data);
StatResponse newStatResponse = new StatResponse();
newStatResponse.setDataSets(Arrays.asList(newDataSet));
newStatResponse.setDataSets(asList(newDataSet));
newStatResponse.setStructure(structure());
return newStatResponse;
}
private DataSet dataset(HashMap<String, List<Object>> data) {
private DataSet dataset(HashMap<String, List<String>> data) {
DataSet newDataSet = new DataSet();
newDataSet.setObservations(data);
return newDataSet;
......@@ -63,4 +63,4 @@ public class StatDbUpdaterTest {
structure.setDimensions(new Dimensions());
return structure;
}
}
\ No newline at end of file
}
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