-
Notifications
You must be signed in to change notification settings - Fork 5
Display estimated time remaining for simulation dataset download #2304
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -12,7 +12,7 @@ import { | |||
| Typography, | ||||
| useTheme | ||||
| } from '@mui/material'; | ||||
| import { useState } from 'react'; | ||||
| import { useEffect,useState } from 'react'; | ||||
|
|
||||
| import { | ||||
| DatasetDownloadStatus, | ||||
|
|
@@ -28,6 +28,21 @@ export enum Geant4DatasetsType { | |||
| FULL | ||||
| } | ||||
|
|
||||
| function formatTime(seconds: number): string { | ||||
| if (seconds < 1) { | ||||
| return '<1s'; | ||||
| } | ||||
|
|
||||
| if (seconds < 60) { | ||||
| return `${Math.ceil(seconds)}s`; | ||||
| } | ||||
|
|
||||
| const minutes = Math.floor(seconds / 60); | ||||
| const remainingSeconds = Math.ceil(seconds % 60); | ||||
|
|
||||
| return `${minutes}m ${remainingSeconds}s`; | ||||
| } | ||||
|
|
||||
| export interface Geant4DatasetsProps { | ||||
| geant4DownloadManagerState: DownloadManagerStatus; | ||||
| geant4DatasetStates: DatasetStatus[]; | ||||
|
|
@@ -36,14 +51,91 @@ export interface Geant4DatasetsProps { | |||
| setGeant4DatasetType: (type: Geant4DatasetsType) => void; | ||||
| } | ||||
|
|
||||
| interface SpeedHistory { | ||||
| lastDone: number; | ||||
| lastTime: number; | ||||
| currentSpeed: number; | ||||
| } | ||||
|
|
||||
| const SMOOTHING = 0.1; | ||||
|
|
||||
| function DatasetCurrentStatus(props: { status: DatasetStatus }) { | ||||
| const { status } = props; | ||||
|
|
||||
| const [speedHistory, setSpeedHistory] = useState<SpeedHistory>({ | ||||
| lastDone: status.done ?? 0, | ||||
| lastTime: Date.now(), | ||||
| currentSpeed: 0 | ||||
| }); | ||||
|
|
||||
| useEffect(() => { | ||||
| const currentDone = status.done ?? 0; | ||||
| const currentTime = Date.now(); | ||||
|
|
||||
| if (status.status === DatasetDownloadStatus.DOWNLOADING) { | ||||
| setSpeedHistory(prev => { | ||||
| const timeDelta = (currentTime - prev.lastTime) / 1000; | ||||
| const bytesDelta = currentDone - prev.lastDone; | ||||
|
|
||||
| if (bytesDelta > 0 && timeDelta > 0) { | ||||
| const instSpeed = bytesDelta / timeDelta; | ||||
|
|
||||
| const newSpeed = | ||||
| prev.currentSpeed === 0 | ||||
| ? instSpeed | ||||
| : prev.currentSpeed * (1 - SMOOTHING) + instSpeed * SMOOTHING; | ||||
|
|
||||
| return { | ||||
| lastDone: currentDone, | ||||
| lastTime: currentTime, | ||||
| currentSpeed: newSpeed | ||||
| }; | ||||
| } | ||||
|
|
||||
| return { | ||||
| ...prev, | ||||
| lastTime: currentTime | ||||
| }; | ||||
| }); | ||||
| } | ||||
|
|
||||
| if ( | ||||
| status.status === DatasetDownloadStatus.DONE || | ||||
| status.status === DatasetDownloadStatus.IDLE | ||||
| ) { | ||||
| setSpeedHistory({ | ||||
| lastDone: status.done ?? 0, | ||||
| lastTime: Date.now(), | ||||
| currentSpeed: 0 | ||||
| }); | ||||
| } | ||||
| }, [status.done, status.status]); | ||||
|
|
||||
| const remainingBytes = (status.total ?? 0) - (status.done ?? 0); | ||||
| let estimatedTimeRemaining = ''; | ||||
|
|
||||
| if ( | ||||
| status.status === DatasetDownloadStatus.DOWNLOADING && | ||||
| speedHistory.currentSpeed > 0 && | ||||
| remainingBytes > 0 | ||||
| ) { | ||||
| const remainingSeconds = remainingBytes / speedHistory.currentSpeed; | ||||
| estimatedTimeRemaining = ` (${formatTime(remainingSeconds)})`; | ||||
| } | ||||
|
|
||||
| return ( | ||||
| <Box sx={{ pb: 1 }}> | ||||
| <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}> | ||||
| <Typography sx={{ flex: 1 }}>{status.name}</Typography> | ||||
|
|
||||
| {status.status === DatasetDownloadStatus.DONE && <CheckIcon color='success' />} | ||||
| {status.status === DatasetDownloadStatus.DOWNLOADING && ( | ||||
| <Typography | ||||
| fontSize={12} | ||||
| color='text.secondary'> | ||||
| {estimatedTimeRemaining} | ||||
| </Typography> | ||||
| )} | ||||
| </Box> | ||||
|
|
||||
| <Box sx={{ mt: 1 }}> | ||||
|
|
@@ -57,6 +149,7 @@ function DatasetCurrentStatus(props: { status: DatasetStatus }) { | |||
| {status.status === DatasetDownloadStatus.PROCESSING && ( | ||||
| <LinearProgress | ||||
| variant='indeterminate' | ||||
| value={status.total ? ((status.done ?? 0) / status.total) * 100 : 0} | ||||
|
||||
| value={status.total ? ((status.done ?? 0) / status.total) * 100 : 0} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after comma in the import statement. Should be
import { useEffect, useState } from 'react';to maintain consistency with the rest of the codebase where all imports have spaces after commas.