import { Loader, NameValueTable, SectionBox, SectionHeader, SimpleTable, StatusLabel, } from '@kinvolk/headlamp-plugin/lib/CommonComponents'; import React from 'react'; import { useParams } from 'react-router-dom'; import { computeScore, countResultsForItems, filterResultsByNamespace, getPolarisProxyUrl, Result, ResultCounts, } from '../api/polaris'; import { usePolarisDataContext } from '../api/PolarisDataContext'; function scoreStatus(score: number): 'success' | 'warning' | 'error' { if (score >= 80) return 'success'; if (score >= 50) return 'warning'; return 'error'; } function resourceCounts(result: Result): ResultCounts { return countResultsForItems([result]); } export default function NamespaceDetailView() { const { namespace } = useParams<{ namespace: string }>(); const { data, loading, error } = usePolarisDataContext(); if (loading) { return ; } if (error) { return ( <> {error}, }, ]} /> ); } if (!data) { return ( <> ); } const results = filterResultsByNamespace(data, namespace); const counts = countResultsForItems(results); const score = computeScore(counts); const status = scoreStatus(score); const countsPerResource = new Map(); for (const r of results) { countsPerResource.set(`${r.Namespace}/${r.Kind}/${r.Name}`, resourceCounts(r)); } function getResourceCounts(row: Result): ResultCounts { return countsPerResource.get(`${row.Namespace}/${row.Kind}/${row.Name}`) ?? resourceCounts(row); } return ( <> View in Polaris Dashboard ), }, ]} /> {score}%, }, { name: 'Total Checks', value: String(counts.total) }, { name: 'Pass', value: {counts.pass}, }, { name: 'Warning', value: {counts.warning}, }, { name: 'Danger', value: {counts.danger}, }, { name: 'Skipped', value: ( {counts.skipped} ), }, ]} /> row.Name }, { label: 'Kind', getter: (row: Result) => row.Kind }, { label: 'Pass', getter: (row: Result) => ( {getResourceCounts(row).pass} ), }, { label: 'Warning', getter: (row: Result) => ( {getResourceCounts(row).warning} ), }, { label: 'Danger', getter: (row: Result) => ( {getResourceCounts(row).danger} ), }, ]} data={results} emptyMessage={`No resources found in namespace "${namespace}".`} /> ); }