Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple metrics in popup content #77

Merged
merged 5 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/worldmap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ describe('Worldmap', () => {
});
});

describe('when the data has multiple metrics', () => {
beforeEach(() => {
ctrl.data = new DataBuilder()
.withCountryAndValue('SE', 1, {
__field_device_urn: 'safecast:903348716',
'__field_ingest.location': 'wecnv3p07bjj',
'__field_Average pms_pm02_5': 33,
'__field_Average lnd_7318u': 110,
})
.build();

ctrl.panel.esGeoPoint = 'ingest.location';
ctrl.panel.esLocationName = 'device_urn';
ctrl.panel.esMetric = 'Average pms_pm02_5';
worldMap.drawCircles();
});

it('should create a circle popup with additional metrics', () => {
expect(worldMap.circles[0]._popup._content).toBe('Sweden: 1 <br />Average lnd_7318u: 110');
});
});

describe('when the data has three points', () => {
beforeEach(() => {
ctrl.data = new DataBuilder()
Expand Down
34 changes: 29 additions & 5 deletions src/worldmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export default class WorldMap {
});

this.createClickthrough(circle, dataPoint);
const content = this.getPopupContent(dataPoint.locationName, dataPoint.valueRounded);
const content = this.getPopupContent(dataPoint);
this.createPopup(circle, content);
return circle;
}
Expand All @@ -319,7 +319,7 @@ export default class WorldMap {

// Re-create popup.
circle.unbindPopup();
const content = this.getPopupContent(dataPoint.locationName, dataPoint.valueRounded);
const content = this.getPopupContent(dataPoint);
this.createPopup(circle, content);

// Re-create clickthrough-link.
Expand Down Expand Up @@ -418,12 +418,16 @@ export default class WorldMap {
extendPopupContent(circle, dataPoint) {
const popup = circle.getPopup();
let popupContent = popup._content;
popupContent += `\n${this.getPopupContent(dataPoint.locationName, dataPoint.valueRounded)}`;
popupContent += `\n${this.getPopupContent(dataPoint)}`;
circle.setPopupContent(popupContent);
}

getPopupContent(locationName, value) {
getPopupContent(dataPoint) {
let unit;

let locationName = dataPoint.locationName;
let value = dataPoint.value;

if (_.isNaN(value)) {
value = 'n/a';
} else {
Expand All @@ -433,7 +437,27 @@ export default class WorldMap {
if (this.ctrl.settings.formatOmitEmptyValue && value === 'n/a') {
return `${locationName}`.trim();
} else {
return `${locationName}: ${value} ${unit || ''}`.trim();
let fieldPrefix = '__field_';

let specialFields = [
fieldPrefix + this.ctrl.settings.esLocationName,
fieldPrefix + this.ctrl.settings.esMetric,
fieldPrefix + this.ctrl.settings.esGeoPoint,
];
Comment on lines +442 to +446
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit specific to Elasticsearch, right? I see that this is your current focus and I will not block merging this PR. However, it would be nice if we can provide this feature for other data sources like InfluxDB.

Do you believe there is a chance this might break anything when not running with ES?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test suite passes for me. If that doesn’t seem like enough confidence I could add a guard. Do you have a dev setup for integration testing other data sources? I’d be happy to check but postgres and Es are the only tools I’ve ever done mapping from.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test suite passes for me. If that doesn’t seem like enough confidence I could add a guard.

I believe it's fine. Thanks.

Do you have a dev setup for integration testing other data sources?

Currently not. I just wanted to take a note on that for coming back to this later.


let freeDataFields = Object.keys(dataPoint).filter(
(key: string) => key.startsWith(fieldPrefix) && !specialFields.includes(key)
);

let freeDataDisplay = freeDataFields
.map((field: string) => {
let name = field.slice(fieldPrefix.length);
let value = dataPoint[field];
return `<br />${name}: ${value}`;
})
.join('');

return `${locationName}: ${value} ${unit || ''}${freeDataDisplay}`.trim();
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/data_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class DataBuilder {
this.data.categories = [];
}

withCountryAndValue(countryCode, value) {
withCountryAndValue(countryCode, value, overrides?: {[key: string]: any}) {
let dataPoint;
if (countryCode === 'SE') {
dataPoint = {
Expand Down Expand Up @@ -39,7 +39,7 @@ export default class DataBuilder {
} else {
throw new Error(`Unable to create fixture for country code ${countryCode}`);
}
this.data.push(dataPoint);
this.data.push({...dataPoint, ...overrides});

return this;
}
Expand Down