2025-01-27 22:22:48 +05:30
|
|
|
import axios from 'api';
|
|
|
|
|
import { ErrorResponseHandler } from 'api/ErrorResponseHandler';
|
|
|
|
|
import { AxiosError } from 'axios';
|
|
|
|
|
import { ErrorResponse, SuccessResponse } from 'types/api';
|
|
|
|
|
import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
|
|
|
|
|
import { TagFilter } from 'types/api/queryBuilder/queryBuilderData';
|
|
|
|
|
|
2025-05-30 15:57:29 +05:30
|
|
|
import { UnderscoreToDotMap } from '../utils';
|
|
|
|
|
|
2025-01-27 22:22:48 +05:30
|
|
|
export interface K8sVolumesListPayload {
|
|
|
|
|
filters: TagFilter;
|
|
|
|
|
groupBy?: BaseAutocompleteData[];
|
|
|
|
|
offset?: number;
|
|
|
|
|
limit?: number;
|
|
|
|
|
orderBy?: {
|
|
|
|
|
columnName: string;
|
|
|
|
|
order: 'asc' | 'desc';
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface K8sVolumesData {
|
|
|
|
|
persistentVolumeClaimName: string;
|
|
|
|
|
volumeAvailable: number;
|
|
|
|
|
volumeCapacity: number;
|
|
|
|
|
volumeInodes: number;
|
|
|
|
|
volumeInodesFree: number;
|
|
|
|
|
volumeInodesUsed: number;
|
|
|
|
|
volumeUsage: number;
|
|
|
|
|
meta: {
|
|
|
|
|
k8s_cluster_name: string;
|
|
|
|
|
k8s_namespace_name: string;
|
|
|
|
|
k8s_node_name: string;
|
|
|
|
|
k8s_persistentvolumeclaim_name: string;
|
|
|
|
|
k8s_pod_name: string;
|
|
|
|
|
k8s_pod_uid: string;
|
|
|
|
|
k8s_statefulset_name: string;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface K8sVolumesListResponse {
|
|
|
|
|
status: string;
|
|
|
|
|
data: {
|
|
|
|
|
type: string;
|
|
|
|
|
records: K8sVolumesData[];
|
|
|
|
|
groups: null;
|
|
|
|
|
total: number;
|
|
|
|
|
sentAnyHostMetricsData: boolean;
|
|
|
|
|
isSendingK8SAgentMetrics: boolean;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-30 15:57:29 +05:30
|
|
|
export const volumesMetaMap: Array<{
|
|
|
|
|
dot: keyof Record<string, unknown>;
|
|
|
|
|
under: keyof K8sVolumesData['meta'];
|
|
|
|
|
}> = [
|
|
|
|
|
{ dot: 'k8s.cluster.name', under: 'k8s_cluster_name' },
|
|
|
|
|
{ dot: 'k8s.namespace.name', under: 'k8s_namespace_name' },
|
|
|
|
|
{ dot: 'k8s.node.name', under: 'k8s_node_name' },
|
|
|
|
|
{
|
|
|
|
|
dot: 'k8s.persistentvolumeclaim.name',
|
|
|
|
|
under: 'k8s_persistentvolumeclaim_name',
|
|
|
|
|
},
|
|
|
|
|
{ dot: 'k8s.pod.name', under: 'k8s_pod_name' },
|
|
|
|
|
{ dot: 'k8s.pod.uid', under: 'k8s_pod_uid' },
|
|
|
|
|
{ dot: 'k8s.statefulset.name', under: 'k8s_statefulset_name' },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export function mapVolumesMeta(
|
|
|
|
|
rawMeta: Record<string, unknown>,
|
|
|
|
|
): K8sVolumesData['meta'] {
|
|
|
|
|
// start with everything that was already there
|
|
|
|
|
const out: Record<string, unknown> = { ...rawMeta };
|
|
|
|
|
|
|
|
|
|
// for each dot→under rule, if the raw has the dot, overwrite the underscore
|
|
|
|
|
volumesMetaMap.forEach(({ dot, under }) => {
|
|
|
|
|
if (dot in rawMeta) {
|
|
|
|
|
const val = rawMeta[dot];
|
|
|
|
|
out[under] = typeof val === 'string' ? val : rawMeta[under];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return out as K8sVolumesData['meta'];
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 22:22:48 +05:30
|
|
|
export const getK8sVolumesList = async (
|
|
|
|
|
props: K8sVolumesListPayload,
|
|
|
|
|
signal?: AbortSignal,
|
|
|
|
|
headers?: Record<string, string>,
|
2025-05-30 15:57:29 +05:30
|
|
|
dotMetricsEnabled = false,
|
2025-01-27 22:22:48 +05:30
|
|
|
): Promise<SuccessResponse<K8sVolumesListResponse> | ErrorResponse> => {
|
|
|
|
|
try {
|
2025-05-30 15:57:29 +05:30
|
|
|
// Prepare filters
|
|
|
|
|
const requestProps =
|
|
|
|
|
dotMetricsEnabled && Array.isArray(props.filters?.items)
|
|
|
|
|
? {
|
|
|
|
|
...props,
|
|
|
|
|
filters: {
|
|
|
|
|
...props.filters,
|
|
|
|
|
items: props.filters.items.reduce<typeof props.filters.items>(
|
|
|
|
|
(acc, item) => {
|
|
|
|
|
if (item.value === undefined) return acc;
|
|
|
|
|
if (
|
|
|
|
|
item.key &&
|
|
|
|
|
typeof item.key === 'object' &&
|
|
|
|
|
'key' in item.key &&
|
|
|
|
|
typeof item.key.key === 'string'
|
|
|
|
|
) {
|
|
|
|
|
const mappedKey = UnderscoreToDotMap[item.key.key] ?? item.key.key;
|
|
|
|
|
acc.push({ ...item, key: { ...item.key, key: mappedKey } });
|
|
|
|
|
} else {
|
|
|
|
|
acc.push(item);
|
|
|
|
|
}
|
|
|
|
|
return acc;
|
|
|
|
|
},
|
|
|
|
|
[] as typeof props.filters.items,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: props;
|
|
|
|
|
|
|
|
|
|
const response = await axios.post('/pvcs/list', requestProps, {
|
2025-01-27 22:22:48 +05:30
|
|
|
signal,
|
|
|
|
|
headers,
|
|
|
|
|
});
|
2025-05-30 15:57:29 +05:30
|
|
|
const payload: K8sVolumesListResponse = response.data;
|
|
|
|
|
|
|
|
|
|
payload.data.records = payload.data.records.map((record) => ({
|
|
|
|
|
...record,
|
|
|
|
|
meta: mapVolumesMeta(record.meta as Record<string, unknown>),
|
|
|
|
|
}));
|
2025-01-27 22:22:48 +05:30
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
error: null,
|
|
|
|
|
message: 'Success',
|
2025-05-30 15:57:29 +05:30
|
|
|
payload,
|
|
|
|
|
params: requestProps,
|
2025-01-27 22:22:48 +05:30
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return ErrorResponseHandler(error as AxiosError);
|
|
|
|
|
}
|
|
|
|
|
};
|