@@ -7,32 +7,40 @@ declare global {
77 interface Chainable {
88 mockIncidents ( incidents : IncidentDefinition [ ] ) : Chainable < Element > ;
99 mockIncidentFixture ( fixturePath : string ) : Chainable < Element > ;
10+ transformMetrics ( ) : Chainable < Element > ;
1011 }
1112 }
1213}
1314
15+ export const NEW_METRIC_NAME = 'cluster_health_components_map' ;
16+ export const OLD_METRIC_NAME = 'cluster:health:components:map' ;
17+ const MOCK_QUERY = '/api/prometheus/api/v1/query_range*' ;
18+
1419/**
1520 * Main mocking function - sets up cy.intercept for Prometheus query_range API
1621 * Intercepts the query_range API and returns the mock data for the incidents
1722 * @param incidents
1823 */
1924export function mockPrometheusQueryRange ( incidents : IncidentDefinition [ ] ) : void {
20- cy . intercept ( 'GET' , '/api/prometheus/api/v1/query_range*' , ( req ) => {
25+ cy . intercept ( 'GET' , MOCK_QUERY , ( req ) => {
2126 const url = new URL ( req . url , window . location . origin ) ;
2227 const query = url . searchParams . get ( 'query' ) || '' ;
2328
2429 console . log ( `INTERCEPTED: ${ req . method } ${ req . url } ` ) ;
2530 console . log ( `Query: ${ query } ` ) ;
2631
2732 let results : any [ ] ;
33+
34+ const versioned_metric = query . includes ( NEW_METRIC_NAME )
35+ ? NEW_METRIC_NAME : OLD_METRIC_NAME ;
2836
29- if ( ! ( query . includes ( 'cluster:health:components:map' ) || query . includes ( 'ALERTS{' ) ) ) {
37+ if ( ! ( query . includes ( versioned_metric ) || query . includes ( 'ALERTS{' ) ) ) {
3038 console . log ( `Passing through non-mocked query` ) ;
3139 req . continue ( ) ;
3240 return ;
3341 }
3442
35- results = query . includes ( 'cluster:health:components:map' ) ? createIncidentMock ( incidents , query ) : createAlertDetailsMock ( incidents , query ) ;
43+ results = query . includes ( versioned_metric ) ? createIncidentMock ( incidents , query ) : createAlertDetailsMock ( incidents , query ) ;
3644 const response : PrometheusResponse = {
3745 status : 'success' ,
3846 data : {
@@ -85,4 +93,46 @@ Cypress.Commands.add('mockIncidentFixture', (fixturePath: string) => {
8593
8694 // The mocking is not applied until the page is reloaded and the components fetch the new data
8795 cy . reload ( ) ;
96+ } ) ;
97+
98+ Cypress . Commands . add ( 'transformMetrics' , ( ) => {
99+ cy . log ( '=== SETTING UP METRIC TRANSFORMATION ===' ) ;
100+ const mockNewMetrics = Cypress . env ( 'MOCK_NEW_METRICS' ) === true ;
101+
102+ if ( ! mockNewMetrics ) {
103+ cy . log ( 'CYPRESS_MOCK_NEW_METRICS is disabled, skipping transformation' ) ;
104+ return ;
105+ }
106+
107+ cy . log ( 'Transforming old metric queries to new format' ) ;
108+
109+ cy . intercept ( 'GET' , MOCK_QUERY , ( req ) => {
110+ const url = new URL ( req . url , window . location . origin ) ;
111+ const query = url . searchParams . get ( 'query' ) || '' ;
112+ const hasNewMetric = query . includes ( NEW_METRIC_NAME ) ;
113+
114+ if ( hasNewMetric ) {
115+ const transformedQuery = query . replace ( new RegExp ( NEW_METRIC_NAME , 'g' ) , OLD_METRIC_NAME ) ;
116+ console . log ( `Transforming metric query: ${ query } -> ${ transformedQuery } ` ) ;
117+
118+ // Update the URL with the transformed query
119+ url . searchParams . set ( 'query' , transformedQuery ) ;
120+ req . url = url . toString ( ) ;
121+
122+ // Also transform the response to use new metric names
123+ req . continue ( ( res ) => {
124+ if ( res . body ?. data ?. result ) {
125+ res . body . data . result . forEach ( ( result : any ) => {
126+ if ( result ?. metric ?. __name__ === OLD_METRIC_NAME ) {
127+ console . log ( `Transforming response metric name: ${ OLD_METRIC_NAME } -> ${ NEW_METRIC_NAME } ` ) ;
128+ result . metric . __name__ = NEW_METRIC_NAME ;
129+ }
130+ } ) ;
131+ }
132+ res . send ( ) ;
133+ } ) ;
134+ } else {
135+ req . continue ( ) ;
136+ }
137+ } ) ;
88138} ) ;
0 commit comments