From d1f811dbcfc6f6bf35f3c14f66176164a5fd8ad8 Mon Sep 17 00:00:00 2001 From: Vitaliy Gulyy Date: Fri, 16 Aug 2024 19:08:47 +0300 Subject: [PATCH] fix: remove trailing slash from metrics server endpoint (#401) Signed-off-by: vitaliy-guliy --- .../src/resource-monitor.ts | 9 +++-- .../src/units-converter.ts | 33 +++++++++++-------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/code/extensions/che-resource-monitor/src/resource-monitor.ts b/code/extensions/che-resource-monitor/src/resource-monitor.ts index 8832672d236..859571a78a6 100644 --- a/code/extensions/che-resource-monitor/src/resource-monitor.ts +++ b/code/extensions/che-resource-monitor/src/resource-monitor.ts @@ -24,8 +24,8 @@ export class ResourceMonitor { @inject(K8sHelper) private k8sHelper!: K8sHelper; - private METRICS_SERVER_ENDPOINT = '/apis/metrics.k8s.io/v1beta1/'; - private METRICS_REQUEST_URL = `${this.METRICS_SERVER_ENDPOINT}namespaces/`; + private METRICS_SERVER_ENDPOINT = '/apis/metrics.k8s.io/v1beta1'; + private WARNING_COLOR = '#FFCC00'; private DEFAULT_COLOR = '#FFFFFF'; private DEFAULT_TOOLTIP = 'Workspace resources'; @@ -121,9 +121,8 @@ export class ResourceMonitor { } async getMetrics(): Promise { - const requestURL = `${this.METRICS_REQUEST_URL}${this.namespace}/pods/${this.podName}`; - const opts = { url: this.METRICS_SERVER_ENDPOINT }; - const response = await this.k8sHelper.sendRawQuery(requestURL, opts); + const requestURL = `${this.METRICS_SERVER_ENDPOINT}/namespaces/${this.namespace}/pods/${this.podName}`; + const response = await this.k8sHelper.sendRawQuery(requestURL, { url: this.METRICS_SERVER_ENDPOINT }); if (response.statusCode !== 200) { // wait when workspace pod's metrics will be available diff --git a/code/extensions/che-resource-monitor/src/units-converter.ts b/code/extensions/che-resource-monitor/src/units-converter.ts index d6a3bc7cb13..3d5b35bc902 100644 --- a/code/extensions/che-resource-monitor/src/units-converter.ts +++ b/code/extensions/che-resource-monitor/src/units-converter.ts @@ -16,18 +16,18 @@ export function convertToBytes(unit: string | undefined): number { if (!unit) { return 0; } - if (unit.substr(unit.length - 2).toUpperCase() === 'MI') { - return fromMebibytes(parseInt(unit.substr(0, unit.length - 2))); - } else if (unit.substr(unit.length - 2).toUpperCase() === 'KI') { - return fromKibibytes(parseInt(unit.substr(0, unit.length - 2))); - } else if (unit.substr(unit.length - 2).toUpperCase() === 'GI') { - return fromGibibytes(parseInt(unit.substr(0, unit.length - 2))); - } else if (unit.substr(unit.length - 1).toUpperCase() === 'M') { - return fromMegabytes(parseInt(unit.substr(0, unit.length - 1))); - } else if (unit.substr(unit.length - 1).toUpperCase() === 'K') { - return fromKilobytes(parseInt(unit.substr(0, unit.length - 1))); - } else if (unit.substr(unit.length - 1).toUpperCase() === 'G') { - return fromGigabytes(parseInt(unit.substr(0, unit.length - 1))); + if (unit.substring(unit.length - 2).toUpperCase() === 'MI') { + return fromMebibytes(parseInt(unit.substring(0, unit.length - 2))); + } else if (unit.substring(unit.length - 2).toUpperCase() === 'KI') { + return fromKibibytes(parseInt(unit.substring(0, unit.length - 2))); + } else if (unit.substring(unit.length - 2).toUpperCase() === 'GI') { + return fromGibibytes(parseInt(unit.substring(0, unit.length - 2))); + } else if (unit.substring(unit.length - 1).toUpperCase() === 'M') { + return fromMegabytes(parseInt(unit.substring(0, unit.length - 1))); + } else if (unit.substring(unit.length - 1).toUpperCase() === 'K') { + return fromKilobytes(parseInt(unit.substring(0, unit.length - 1))); + } else if (unit.substring(unit.length - 1).toUpperCase() === 'G') { + return fromGigabytes(parseInt(unit.substring(0, unit.length - 1))); } else { return parseInt(unit); } @@ -61,8 +61,13 @@ export function convertToMilliCPU(unit: string | undefined): number { if (!unit) { return 0; } - if (unit.substr(unit.length - 1).toUpperCase() === 'M') { - return parseInt(unit.substr(0, unit.length - 1)); + + if (unit.substring(unit.length - 1).toUpperCase() === 'M') { + return parseInt(unit.substring(0, unit.length - 1)); + } else if (unit.substring(unit.length - 1).toUpperCase() === 'N') { + const value = parseInt(unit.substring(0, unit.length - 1)); + // convert nanoCPU to miliCPU + return Math.round(value / 1000 / 1000); } else { return parseInt(unit) * 1000; }