Skip to content

Commit

Permalink
Add handling for data coming from datasources using dataframes (#49)
Browse files Browse the repository at this point in the history
* Add handling for data coming from data sources using data frames
* Add method for computing value range
* Add method `createDataValue`

Co-authored-by: Sarah León Rojas <sarah.leon.rojas@fit.fraunhofer.de>
  • Loading branch information
sarahleon and Sarah León Rojas committed Jun 8, 2020
1 parent e8cd4c6 commit 4f4765b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export class WorldmapCore {
// Todo: Don't misuse `this.series` for this as it is a completely different format.
// Better pass the payload to `setJsonValues()` like seen with `setTableValues()`.
series = dataList;
this.dataFormatter.setJsonValues(data);
// we need to pass series here, because setJsonValues accesses the series in the ctrl which is not set at this moment
this.dataFormatter.setJsonValues(series, data);
} else if (this.settings.locationData) {
this.assertDataFormat(dataFormat === DataFormat.Timeseries, dataFormat, DataFormat.Timeseries);
console.info('Interpreting data as timeseries format');
Expand Down
73 changes: 45 additions & 28 deletions src/data_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default class DataFormatter {
}
}

createDataValue(encodedGeohash, decodedGeohash, locationName, value, link) {
createDataValueWithGeohash(encodedGeohash, decodedGeohash, locationName, value, link) {
// Todo: Bring this up to speed with the current state in `setTableValues`.
const dataValue = {
key: encodedGeohash,
Expand All @@ -144,6 +144,21 @@ export default class DataFormatter {
return dataValue;
}

createDataValueFromPoint(point) {
const dataValue = {
key: point.key,
locationName: point.name,
locationLatitude: point.latitude,
locationLongitude: point.longitude,
value: point.value !== undefined ? point.value : 1,
valueRounded: 0,
};

dataValue.valueRounded = kbn.roundValue(dataValue.value, this.settings.decimals || 0);

return dataValue;
}

decodeGeohashSafe(encodedGeohash) {
// Safely decode the geohash value, either by raising an exception or by ignoring it.
if (!encodedGeohash) {
Expand Down Expand Up @@ -201,7 +216,7 @@ export default class DataFormatter {
const value = row[columnNames[this.settings.esMetric]];
const link = this.settings.esLink ? row[columnNames[this.settings.esLink]] : null;

const dataValue = this.createDataValue(encodedGeohash, decodedGeohash, locationName, value, link);
const dataValue = this.createDataValueWithGeohash(encodedGeohash, decodedGeohash, locationName, value, link);

// Add all values from the original datapoint as attributes prefixed with `__field_`.
for (const columnName in columnNames) {
Expand Down Expand Up @@ -241,7 +256,7 @@ export default class DataFormatter {
const value = datapoint[this.settings.esMetric];
const link = this.settings.esLink ? datapoint[this.settings.esLink] : null;

const dataValue = this.createDataValue(encodedGeohash, decodedGeohash, locationName, value, link);
const dataValue = this.createDataValueWithGeohash(encodedGeohash, decodedGeohash, locationName, value, link);

// Add all values from the original datapoint as attributes prefixed with `__field_`.
for (let key in datapoint) {
Expand Down Expand Up @@ -395,38 +410,40 @@ export default class DataFormatter {
}
}

setJsonValues(data) {
if (this.ctrl.series && this.ctrl.series.length > 0) {
let highestValue = 0;
let lowestValue = Number.MAX_VALUE;

this.ctrl.series.forEach(point => {
// Todo: Bring this up to speed with the current state in `setTableValues`.
const dataValue = {
key: point.key,
locationName: point.name,
locationLatitude: point.latitude,
locationLongitude: point.longitude,
value: point.value !== undefined ? point.value : 1,
valueRounded: 0,
};
if (dataValue.value > highestValue) {
highestValue = dataValue.value;
}
if (dataValue.value < lowestValue) {
lowestValue = dataValue.value;
setJsonValues(series, data) {
if (series && series.length > 0) {
series.forEach(serie => {
if (serie.datapoints && serie.datapoints.length > 0) {
serie.datapoints.forEach(point => {
// Todo: Bring this up to speed with the current state in `setTableValues`.
data.push(this.createDataValueFromPoint(point));
});
} else {
// Todo: Bring this up to speed with the current state in `setTableValues`.
data.push(this.createDataValueFromPoint(serie));
}
dataValue.valueRounded = Math.round(dataValue.value);
data.push(dataValue);
});
data.highestValue = highestValue;
data.lowestValue = lowestValue;
data.valueRange = highestValue - lowestValue;

this.computeValueRange(data);
} else {
this.addWarning('No data in JSON format received');
}
}

computeValueRange(data) {
const sortedValues = data.map(datapoint => {
return datapoint.value;
});

sortedValues.sort((a, b) => a - b);

data.highestValue = sortedValues[sortedValues.length - 1];
data.lowestValue = sortedValues[0];
data.valueRange = data.highestValue - data.lowestValue;

return data;
}

addWarning(message) {
this.ctrl.errors.add(message, { level: 'warning', domain: 'data' });
}
Expand Down

0 comments on commit 4f4765b

Please sign in to comment.