Skip to content

Commit

Permalink
fix: fix cluster resource pie chart color mapping [DET-3861] (#1094)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidzr authored Aug 17, 2020
1 parent cc9a7c1 commit 2318ea0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
4 changes: 2 additions & 2 deletions webui/react/src/components/Badge.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Tooltip } from 'antd';
import React, { CSSProperties, PropsWithChildren } from 'react';

import { getStateColor } from 'themes';
import { getStateColorCssVar } from 'themes';
import { CommandState, RunState } from 'types';
import { stateToLabel } from 'utils/types';

Expand Down Expand Up @@ -30,7 +30,7 @@ const Badge: React.FC<Props> = ({

if (type === BadgeType.State) {
classes.push(css.state);
style.backgroundColor = getStateColor(state);
style.backgroundColor = getStateColorCssVar(state);
} else if (type === BadgeType.Id) {
classes.push(css.id);
}
Expand Down
4 changes: 2 additions & 2 deletions webui/react/src/components/ProgressBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Tooltip } from 'antd';
import React from 'react';

import { getStateColor } from 'themes';
import { getStateColorCssVar } from 'themes';
import { CommandState, RunState } from 'types';
import { floatToPercent } from 'utils/string';

Expand All @@ -20,7 +20,7 @@ const defaultProps = {
const ProgressBar: React.FC<Props> = ({ barOnly, percent, state }: Props) => {
const classes = [ css.base ];
const style = {
backgroundColor: getStateColor(state),
backgroundColor: getStateColorCssVar(state),
width: `${percent}%`,
};

Expand Down
20 changes: 20 additions & 0 deletions webui/react/src/components/ResourceChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,24 @@ export const HalfHalf = (): React.ReactNode => <ResourceChart resources={[
{ container: { state: ResourceState.Running } },
] as Resource[]} title="GPUs" />;

export const MostlyStarting = (): React.ReactNode => <ResourceChart resources={[
{ container: undefined },
{ container: undefined },
{ container: { state: ResourceState.Running } },
{ container: { state: ResourceState.Starting } },
{ container: { state: ResourceState.Starting } },
{ container: { state: ResourceState.Starting } },
{ container: { state: ResourceState.Starting } },
] as Resource[]} title="GPUs" />;

export const MostlyRunning = (): React.ReactNode => <ResourceChart resources={[
{ container: undefined },
{ container: undefined },
{ container: { state: ResourceState.Starting } },
{ container: { state: ResourceState.Running } },
{ container: { state: ResourceState.Running } },
{ container: { state: ResourceState.Running } },
{ container: { state: ResourceState.Running } },
] as Resource[]} title="GPUs" />;

export const Empty = (): React.ReactNode => <ResourceChart resources={[]} title="DPUs" />;
6 changes: 1 addition & 5 deletions webui/react/src/components/ResourceChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ const genPlotInfo = (title: string, resources: Resource[]): PlotInfo | null => {
if (rsValue === 0) return;
labels.push(rsState);
values.push(rsValue);
if (rsState === ResourceState.Free) {
colors.push(lightTheme.colors.states.inactive);
} else {
colors.push(getStateColor(rsState as CommandState));
}
colors.push(getStateColor(rsState as CommandState));
});

const data: Data[] = [ {
Expand Down
65 changes: 39 additions & 26 deletions webui/react/src/themes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommandState, RunState } from 'types';
import { CommandState, ResourceState, RunState } from 'types';

export enum ShirtSize {
micro = 'micro',
Expand All @@ -12,6 +12,14 @@ export enum ShirtSize {
giant = 'giant',
}

enum StateColors {
active = 'active',
failed = 'failed',
inactive = 'inactive',
success = 'success',
suspended = 'suspended',
}

export interface Theme {
colors: {
core: {
Expand All @@ -21,13 +29,7 @@ export interface Theme {
tertiary: string;
};
monochrome: string[];
states: {
active: string;
failed: string;
inactive: string;
success: string;
suspended: string;
};
states: Record<StateColors, string>;
};
focus: {
shadow: string;
Expand Down Expand Up @@ -154,26 +156,37 @@ export const lightTheme: Theme = {
};

const stateColorMapping = {
[RunState.Active]: 'var(--theme-colors-states-active)',
[RunState.Canceled]: 'var(--theme-colors-states-inactive)',
[RunState.Completed]: 'var(--theme-colors-states-success)',
[RunState.Deleted]: 'var(--theme-colors-states-failed)',
[RunState.Errored]: 'var(--theme-colors-states-failed)',
[RunState.Paused]: 'var(--theme-colors-states-suspended)',
[RunState.StoppingCanceled]: 'var(--theme-colors-states-inactive)',
[RunState.StoppingCompleted]: 'var(--theme-colors-states-success)',
[RunState.StoppingError]: 'var(--theme-colors-states-failed)',
[CommandState.Pending]: 'var(--theme-colors-states-suspended)',
[CommandState.Assigned]: 'var(--theme-colors-states-suspended)',
[CommandState.Pulling]: 'var(--theme-colors-states-active)',
[CommandState.Starting]: 'var(--theme-colors-states-active)',
[CommandState.Running]: 'var(--theme-colors-states-active)',
[CommandState.Terminating]: 'var(--theme-colors-states-inactive)',
[CommandState.Terminated]: 'var(--theme-colors-states-inactive)',
[RunState.Active]: 'active',
[RunState.Canceled]: 'inactive',
[RunState.Completed]: 'success',
[RunState.Deleted]: 'failed',
[RunState.Errored]: 'failed',
[RunState.Paused]: 'suspended',
[RunState.StoppingCanceled]: 'inactive',
[RunState.StoppingCompleted]: 'success',
[RunState.StoppingError]: 'failed',
[CommandState.Pending]: 'suspended',
[CommandState.Assigned]: 'suspended',
[CommandState.Pulling]: 'active',
[CommandState.Starting]: 'active',
[CommandState.Running]: 'active',
[CommandState.Terminating]: 'inactive',
[CommandState.Terminated]: 'inactive',
[ResourceState.Free]: 'inactive',
};

export const getStateColorCssVar = (
state: RunState | CommandState | ResourceState | undefined,
): string => {
const name = state ? stateColorMapping[state] : 'active';
return `var(--theme-colors-states-${name})`;
};

export const getStateColor = (state: RunState | CommandState | undefined): string => {
return stateColorMapping[state || RunState.Active];
export const getStateColor = (
state: RunState | CommandState | ResourceState,
): string => {
const name = state ? stateColorMapping[state] : 'active';
return window.getComputedStyle(document.body).getPropertyValue(`--theme-colors-states-${name}`);
};

export enum ThemeId {
Expand Down

0 comments on commit 2318ea0

Please sign in to comment.