Skip to content

Commit

Permalink
Merge pull request #144 from kylios/allow-lat-and-lon-in-query
Browse files Browse the repository at this point in the history
Use latitude and longitude if given in table data
  • Loading branch information
daniellee committed Jun 21, 2018
2 parents 886ed1b + 365094b commit 81fea16
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/data_formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,25 @@ export default class DataFormatter {
let lowestValue = Number.MAX_VALUE;

tableData[0].forEach((datapoint) => {
if (!datapoint.geohash) {
return;
}
let latitude = datapoint.latitude;
let longitude = datapoint.longitude;
let key = `${latitude}_${longitude}`;

if (datapoint.geohash) {
const encodedGeohash = datapoint.geohash;
const decodedGeohash = decodeGeoHash(encodedGeohash);

const encodedGeohash = datapoint.geohash;
const decodedGeohash = decodeGeoHash(encodedGeohash);
latitude = decodedGeohash.latitude;
longitude = decodedGeohash.longitude;

key = encodedGeohash;
}

const dataValue = {
key: encodedGeohash,
key: key,
locationName: datapoint[this.ctrl.panel.tableLabel] || 'n/a',
locationLatitude: decodedGeohash.latitude,
locationLongitude: decodedGeohash.longitude,
locationLatitude: latitude,
locationLongitude: longitude,
value: datapoint.metric,
valueFormatted: datapoint.metric,
valueRounded: 0
Expand Down Expand Up @@ -198,4 +205,3 @@ export default class DataFormatter {
}
}
}

57 changes: 57 additions & 0 deletions test/data_formatter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,63 @@ describe('DataFormatter', () => {
let dataFormatter;
let formattedData = [];

describe('when latitude and longitude are given in table data', () => {
beforeEach(() => {
const ctrl = {
panel: {}
};
dataFormatter = new DataFormatter(ctrl, {roundValue: () => {}});
});

it('should use latitude and longitude if no geohash is given', () => {
const tableData = [
[
{
latitude: 1,
longitude: 2
},
{
latitude: 3,
longitude: 4
}
]
];
const data = [];

dataFormatter.setTableValues(tableData, data);

expect(data[0].locationLatitude).to.equal(1);
expect(data[0].locationLongitude).to.equal(2);
expect(data[1].locationLatitude).to.equal(3);
expect(data[1].locationLongitude).to.equal(4);
});

it('should prefer geohash if given', () => {
const tableData = [
[
{
latitude: 1,
longitude: 2,
geohash: 'stq4s3x' // 29.9796, 31.1345
},
{
latitude: 3,
longitude: 4,
geohash: 'p05010r' // -89.997, 139.273
}
]
];
const data = [];

dataFormatter.setTableValues(tableData, data);

expect(data[0].locationLatitude).to.be.within(29.9796, 29.9797);
expect(data[0].locationLongitude).to.be.within(31.1345, 31.1346);
expect(data[1].locationLatitude).to.be.within(-89.998, -89.997);
expect(data[1].locationLongitude).to.be.within(139.272, 139.273);
});
});

describe('when the time series data matches the location', () => {
beforeEach(() => {
const ctrl = {
Expand Down

0 comments on commit 81fea16

Please sign in to comment.