Skip to content

Commit f1891ec

Browse files
authored
Fix calculation of metrics (#67)
1 parent ee98df0 commit f1891ec

File tree

3 files changed

+15
-155
lines changed

3 files changed

+15
-155
lines changed

coder/cli_scripts/suspend_users.sh

Lines changed: 0 additions & 147 deletions
This file was deleted.

services/analytics/lib/metrics.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,30 @@ function classifyActivityStatus(daysSinceActive: number): ActivityStatus {
146146

147147
/**
148148
* Extract the most recent last_connected_at timestamp from workspace agents
149+
* Falls back to last_used_at (from Coder API) if no agent data is available
149150
*/
150151
export function getLastActiveTimestamp(workspace: CoderWorkspace): string {
151152
let mostRecent = workspace.created_at;
152153

153154
try {
154155
const resources = workspace.latest_build?.resources || [];
156+
let foundAgentData = false;
157+
155158
for (const resource of resources) {
156159
const agents = resource.agents || [];
157160
for (const agent of agents) {
158161
if (agent.last_connected_at && agent.last_connected_at > mostRecent) {
159162
mostRecent = agent.last_connected_at;
163+
foundAgentData = true;
160164
}
161165
}
162166
}
167+
168+
// If no agent data found (e.g., workspace is stopped), use last_used_at
169+
// This field is provided by Coder API and tracks the last workspace usage
170+
if (!foundAgentData && workspace.last_used_at && workspace.last_used_at > mostRecent) {
171+
mostRecent = workspace.last_used_at;
172+
}
163173
} catch (error) {
164174
console.warn(`Error extracting last active timestamp for workspace ${workspace.id}:`, error);
165175
}
@@ -357,16 +367,12 @@ export function aggregateByTeam(
357367
const avgWorkspaceHours =
358368
teamWorkspaces.length > 0 ? currentWorkspaceHours / teamWorkspaces.length : 0;
359369

360-
// Calculate active days (unique dates when workspaces were active)
370+
// Calculate active days (unique dates in last 7 days when a workspace was active)
371+
// Only count workspaces with activity_status === 'active', consistent with unique_active_users
361372
const activeDates = new Set<string>();
362373
teamWorkspaces.forEach((workspace) => {
363-
// Add creation date
364-
const createdDate = new Date(workspace.created_at).toISOString().split('T')[0];
365-
activeDates.add(createdDate);
366-
367-
// Add last active date if different from created
368-
const lastActiveDate = new Date(workspace.last_active).toISOString().split('T')[0];
369-
if (lastActiveDate !== createdDate) {
374+
if (workspace.activity_status === 'active') {
375+
const lastActiveDate = new Date(workspace.last_active).toISOString().split('T')[0];
370376
activeDates.add(lastActiveDate);
371377
}
372378
});

services/analytics/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface CoderWorkspace {
44
id: string;
55
created_at: string;
66
updated_at: string;
7+
last_used_at: string; // Last time the workspace was used (from Coder API)
78
owner_id: string;
89
owner_name: string; // This is the GitHub username!
910
owner_avatar_url?: string;

0 commit comments

Comments
 (0)