Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Do not show the indicator if metrics not available
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio committed Apr 3, 2020
1 parent 95e777f commit 91d5750
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
1 change: 1 addition & 0 deletions packages/system-monitor-base/src/cpuView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const CpuViewComponent = ({

return (
<IndicatorComponent
enabled={model.cpuAvailable}
values={values}
label={label}
color={'#0072B3'}
Expand Down
30 changes: 17 additions & 13 deletions packages/system-monitor-base/src/indicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,34 @@ const IndicatorBar = ({
*
*/
export const IndicatorComponent = ({
enabled,
values,
label,
color,
text,
}: {
enabled: boolean;
values: number[];
label: string;
color: string;
text: string;
}): ReactElement => {
const percentage = values[values.length - 1];
return (
<div className="jp-IndicatorContainer">
<div className="jp-IndicatorText">{label}</div>
{percentage !== null && (
<div className="jp-IndicatorWrapper">
<IndicatorBar
values={values}
percentage={percentage}
baseColor={color}
/>
</div>
)}
<div className="jp-IndicatorText">{text}</div>
</div>
enabled && (
<div className="jp-IndicatorContainer">
<div className="jp-IndicatorText">{label}</div>
{percentage !== null && (
<div className="jp-IndicatorWrapper">
<IndicatorBar
values={values}
percentage={percentage}
baseColor={color}
/>
</div>
)}
<div className="jp-IndicatorText">{text}</div>
</div>
)
);
};
1 change: 1 addition & 0 deletions packages/system-monitor-base/src/memoryView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const MemoryViewComponent = ({

return (
<IndicatorComponent
enabled={model.memoryAvailable}
values={values}
label={label}
color={'#00B35B'}
Expand Down
40 changes: 26 additions & 14 deletions packages/system-monitor-base/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ export namespace ResourceUsage {
return;
}
if (phase === 'rejected') {
const oldMetricsAvailable = this._metricsAvailable;
this._metricsAvailable = false;
const oldMemoryAvailable = this._memoryAvailable;
const oldCpuAvailable = this._cpuAvailable;
this._memoryAvailable = false;
this._cpuAvailable = false;
this._currentMemory = 0;
this._memoryLimit = null;
this._units = 'B';

if (oldMetricsAvailable) {
if (oldMemoryAvailable || oldCpuAvailable) {
this._changed.emit();
}
return;
Expand All @@ -63,10 +65,17 @@ export namespace ResourceUsage {
}

/**
* Whether the metrics server extension is available.
* Whether the memory metric is available.
*/
get metricsAvailable(): boolean {
return this._metricsAvailable;
get memoryAvailable(): boolean {
return this._memoryAvailable;
}

/**
* Whether the cpu metric is available.
*/
get cpuAvailable(): boolean {
return this._cpuAvailable;
}

/**
Expand Down Expand Up @@ -127,7 +136,8 @@ export namespace ResourceUsage {
value: Private.IMetricRequestResult | null
): void {
if (value === null) {
this._metricsAvailable = false;
this._memoryAvailable = false;
this._cpuAvailable = false;
this._currentMemory = 0;
this._memoryLimit = null;
this._units = 'B';
Expand All @@ -137,28 +147,30 @@ export namespace ResourceUsage {
const numBytes = value.rss;
const memoryLimit = value.limits.memory ? value.limits.memory.rss : null;
const [currentMemory, units] = Private.convertToLargestUnit(numBytes);

this._metricsAvailable = true;
this._memoryAvailable = numBytes !== undefined;
this._currentMemory = currentMemory;
this._units = units;
this._memoryLimit = memoryLimit
? memoryLimit / Private.MEMORY_UNIT_LIMITS[units]
: null;
this._currentCpuPercent =
value.cpu_percent === undefined
? 0
: Math.min(1, value.cpu_percent / 100);

const memoryPercent = this.memoryLimit
? Math.min(this._currentMemory / this.memoryLimit, 1)
: null;

const cpuPercent = value.cpu_percent;
this._cpuAvailable = cpuPercent !== undefined;
this._currentCpuPercent = this._cpuAvailable
? Math.min(1, cpuPercent / 100)
: 0;

this._values.push({ memoryPercent, cpuPercent: this._currentCpuPercent });
this._values.shift();
this._changed.emit(void 0);
}

private _metricsAvailable = false;
private _memoryAvailable = false;
private _cpuAvailable = false;
private _currentMemory = 0;
private _currentCpuPercent = 0;
private _memoryLimit: number | null = null;
Expand Down

0 comments on commit 91d5750

Please sign in to comment.