@@ -459,7 +459,7 @@ const getSupabaseStorageLogsQuery = () => {
459459/**
460460 * Combine all log sources to create the unified logs CTE
461461 */
462- const getUnifiedLogsCTE = ( ) => {
462+ export const getUnifiedLogsCTE = ( ) => {
463463 return `
464464WITH unified_logs AS (
465465 ${ getPostgrestLogsQuery ( ) }
@@ -510,39 +510,72 @@ ${finalWhere}
510510 * Get a count query for the total logs within the timeframe
511511 * Uses proper faceted search behavior where facets show "what would I get if I selected ONLY this option"
512512 */
513- export const getLogsCountQuery = ( search : QuerySearchParamsType ) : string => {
514- const { finalWhere } = buildQueryConditions ( search )
515513
516- // Helper function to build WHERE clause excluding a specific field
517- const buildFacetWhere = ( excludeField : string ) : string => {
518- const conditions : string [ ] = [ ]
514+ // Helper function to build WHERE clause excluding a specific field
515+ const buildFacetWhere = ( search : QuerySearchParamsType , excludeField : string ) : string => {
516+ const conditions : string [ ] = [ ]
519517
520- Object . entries ( search ) . forEach ( ( [ key , value ] ) => {
521- if ( key === excludeField ) return // Skip the field we're getting facets for
522- if ( EXCLUDED_QUERY_PARAMS . includes ( key as any ) ) return // Skip pagination and special params
518+ Object . entries ( search ) . forEach ( ( [ key , value ] ) => {
519+ if ( key === excludeField ) return // Skip the field we're getting facets for
520+ if ( EXCLUDED_QUERY_PARAMS . includes ( key as any ) ) return // Skip pagination and special params
523521
524- // Handle array filters (IN clause)
525- if ( Array . isArray ( value ) && value . length > 0 ) {
526- conditions . push ( `${ key } IN (${ value . map ( ( v ) => `'${ v } '` ) . join ( ',' ) } )` )
527- return
528- }
522+ // Handle array filters (IN clause)
523+ if ( Array . isArray ( value ) && value . length > 0 ) {
524+ conditions . push ( `${ key } IN (${ value . map ( ( v ) => `'${ v } '` ) . join ( ',' ) } )` )
525+ return
526+ }
529527
530- // Handle scalar values
531- if ( value !== null && value !== undefined ) {
532- if ( [ 'host' , 'pathname' ] . includes ( key ) ) {
533- conditions . push ( `${ key } LIKE '%${ value } %'` )
534- } else {
535- conditions . push ( `${ key } = '${ value } '` )
536- }
528+ // Handle scalar values
529+ if ( value !== null && value !== undefined ) {
530+ if ( [ 'host' , 'pathname' ] . includes ( key ) ) {
531+ conditions . push ( `${ key } LIKE '%${ value } %'` )
532+ } else {
533+ conditions . push ( `${ key } = '${ value } '` )
537534 }
538- } )
535+ }
536+ } )
539537
540- return conditions . length > 0 ? `WHERE ${ conditions . join ( ' AND ' ) } ` : ''
541- }
538+ return conditions . length > 0 ? `WHERE ${ conditions . join ( ' AND ' ) } ` : ''
539+ }
540+
541+ export const getFacetCountCTE = ( {
542+ search,
543+ facet,
544+ facetSearch,
545+ } : {
546+ search : QuerySearchParamsType
547+ facet : string
548+ facetSearch ?: string
549+ } ) => {
550+ const MAX_FACETS_QUANTITY = 20
551+
552+ return `
553+ ${ facet } _count AS (
554+ SELECT '${ facet } ' as dimension, ${ facet } as value, COUNT(*) as count
555+ FROM unified_logs
556+ ${ buildFacetWhere ( search , `${ facet } ` ) || `WHERE ${ facet } IS NOT NULL` }
557+ ${ buildFacetWhere ( search , `${ facet } ` ) ? ` AND ${ facet } IS NOT NULL` : '' }
558+ ${ ! ! facetSearch ? `AND ${ facet } LIKE '%${ facetSearch } %'` : '' }
559+ GROUP BY ${ facet }
560+ LIMIT ${ MAX_FACETS_QUANTITY }
561+ )
562+ ` . trim ( )
563+ }
564+
565+ export const getLogsCountQuery = ( search : QuerySearchParamsType ) : string => {
566+ const { finalWhere } = buildQueryConditions ( search )
542567
543568 // Create a count query using the same unified logs CTE
544569 const sql = `
545- ${ getUnifiedLogsCTE ( ) }
570+ ${ getUnifiedLogsCTE ( ) } ,
571+ ${ getFacetCountCTE ( { search, facet : 'log_type' } ) } ,
572+ ${ getFacetCountCTE ( { search, facet : 'method' } ) } ,
573+ ${ getFacetCountCTE ( { search, facet : 'level' } ) } ,
574+ ${ getFacetCountCTE ( { search, facet : 'status' } ) } ,
575+ ${ getFacetCountCTE ( { search, facet : 'host' } ) } ,
576+ ${ getFacetCountCTE ( { search, facet : 'pathname' } ) } ,
577+ ${ getFacetCountCTE ( { search, facet : 'auth_user' } ) }
578+
546579-- Get total count
547580SELECT 'total' as dimension, 'all' as value, COUNT(*) as count
548581FROM unified_logs
@@ -551,65 +584,37 @@ ${finalWhere}
551584UNION ALL
552585
553586-- Get counts by log_type (exclude log_type filter to avoid self-filtering)
554- SELECT 'log_type' as dimension, log_type as value, COUNT(*) as count
555- FROM unified_logs
556- ${ buildFacetWhere ( 'log_type' ) || 'WHERE log_type IS NOT NULL' }
557- ${ buildFacetWhere ( 'log_type' ) ? ' AND log_type IS NOT NULL' : '' }
558- GROUP BY log_type
587+ SELECT dimension, value, count from log_type_count
559588
560589UNION ALL
561590
562591-- Get counts by method (exclude method filter to avoid self-filtering)
563- SELECT 'method' as dimension, method as value, COUNT(*) as count
564- FROM unified_logs
565- ${ buildFacetWhere ( 'method' ) || 'WHERE method IS NOT NULL' }
566- ${ buildFacetWhere ( 'method' ) ? ' AND method IS NOT NULL' : '' }
567- GROUP BY method
592+ SELECT dimension, value, count from method_count
568593
569594UNION ALL
570595
571596-- Get counts by level (exclude level filter to avoid self-filtering)
572- SELECT 'level' as dimension, level as value, COUNT(*) as count
573- FROM unified_logs
574- ${ buildFacetWhere ( 'level' ) || 'WHERE level IS NOT NULL' }
575- ${ buildFacetWhere ( 'level' ) ? ' AND level IS NOT NULL' : '' }
576- GROUP BY level
597+ SELECT dimension, value, count from level_count
577598
578599UNION ALL
579600
580601-- Get counts by status (exclude status filter to avoid self-filtering)
581- SELECT 'status' as dimension, status as value, COUNT(*) as count
582- FROM unified_logs
583- ${ buildFacetWhere ( 'status' ) || 'WHERE status IS NOT NULL' }
584- ${ buildFacetWhere ( 'status' ) ? ' AND status IS NOT NULL' : '' }
585- GROUP BY status
602+ SELECT dimension, value, count from status_count
586603
587604UNION ALL
588605
589606-- Get counts by host (exclude host filter to avoid self-filtering)
590- SELECT 'host' as dimension, host as value, COUNT(*) as count
591- FROM unified_logs
592- ${ buildFacetWhere ( 'host' ) || 'WHERE host IS NOT NULL' }
593- ${ buildFacetWhere ( 'host' ) ? ' AND host IS NOT NULL' : '' }
594- GROUP BY host
607+ SELECT dimension, value, count from host_count
595608
596609UNION ALL
597610
598611-- Get counts by pathname (exclude pathname filter to avoid self-filtering)
599- SELECT 'pathname' as dimension, pathname as value, COUNT(*) as count
600- FROM unified_logs
601- ${ buildFacetWhere ( 'pathname' ) || 'WHERE pathname IS NOT NULL' }
602- ${ buildFacetWhere ( 'pathname' ) ? ' AND pathname IS NOT NULL' : '' }
603- GROUP BY pathname
612+ SELECT dimension, value, count from pathname_count
604613
605614UNION ALL
606615
607616-- Get counts by auth_user (exclude auth_user filter to avoid self-filtering)
608- SELECT 'auth_user' as dimension, auth_user as value, COUNT(*) as count
609- FROM unified_logs
610- ${ buildFacetWhere ( 'auth_user' ) || 'WHERE auth_user IS NOT NULL' }
611- ${ buildFacetWhere ( 'auth_user' ) ? ' AND auth_user IS NOT NULL' : '' }
612- GROUP BY auth_user
617+ SELECT dimension, value, count from auth_user_count
613618`
614619
615620 return sql
0 commit comments