@@ -361,6 +361,8 @@ export async function queryMembersAdvanced(
361361 attributeSettings = [ ] as IDbMemberAttributeSetting [ ] ,
362362 } ,
363363) : Promise < PageData < IDbMemberData > > {
364+ const startTime = Date . now ( )
365+
364366 const withAggregates = ! ! segmentId
365367 const searchConfig = buildSearchCTE ( search )
366368
@@ -422,6 +424,7 @@ export async function queryMembersAdvanced(
422424 }
423425
424426 // Prepare fields for main query
427+ const fieldsStartTime = Date . now ( )
425428 const preparedFields = fields
426429 . map ( ( f ) => {
427430 const mappedField = QUERY_FILTER_COLUMN_MAP . get ( f )
@@ -438,6 +441,7 @@ export async function queryMembersAdvanced(
438441 } )
439442 . map ( ( mappedField ) => `${ mappedField . name } AS "${ mappedField . alias } "` )
440443 . join ( ',\n' )
444+ log . info ( `[PERF] Field preparation took: ${ Date . now ( ) - fieldsStartTime } ms` )
441445
442446 const mainQuery = `
443447 ${ buildQuery (
@@ -456,10 +460,16 @@ export async function queryMembersAdvanced(
456460 log . info ( `count query: ${ formatSql ( countQuery , params ) } ` )
457461
458462 // Execute queries in parallel
463+ const mainQueryStartTime = Date . now ( )
464+
459465 const [ rows , countResult ] = await Promise . all ( [
460466 qx . select ( mainQuery , params ) ,
461467 qx . selectOne ( countQuery , params ) ,
462468 ] )
469+ const mainQueryDuration = Date . now ( ) - mainQueryStartTime
470+ log . info (
471+ `[PERF] Main queries (parallel) took: ${ mainQueryDuration } ms - returned ${ rows . length } rows` ,
472+ )
463473
464474 // TODO: ci serve davvero questo filtro ?
465475 // rows.forEach((row) => {
@@ -482,24 +492,181 @@ export async function queryMembersAdvanced(
482492 return { rows : [ ] , count, limit, offset }
483493 }
484494
495+ // const [memberOrganizations, identities, memberSegments, maintainerRoles] = await Promise.all([
496+ // include.memberOrganizations ? fetchManyMemberOrgs(qx, memberIds) : Promise.resolve([]),
497+ // include.identities ? fetchManyMemberIdentities(qx, memberIds) : Promise.resolve([]),
498+ // include.segments ? fetchManyMemberSegments(qx, memberIds) : Promise.resolve([]),
499+ // include.maintainers ? findMaintainerRoles(qx, memberIds) : Promise.resolve([]),
500+ // ])
501+ const firstBatchStartTime = Date . now ( )
502+
485503 const [ memberOrganizations , identities , memberSegments , maintainerRoles ] = await Promise . all ( [
486- include . memberOrganizations ? fetchManyMemberOrgs ( qx , memberIds ) : Promise . resolve ( [ ] ) ,
487- include . identities ? fetchManyMemberIdentities ( qx , memberIds ) : Promise . resolve ( [ ] ) ,
488- include . segments ? fetchManyMemberSegments ( qx , memberIds ) : Promise . resolve ( [ ] ) ,
489- include . maintainers ? findMaintainerRoles ( qx , memberIds ) : Promise . resolve ( [ ] ) ,
504+ include . memberOrganizations
505+ ? ( async ( ) => {
506+ const start = Date . now ( )
507+ const result = await fetchManyMemberOrgs ( qx , memberIds )
508+ log . info ( `[PERF] fetchManyMemberOrgs took: ${ Date . now ( ) - start } ms` )
509+ return result
510+ } ) ( )
511+ : Promise . resolve ( [ ] ) ,
512+ include . identities
513+ ? ( async ( ) => {
514+ const start = Date . now ( )
515+ const result = await fetchManyMemberIdentities ( qx , memberIds )
516+ log . info ( `[PERF] fetchManyMemberIdentities took: ${ Date . now ( ) - start } ms` )
517+ return result
518+ } ) ( )
519+ : Promise . resolve ( [ ] ) ,
520+ include . segments
521+ ? ( async ( ) => {
522+ const start = Date . now ( )
523+ const result = await fetchManyMemberSegments ( qx , memberIds )
524+ log . info ( `[PERF] fetchManyMemberSegments took: ${ Date . now ( ) - start } ms` )
525+ return result
526+ } ) ( )
527+ : Promise . resolve ( [ ] ) ,
528+ include . maintainers
529+ ? ( async ( ) => {
530+ const start = Date . now ( )
531+ const result = await findMaintainerRoles ( qx , memberIds )
532+ log . info ( `[PERF] findMaintainerRoles took: ${ Date . now ( ) - start } ms` )
533+ return result
534+ } ) ( )
535+ : Promise . resolve ( [ ] ) ,
490536 ] )
491-
537+ const firstBatchDuration = Date . now ( ) - firstBatchStartTime
538+ log . info ( `[PERF] First parallel batch took: ${ firstBatchDuration } ms` )
539+
540+ // const [orgExtra, segmentsInfo, maintainerSegmentsInfo] = await Promise.all([
541+ // include.memberOrganizations
542+ // ? fetchOrganizationData(qx, memberOrganizations)
543+ // : Promise.resolve({ orgs: [], lfx: [] }),
544+ // include.segments ? fetchSegmentData(qx, memberSegments) : Promise.resolve([]),
545+ // include.maintainers && maintainerRoles.length > 0
546+ // ? fetchManySegments(qx, uniq(maintainerRoles.map((m) => m.segmentId)))
547+ // : Promise.resolve([]),
548+ // ])
549+
550+ // if (include.memberOrganizations) {
551+ // const { orgs = [], lfx = [] } = orgExtra
552+
553+ // for (const member of rows) {
554+ // member.organizations = []
555+
556+ // const memberOrgs =
557+ // memberOrganizations.find((o) => o.memberId === member.id)?.organizations || []
558+
559+ // const activeOrgs = memberOrgs.filter((org) => !org.dateEnd)
560+
561+ // const sortedActiveOrgs = sortActiveOrganizations(activeOrgs, orgs)
562+
563+ // const activeOrg = sortedActiveOrgs[0]
564+
565+ // if (activeOrg) {
566+ // const orgInfo = orgs.find((odn) => odn.id === activeOrg.organizationId)
567+
568+ // if (orgInfo) {
569+ // const lfxMembership = lfx.find((m) => m.organizationId === activeOrg.organizationId)
570+ // member.organizations = [
571+ // {
572+ // id: activeOrg.organizationId,
573+ // displayName: orgInfo.displayName || '',
574+ // logo: orgInfo.logo || '',
575+ // lfxMembership: !!lfxMembership,
576+ // },
577+ // ]
578+ // }
579+ // }
580+ // }
581+ // }
582+
583+ // if (include.segments) {
584+ // const segments = segmentsInfo || []
585+
586+ // rows.forEach((member) => {
587+ // member.segments = (memberSegments.find((i) => i.memberId === member.id)?.segments || [])
588+ // .map((segment) => {
589+ // const segmentInfo = segments.find((s) => s.id === segment.segmentId)
590+
591+ // if (include.onlySubProjects && segmentInfo?.type !== SegmentType.SUB_PROJECT) {
592+ // return null
593+ // }
594+
595+ // return {
596+ // id: segment.segmentId,
597+ // name: segmentInfo?.name,
598+ // activityCount: segment.activityCount,
599+ // }
600+ // })
601+ // .filter(Boolean)
602+ // })
603+ // }
604+
605+ // if (include.maintainers) {
606+ // const groupedMaintainers = groupBy(maintainerRoles, (m) => m.memberId)
607+ // rows.forEach((member) => {
608+ // member.maintainerRoles = (groupedMaintainers.get(member.id) || []).map((role) => {
609+ // const segmentInfo = maintainerSegmentsInfo.find((s) => s.id === role.segmentId)
610+ // return {
611+ // ...role,
612+ // segmentName: segmentInfo?.name,
613+ // }
614+ // })
615+ // })
616+ // }
617+
618+ // if (include.identities) {
619+ // rows.forEach((member) => {
620+ // const memberIdentities = identities.find((i) => i.memberId === member.id)?.identities || []
621+
622+ // member.identities = memberIdentities.map((identity) => ({
623+ // type: identity.type,
624+ // value: identity.value,
625+ // platform: identity.platform,
626+ // verified: identity.verified,
627+ // }))
628+ // })
629+ // }
630+
631+ // Second parallel batch - fetch related data
632+ const secondBatchStartTime = Date . now ( )
492633 const [ orgExtra , segmentsInfo , maintainerSegmentsInfo ] = await Promise . all ( [
493634 include . memberOrganizations
494- ? fetchOrganizationData ( qx , memberOrganizations )
635+ ? ( async ( ) => {
636+ const start = Date . now ( )
637+ const result = await fetchOrganizationData ( qx , memberOrganizations )
638+ log . info ( `[PERF] fetchOrganizationData took: ${ Date . now ( ) - start } ms` )
639+ return result
640+ } ) ( )
495641 : Promise . resolve ( { orgs : [ ] , lfx : [ ] } ) ,
496- include . segments ? fetchSegmentData ( qx , memberSegments ) : Promise . resolve ( [ ] ) ,
642+ include . segments
643+ ? ( async ( ) => {
644+ const start = Date . now ( )
645+ const result = await fetchSegmentData ( qx , memberSegments )
646+ log . info ( `[PERF] fetchSegmentData took: ${ Date . now ( ) - start } ms` )
647+ return result
648+ } ) ( )
649+ : Promise . resolve ( [ ] ) ,
497650 include . maintainers && maintainerRoles . length > 0
498- ? fetchManySegments ( qx , uniq ( maintainerRoles . map ( ( m ) => m . segmentId ) ) )
651+ ? ( async ( ) => {
652+ const start = Date . now ( )
653+ const segmentIds = uniq ( maintainerRoles . map ( ( m ) => m . segmentId ) )
654+ const result = await fetchManySegments ( qx , segmentIds )
655+ log . info (
656+ `[PERF] fetchManySegments for maintainers (${ segmentIds . length } segments) took: ${ Date . now ( ) - start } ms` ,
657+ )
658+ return result
659+ } ) ( )
499660 : Promise . resolve ( [ ] ) ,
500661 ] )
662+ const secondBatchDuration = Date . now ( ) - secondBatchStartTime
663+ log . info ( `[PERF] Second parallel batch took: ${ secondBatchDuration } ms` )
664+
665+ // Data processing section
666+ const processingStartTime = Date . now ( )
501667
502668 if ( include . memberOrganizations ) {
669+ const orgProcessingStart = Date . now ( )
503670 const { orgs = [ ] , lfx = [ ] } = orgExtra
504671
505672 for ( const member of rows ) {
@@ -530,9 +697,11 @@ export async function queryMembersAdvanced(
530697 }
531698 }
532699 }
700+ log . info ( `[PERF] Member organizations processing took: ${ Date . now ( ) - orgProcessingStart } ms` )
533701 }
534702
535703 if ( include . segments ) {
704+ const segmentProcessingStart = Date . now ( )
536705 const segments = segmentsInfo || [ ]
537706
538707 rows . forEach ( ( member ) => {
@@ -552,9 +721,11 @@ export async function queryMembersAdvanced(
552721 } )
553722 . filter ( Boolean )
554723 } )
724+ log . info ( `[PERF] Segments processing took: ${ Date . now ( ) - segmentProcessingStart } ms` )
555725 }
556726
557727 if ( include . maintainers ) {
728+ const maintainerProcessingStart = Date . now ( )
558729 const groupedMaintainers = groupBy ( maintainerRoles , ( m ) => m . memberId )
559730 rows . forEach ( ( member ) => {
560731 member . maintainerRoles = ( groupedMaintainers . get ( member . id ) || [ ] ) . map ( ( role ) => {
@@ -565,9 +736,11 @@ export async function queryMembersAdvanced(
565736 }
566737 } )
567738 } )
739+ log . info ( `[PERF] Maintainer roles processing took: ${ Date . now ( ) - maintainerProcessingStart } ms` )
568740 }
569741
570742 if ( include . identities ) {
743+ const identityProcessingStart = Date . now ( )
571744 rows . forEach ( ( member ) => {
572745 const memberIdentities = identities . find ( ( i ) => i . memberId === member . id ) ?. identities || [ ]
573746
@@ -578,8 +751,18 @@ export async function queryMembersAdvanced(
578751 verified : identity . verified ,
579752 } ) )
580753 } )
754+ log . info ( `[PERF] Identities processing took: ${ Date . now ( ) - identityProcessingStart } ms` )
581755 }
582756
757+ const processingDuration = Date . now ( ) - processingStartTime
758+ log . info ( `[PERF] Total data processing took: ${ processingDuration } ms` )
759+
760+ const totalDuration = Date . now ( ) - startTime
761+ log . info ( `[PERF] Total queryMembersAdvanced took: ${ totalDuration } ms` )
762+ log . info (
763+ `[PERF] Breakdown - Main queries: ${ mainQueryDuration } ms (${ ( ( mainQueryDuration / totalDuration ) * 100 ) . toFixed ( 1 ) } %), First batch: ${ firstBatchDuration } ms (${ ( ( firstBatchDuration / totalDuration ) * 100 ) . toFixed ( 1 ) } %), Second batch: ${ secondBatchDuration } ms (${ ( ( secondBatchDuration / totalDuration ) * 100 ) . toFixed ( 1 ) } %), Processing: ${ processingDuration } ms (${ ( ( processingDuration / totalDuration ) * 100 ) . toFixed ( 1 ) } %)` ,
764+ )
765+
583766 return { rows, count, limit, offset }
584767}
585768
0 commit comments