@@ -18,14 +18,25 @@ import { filterHolidaysFromMetrics } from '@/utils/dateUtils';
1818import { getMetricsData as getLegacyMetricsData } from './metrics-util' ;
1919import { getMetricsByDateRange , getReportDataByDateRange , saveMetrics } from '../../server/storage/metrics-storage' ;
2020import { fetchLatestReport , type MetricsReportRequest } from '../../server/services/github-copilot-usage-api' ;
21- import { transformReportToMetrics } from '../../server/services/report-transformer' ;
21+ import {
22+ sortCopilotMetricsByDate ,
23+ sortReportDayTotalsByDay ,
24+ transformReportToMetrics ,
25+ } from '../../server/services/report-transformer' ;
2226import { isMockMode } from '../../server/services/github-copilot-usage-api-mock' ;
2327
2428export interface MetricsDataResult {
2529 metrics : CopilotMetrics [ ] ;
2630 reportData : ReportDayTotals [ ] ;
2731}
2832
33+ function sortMetricsDataResult ( result : MetricsDataResult ) : MetricsDataResult {
34+ return {
35+ metrics : sortCopilotMetricsByDate ( result . metrics ) ,
36+ reportData : sortReportDayTotalsByDay ( result . reportData ) ,
37+ } ;
38+ }
39+
2940/**
3041 * Returns true ONLY when USE_LEGACY_API is explicitly set to "true".
3142 * Default behavior is new API — no legacy calls unless opted in.
@@ -77,7 +88,7 @@ async function fetchFromNewApi(
7788 } ) ;
7889 }
7990
80- return { metrics, reportData } ;
91+ return sortMetricsDataResult ( { metrics, reportData } ) ;
8192}
8293
8394/**
@@ -106,15 +117,15 @@ export async function getMetricsDataV2(event: H3Event<EventHandlerRequest>): Pro
106117 if ( isLegacyMode ( ) ) {
107118 logger . info ( 'Using mocked data mode (legacy format — USE_LEGACY_API=true)' ) ;
108119 const metrics = await getLegacyMetricsData ( event ) ;
109- return { metrics, reportData : [ ] } ;
120+ return sortMetricsDataResult ( { metrics, reportData : [ ] } ) ;
110121 }
111122 // Default: exercise full new-API mock pipeline
112123 logger . info ( 'Using mocked data mode (new API format via HTTP download)' ) ;
113124 const identifier = options . githubOrg || options . githubEnt || 'mock-org' ;
114125 const scope = ( options . scope || 'organization' ) as MetricsReportRequest [ 'scope' ] ;
115126 const report = await fetchLatestReport ( { scope, identifier } , new Headers ( ) ) ;
116127 const metrics = transformReportToMetrics ( report ) ;
117- return { metrics, reportData : report . day_totals } ;
128+ return sortMetricsDataResult ( { metrics, reportData : report . day_totals } ) ;
118129 }
119130
120131 const identifier = options . githubOrg || options . githubEnt || '' ;
@@ -146,7 +157,7 @@ export async function getMetricsDataV2(event: H3Event<EventHandlerRequest>): Pro
146157 options . excludeHolidays || false ,
147158 options . locale
148159 ) ;
149- return { metrics : filteredMetrics , reportData } ;
160+ return sortMetricsDataResult ( { metrics : filteredMetrics , reportData } ) ;
150161 }
151162
152163 // DB empty — sync on miss: fetch from API, store, return
@@ -165,7 +176,7 @@ export async function getMetricsDataV2(event: H3Event<EventHandlerRequest>): Pro
165176 options . excludeHolidays || false ,
166177 options . locale
167178 ) ;
168- return { metrics : filteredMetrics , reportData : result . reportData } ;
179+ return sortMetricsDataResult ( { metrics : filteredMetrics , reportData : result . reportData } ) ;
169180 } catch ( error ) {
170181 // If it's already an H3 error (like 401), re-throw
171182 if ( error && typeof error === 'object' && 'statusCode' in error ) throw error ;
@@ -189,15 +200,18 @@ export async function getMetricsDataV2(event: H3Event<EventHandlerRequest>): Pro
189200 if ( isLegacyMode ( ) ) {
190201 logger . info ( 'Using legacy Copilot Metrics API (USE_LEGACY_API=true, direct, no DB)' ) ;
191202 const metrics = await getLegacyMetricsData ( event ) ;
192- return { metrics : filterHolidaysFromMetrics ( metrics , options . excludeHolidays || false , options . locale ) , reportData : [ ] } ;
203+ return sortMetricsDataResult ( {
204+ metrics : filterHolidaysFromMetrics ( metrics , options . excludeHolidays || false , options . locale ) ,
205+ reportData : [ ]
206+ } ) ;
193207 }
194208
195209 logger . info ( 'Using new Copilot Metrics API (direct, no DB)' ) ;
196210 const result = await fetchFromNewApi ( options , event . context . headers ) ;
197- return {
211+ return sortMetricsDataResult ( {
198212 metrics : filterHolidaysFromMetrics ( result . metrics , options . excludeHolidays || false , options . locale ) ,
199213 reportData : result . reportData
200- } ;
214+ } ) ;
201215}
202216
203217/**
@@ -254,5 +268,5 @@ async function fetchAndStore(
254268 } ) ;
255269 }
256270
257- return { metrics, reportData } ;
271+ return sortMetricsDataResult ( { metrics, reportData } ) ;
258272}
0 commit comments