Skip to content

Commit 81fea16

Browse files
authored
Merge pull request grafana#144 from kylios/allow-lat-and-lon-in-query
Use latitude and longitude if given in table data
2 parents 886ed1b + 365094b commit 81fea16

File tree

2 files changed

+72
-9
lines changed

2 files changed

+72
-9
lines changed

src/data_formatter.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,25 @@ export default class DataFormatter {
143143
let lowestValue = Number.MAX_VALUE;
144144

145145
tableData[0].forEach((datapoint) => {
146-
if (!datapoint.geohash) {
147-
return;
148-
}
146+
let latitude = datapoint.latitude;
147+
let longitude = datapoint.longitude;
148+
let key = `${latitude}_${longitude}`;
149+
150+
if (datapoint.geohash) {
151+
const encodedGeohash = datapoint.geohash;
152+
const decodedGeohash = decodeGeoHash(encodedGeohash);
149153

150-
const encodedGeohash = datapoint.geohash;
151-
const decodedGeohash = decodeGeoHash(encodedGeohash);
154+
latitude = decodedGeohash.latitude;
155+
longitude = decodedGeohash.longitude;
156+
157+
key = encodedGeohash;
158+
}
152159

153160
const dataValue = {
154-
key: encodedGeohash,
161+
key: key,
155162
locationName: datapoint[this.ctrl.panel.tableLabel] || 'n/a',
156-
locationLatitude: decodedGeohash.latitude,
157-
locationLongitude: decodedGeohash.longitude,
163+
locationLatitude: latitude,
164+
locationLongitude: longitude,
158165
value: datapoint.metric,
159166
valueFormatted: datapoint.metric,
160167
valueRounded: 0
@@ -198,4 +205,3 @@ export default class DataFormatter {
198205
}
199206
}
200207
}
201-

test/data_formatter_test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,63 @@ describe('DataFormatter', () => {
55
let dataFormatter;
66
let formattedData = [];
77

8+
describe('when latitude and longitude are given in table data', () => {
9+
beforeEach(() => {
10+
const ctrl = {
11+
panel: {}
12+
};
13+
dataFormatter = new DataFormatter(ctrl, {roundValue: () => {}});
14+
});
15+
16+
it('should use latitude and longitude if no geohash is given', () => {
17+
const tableData = [
18+
[
19+
{
20+
latitude: 1,
21+
longitude: 2
22+
},
23+
{
24+
latitude: 3,
25+
longitude: 4
26+
}
27+
]
28+
];
29+
const data = [];
30+
31+
dataFormatter.setTableValues(tableData, data);
32+
33+
expect(data[0].locationLatitude).to.equal(1);
34+
expect(data[0].locationLongitude).to.equal(2);
35+
expect(data[1].locationLatitude).to.equal(3);
36+
expect(data[1].locationLongitude).to.equal(4);
37+
});
38+
39+
it('should prefer geohash if given', () => {
40+
const tableData = [
41+
[
42+
{
43+
latitude: 1,
44+
longitude: 2,
45+
geohash: 'stq4s3x' // 29.9796, 31.1345
46+
},
47+
{
48+
latitude: 3,
49+
longitude: 4,
50+
geohash: 'p05010r' // -89.997, 139.273
51+
}
52+
]
53+
];
54+
const data = [];
55+
56+
dataFormatter.setTableValues(tableData, data);
57+
58+
expect(data[0].locationLatitude).to.be.within(29.9796, 29.9797);
59+
expect(data[0].locationLongitude).to.be.within(31.1345, 31.1346);
60+
expect(data[1].locationLatitude).to.be.within(-89.998, -89.997);
61+
expect(data[1].locationLongitude).to.be.within(139.272, 139.273);
62+
});
63+
});
64+
865
describe('when the time series data matches the location', () => {
966
beforeEach(() => {
1067
const ctrl = {

0 commit comments

Comments
 (0)