-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add status popover when storage is nearly full
- Loading branch information
1 parent
ae1c98b
commit 8dadace
Showing
11 changed files
with
255 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
frontend/src/pages/projects/screens/detail/storage/StorageWarningStatus.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import * as React from 'react'; | ||
import { Bullseye, Button, Icon, Popover } from '@patternfly/react-core'; | ||
import { | ||
ExclamationCircleIcon, | ||
ExclamationTriangleIcon, | ||
InfoCircleIcon, | ||
} from '@patternfly/react-icons'; | ||
import { PersistentVolumeClaimKind } from '../../../../../k8sTypes'; | ||
import { usePVCFreeAmount } from '../../../../../api'; | ||
import { getPvcTotalSize } from '../../../utils'; | ||
import { bytesAsGB } from '../../../../../utilities/number'; | ||
import { getFullStatusFromPercentage } from './utils'; | ||
import useStorageStatusAlert from './useStorageStatusAlert'; | ||
|
||
type StorageWarningStatusProps = { | ||
obj: PersistentVolumeClaimKind; | ||
onAddPVC: () => void; | ||
onEditPVC: (pvc: PersistentVolumeClaimKind) => void; | ||
}; | ||
|
||
const StorageWarningStatus: React.FC<StorageWarningStatusProps> = ({ | ||
obj, | ||
onEditPVC, | ||
onAddPVC, | ||
}) => { | ||
const [inUseInBytes, loaded] = usePVCFreeAmount(obj); | ||
const percentage = loaded | ||
? Number(((bytesAsGB(inUseInBytes) / parseFloat(getPvcTotalSize(obj))) * 100).toFixed(2)) | ||
: NaN; | ||
useStorageStatusAlert(obj, percentage); | ||
|
||
const percentageStatus = getFullStatusFromPercentage(percentage); | ||
|
||
if (!percentageStatus) { | ||
return null; | ||
} | ||
|
||
let renderIcon: React.ReactNode; | ||
let popoverHeader: React.ReactNode; | ||
let popoverBody: React.ReactNode; | ||
if (percentageStatus === 'error') { | ||
renderIcon = ( | ||
<Icon status="danger"> | ||
<ExclamationCircleIcon /> | ||
</Icon> | ||
); | ||
popoverHeader = `Storage full`; | ||
popoverBody = 'Save actions will fail and you can no longer write new data.'; | ||
} else { | ||
popoverHeader = `Storage ${percentage}% full`; | ||
popoverBody = | ||
'Once storage is 100% full, you will no longer be able to write new data and save actions will fail.'; | ||
if (percentageStatus === 'warning') { | ||
renderIcon = ( | ||
<Icon status="warning"> | ||
<ExclamationTriangleIcon /> | ||
</Icon> | ||
); | ||
} else { | ||
renderIcon = ( | ||
<Icon status="info"> | ||
<InfoCircleIcon /> | ||
</Icon> | ||
); | ||
} | ||
} | ||
|
||
return ( | ||
<Popover | ||
position="right" | ||
headerIcon={<Bullseye>{renderIcon}</Bullseye>} | ||
headerContent={popoverHeader} | ||
bodyContent={popoverBody} | ||
footerContent={(hide) => ( | ||
<> | ||
To increase available storage, delete files,{' '} | ||
<Button | ||
isInline | ||
variant="link" | ||
onClick={() => { | ||
hide(); | ||
onEditPVC(obj); | ||
}} | ||
> | ||
edit storage size | ||
</Button> | ||
, or{' '} | ||
<Button | ||
isInline | ||
variant="link" | ||
onClick={() => { | ||
hide(); | ||
onAddPVC(); | ||
}} | ||
> | ||
add new cluster storage | ||
</Button> | ||
. | ||
</> | ||
)} | ||
removeFindDomNode | ||
> | ||
<Bullseye>{renderIcon}</Bullseye> | ||
</Popover> | ||
); | ||
}; | ||
|
||
export default StorageWarningStatus; |
61 changes: 61 additions & 0 deletions
61
frontend/src/pages/projects/screens/detail/storage/useStorageStatusAlert.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as React from 'react'; | ||
import { useBrowserStorage } from '../../../../../components/browserStorage'; | ||
import { PersistentVolumeClaimKind } from '../../../../../k8sTypes'; | ||
import useNotification from '../../../../../utilities/useNotification'; | ||
import { getPvcDisplayName } from '../../../utils'; | ||
import { getFullStatusFromPercentage } from './utils'; | ||
|
||
type PercentageStorageStatus = { | ||
[projectName: string]: { | ||
/** string value is percentage full status of the PVC since last check */ | ||
[pvcName: string]: string; | ||
}; | ||
}; | ||
|
||
const useStorageStatusAlert = (pvc: PersistentVolumeClaimKind, percentageFull: number): void => { | ||
const notification = useNotification(); | ||
|
||
const [percentageStorageStatuses, setPercentageStorageStatuses] = | ||
useBrowserStorage<PercentageStorageStatus>( | ||
'odh.dashboard.pvc.last.notification', | ||
{}, | ||
true, | ||
true, | ||
); | ||
|
||
const lastStatus = percentageStorageStatuses[pvc.metadata.namespace]?.[pvc.metadata.name]; | ||
|
||
React.useEffect(() => { | ||
const currentStatus = getFullStatusFromPercentage(percentageFull); | ||
if (!isNaN(percentageFull)) { | ||
if (lastStatus !== currentStatus) { | ||
if (currentStatus) { | ||
notification[currentStatus]( | ||
`Cluster storage "${getPvcDisplayName(pvc)}" is ${percentageFull}% full`, | ||
); | ||
const newValue = { | ||
...percentageStorageStatuses, | ||
[pvc.metadata.namespace]: { | ||
...percentageStorageStatuses[pvc.metadata.namespace], | ||
[pvc.metadata.name]: currentStatus, | ||
}, | ||
}; | ||
setPercentageStorageStatuses(newValue); | ||
} else if (lastStatus) { | ||
const newValue = Object.assign({}, percentageStorageStatuses); | ||
delete newValue[pvc.metadata.namespace]?.[pvc.metadata.name]; | ||
setPercentageStorageStatuses(newValue); | ||
} | ||
} | ||
} | ||
}, [ | ||
lastStatus, | ||
percentageFull, | ||
notification, | ||
pvc, | ||
percentageStorageStatuses, | ||
setPercentageStorageStatuses, | ||
]); | ||
}; | ||
|
||
export default useStorageStatusAlert; |
Oops, something went wrong.