Skip to content

Commit

Permalink
fix: avoid unnecessary re-rendering on each agent poll [DET-3427] (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidzr authored Jun 24, 2020
1 parent a93c794 commit 3477ac8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions webui/react/src/components/ResourceChart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Plotly, { Data, Layout } from 'plotly.js-basic-dist';
import React from 'react';
import React, { useMemo, useState } from 'react';
import createPlotlyComponent from 'react-plotly.js/factory';

import { getStateColor, lightTheme } from 'themes';
Expand All @@ -9,7 +9,7 @@ import { clone } from 'utils/data';
const Plot = createPlotlyComponent(Plotly);
interface Props extends CommonProps {
title: string;
resources: Resource[];
resources?: Resource[];
}

export interface PlotInfo {
Expand All @@ -25,7 +25,6 @@ const initialTally = Object.values(ResourceState).reduce((acc, key) => {
}, {} as Tally);

const genPlotInfo = (title: string, resources: Resource[]): PlotInfo | null => {

const tally = clone(initialTally) as Tally;

resources.forEach(resource => {
Expand Down Expand Up @@ -90,12 +89,21 @@ const genPlotInfo = (title: string, resources: Resource[]): PlotInfo | null => {
};

const SlotChart: React.FC<Props> = ({ title, resources, ...rest }: Props) => {
const plotInfo = genPlotInfo(title, resources);
const [ oldPlotInfo, setOldPlotInfo ] = useState<PlotInfo | null>(null);

const plotInfo = useMemo(() => {
const newPlotInfo = genPlotInfo(title, resources || []);
if (JSON.stringify(newPlotInfo) === JSON.stringify(oldPlotInfo)) return oldPlotInfo;
setOldPlotInfo(newPlotInfo);
return newPlotInfo;
}, [ oldPlotInfo, resources, title ]);

if (plotInfo === null) return <React.Fragment />;

return (
<Plot
{...rest}
config={{ displaylogo: false, responsive: true }}
config={{ displaylogo: false, responsive: false }}
data={plotInfo.data}
layout={plotInfo.layout}
/>
Expand Down
2 changes: 1 addition & 1 deletion webui/react/src/pages/Cluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Cluster: React.FC = () => {
<Grid minItemWidth={50}>
{Object.values(ResourceType).map((resourceType, idx) => (
<ResourceChart key={idx}
resources={availableResources[resourceType] || []}
resources={availableResources[resourceType]}
title={resourceType + 's'} />
))}
</Grid>
Expand Down

0 comments on commit 3477ac8

Please sign in to comment.