|
| 1 | +import { |
| 2 | + K8sResourceKind, |
| 3 | + PrometheusEndpoint, |
| 4 | + PrometheusResponse, |
| 5 | + usePrometheusPoll |
| 6 | +} from '@openshift-console/dynamic-plugin-sdk'; |
| 7 | +import { Flex, FlexItem, Spinner, Text, TextVariants } from '@patternfly/react-core'; |
| 8 | +import { WarningTriangleIcon } from '@patternfly/react-icons'; |
| 9 | +import { Table, Tbody, Td, Th, Thead, Tr } from '@patternfly/react-table'; |
| 10 | +import _ from 'lodash'; |
| 11 | +import React, { FC } from 'react'; |
| 12 | +import { useTranslation } from 'react-i18next'; |
| 13 | +import './forms.css'; |
| 14 | + |
| 15 | +export type ResourceCalculatorProps = { |
| 16 | + flowCollector: K8sResourceKind | null; |
| 17 | +}; |
| 18 | + |
| 19 | +export const Consumption: FC<ResourceCalculatorProps> = ({ flowCollector }) => { |
| 20 | + const { t } = useTranslation('plugin__netobserv-plugin'); |
| 21 | + |
| 22 | + const [receivedPackets, rpLoaded, rpError] = usePrometheusPoll({ |
| 23 | + endpoint: PrometheusEndpoint.QUERY, |
| 24 | + query: `sort_desc(sum(irate(container_network_receive_packets_total{cluster="",namespace=~".+"}[4h])) by (node,namespace,pod))` |
| 25 | + }); |
| 26 | + |
| 27 | + const [transmittedPackets, tpLoaded, tpError] = usePrometheusPoll({ |
| 28 | + endpoint: PrometheusEndpoint.QUERY, |
| 29 | + query: `sort_desc(sum(irate(container_network_transmit_packets_total{cluster="",namespace=~".+"}[4h])) by (node,namespace,pod))` |
| 30 | + }); |
| 31 | + |
| 32 | + const getCurrentSampling = React.useCallback(() => { |
| 33 | + return flowCollector?.spec?.agent?.ebpf?.sampling || 50; |
| 34 | + }, [flowCollector?.spec?.agent?.ebpf?.sampling]); |
| 35 | + |
| 36 | + const getSamplings = React.useCallback(() => { |
| 37 | + const current = getCurrentSampling(); |
| 38 | + let samplings = [1, 50, 100, 250]; |
| 39 | + if (!samplings.includes(current)) { |
| 40 | + samplings.push(current); |
| 41 | + samplings = _.sortBy(samplings); |
| 42 | + } |
| 43 | + return samplings; |
| 44 | + }, [getCurrentSampling]); |
| 45 | + |
| 46 | + const loadingComponent = () => <Spinner size="lg" />; |
| 47 | + |
| 48 | + const errorComponent = () => <WarningTriangleIcon />; |
| 49 | + |
| 50 | + const value = (response?: PrometheusResponse) => { |
| 51 | + if (!response) { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + return _.sumBy(response.data.result, r => Number(r.value![1])); |
| 55 | + }; |
| 56 | + |
| 57 | + const labelsCount = React.useCallback( |
| 58 | + (label: string) => { |
| 59 | + if (!rpLoaded) { |
| 60 | + return loadingComponent(); |
| 61 | + } else if (rpError) { |
| 62 | + return errorComponent(); |
| 63 | + } else if (!receivedPackets) { |
| 64 | + return t('n/a'); |
| 65 | + } |
| 66 | + return _.uniq(_.map(receivedPackets.data.result, r => r.metric[label])).length; |
| 67 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 68 | + }, |
| 69 | + [receivedPackets, rpError, rpLoaded] |
| 70 | + ); |
| 71 | + |
| 72 | + return ( |
| 73 | + <Flex direction={{ default: 'column' }}> |
| 74 | + <FlexItem className="calculator-item"> |
| 75 | + <Text component={TextVariants.h2}>{t('Cluster metrics')}</Text> |
| 76 | + <Table> |
| 77 | + <Thead> |
| 78 | + <Tr> |
| 79 | + <Th>{t('Bandwidth')}</Th> |
| 80 | + <Th>{t('Nodes')}</Th> |
| 81 | + <Th>{t('Namespaces')}</Th> |
| 82 | + <Th>{t('Pods')}</Th> |
| 83 | + </Tr> |
| 84 | + </Thead> |
| 85 | + <Tbody> |
| 86 | + <Tr> |
| 87 | + <Td> |
| 88 | + {!rpLoaded || !tpLoaded |
| 89 | + ? loadingComponent() |
| 90 | + : rpError || tpError |
| 91 | + ? errorComponent() |
| 92 | + : `${Math.round(value(receivedPackets) + value(transmittedPackets))} pps`} |
| 93 | + </Td> |
| 94 | + <Td>{labelsCount('node')}</Td> |
| 95 | + <Td>{labelsCount('namespace')}</Td> |
| 96 | + <Td>{labelsCount('pod')}</Td> |
| 97 | + </Tr> |
| 98 | + </Tbody> |
| 99 | + </Table> |
| 100 | + </FlexItem> |
| 101 | + <FlexItem> |
| 102 | + <Text component={TextVariants.h2}>{t('Estimation')}</Text> |
| 103 | + <Table> |
| 104 | + <Thead> |
| 105 | + <Tr> |
| 106 | + <Th>{t('Sampling')}</Th> |
| 107 | + <Th>{t('vCPU')}</Th> |
| 108 | + <Th>{t('Memory')}</Th> |
| 109 | + <Th>{t('Storage')}</Th> |
| 110 | + <Th>{t('Latency overhead')}</Th> |
| 111 | + </Tr> |
| 112 | + </Thead> |
| 113 | + <Tbody> |
| 114 | + {getSamplings().map((sampling, i) => { |
| 115 | + const current = getCurrentSampling() === sampling; |
| 116 | + return ( |
| 117 | + <Tr key={i} isRowSelected={current}> |
| 118 | + <Td>{`${sampling} ${current ? t('(current)') : ''}`}</Td> |
| 119 | + <Td></Td> |
| 120 | + <Td></Td> |
| 121 | + <Td></Td> |
| 122 | + <Td></Td> |
| 123 | + </Tr> |
| 124 | + ); |
| 125 | + })} |
| 126 | + </Tbody> |
| 127 | + </Table> |
| 128 | + </FlexItem> |
| 129 | + </Flex> |
| 130 | + ); |
| 131 | +}; |
| 132 | + |
| 133 | +export default Consumption; |
0 commit comments