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

Update notebook execution timer #13366

Merged
merged 2 commits into from
Feb 8, 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
42 changes: 33 additions & 9 deletions packages/notebook/src/browser/view/notebook-code-cell-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,40 @@ export class NotebookCodeCellRenderer implements CellRenderer {

export interface NotebookCodeCellStatusProps {
cell: NotebookCellModel;
executionStateService: NotebookExecutionStateService
executionStateService: NotebookExecutionStateService;
}

export class NotebookCodeCellStatus extends React.Component<NotebookCodeCellStatusProps, { currentExecution?: CellExecution }> {
export interface NotebookCodeCellStatusState {
currentExecution?: CellExecution;
executionTime: number;
}

export class NotebookCodeCellStatus extends React.Component<NotebookCodeCellStatusProps, NotebookCodeCellStatusState> {

protected toDispose = new DisposableCollection();

constructor(props: NotebookCodeCellStatusProps) {
super(props);

this.state = {};
this.state = {
executionTime: 0
};

let currentInterval: NodeJS.Timeout | undefined;
this.toDispose.push(props.executionStateService.onDidChangeExecution(event => {
if (event.affectsCell(this.props.cell.uri)) {
this.setState({ currentExecution: event.changed });
this.setState({ currentExecution: event.changed, executionTime: 0 });
clearInterval(currentInterval);
if (event.changed?.state === NotebookCellExecutionState.Executing) {
const startTime = Date.now();
// The resolution of the time display is only a single digit after the decimal point.
// Therefore, we only need to update the display every 100ms.
currentInterval = setInterval(() => {
this.setState({
executionTime: Date.now() - startTime
});
}, 100);
}
}
}));
}
Expand Down Expand Up @@ -126,17 +145,22 @@ export class NotebookCodeCellStatus extends React.Component<NotebookCodeCellStat
{iconClasses &&
<>
<span className={`${iconClasses} notebook-cell-status-item`} style={{ color }}></span>
<div className='notebook-cell-status-item'>{this.getExecutionTime()}</div>
<div className='notebook-cell-status-item'>{this.renderTime(this.getExecutionTime())}</div>
</>}
</>;
}

private getExecutionTime(): string {
private getExecutionTime(): number {
const { runStartTime, runEndTime } = this.props.cell.internalMetadata;
if (runStartTime && runEndTime) {
return `${((runEndTime - runStartTime) / 1000).toLocaleString(undefined, { maximumFractionDigits: 1, minimumFractionDigits: 1 })}s`;
const { executionTime } = this.state;
if (runStartTime !== undefined && runEndTime !== undefined) {
return runEndTime - runStartTime;
}
return '0.0s';
return executionTime;
}

private renderTime(ms: number): string {
return `${(ms / 1000).toLocaleString(undefined, { maximumFractionDigits: 1, minimumFractionDigits: 1 })}s`;
}
}

Expand Down
Loading