Skip to content

Commit

Permalink
Fix #1027: Remove default values from URLs (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson authored Nov 12, 2020
1 parent b526338 commit 9e2b170
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [unreleased](https://github.com/mozilla/glam/compare/2020.11.0...HEAD) (date TBD)

- Remove default values from URLs so they aren't as unwieldy
([#1027](https://github.com/mozilla/glam/issues/1027))
- Default to "All" ping types for unknown ping types
([#1026](https://github.com/mozilla/glam/issues/1026))
- Update labeled counters to use linear probe view
Expand Down
4 changes: 3 additions & 1 deletion src/config/fenix.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { transformAPIResponse } from '../utils/transform-data';
import { stripDefaultValues } from '../utils/urls';
import { extractBucketMetadata } from './shared';
import { getProbeData } from '../state/api';
import {
Expand Down Expand Up @@ -66,7 +67,7 @@ export default {
// These parameters will map to a ${key}=${value}&... in the querystring,
// which is used to convey the view state when the GLAM URL is shared with
// others.
return {
const params = {
app_id: storeValue.productDimensions.app_id,
os: storeValue.productDimensions.os,
ping_type: storeValue.productDimensions.ping_type,
Expand All @@ -77,6 +78,7 @@ export default {
visiblePercentiles: storeValue.visiblePercentiles,
reference: storeValue.reference,
};
return stripDefaultValues(params);
},
getParamsForDataAPI(storeValue) {
// These parameters are needed to request the data from the API itself
Expand Down
4 changes: 3 additions & 1 deletion src/config/firefox-desktop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import produce from 'immer';
import { extractBucketMetadata } from './shared';
import { stripDefaultValues } from '../utils/urls';
import { transformAPIResponse } from '../utils/transform-data';
import { isSelectedProcessValid } from '../utils/probe-utils';
import { getProbeData } from '../state/api';
Expand Down Expand Up @@ -87,7 +88,7 @@ export default {
// These parameters will map to a ${key}=${value}&... in the querystring,
// which is used to convey the view state when the GLAM URL is shared with
// others.
return {
const params = {
channel: storeValue.productDimensions.channel,
os: storeValue.productDimensions.os,
aggregationLevel: storeValue.productDimensions.aggregationLevel,
Expand All @@ -98,6 +99,7 @@ export default {
visiblePercentiles: storeValue.visiblePercentiles,
reference: storeValue.reference,
};
return stripDefaultValues(params);
},
getParamsForDataAPI(storeValue) {
// These parameters are needed to request the data from the API itself
Expand Down
32 changes: 32 additions & 0 deletions src/utils/urls.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import paramDefaults from '../config/shared';

// Return a shortened URL for bugzilla and github URLs.
export function extractBugId(url) {
if (url.includes('bugzilla')) {
const match = /id=(?<id>\d+)/.exec(url);
Expand All @@ -14,3 +17,32 @@ export function extractBugId(url) {
}
return url;
}

// Given an object of query params, return the object stripped of query
// parameters that are set to their default values.
export function stripDefaultValues(params) {
return Object.keys(params)
.filter((k) => {
if (Object.keys(paramDefaults).includes(k)) {
// If this param has a default value defined.
if (paramDefaults[k].isMulti) {
// If it has multiple values, use JSON.stringify for an easy
// equality check since these are arrays.
return (
JSON.stringify(params[k]) !==
JSON.stringify(paramDefaults[k].defaultValue)
);
}
// It's not `isMulti`, so just compare value to value.
return params[k] !== paramDefaults[k].defaultValue;
}
// If no default defined, return true to always include it in the URL.
return true;
})
.reduce((obj, k) => {
return {
...obj,
[k]: params[k],
};
}, {});
}

0 comments on commit 9e2b170

Please sign in to comment.