Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
add a timer with react hook to ensure the panel is refreshed - fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
echarles committed Feb 3, 2022
1 parent 7d6d580 commit 9a7f312
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ lib/
node_modules/
*.egg-info/
.ipynb_checkpoints
.vscode
*.tsbuildinfo
jupyterlab_kernel_usage/labextension

Expand Down
7 changes: 0 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ const plugin: JupyterFrontEndPlugin<void> = {
const { commands, shell } = app;
const category = 'Kernel Resource';

if (launcher) {
launcher.add({
command: CommandIDs.getKernelUsage,
category: category
});
}

async function createPanel(): Promise<KernelUsagePanel> {
const panel = new KernelUsagePanel({
widgetAdded: notebookTracker.widgetAdded,
Expand Down
25 changes: 25 additions & 0 deletions src/useInterval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useEffect, useRef } from 'react';

const useInterval = (callback: () => any, delay: number): void => {
const savedCallback = useRef<() => any>();

useEffect(() => {
savedCallback.current = callback;
}, [callback]);

useEffect(() => {
function tick() {
if (savedCallback.current) {
savedCallback.current();
}
}
if (delay !== null) {
const id = setInterval(tick, delay);
return () => {
clearInterval(id);
};
}
}, [callback, delay]);
};

export default useInterval;
21 changes: 19 additions & 2 deletions src/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { IChangedArgs } from '@jupyterlab/coreutils';
import { Kernel } from '@jupyterlab/services';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { requestAPI } from './handler';
import useInterval from './useInterval';

type Usage = {
timestamp: Date | null;
kernelId: string;
kernel_cpu: number;
kernel_memory: number;
host_cpu_percent: number;
Expand All @@ -24,6 +27,8 @@ type Usage = {
};

const UNKONWN_USAGE: Usage = {
timestamp: null,
kernelId: '',
kernel_cpu: -1,
kernel_memory: -1,
host_cpu_percent: -1,
Expand Down Expand Up @@ -63,13 +68,22 @@ const KernelUsage = (props: {
currentNotebookChanged: ISignal<INotebookTracker, NotebookPanel | null>;
}) => {
const [kernelId, setKernelId] = useState<string>();
const [refresh, setRefresh] = useState<boolean>(true);

useInterval(async () => {
setRefresh(!refresh);
}, POLL_INTERVAL_SEC * 1000);

const requestUsage = async (kernelId: string) => {
requestAPI<any>(`get_usage/${kernelId}`)
.then(data => {
const kernelPoll = kernelPools.get(kernelId);
if (kernelPoll) {
kernelPoll.usage = data.content;
kernelPoll.usage = {
...data.content,
kernelId,
timestamp: new Date()
};
kernelPools.set(kernelId, kernelPoll);
}
})
Expand All @@ -96,7 +110,7 @@ const KernelUsage = (props: {
max: POLL_MAX_INTERVAL_SEC * 1000
},
name: `@jupyterlab/kernel:KernelUsage#${kernelId}`,
standby: 'when-hidden'
standby: 'never'
});
kernelPoll = {
poll,
Expand Down Expand Up @@ -135,6 +149,9 @@ const KernelUsage = (props: {
<>
<h3 className="jp-kernelusage-separator">Kernel Usage</h3>
<div className="jp-kernelusage-separator">Kernel ID: {kernelId}</div>
<div className="jp-kernelusage-separator">
Timestamp: {kernelId && getUsage(kernelId).timestamp?.toLocaleString()}
</div>
<div className="jp-kernelusage-separator">
CPU: {kernelId && getUsage(kernelId).kernel_cpu}
</div>
Expand Down

0 comments on commit 9a7f312

Please sign in to comment.