This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

fix collection breakdown query

+34 -13
+34 -13
src/modules/user/infrastructure/repositories/DrizzleUserStatsRepository.ts
··· 437 437 438 438 // Optimized query for collections: use incremental aggregation with window functions 439 439 const collectionsQuery = sql` 440 - WITH period_collection_counts AS ( 440 + WITH all_collections AS ( 441 + SELECT 442 + access_type, 443 + created_at 444 + FROM ${collections} 445 + WHERE created_at IS NOT NULL 446 + ), 447 + all_periods AS ( 448 + SELECT DISTINCT date_trunc(${interval}, created_at) AS period 449 + FROM all_collections 450 + ), 451 + all_access_types AS ( 452 + SELECT DISTINCT access_type 453 + FROM all_collections 454 + ), 455 + period_type_grid AS ( 456 + SELECT p.period, t.access_type 457 + FROM all_periods p 458 + CROSS JOIN all_access_types t 459 + ), 460 + period_counts AS ( 441 461 SELECT 442 462 date_trunc(${interval}, created_at) AS period, 443 463 access_type, 444 464 COUNT(*)::int AS period_count 445 - FROM ${collections} 446 - WHERE created_at IS NOT NULL 465 + FROM all_collections 447 466 GROUP BY 1, 2 467 + ), 468 + cumulative_counts AS ( 469 + SELECT 470 + ptg.period, 471 + ptg.access_type, 472 + SUM(COALESCE(pc.period_count, 0)) OVER ( 473 + PARTITION BY ptg.access_type 474 + ORDER BY ptg.period 475 + ) AS cumulative_count 476 + FROM period_type_grid ptg 477 + LEFT JOIN period_counts pc ON ptg.period = pc.period AND ptg.access_type = pc.access_type 448 478 ), 449 479 collection_aggregated AS ( 450 480 SELECT 451 481 period::text AS date, 452 482 jsonb_object_agg(access_type, cumulative_count) AS by_access_type, 453 483 SUM(cumulative_count)::int AS total 454 - FROM ( 455 - SELECT 456 - period, 457 - access_type, 458 - SUM(period_count) OVER ( 459 - PARTITION BY access_type 460 - ORDER BY period 461 - ) AS cumulative_count 462 - FROM period_collection_counts 463 - ) cumulative_data 484 + FROM cumulative_counts 464 485 GROUP BY period 465 486 ORDER BY period DESC 466 487 LIMIT ${limit}