Skip to content

Commit

Permalink
feat: Uptime component (#2240)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleixhub authored Oct 30, 2024
1 parent 436c1e4 commit 45d3d3e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
5 changes: 4 additions & 1 deletion catalog/ui/src/app/Catalog/CatalogItemDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import useHelpLink from '@app/utils/useHelpLink';
import useSWRImmutable from 'swr/immutable';

import './catalog-item-details.css';
import UptimeDisplay from '@app/components/UptimeDisplay';

enum CatalogItemAccess {
Allow,
Expand Down Expand Up @@ -379,7 +380,9 @@ const CatalogItemDetails: React.FC<{ catalogItem: CatalogItem; onClose: () => vo
<DescriptionListGroup className="catalog-item-details__uptime">
<DescriptionListTerm>Uptime</DescriptionListTerm>
<DescriptionListDescription>
{`${catalogItemIncident ? calculateUptimePercentage(catalogItemIncident.downtime_hours) : 100}%`}
<UptimeDisplay
uptime={catalogItemIncident ? calculateUptimePercentage(catalogItemIncident.downtime_hours) : 100}
/>
</DescriptionListDescription>
</DescriptionListGroup>

Expand Down
48 changes: 48 additions & 0 deletions catalog/ui/src/app/components/UptimeDisplay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';

interface UptimeDisplayProps {
uptime: number;
}

const UptimeDisplay: React.FC<UptimeDisplayProps> = ({ uptime }) => {
// Function to get color based on uptime percentage
const getColor = (): string => {
if (uptime >= 99) return '#4caf50'; // Green for high uptime
if (uptime >= 95) return '#ff9800'; // Orange for moderate uptime
return '#f44336'; // Red for low uptime
};

return (
<div style={styles.container}>
<div
style={{
...styles.progressBar,
backgroundColor: getColor(),
width: `${uptime}%`,
}}
/>
<p>{Math.round(uptime)}%</p>
</div>
);
};

// Styles object with type annotation
const styles: { [key: string]: React.CSSProperties } = {
container: {
width: '100%',
maxWidth: '400px',
margin: '2px auto',
textAlign: 'center',
display: 'flex',
flexDirection: 'row',
gap: '6px',
paddingRight: '8px'
},
progressBar: {
height: '20px',
borderRadius: '5px',
transition: 'width 0.5s ease',
},
};

export default UptimeDisplay;
2 changes: 1 addition & 1 deletion catalog/ui/src/app/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,5 @@ export function calculateUptimePercentage(downtimeHours: number) {
const uptimeHours = Math.max(0, totalTimeHours - downtimeHours);
const uptimePercentage = (uptimeHours / totalTimeHours) * 100;

return uptimePercentage.toFixed(2);
return uptimePercentage;
}

0 comments on commit 45d3d3e

Please sign in to comment.