@@ -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 */
150151export 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 } ) ;
0 commit comments