Skip to content

Commit

Permalink
chore: Replace lodash with ES6 funcs
Browse files Browse the repository at this point in the history
Summary:
lodash isn't heavily used in our codebase and ES6 should cover everthing
we need anyway so this is just a step towards getting rid of lodash.

Test Plan: Ran the dev UI. Checked charts, and their legends.

Reviewers: nlanam, zasgar

Reviewed By: zasgar

Signed-off-by: Vihang Mehta <vihang@pixielabs.ai>

Differential Revision: https://phab.corp.pixielabs.ai/D12351

GitOrigin-RevId: 9fb1a5879fffd677822a167aa850c65edddfb1e9
  • Loading branch information
vihangm authored and copybaranaut committed Oct 6, 2022
1 parent fecd0c5 commit 39aa963
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/ui/src/containers/legend/legend-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

import * as _ from 'lodash';
import { View } from 'vega-typings';

import { COLOR_SCALE } from 'app/containers/live/convert-to-vega-spec';
Expand Down Expand Up @@ -127,25 +126,28 @@ const keyAvgs = (hoverData: ValidHoverDatum[]): { [key: string]: number } => {
keyedAvgState[key].n += 1;
});
});
return _.mapValues(keyedAvgState, (state: { sum: number; n: number }) => {
if (state.n === 0) {
return 0.0;
}
return state.sum / state.n;
});
return Object.fromEntries(
Object.entries(keyedAvgState).map(([key, state]) => {
if (state.n === 0) {
return [key, 0.0];
}
return [key, state.sum / state.n];
}),
);
};

const buildTimeHashMap = (hoverData: ValidHoverDatum[], sortBy: (key: string) => number): TimeHashMap => {
function buildTimeHashMap(
hoverData: ValidHoverDatum[],
sortBy: (a: UnformattedLegendEntry, b: UnformattedLegendEntry) => number,
): TimeHashMap {
const timeHashMap: TimeHashMap = {};
hoverData.forEach((datum) => {
for (const datum of hoverData) {
const rest: UnformattedLegendEntry[] = Object.entries(datum).map((entry) => ({ key: entry[0], val: entry[1] }))
.filter((item) => item.key !== 'time' && item.key !== 'sum');
// noinspection UnnecessaryLocalVariableJS
const sortedRest = _.sortBy(rest, (item) => sortBy(item.key));
timeHashMap[datum.time] = sortedRest;
});
.filter((item) => item.key !== 'time' && item.key !== 'sum').sort(sortBy);
timeHashMap[datum.time] = rest;
}
return timeHashMap;
};
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const buildHoverDataCache = (hoverData: any): HoverDataCache => {
Expand Down Expand Up @@ -177,7 +179,7 @@ export const buildHoverDataCache = (hoverData: any): HoverDataCache => {
const { minTime, maxTime } = minMaxTimes(validEntries);
const keyedAvgs = keyAvgs(validEntries);

const timeHashMap: TimeHashMap = buildTimeHashMap(validEntries, (key) => -keyedAvgs[key]);
const timeHashMap: TimeHashMap = buildTimeHashMap(validEntries, (a, b) => keyedAvgs[b.key] - keyedAvgs[a.key]);

return {
timeHashMap,
Expand Down

0 comments on commit 39aa963

Please sign in to comment.