Skip to content

Commit 834f136

Browse files
authored
refactor: metrics uses named exports (#69)
1 parent 0b11f3c commit 834f136

File tree

5 files changed

+25
-41
lines changed

5 files changed

+25
-41
lines changed

src/main/frontend/src/containers/ContainerList.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*/
1717
import React from 'react';
18-
import metrics from '../metrics';
18+
import {bytesToHumanReadable} from '../metrics';
1919
import {Icon} from '../components';
2020
import Table from '../components/Table';
2121

@@ -57,9 +57,7 @@ export const ContainerList = ({containers, podMetrics, ...properties}) => (
5757
</Table.Cell>
5858
<Table.Cell>
5959
{podMetrics &&
60-
metrics.selectors.bytesToHumanReadable(
61-
podMetrics.containerMemory(c.name)
62-
)}
60+
bytesToHumanReadable(podMetrics.containerMemory(c.name))}
6361
</Table.Cell>
6462
</Table.Row>
6563
))}

src/main/frontend/src/metrics/index.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,4 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
import selectors from './selectors';
18-
19-
const metrics = {};
20-
21-
metrics.selectors = selectors;
22-
23-
export default metrics;
17+
export {bytesToHumanReadable, podMetrics, quantityToScalar} from './selectors';

src/main/frontend/src/metrics/selectors.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
const selectors = {};
18-
19-
selectors.bytesToHumanReadable = (bytes = 0) => {
17+
export const bytesToHumanReadable = (bytes = 0) => {
2018
if (bytes > Math.pow(1024, 3)) {
2119
return `${(bytes / Math.pow(1024, 3)).toFixed(3)} GiB`;
2220
} else if (bytes > Math.pow(1024, 2)) {
@@ -27,7 +25,7 @@ selectors.bytesToHumanReadable = (bytes = 0) => {
2725
return bytes;
2826
};
2927

30-
selectors.quantityToScalar = (quantity = 0) => {
28+
export const quantityToScalar = (quantity = 0) => {
3129
const quantityString = quantity.toString();
3230
if (quantityString.endsWith('n')) {
3331
return (
@@ -48,25 +46,23 @@ selectors.quantityToScalar = (quantity = 0) => {
4846
return parseFloat(quantityString);
4947
};
5048

51-
selectors.podMetrics = (podMetrics = {containers: []}) => {
49+
export const podMetrics = (podMetrics = {containers: []}) => {
5250
const ret = {};
5351
ret.totalCpu = () =>
5452
podMetrics.containers?.reduce(
55-
(total, c) => total + selectors.quantityToScalar(c.usage?.cpu),
53+
(total, c) => total + quantityToScalar(c.usage?.cpu),
5654
0
5755
);
5856
ret.totalMemory = () =>
5957
podMetrics.containers?.reduce(
60-
(total, c) => total + selectors.quantityToScalar(c.usage?.memory),
58+
(total, c) => total + quantityToScalar(c.usage?.memory),
6159
0
6260
);
6361
ret.container = containerName =>
6462
podMetrics.containers?.find(c => c.name === containerName) ?? {};
6563
ret.containerCpu = containerName =>
66-
selectors.quantityToScalar(ret.container(containerName)?.usage?.cpu);
64+
quantityToScalar(ret.container(containerName)?.usage?.cpu);
6765
ret.containerMemory = containerName =>
68-
selectors.quantityToScalar(ret.container(containerName)?.usage?.memory);
66+
quantityToScalar(ret.container(containerName)?.usage?.memory);
6967
return ret;
7068
};
71-
72-
export default selectors;

src/main/frontend/src/nodes/NodesDetailPage.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import React from 'react';
1818
import {connect} from 'react-redux';
1919
import {withParams} from '../router';
2020
import {Details, name} from '../metadata';
21-
import metrics from '../metrics';
21+
import {bytesToHumanReadable, quantityToScalar} from '../metrics';
2222
import {selectors} from './';
2323
import p from '../pods';
2424
import {Card, DonutChart, Form} from '../components';
@@ -73,13 +73,13 @@ export const NodesDetailPage = withParams(
7373
.map(c => c.resources.requests ?? {});
7474
const requestedCpu = requests
7575
.map(r => r.cpu ?? 0)
76-
.reduce((acc, c) => acc + metrics.selectors.quantityToScalar(c), 0);
77-
const allocatableMemory = metrics.selectors.quantityToScalar(
76+
.reduce((acc, c) => acc + quantityToScalar(c), 0);
77+
const allocatableMemory = quantityToScalar(
7878
selectors.statusAllocatableMemory(node)
7979
);
8080
const requestedMemory = requests
8181
.map(r => r.memory ?? 0)
82-
.reduce((acc, c) => acc + metrics.selectors.quantityToScalar(c), 0);
82+
.reduce((acc, c) => acc + quantityToScalar(c), 0);
8383
return (
8484
<ResourceDetailPage
8585
kind='Nodes'
@@ -94,20 +94,16 @@ export const NodesDetailPage = withParams(
9494
title='CPU'
9595
description='Requested vs. allocatable'
9696
partial={requestedCpu.toFixed(3)}
97-
total={metrics.selectors
98-
.quantityToScalar(selectors.statusAllocatableCpu(node))
99-
.toFixed(3)}
97+
total={quantityToScalar(
98+
selectors.statusAllocatableCpu(node)
99+
).toFixed(3)}
100100
/>
101101
<Dial
102102
title='Memory'
103103
description='Requested vs. allocatable'
104104
percent={(requestedMemory / allocatableMemory) * 100}
105-
partial={metrics.selectors.bytesToHumanReadable(
106-
requestedMemory
107-
)}
108-
total={metrics.selectors.bytesToHumanReadable(
109-
allocatableMemory
110-
)}
105+
partial={bytesToHumanReadable(requestedMemory)}
106+
total={bytesToHumanReadable(allocatableMemory)}
111107
/>
112108
<Dial
113109
title='Pods'

src/main/frontend/src/pods/PodsDetailPage.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {connect} from 'react-redux';
1919
import {withParams} from '../router';
2020
import {Details, uid} from '../metadata';
2121
import {ContainerList} from '../containers';
22-
import mts from '../metrics';
22+
import {bytesToHumanReadable, podMetrics} from '../metrics';
2323
import p from './';
2424
import {Card, Form, Icon} from '../components';
2525
import Link from '../components/Link';
@@ -66,7 +66,7 @@ const ActionLink = ({to, title, stylePrefix, icon}) => (
6666

6767
const PodsDetailPage = ({pod}) => {
6868
const metrics = useMetrics(pod);
69-
const podMetrics = metrics && mts.selectors.podMetrics(metrics);
69+
const currentPodMetrics = metrics && podMetrics(metrics);
7070
return (
7171
<ResourceDetailPage
7272
kind='Pods'
@@ -109,15 +109,15 @@ const PodsDetailPage = ({pod}) => {
109109
{p.selectors.restartPolicy(pod)}
110110
</Form.Field>
111111
<Form.Field label='Pod IP'>{p.selectors.statusPodIP(pod)}</Form.Field>
112-
{podMetrics && (
112+
{currentPodMetrics && (
113113
<>
114114
<Form.Field label='Used CPU'>
115115
<Icon icon='fa-microchip' className='text-gray-600 mr-2' />
116-
{podMetrics.totalCpu().toFixed(3)}
116+
{currentPodMetrics.totalCpu().toFixed(3)}
117117
</Form.Field>
118118
<Form.Field label='Used Memory'>
119119
<Icon icon='fa-memory' className='text-gray-600 mr-2' />
120-
{mts.selectors.bytesToHumanReadable(podMetrics.totalMemory())}
120+
{bytesToHumanReadable(currentPodMetrics.totalMemory())}
121121
</Form.Field>
122122
</>
123123
)}
@@ -129,7 +129,7 @@ const PodsDetailPage = ({pod}) => {
129129
titleVariant={Card.titleVariants.medium}
130130
className='mt-2'
131131
containers={p.selectors.containers(pod)}
132-
podMetrics={podMetrics}
132+
podMetrics={currentPodMetrics}
133133
/>
134134
</ResourceDetailPage>
135135
);

0 commit comments

Comments
 (0)