Skip to content
Open
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
84 changes: 79 additions & 5 deletions patches/sagemaker-idle-extension.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-idle-extension/README.m
@@ -0,0 +1,3 @@
+# Code Editor Idle Extension
+
+The Code Editor Idle Extension tracks user activity and logs the last active timestamp (in UTC) to a local file. User activities monitored include file changes, text editor selection changes, and terminal interactions. Additionally, it provides an API endpoint `/api/idle` that returns the lastActiveTimestamp.
+The Code Editor Idle Extension tracks user activity and logs the last active timestamp (in UTC) to a local file. User activities monitored include file changes, text editor selection changes, terminal interactions, background processes, notebook kernel activity, and unsaved work. Additionally, it provides an API endpoint `/api/idle` that returns the lastActiveTimestamp.
\ No newline at end of file
Index: sagemaker-code-editor/vscode/extensions/sagemaker-idle-extension/extension-browser.webpack.config.js
===================================================================
Expand Down Expand Up @@ -147,19 +147,25 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-idle-extension/src/exte
===================================================================
--- /dev/null
+++ sagemaker-code-editor/vscode/extensions/sagemaker-idle-extension/src/extension.ts
@@ -0,0 +1,58 @@
@@ -0,0 +1,113 @@
+import * as vscode from "vscode";
+import * as fs from "fs";
+import * as path from "path";
+
+let idleFilePath: string;
+let checkInterval: NodeJS.Timeout;
+
+export function activate(context: vscode.ExtensionContext) {
+ initializeIdleFilePath();
+ registerEventListeners(context);
+ startPeriodicChecks();
+}
+
+export function deactivate() {}
+export function deactivate() {
+ if (checkInterval) {
+ clearInterval(checkInterval);
+ }
+}
+
+/**
+ * Initializes the file path where the idle timestamp will be stored.
Expand All @@ -175,35 +181,102 @@ Index: sagemaker-code-editor/vscode/extensions/sagemaker-idle-extension/src/exte
+
+/**
+ * Registers event listeners to monitor user activity within the VSCode editor.
+ * It listens to document changes, editor focus changes, text selection changes, and terminal events.
+ * It listens to document changes, editor focus changes, text selection changes, terminal events,
+ * task execution, and notebook kernel state changes.
+ * @param context - The context in which the extension is running.
+ */
+function registerEventListeners(context: vscode.ExtensionContext) {
+ context.subscriptions.push(
+ vscode.workspace.onDidChangeTextDocument((_) => {
+ console.log('[IdleMonitor] Text document changed');
+ updateLastActivityTimestamp();
+ }),
+ vscode.window.onDidChangeActiveTextEditor((_) => {
+ console.log('[IdleMonitor] Active text editor changed');
+ updateLastActivityTimestamp();
+ }),
+ vscode.window.onDidChangeTextEditorSelection((_) => {
+ console.log('[IdleMonitor] Text editor selection changed');
+ updateLastActivityTimestamp();
+ }),
+ vscode.window.onDidOpenTerminal((_) => {
+ console.log('[IdleMonitor] Terminal opened');
+ updateLastActivityTimestamp();
+ }),
+ vscode.window.onDidCloseTerminal((_) => {
+ console.log('[IdleMonitor] Terminal closed');
+ updateLastActivityTimestamp();
+ }),
+ vscode.tasks.onDidStartTask((_) => {
+ console.log('[IdleMonitor] Task started');
+ updateLastActivityTimestamp();
+ }),
+ vscode.tasks.onDidEndTask((_) => {
+ console.log('[IdleMonitor] Task ended');
+ updateLastActivityTimestamp();
+ }),
+ vscode.workspace.onDidChangeNotebookDocument((_) => {
+ console.log('[IdleMonitor] Notebook document changed');
+ updateLastActivityTimestamp();
+ })
+ );
+}
+
+/**
+ * Starts periodic checks for background activity (running tasks, executing kernels, unsaved work).
+ */
+function startPeriodicChecks() {
+ checkInterval = setInterval(() => {
+ const activity = hasBackgroundActivity();
+ console.log('[IdleMonitor] Periodic check - background activity:', activity);
+ if (activity) {
+ updateLastActivityTimestamp();
+ }
+ }, 60000); // Check every 60 seconds
+}
+
+/**
+ * Checks if there is any background activity that should prevent idle state.
+ * @returns true if there are running tasks, executing notebook kernels, or unsaved work.
+ */
+function hasBackgroundActivity(): boolean {
+ // Check for running tasks
+ const hasActiveTasks = vscode.tasks.taskExecutions.length > 0;
+ console.log('[IdleMonitor] Active tasks:', hasActiveTasks);
+
+ let hasExecutingKernels = false;
+ let hasUnsavedNotebooks = false;
+
+ // Check for executing notebook kernels or unsaved work
+ for (const notebook of vscode.workspace.notebookDocuments) {
+ // Check if any cell is executing
+ for (const cell of notebook.getCells()) {
+ if (cell.executionSummary?.executionOrder !== undefined &&
+ cell.executionSummary.timing?.endTime === undefined) {
+ hasExecutingKernels = true;
+ break;
+ }
+ }
+ // Check if notebook has unsaved changes
+ if (notebook.isDirty) {
+ hasUnsavedNotebooks = true;
+ }
+ }
+
+ const hasUnsavedText = vscode.workspace.textDocuments.some(doc => doc.isDirty && doc.uri.scheme === 'file');
+
+ console.log('[IdleMonitor] Executing kernels:', hasExecutingKernels, 'Unsaved notebooks:', hasUnsavedNotebooks, 'Unsaved text:', hasUnsavedText);
+
+ return hasActiveTasks || hasExecutingKernels || hasUnsavedNotebooks || hasUnsavedText;
+}
+
+/**
+ * Updates the last activity timestamp by recording the current timestamp in the idle file and
+ * refreshing the status bar. The timestamp should be in ISO 8601 format and set to the UTC timezone.
+ */
+function updateLastActivityTimestamp() {
+ const timestamp = new Date().toISOString();
+ console.log('[IdleMonitor] Updating timestamp:', timestamp);
+ fs.writeFileSync(idleFilePath, timestamp);
+}
\ No newline at end of file
Expand Down Expand Up @@ -278,11 +351,12 @@ Index: sagemaker-code-editor/vscode/src/vs/server/node/webClientServer.ts
+ return now.getTime() - mtime < CHECK_INTERVAL;
+ });
+
+ console.log('[IdleMonitor] Terminal activity detected:', activityDetected);
+ if (activityDetected) {
+ fs.writeFileSync(idleFilePath, now.toISOString());
+ }
+ } catch (err) {
+ console.error('Error checking terminal activity:', err);
+ console.error('[IdleMonitor] Error checking terminal activity:', err);
+ }
+}
+
Expand Down