Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ Show uptime as percentage #45

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/components/Table/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { JSXElementConstructor, useState } from 'react'
import React, { JSXElementConstructor, useEffect, useState } from 'react'
import {
DataGrid,
GridColDef,
Expand All @@ -14,6 +14,7 @@
import CheckCircleOutlineIcon from '@mui/icons-material/CheckCircleOutline'
import ReportIcon from '@mui/icons-material/Report';
import CustomToolbar from '../Toolbar'
import axios from 'axios'

const getAllNetworks = (indexers: NodeData['indexer']): string => {
return indexers?.map((indexer) => indexer.network).join(', ')
Expand Down Expand Up @@ -50,6 +51,48 @@

return `${dayStr}${hourStr}${minuteStr}`.trim()
}
const formatUptimePercentage = (uptimeInSeconds: number, totalUptime: number): string => {
console.log('real uptimeInSeconds: ', uptimeInSeconds)
console.log('real totalUptime: ', totalUptime)

const uptimePercentage = (uptimeInSeconds / totalUptime) * 100;
console.log('real uptime percentage: ', uptimePercentage)
const percentage = uptimePercentage > 100 ? 100 : uptimePercentage;
return `${percentage.toFixed(2)}%`;
};

const UptimeCell: React.FC<{ uptimeInSeconds: number, lastCheck: number }> = ({ uptimeInSeconds, lastCheck}) => {
const [totalUptime, setTotalUptime] = useState<number | null>(null);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
const fetchTotalUptime = async () => {
try {
const response = await axios.get(`https://incentive-backend.oceanprotocol.com/weekStats?date=${(lastCheck/ 1000).toFixed(0)}`);
const data = response?.data
if (data && data.length > 0) {
setTotalUptime(data[0]._source.totalUptime);
} else {
throw new Error('Invalid API response');
}
} catch (error) {
setError('Failed to fetch uptime data');
}
};

fetchTotalUptime();
}, []);

Check warning on line 84 in src/components/Table/index.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'lastCheck'. Either include it or remove the dependency array

if (error) {
return <span>{error}</span>;
}

if (totalUptime === null) {
return <span>Loading...</span>;
}

return <span>{formatUptimePercentage(uptimeInSeconds, totalUptime)}</span>;
};

const getEligibleCheckbox = (eligible: boolean): React.ReactElement => {
return eligible ? (
Expand Down Expand Up @@ -95,12 +138,12 @@
},
{
field: 'uptime',
headerName: 'Eligible Week Uptime',
headerName: "Weekly Uptime",
sortable: true,
flex: 1,
minWidth: 150,
renderCell: (params: GridRenderCellParams<NodeData>) => (
<span>{formatUptime(params.row.uptime)}</span>
<UptimeCell uptimeInSeconds={params.row.uptime} lastCheck={params.row.lastCheck} />
)
},
{
Expand Down
Loading