|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import _ from 'lodash'; |
| 3 | +import moment from 'moment'; |
| 4 | +import { GetResourceCleanDataProps, NumberState } from './types'; |
| 5 | + |
| 6 | +export const useResourceCleanData = () => { |
| 7 | + const structureNumberStates = (parsedStatus: any, parsedSpec: any): NumberState[] => { |
| 8 | + const numberStates: NumberState[] = []; |
| 9 | + |
| 10 | + if (parsedSpec?.priority !== undefined) { |
| 11 | + numberStates.push({ |
| 12 | + title: 'Priority', |
| 13 | + value: parsedSpec.priority, |
| 14 | + quantity: '' |
| 15 | + }); |
| 16 | + } |
| 17 | + |
| 18 | + if (parsedSpec?.containers) { |
| 19 | + numberStates.push({ |
| 20 | + title: 'Containers', |
| 21 | + value: parsedSpec.containers.length, |
| 22 | + quantity: 'total' |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + if (parsedStatus?.containerStatuses) { |
| 27 | + const totalRestarts = parsedStatus.containerStatuses.reduce( |
| 28 | + (sum: number, container: { restartCount?: number }) => sum + (container.restartCount || 0), |
| 29 | + 0 |
| 30 | + ); |
| 31 | + numberStates.push({ |
| 32 | + title: 'Total Restarts', |
| 33 | + value: totalRestarts, |
| 34 | + quantity: 'times' |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + return numberStates; |
| 39 | + }; |
| 40 | + |
| 41 | + const getAge = (creationTimestamp: string): string => { |
| 42 | + const creationTime = moment(creationTimestamp); |
| 43 | + const currentTime = moment(); |
| 44 | + const ageInHours = currentTime.diff(creationTime, 'hours'); |
| 45 | + return ageInHours >= 24 ? `${Math.floor(ageInHours / 24)} days` : `${ageInHours} hours`; |
| 46 | + }; |
| 47 | + |
| 48 | + const getStatus = (attribute: any): string | false => { |
| 49 | + if (attribute?.phase) { |
| 50 | + return attribute.phase; |
| 51 | + } |
| 52 | + const readyCondition = attribute?.conditions?.find( |
| 53 | + (cond: { type: string }) => cond.type === 'Ready' |
| 54 | + ); |
| 55 | + return readyCondition ? 'Ready' : false; |
| 56 | + }; |
| 57 | + |
| 58 | + const joinwithEqual = (object: Record<string, string> | undefined): string[] => { |
| 59 | + if (!object) return []; |
| 60 | + return Object.entries(object).map(([key, value]) => { |
| 61 | + return `${key}=${value}`; |
| 62 | + }); |
| 63 | + }; |
| 64 | + |
| 65 | + const getResourceCleanData = ({ |
| 66 | + resource, |
| 67 | + activeLabels, |
| 68 | + dispatchMsgToEditor, |
| 69 | + showStatus |
| 70 | + }: GetResourceCleanDataProps) => { |
| 71 | + const parsedStatus = resource?.status?.attribute && JSON.parse(resource?.status?.attribute); |
| 72 | + const parsedSpec = resource?.spec?.attribute && JSON.parse(resource?.spec.attribute); |
| 73 | + const numberStates = structureNumberStates(parsedStatus, parsedSpec); |
| 74 | + |
| 75 | + const kind = resource?.kind; |
| 76 | + const cleanData = { |
| 77 | + age: getAge(resource?.metadata?.creationTimestamp || ''), |
| 78 | + kind: kind, |
| 79 | + status: showStatus && getStatus(parsedStatus), |
| 80 | + kubeletVersion: parsedStatus?.nodeInfo?.kubeletVersion, |
| 81 | + podIP: parsedStatus?.podIP, |
| 82 | + hostIP: parsedStatus?.hostIP, |
| 83 | + QoSClass: parsedStatus?.qosClass, |
| 84 | + size: parsedSpec?.resources?.requests?.storage, |
| 85 | + claim: parsedSpec?.claimRef?.name, |
| 86 | + claimNamespace: parsedSpec?.claimRef?.namespace, |
| 87 | + apiVersion: resource?.apiVersion, |
| 88 | + pods: |
| 89 | + parsedStatus?.replicas === undefined |
| 90 | + ? parsedStatus?.availableReplicas?.toString() |
| 91 | + : `${ |
| 92 | + parsedStatus?.availableReplicas?.toString() ?? '0' |
| 93 | + } / ${parsedStatus?.replicas?.toString()}`, |
| 94 | + replicas: |
| 95 | + parsedStatus?.readyReplicas !== undefined && |
| 96 | + parsedStatus?.replicas !== undefined && |
| 97 | + `${parsedStatus?.readyReplicas} / ${parsedStatus?.replicas}`, |
| 98 | + strategyType: resource?.configuration?.spec?.strategy?.type, |
| 99 | + storageClass: parsedSpec?.storageClassName, |
| 100 | + secretType: resource?.type, |
| 101 | + serviceType: parsedSpec?.type, |
| 102 | + clusterIp: parsedSpec?.clusterIP, |
| 103 | + updateStrategy: parsedSpec?.updateStrategy?.type, |
| 104 | + externalIp: parsedSpec?.externalIPs, |
| 105 | + finalizers: parsedSpec?.finalizers, |
| 106 | + accessModes: parsedSpec?.accessModes, |
| 107 | + deeplinks: { |
| 108 | + links: [ |
| 109 | + { nodeName: parsedSpec?.nodeName, label: 'Node' }, |
| 110 | + { namespace: resource?.metadata?.namespace, label: 'Namespace' }, |
| 111 | + { serviceAccount: parsedSpec?.serviceAccountName, label: 'ServiceAccount' } |
| 112 | + ], |
| 113 | + dispatchMsgToEditor: dispatchMsgToEditor |
| 114 | + }, |
| 115 | + selector: parsedSpec?.selector?.matchLabels |
| 116 | + ? joinwithEqual(parsedSpec?.selector?.matchLabels) |
| 117 | + : joinwithEqual(parsedSpec?.selector), |
| 118 | + images: parsedSpec?.template?.spec?.containers?.map((container: { image?: string }) => { |
| 119 | + return container?.image; |
| 120 | + }), |
| 121 | + numberStates: numberStates, |
| 122 | + nodeSelector: |
| 123 | + joinwithEqual(parsedSpec?.nodeSelector) || |
| 124 | + joinwithEqual(parsedSpec?.template?.spec?.nodeSelector), |
| 125 | + loadBalancer: parsedStatus?.loadBalancer?.ingress?.map((ingress: { ip?: string }) => { |
| 126 | + return ingress?.ip; |
| 127 | + }), |
| 128 | + rules: parsedSpec?.rules?.map((rule: { host?: string }) => { |
| 129 | + return rule?.host; |
| 130 | + }), |
| 131 | + usage: { |
| 132 | + allocatable: parsedStatus?.allocatable, |
| 133 | + capacity: parsedStatus?.capacity |
| 134 | + }, |
| 135 | + configData: resource?.configuration?.data, |
| 136 | + capacity: parsedSpec?.capacity?.storage, |
| 137 | + totalCapacity: parsedStatus?.capacity, |
| 138 | + totalAllocatable: parsedStatus?.allocatable, |
| 139 | + conditions: { |
| 140 | + ...parsedStatus?.conditions?.map((condition: { type?: string }) => { |
| 141 | + return condition?.type; |
| 142 | + }) |
| 143 | + }, |
| 144 | + tolerations: parsedSpec?.tolerations, |
| 145 | + podVolumes: parsedSpec?.volumes, |
| 146 | + ingressRules: parsedSpec?.rules, |
| 147 | + connections: kind === 'Service' && _.omit(parsedSpec, ['selector', 'type']), |
| 148 | + labels: { |
| 149 | + data: resource?.metadata?.labels?.map((label) => { |
| 150 | + const value = label?.value !== undefined ? label?.value : ''; |
| 151 | + return `${label?.key}=${value}`; |
| 152 | + }), |
| 153 | + dispatchMsgToEditor: dispatchMsgToEditor, |
| 154 | + activeViewFilters: activeLabels |
| 155 | + }, |
| 156 | + annotations: resource?.metadata?.annotations?.map((annotation) => { |
| 157 | + const value = annotation?.value !== undefined ? annotation?.value : ''; |
| 158 | + return `${annotation?.key}=${value}`; |
| 159 | + }), |
| 160 | + secret: resource?.data, |
| 161 | + initContainers: parsedSpec?.initContainers && |
| 162 | + parsedStatus?.initContainerStatuses && { |
| 163 | + spec: parsedSpec?.initContainers, |
| 164 | + status: parsedStatus?.initContainerStatuses |
| 165 | + }, |
| 166 | + containers: parsedSpec?.containers && |
| 167 | + parsedStatus?.containerStatuses && { |
| 168 | + spec: parsedSpec?.containers, |
| 169 | + status: parsedStatus?.containerStatuses |
| 170 | + } |
| 171 | + }; |
| 172 | + return cleanData; |
| 173 | + }; |
| 174 | + |
| 175 | + return { getResourceCleanData, structureNumberStates, getAge, getStatus, joinwithEqual }; |
| 176 | +}; |
0 commit comments