@@ -124,11 +124,15 @@ export default function ({ getService }: FtrProviderContext) {
124124
125125 it ( 'should return metrics if available and called with withMetrics' , async ( ) => {
126126 const now = Date . now ( ) ;
127+ // We need to create data points in precise time buckets to ensure the derivative works properly
128+ // 4 minutes ago (first data point for component1)
129+ const fourMinutesAgo = new Date ( now - 4 * 60 * 1000 ) ;
130+ fourMinutesAgo . setSeconds ( 0 , 0 ) ; // Set to exact minute boundary
127131 await es . index ( {
128132 index : 'metrics-elastic_agent.elastic_agent-default' ,
129133 refresh : 'wait_for' ,
130134 document : {
131- '@timestamp' : new Date ( now - 2 * 60 * 1000 ) . toISOString ( ) ,
135+ '@timestamp' : fourMinutesAgo . toISOString ( ) ,
132136 data_stream : {
133137 namespace : 'default' ,
134138 type : 'metrics' ,
@@ -143,18 +147,52 @@ export default function ({ getService }: FtrProviderContext) {
143147 } ,
144148 cpu : {
145149 total : {
146- value : 500 ,
150+ value : 500 , // Starting value
147151 } ,
148152 } ,
149153 } ,
150154 } ,
151155 } ,
152156 } ) ;
157+
158+ // 3 minutes ago (second data point for component1)
159+ const threeMinutesAgo = new Date ( now - 3 * 60 * 1000 ) ;
160+ threeMinutesAgo . setSeconds ( 0 , 0 ) ; // Set to exact minute boundary
153161 await es . index ( {
154162 index : 'metrics-elastic_agent.elastic_agent-default' ,
155163 refresh : 'wait_for' ,
156164 document : {
157- '@timestamp' : new Date ( now - 1 * 60 * 1000 ) . toISOString ( ) ,
165+ '@timestamp' : threeMinutesAgo . toISOString ( ) ,
166+ data_stream : {
167+ namespace : 'default' ,
168+ type : 'metrics' ,
169+ dataset : 'elastic_agent.elastic_agent' ,
170+ } ,
171+ elastic_agent : { id : 'agent1' , process : 'elastic_agent' } ,
172+ component : { id : 'component1' } ,
173+ system : {
174+ process : {
175+ memory : {
176+ size : 25510920 ,
177+ } ,
178+ cpu : {
179+ total : {
180+ value : 1200 , // Higher value to ensure derivative is positive
181+ } ,
182+ } ,
183+ } ,
184+ } ,
185+ } ,
186+ } ) ;
187+
188+ // 1 minute ago (data point for component2) - same agent but different component
189+ const oneMinuteAgo = new Date ( now - 1 * 60 * 1000 ) ;
190+ oneMinuteAgo . setSeconds ( 0 , 0 ) ; // Set to exact minute boundary
191+ await es . index ( {
192+ index : 'metrics-elastic_agent.elastic_agent-default' ,
193+ refresh : 'wait_for' ,
194+ document : {
195+ '@timestamp' : oneMinuteAgo . toISOString ( ) ,
158196 elastic_agent : { id : 'agent1' , process : 'elastic_agent' } ,
159197 component : { id : 'component2' } ,
160198 data_stream : {
@@ -169,7 +207,7 @@ export default function ({ getService }: FtrProviderContext) {
169207 } ,
170208 cpu : {
171209 total : {
172- value : 1500 ,
210+ value : 2500 , // Even higher value
173211 } ,
174212 } ,
175213 } ,
@@ -185,9 +223,9 @@ export default function ({ getService }: FtrProviderContext) {
185223 expect ( apiResponse . total ) . to . eql ( 4 ) ;
186224
187225 const agent1 : Agent = apiResponse . items . find ( ( agent : any ) => agent . id === 'agent1' ) ;
188-
189- expect ( agent1 . metrics ?. memory_size_byte_avg ) . to . eql ( '25510920 ' ) ;
190- expect ( agent1 . metrics ?. cpu_avg ) . to . eql ( '0.01666 ' ) ;
226+ // As both of the indexed items have the same agent id, and each one has its own memory/cpu item, the metrics should include both values combined as each is now uniquely counted towards total memory/cpu usage
227+ expect ( agent1 . metrics ?. memory_size_byte_avg ) . to . eql ( '51021840 ' ) ;
228+ expect ( agent1 . metrics ?. cpu_avg ) . to . eql ( '0.01166 ' ) ;
191229
192230 const agent2 : Agent = apiResponse . items . find ( ( agent : any ) => agent . id === 'agent2' ) ;
193231 expect ( agent2 . metrics ?. memory_size_byte_avg ) . equal ( undefined ) ;
0 commit comments