import "./pod-details-list.scss"; import React from "react"; import kebabCase from "lodash/kebabCase"; import { disposeOnUnmount, observer } from "mobx-react"; import { Trans } from "@lingui/macro"; import { podsStore } from "./pods.store"; import { Pod } from "../../api/endpoints"; import { autobind, bytesToUnits, cssNames, interval, prevDefault } from "../../utils"; import { KubeEventIcon } from "../+events/kube-event-icon"; import { LineProgress } from "../line-progress"; import { KubeObject } from "../../api/kube-object"; import { Table, TableCell, TableHead, TableRow } from "../table"; import { showDetails } from "../../navigation"; import { reaction } from "mobx"; import { Spinner } from "../spinner"; import { DrawerTitle } from "../drawer"; enum sortBy { name = "name", namespace = "namespace", cpu = "cpu", memory = "memory", } interface Props extends OptionalProps { pods: Pod[]; owner: KubeObject; } interface OptionalProps { maxCpu?: number; maxMemory?: number; showTitle?: boolean; } @observer export class PodDetailsList extends React.Component { static defaultProps: OptionalProps = { showTitle: true } private metricsWatcher = interval(120, () => { podsStore.loadKubeMetrics(this.props.owner.getNs()); }); private sortingCallbacks = { [sortBy.name]: (pod: Pod) => pod.getName(), [sortBy.namespace]: (pod: Pod) => pod.getNs(), [sortBy.cpu]: (pod: Pod) => podsStore.getPodKubeMetrics(pod).cpu, [sortBy.memory]: (pod: Pod) => podsStore.getPodKubeMetrics(pod).memory, } componentDidMount() { this.metricsWatcher.start(true); disposeOnUnmount(this, [ reaction(() => this.props.owner, () => this.metricsWatcher.restart(true)) ]) } componentWillUnmount() { this.metricsWatcher.stop(); } renderCpuUsage(id: string, usage: number) { const { maxCpu } = this.props; const value = usage.toFixed(3); const tooltip = (

CPU: {Math.ceil(usage * 100) / maxCpu}%
{usage.toFixed(3)}

); if (!maxCpu) { if (parseFloat(value) === 0) return 0; return value; } return ( ); } renderMemoryUsage(id: string, usage: number) { const { maxMemory } = this.props; const tooltip = (

Memory: {Math.ceil(usage * 100 / maxMemory)}%
{bytesToUnits(usage, 3)}

); if (!maxMemory) return usage ? bytesToUnits(usage) : 0; return ( ); } @autobind() getTableRow(uid: string) { const { pods } = this.props; const pod = pods.find(pod => pod.getId() == uid); const metrics = podsStore.getPodKubeMetrics(pod); return ( showDetails(pod.selfLink, false))} > {pod.getName()} {pod.hasIssues() && } {pod.getNs()} {this.renderCpuUsage(`cpu-${pod.getId()}`, metrics.cpu)} {this.renderMemoryUsage(`memory-${pod.getId()}`, metrics.memory)} {pod.getStatusMessage()} ); } render() { const { pods, showTitle } = this.props; const virtual = pods.length > 100; if (!pods.length && !podsStore.isLoaded) return (
); if (!pods.length) return null; return (
{showTitle && Pods}/>} Name Namespace CPU Memory Status { !virtual && pods.map(pod => this.getTableRow(pod.getId())) }
); } }