1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix not being able to switch metrics if none are available (#5430)

Co-authored-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Sebastian Malton 2022-08-02 07:05:15 -07:00 committed by GitHub
parent 3d62d1c029
commit 57e4e29a16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 11 deletions

View File

@ -6,13 +6,13 @@
import React from "react"; import React from "react";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import type { NodeStore } from "../+nodes/store"; import type { NodeStore } from "../+nodes/store";
import { cssNames } from "../../utils";
import { Radio, RadioGroup } from "../radio"; import { Radio, RadioGroup } from "../radio";
import type { ClusterOverviewStore } from "./cluster-overview-store/cluster-overview-store"; import type { ClusterOverviewStore } from "./cluster-overview-store/cluster-overview-store";
import { MetricNodeRole, MetricType } from "./cluster-overview-store/cluster-overview-store"; import { MetricNodeRole, MetricType } from "./cluster-overview-store/cluster-overview-store";
import clusterOverviewStoreInjectable from "./cluster-overview-store/cluster-overview-store.injectable"; import clusterOverviewStoreInjectable from "./cluster-overview-store/cluster-overview-store.injectable";
import { withInjectables } from "@ogre-tools/injectable-react"; import { withInjectables } from "@ogre-tools/injectable-react";
import nodeStoreInjectable from "../+nodes/store.injectable"; import nodeStoreInjectable from "../+nodes/store.injectable";
import { normalizeMetrics } from "../../../common/k8s-api/endpoints/metrics.api";
interface Dependencies { interface Dependencies {
clusterOverviewStore: ClusterOverviewStore; clusterOverviewStore: ClusterOverviewStore;
@ -24,32 +24,50 @@ const NonInjectedClusterMetricSwitchers = observer(({
nodeStore, nodeStore,
}: Dependencies) => { }: Dependencies) => {
const { masterNodes, workerNodes } = nodeStore; const { masterNodes, workerNodes } = nodeStore;
const metricsValues = clusterOverviewStore.getMetricsValues(clusterOverviewStore.metrics); const { cpuUsage, memoryUsage } = clusterOverviewStore.metrics;
const disableRoles = !masterNodes.length || !workerNodes.length; const hasMasterNodes = masterNodes.length > 0;
const disableMetrics = !metricsValues.length; const hasWorkerNodes = workerNodes.length > 0;
const hasCpuMetrics = normalizeMetrics(cpuUsage).data.result[0].values.length > 0;
const hasMemoryMetrics = normalizeMetrics(memoryUsage).data.result[0].values.length > 0;
return ( return (
<div className="flex gaps" style={{ marginBottom: "calc(var(--margin) * 2)" }}> <div className="flex gaps" style={{ marginBottom: "calc(var(--margin) * 2)" }}>
<div className="box grow"> <div className="box grow">
<RadioGroup <RadioGroup
asButtons asButtons
className={cssNames("RadioGroup flex gaps", { disabled: disableRoles })} className="RadioGroup flex gaps"
value={clusterOverviewStore.metricNodeRole} value={clusterOverviewStore.metricNodeRole}
onChange={metric => clusterOverviewStore.metricNodeRole = metric} onChange={metric => clusterOverviewStore.metricNodeRole = metric}
> >
<Radio label="Master" value={MetricNodeRole.MASTER}/> <Radio
<Radio label="Worker" value={MetricNodeRole.WORKER}/> label="Master"
value={MetricNodeRole.MASTER}
disabled={!hasMasterNodes}
/>
<Radio
label="Worker"
value={MetricNodeRole.WORKER}
disabled={!hasWorkerNodes}
/>
</RadioGroup> </RadioGroup>
</div> </div>
<div className="box grow" style={{ textAlign: "right" }}> <div className="box grow" style={{ textAlign: "right" }}>
<RadioGroup <RadioGroup
asButtons asButtons
className={cssNames("RadioGroup flex gaps", { disabled: disableMetrics })} className="RadioGroup flex gaps"
value={clusterOverviewStore.metricType} value={clusterOverviewStore.metricType}
onChange={value => clusterOverviewStore.metricType = value} onChange={value => clusterOverviewStore.metricType = value}
> >
<Radio label="CPU" value={MetricType.CPU}/> <Radio
<Radio label="Memory" value={MetricType.MEMORY}/> label="CPU"
value={MetricType.CPU}
disabled={!hasCpuMetrics}
/>
<Radio
label="Memory"
value={MetricType.MEMORY}
disabled={!hasMemoryMetrics}
/>
</RadioGroup> </RadioGroup>
</div> </div>
</div> </div>

View File

@ -17,7 +17,7 @@
} }
.empty { .empty {
margin-top: -45px;
text-align: center; text-align: center;
padding-bottom: 45px;
} }
} }

View File

@ -21,6 +21,11 @@
white-space: nowrap; white-space: nowrap;
transition: 250ms color; transition: 250ms color;
&.disabled {
opacity: 0.6;
pointer-events: none;
}
&:not(.checked):not(.disabled) { &:not(.checked):not(.disabled) {
cursor: pointer; cursor: pointer;
&:not(:active):focus { &:not(:active):focus {

View File

@ -85,6 +85,7 @@ export function Radio<T>({
type="radio" type="radio"
checked={checked} checked={checked}
onChange={() => ctx.onSelect(value)} onChange={() => ctx.onSelect(value)}
disabled={disabled || ctx.disabled}
/> />
<i className="tick flex center"/> <i className="tick flex center"/>
{label ? <div className="label">{label}</div> : null} {label ? <div className="label">{label}</div> : null}