@@ -20,7 +20,7 @@ public function setUp(): void
2020
2121 public function tearDown (): void
2222 {
23- $ this ->usage ->purge (DateTime:: now () );
23+ $ this ->usage ->purge ();
2424 }
2525
2626 public function createUsageMetrics (): void
@@ -33,7 +33,7 @@ public function createUsageMetrics(): void
3333
3434 public function testIncrement (): void
3535 {
36- $ this ->usage ->purge (DateTime:: now () );
36+ $ this ->usage ->purge ();
3737
3838 // increment() should auto fan-out to all 3 periods
3939 $ this ->assertTrue ($ this ->usage ->increment ('inc-metric ' , 10 ));
@@ -52,7 +52,7 @@ public function testIncrement(): void
5252 public function testIncrementBatch (): void
5353 {
5454 // First cleanup existing data
55- $ this ->usage ->purge (DateTime:: now () );
55+ $ this ->usage ->purge ();
5656
5757 $ metrics = [
5858 [
@@ -123,7 +123,7 @@ public function testSumByPeriod(): void
123123 public function testIncrementingDefaultBehavior (): void
124124 {
125125 // Ensure clean state
126- $ this ->usage ->purge (\ Utopia \ Database \DateTime:: now () );
126+ $ this ->usage ->purge ();
127127
128128 // Increment the same metric twice
129129 $ this ->assertTrue ($ this ->usage ->increment ('increment-test ' , 5 ));
@@ -211,15 +211,62 @@ public function testPurge(): void
211211 // Wait a bit
212212 sleep (2 );
213213
214- // Purge all metrics
215- $ status = $ this ->usage ->purge (DateTime:: now () );
214+ // Purge all metrics (no queries = delete everything)
215+ $ status = $ this ->usage ->purge ();
216216 $ this ->assertTrue ($ status );
217217
218218 // Verify metrics were purged
219219 $ results = $ this ->usage ->getByPeriod ('purge-test ' , '1h ' );
220220 $ this ->assertEquals (0 , count ($ results ));
221221 }
222222
223+ public function testPurgeWithQueries (): void
224+ {
225+ $ this ->usage ->purge ();
226+
227+ $ this ->assertTrue ($ this ->usage ->increment ('purge-keep ' , 10 ));
228+ $ this ->assertTrue ($ this ->usage ->increment ('purge-remove ' , 20 ));
229+
230+ // Purge only the 'purge-remove' metric
231+ $ status = $ this ->usage ->purge ([
232+ Query::equal ('metric ' , ['purge-remove ' ]),
233+ ]);
234+ $ this ->assertTrue ($ status );
235+
236+ // 'purge-remove' should be gone
237+ $ sum = $ this ->usage ->sumByPeriod ('purge-remove ' , '1h ' );
238+ $ this ->assertEquals (0 , $ sum );
239+
240+ // 'purge-keep' should still exist
241+ $ sum = $ this ->usage ->sumByPeriod ('purge-keep ' , '1h ' );
242+ $ this ->assertEquals (10 , $ sum );
243+ }
244+
245+ public function testPurgeByPeriod (): void
246+ {
247+ $ this ->usage ->purge ();
248+
249+ // Insert into specific periods
250+ $ this ->assertTrue ($ this ->usage ->incrementBatch ([
251+ ['metric ' => 'purge-period ' , 'value ' => 10 , 'period ' => '1h ' , 'tags ' => []],
252+ ['metric ' => 'purge-period ' , 'value ' => 20 , 'period ' => '1d ' , 'tags ' => []],
253+ ]));
254+
255+ // Purge only 1h period
256+ $ this ->assertTrue ($ this ->usage ->purge ([
257+ Query::equal ('metric ' , ['purge-period ' ]),
258+ Query::equal ('period ' , ['1h ' ]),
259+ ]));
260+
261+ // 1h should be gone
262+ $ sum1h = $ this ->usage ->sumByPeriod ('purge-period ' , '1h ' );
263+ $ this ->assertEquals (0 , $ sum1h );
264+
265+ // 1d should still exist
266+ $ sum1d = $ this ->usage ->sumByPeriod ('purge-period ' , '1d ' );
267+ $ this ->assertEquals (20 , $ sum1d );
268+ }
269+
223270 public function testPeriodFormats (): void
224271 {
225272 $ periods = Usage::PERIODS ;
@@ -235,7 +282,7 @@ public function testPeriodFormats(): void
235282
236283 public function testSet (): void
237284 {
238- $ this ->usage ->purge (DateTime:: now () );
285+ $ this ->usage ->purge ();
239286
240287 // set() should auto fan-out to all 3 periods with replace semantics
241288 $ this ->assertTrue ($ this ->usage ->set ('set-metric ' , 100 ));
@@ -253,7 +300,7 @@ public function testSet(): void
253300
254301 public function testCollectAndFlush (): void
255302 {
256- $ this ->usage ->purge (DateTime:: now () );
303+ $ this ->usage ->purge ();
257304
258305 // collect() accumulates in memory, nothing written yet
259306 $ this ->usage ->collect ('collect-metric ' , 10 );
@@ -288,7 +335,7 @@ public function testCollectAndFlush(): void
288335
289336 public function testCollectMultipleMetrics (): void
290337 {
291- $ this ->usage ->purge (DateTime:: now () );
338+ $ this ->usage ->purge ();
292339
293340 $ this ->usage ->collect ('metric-a ' , 10 );
294341 $ this ->usage ->collect ('metric-b ' , 20 );
@@ -309,7 +356,7 @@ public function testCollectMultipleMetrics(): void
309356
310357 public function testCollectSetAndFlush (): void
311358 {
312- $ this ->usage ->purge (DateTime:: now () );
359+ $ this ->usage ->purge ();
313360
314361 // collectSet() uses last-write-wins semantics
315362 $ this ->usage ->collectSet ('set-collect ' , 100 );
@@ -334,7 +381,7 @@ public function testCollectSetAndFlush(): void
334381
335382 public function testMixedCollectAndCollectSet (): void
336383 {
337- $ this ->usage ->purge (DateTime:: now () );
384+ $ this ->usage ->purge ();
338385
339386 // Mix both types in the same buffer
340387 $ this ->usage ->collect ('inc-mixed ' , 10 );
@@ -417,7 +464,7 @@ public function testFlushThresholdConfiguration(): void
417464
418465 public function testSumByPeriodBatch (): void
419466 {
420- $ this ->usage ->purge (DateTime:: now () );
467+ $ this ->usage ->purge ();
421468
422469 // Insert known metrics
423470 $ this ->assertTrue ($ this ->usage ->increment ('batch-sum-a ' , 10 ));
@@ -440,7 +487,7 @@ public function testSumByPeriodBatch(): void
440487
441488 public function testSumByPeriodBatchWithMissingMetric (): void
442489 {
443- $ this ->usage ->purge (DateTime:: now () );
490+ $ this ->usage ->purge ();
444491
445492 $ this ->assertTrue ($ this ->usage ->increment ('batch-exists ' , 42 ));
446493
@@ -460,7 +507,7 @@ public function testSumByPeriodBatchEmpty(): void
460507
461508 public function testGetByPeriodBatch (): void
462509 {
463- $ this ->usage ->purge (DateTime:: now () );
510+ $ this ->usage ->purge ();
464511
465512 $ this ->assertTrue ($ this ->usage ->increment ('batch-get-a ' , 10 ));
466513 $ this ->assertTrue ($ this ->usage ->increment ('batch-get-b ' , 20 ));
@@ -482,7 +529,7 @@ public function testGetByPeriodBatch(): void
482529
483530 public function testGetByPeriodBatchWithMissingMetric (): void
484531 {
485- $ this ->usage ->purge (DateTime:: now () );
532+ $ this ->usage ->purge ();
486533
487534 $ this ->assertTrue ($ this ->usage ->increment ('batch-get-exists ' , 99 ));
488535
@@ -501,7 +548,7 @@ public function testGetByPeriodBatchEmpty(): void
501548
502549 public function testSumByPeriodBatchConsistencyWithSumByPeriod (): void
503550 {
504- $ this ->usage ->purge (DateTime:: now () );
551+ $ this ->usage ->purge ();
505552
506553 $ this ->assertTrue ($ this ->usage ->increment ('consistency-a ' , 15 ));
507554 $ this ->assertTrue ($ this ->usage ->increment ('consistency-b ' , 25 ));
@@ -517,7 +564,7 @@ public function testSumByPeriodBatchConsistencyWithSumByPeriod(): void
517564
518565 public function testSumByPeriodBatchAcrossPeriods (): void
519566 {
520- $ this ->usage ->purge (DateTime:: now () );
567+ $ this ->usage ->purge ();
521568
522569 // increment() fans out to all periods
523570 $ this ->assertTrue ($ this ->usage ->increment ('period-batch ' , 77 ));
0 commit comments