Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(4494): add hard limit to metrics table #4547

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { capitalizeFirstLetter, cn } from '@/utils/js';
interface MetricsSummary {
totalUsage: number;
totalRequest: number;
totalLimit: number;
}

interface TableProps {
rows: (TransformedPodData | TransformedPVCData)[];
resource: ResourceType;
totalMetrics: MetricsSummary;
productRequest: number;
}

const isPVC = (row: TransformedPodData | TransformedPVCData): row is TransformedPVCData => 'pvName' in row;
Expand All @@ -41,7 +41,7 @@ const getPodMetricValue = (

const getPVCMetricValue = (row: TransformedPVCData, key: 'usage' | 'requests'): string | number => row[key] ?? '-';

export default function MetricsTable({ rows, resource, totalMetrics }: TableProps) {
export default function MetricsTable({ rows, resource, totalMetrics, productRequest }: TableProps) {
return (
<>
<div className="border-2 rounded-xl overflow-hidden">
Expand Down Expand Up @@ -105,23 +105,27 @@ export default function MetricsTable({ rows, resource, totalMetrics }: TableProp
})}
</div>
</div>
<div className="border-2 rounded-xl max-w-2xl my-6">
<div className="border-2 rounded-xl max-w-4xl my-6">
<div className="divide-y divide-grey-200/5">
<div className="grid grid-cols-1 md:grid-cols-9 lg:grid-cols-9 gap-4 px-4 py-3 sm:px-6 lg:px-8 bg-gray-100">
<div className="md:col-span-3 lg:col-span-3 text-center font-bold">Total {resource} request:</div>
<div className="md:col-span-3 lg:col-span-3 text-center font-bold">Current {resource} usage:</div>
<div className="md:col-span-3 lg:col-span-3 text-center font-bold">Utilization rate:</div>
<div className="grid grid-cols-1 md:grid-cols-12 lg:grid-cols-12 gap-4 px-4 py-3 sm:px-6 lg:px-8 bg-gray-100">
<div className="md:col-span-3 lg:col-span-3 text-center font-bold">Hard {resource} limit</div>
<div className="md:col-span-3 lg:col-span-3 text-center font-bold">Total {resource} request</div>
<div className="md:col-span-3 lg:col-span-3 text-center font-bold">Current {resource} usage</div>
<div className="md:col-span-3 lg:col-span-3 text-center font-bold">Utilization rate</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-9 lg:grid-cols-9 gap-4 px-4 py-3 sm:px-6 lg:px-8 ">
<div className="grid grid-cols-1 md:grid-cols-12 lg:grid-cols-12 gap-4 px-4 py-3 sm:px-6 lg:px-8 ">
<div className={cn('md:col-span-3 lg:col-span-3 text-center')}>
{formatMetric(resource, productRequest)}
</div>
<div className={cn('md:col-span-3 lg:col-span-3 text-center')}>
{formatMetric(resource, totalMetrics.totalRequest)}
</div>
<div className={cn('md:col-span-3 lg:col-span-3 text-center')}>
{formatMetric(resource, totalMetrics.totalUsage)}
</div>
<div className={cn('md:col-span-3 lg:col-span-3 text-center')}>
{totalMetrics.totalRequest > 0
? `${((totalMetrics.totalUsage / totalMetrics.totalRequest) * 100).toFixed(2)}%`
{productRequest && productRequest > 0
? `${((totalMetrics.totalUsage / productRequest) * 100).toFixed(2)}%`
: 'N/A'}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import { useQuery } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { z } from 'zod';
import FormSelect from '@/components/generic/select/FormSelect';
import { GlobalRole } from '@/constants';
import { environmentLongNames, environmentShortNames, GlobalRole } from '@/constants';
import createClientPage from '@/core/client-page';
import {
getTotalMetrics,
transformPVCData,
TransformedPVCData,
transformPodData,
TransformedPodData,
normalizeCpu,
normalizeMemory,
} from '@/helpers/resource-metrics';
import { getPodUsageMetrics } from '@/services/backend/private-cloud/products';
import { usePrivateProductState } from '@/states/global';
import MetricsTable from './MetricsTable';

const selectOptions = [
const selectOptions: { name: string; value: EnvironmentShort }[] = [
{
name: 'Development namespace',
value: 'dev',
Expand Down Expand Up @@ -46,15 +48,21 @@ const privateCloudProductUsageMetrics = createClientPage({
validations: { pathParams: pathParamSchema },
});

export default privateCloudProductUsageMetrics(({ getPathParams, session }) => {
type EnvironmentShort = keyof typeof environmentLongNames;
type EnvironmentLong = keyof typeof environmentShortNames;

export default privateCloudProductUsageMetrics(({ getPathParams }) => {
const [pathParams, setPathParams] = useState<z.infer<typeof pathParamSchema>>();

useEffect(() => {
getPathParams().then((v) => setPathParams(v));
}, []);

const [environment, setenvironment] = useState('dev');
const [environment, setEnvironment] = useState<EnvironmentShort>('dev');

const [, privateSnap] = usePrivateProductState();
const productRequest =
privateSnap.currentProduct?.resourceRequests[environmentLongNames[environment] as EnvironmentLong];

const { licencePlate = '' } = pathParams ?? {};

Expand All @@ -63,16 +71,16 @@ export default privateCloudProductUsageMetrics(({ getPathParams, session }) => {
queryFn: () => getPodUsageMetrics(licencePlate, environment, privateSnap.currentProduct?.cluster || ''),
});

const handleNamespaceChange = (namespace: string) => {
setenvironment(namespace);
const handleNamespaceChange = (namespace: EnvironmentShort) => {
setEnvironment(namespace);
};

const rowsPod: TransformedPodData[] = [
{
name: 'Pod name',
containerName: 'Container name',
usage: { cpu: 'CPU usage', memory: 'Memory usage' },
requests: { cpu: 'CPU requests', memory: 'Memory requests' },
requests: { cpu: 'CPU request', memory: 'Memory request' },
limits: { cpu: 'CPU limits', memory: 'Memory limits' },
},
...transformPodData(data.podMetrics),
Expand All @@ -91,13 +99,17 @@ export default privateCloudProductUsageMetrics(({ getPathParams, session }) => {

return (
<div>
<p className="w-full block text-sm font-medium leading-6 text-gray-900 pb-3">
Average utilization rate for CPU and Memory is being counted based on the metrics of your namespace received in
last 2 weeks
</p>
<fieldset className="w-full md:w-48 2xl:w-64 pb-6">
<FormSelect
id="id"
label="Filter by namespace"
options={selectOptions.map((v) => ({ label: v.name, value: v.value }))}
defaultValue={'dev'}
onChange={handleNamespaceChange}
defaultValue={environment}
onChange={(value) => handleNamespaceChange(value as EnvironmentShort)}
/>
</fieldset>
<Box pos="relative" className="min-h-96">
Expand All @@ -108,10 +120,21 @@ export default privateCloudProductUsageMetrics(({ getPathParams, session }) => {
) : (
<>
<LoadingOverlay visible={isLoading} zIndex={1000} overlayProps={{ radius: 'sm', blur: 2 }} />
<MetricsTable rows={rowsPod} resource="cpu" totalMetrics={getTotalMetrics(data.podMetrics, 'cpu')} />
<MetricsTable rows={rowsPod} resource="memory" totalMetrics={getTotalMetrics(data.podMetrics, 'memory')} />
<MetricsTable
rows={rowsPod}
productRequest={normalizeCpu((productRequest?.cpu ?? 0) + 'c')}
resource="cpu"
totalMetrics={getTotalMetrics(data.podMetrics, 'cpu')}
/>
<MetricsTable
rows={rowsPod}
productRequest={normalizeMemory((productRequest?.memory ?? 0) + 'Gi')}
resource="memory"
totalMetrics={getTotalMetrics(data.podMetrics, 'memory')}
/>
<MetricsTable
rows={rowsPVC}
productRequest={normalizeMemory((productRequest?.storage ?? 0) + 'Gi')}
resource="storage"
totalMetrics={getTotalMetrics(data.pvcMetrics, 'storage')}
/>
Expand Down
4 changes: 2 additions & 2 deletions app/components/private-cloud/sections/QuotasDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function QuotasDescription() {
<ol className="list-decimal pl-5">
<b>For CPU and Memory:</b>

<li>Your namespaces resource utilization rate is at least 35%.</li>
<li>Your namespace&rsquo;s resource utilization rate is at least 35%.</li>
<li>
The adjustment satisfies one of the following:
<ul className="list-disc pl-5">
Expand All @@ -31,7 +31,7 @@ export default function QuotasDescription() {
<ol className="list-decimal pl-5">
<b>For Storage:</b>

<li>Your namespaces current usage exceeds 80% of its requested capacity.</li>
<li>Your namespace&rsquo;s current usage exceeds 80% of its requested capacity.</li>
<li>
The adjustment satisfies one of the following:
<ul className="list-disc pl-5">
Expand Down
Loading