import { DocumentTitle, K8sResourceCommon, k8sPatch, useK8sModel, useK8sWatchResource, } from '@openshift-console/dynamic-plugin-sdk'; import { Alert, Card, CardBody, CardTitle, PageSection, Spinner, Switch, Title, } from '@patternfly/react-core'; import type { FC } from 'react'; import { useState } from 'react'; import NowPlayingCard from './NowPlayingCard'; import GamingCard from './GamingCard'; const TUNED_NAMESPACE = 'openshift-cluster-node-tuning-operator'; const TUNED_NAME = 'custom-tuned-profile'; const POWERSAVE_PROFILE = 'homelab-powersave'; const PERFORMANCE_PROFILE = 'homelab-performance'; const TunedGVK = { group: 'tuned.openshift.io', version: 'v1', kind: 'Tuned', }; interface TunedResource extends K8sResourceCommon { spec?: { recommend?: Array<{ profile?: string; [key: string]: unknown; }>; }; } const DashboardPage: FC = () => { const [patching, setPatching] = useState(false); const [patchError, setPatchError] = useState(); const [tunedModel, modelLoading] = useK8sModel(TunedGVK); const [tuned, loaded, loadError] = useK8sWatchResource({ groupVersionKind: TunedGVK, name: TUNED_NAME, namespace: TUNED_NAMESPACE, }); const recommendations = tuned?.spec?.recommend ?? []; const profileIndex = recommendations.findIndex( (recommendation) => recommendation.profile === POWERSAVE_PROFILE || recommendation.profile === PERFORMANCE_PROFILE, ); const currentProfile = profileIndex >= 0 ? recommendations[profileIndex]?.profile : undefined; const performanceEnabled = currentProfile === PERFORMANCE_PROFILE; const profileKnown = currentProfile === POWERSAVE_PROFILE || currentProfile === PERFORMANCE_PROFILE; const changePerformanceMode = async (enabled: boolean) => { if (!tunedModel || !tuned || profileIndex < 0) { return; } setPatching(true); setPatchError(undefined); try { await k8sPatch({ model: tunedModel, resource: tuned, data: [ { op: 'replace', path: `/spec/recommend/${profileIndex}/profile`, value: enabled ? PERFORMANCE_PROFILE : POWERSAVE_PROFILE, }, ], }); } catch (error) { setPatchError( error instanceof Error ? error.message : String(error), ); } finally { setPatching(false); } }; return ( <> Dashboard Homelab Dashboard Cluster performance {!loaded || modelLoading ? ( ) : loadError ? ( {String(loadError)} ) : ( <> void changePerformanceMode(checked) } />

Current profile:{' '} {currentProfile ?? 'Unknown'}

{patching && } {!profileKnown && ( Expected {POWERSAVE_PROFILE} or{' '} {PERFORMANCE_PROFILE}. )} {patchError && ( {patchError} )} )}
); }; export default DashboardPage;