@@ -742,6 +742,44 @@ export interface TicketFilters {
742742 scope ?: 'open' | 'all' ; // open = no cerrados (default)
743743}
744744
745+ interface TicketActor { name : string ; avatarUrl : string | null ; login : string | null ; devId : string | null ; reviewState : string | null }
746+
747+ /** Carga la atribución (autor/revisor/merger) de `roz.work_item_actor` para un set de tickets. */
748+ async function loadTicketActors (
749+ ids : string [ ] ,
750+ devName : Map < string , string > ,
751+ devAvatar : Map < string , string | null > ,
752+ ) : Promise < Map < string , { authors : TicketActor [ ] ; reviewers : TicketActor [ ] ; merger : TicketActor | null } > > {
753+ const map = new Map < string , { authors : TicketActor [ ] ; reviewers : TicketActor [ ] ; merger : TicketActor | null } > ( ) ;
754+ if ( ! ids . length ) return map ;
755+ const { data } = await db ( )
756+ . from ( 'work_item_actor' )
757+ . select ( 'work_item_id, dev_id, github_login, role, review_state' )
758+ . in ( 'work_item_id' , ids ) ;
759+ for ( const a of ( data ?? [ ] ) as any [ ] ) {
760+ const person : TicketActor = {
761+ name : a . dev_id ? devName . get ( a . dev_id ) ?? a . github_login : a . github_login ,
762+ avatarUrl : a . dev_id ? devAvatar . get ( a . dev_id ) ?? avatarFor ( a . github_login ) : avatarFor ( a . github_login ) ,
763+ login : a . github_login ?? null ,
764+ devId : a . dev_id ?? null ,
765+ reviewState : a . review_state ?? null ,
766+ } ;
767+ if ( ! map . has ( a . work_item_id ) ) map . set ( a . work_item_id , { authors : [ ] , reviewers : [ ] , merger : null } ) ;
768+ const e = map . get ( a . work_item_id ) ! ;
769+ if ( a . role === 'author' ) e . authors . push ( person ) ;
770+ else if ( a . role === 'reviewer' ) e . reviewers . push ( person ) ;
771+ else if ( a . role === 'merger' ) e . merger = person ;
772+ }
773+ return map ;
774+ }
775+
776+ /** ¿Es la misma persona? Prefiere dev_id; cae a github_login. */
777+ function samePerson ( a : TicketActor , b : TicketActor ) : boolean {
778+ if ( a . devId && b . devId ) return a . devId === b . devId ;
779+ if ( a . login && b . login ) return a . login === b . login ;
780+ return false ;
781+ }
782+
745783export async function getTickets ( f : TicketFilters ) {
746784 const [ devs , projects ] = await Promise . all ( [ allDevs ( ) , allProjects ( ) ] ) ;
747785 const devName = new Map ( devs . map ( ( d ) => [ d . id , d . name ] ) ) ;
@@ -751,7 +789,7 @@ export async function getTickets(f: TicketFilters) {
751789 let q = db ( )
752790 . from ( 'work_item' )
753791 . select (
754- 'id, identifier, number, title, state, state_name, priority, project_id, assignee_dev_id, estimate, due_date, labels, creator_name, url, started_at, completed_at, linear_created_at, linear_updated_at' ,
792+ 'id, identifier, number, title, state, state_name, priority, project_id, assignee_dev_id, estimate, due_date, labels, creator_name, url, started_at, completed_at, linear_created_at, linear_updated_at, pr_number, repo, source, merger_dev_id ' ,
755793 ) ;
756794 if ( f . projectId ) q = q . eq ( 'project_id' , f . projectId ) ;
757795 if ( f . assigneeDevId ) q = q . eq ( 'assignee_dev_id' , f . assigneeDevId ) ;
@@ -764,43 +802,68 @@ export async function getTickets(f: TicketFilters) {
764802 const PRIO_ORDER : Record < string , number > = { urgent : 0 , high : 1 , medium : 2 , low : 3 } ;
765803 const now = Date . now ( ) ;
766804
805+ // Atribución por PR (autor/revisor/merger) para los tickets visibles.
806+ const actorsByItem = await loadTicketActors ( rows . map ( ( w ) => w . id ) , devName , devAvatar ) ;
807+
767808 const tickets = rows
768- . map ( ( w ) => ( {
769- id : w . id ,
770- identifier : w . identifier ,
771- number : w . number ,
772- title : w . title ,
773- state : w . state ,
774- stateName : w . state_name ?? w . state ,
775- priority : w . priority ,
776- projectId : w . project_id ,
777- projectName : w . project_id ? projName . get ( w . project_id ) ?? null : null ,
778- assignee : w . assignee_dev_id ? { name : devName . get ( w . assignee_dev_id ) ?? '—' , avatarUrl : devAvatar . get ( w . assignee_dev_id ) ?? null } : null ,
779- estimate : w . estimate ,
780- dueDate : w . due_date ,
781- overdue : w . due_date ? ! [ 'completed' , 'done' , 'canceled' ] . includes ( w . state ) && new Date ( w . due_date ) . getTime ( ) < now : false ,
782- labels : w . labels ?? [ ] ,
783- creatorName : w . creator_name ,
784- url : w . url ,
785- updatedAt : w . linear_updated_at ,
786- ageDays : w . linear_created_at ? Math . floor ( ( now - new Date ( w . linear_created_at ) . getTime ( ) ) / 86_400_000 ) : null ,
787- } ) )
809+ . map ( ( w ) => {
810+ const act = actorsByItem . get ( w . id ) ;
811+ const merger = act ?. merger
812+ ?? ( w . merger_dev_id
813+ ? { name : devName . get ( w . merger_dev_id ) ?? '—' , avatarUrl : devAvatar . get ( w . merger_dev_id ) ?? null , login : null , devId : w . merger_dev_id , reviewState : null }
814+ : null ) ;
815+ return {
816+ id : w . id ,
817+ identifier : w . identifier ,
818+ number : w . number ,
819+ title : w . title ,
820+ state : w . state ,
821+ stateName : w . state_name ?? w . state ,
822+ priority : w . priority ,
823+ projectId : w . project_id ,
824+ projectName : w . project_id ? projName . get ( w . project_id ) ?? null : null ,
825+ assignee : w . assignee_dev_id ? { name : devName . get ( w . assignee_dev_id ) ?? '—' , avatarUrl : devAvatar . get ( w . assignee_dev_id ) ?? null } : null ,
826+ estimate : w . estimate ,
827+ dueDate : w . due_date ,
828+ overdue : w . due_date ? ! [ 'completed' , 'done' , 'canceled' ] . includes ( w . state ) && new Date ( w . due_date ) . getTime ( ) < now : false ,
829+ labels : w . labels ?? [ ] ,
830+ creatorName : w . creator_name ,
831+ url : w . url ,
832+ updatedAt : w . linear_updated_at ,
833+ ageDays : w . linear_created_at ? Math . floor ( ( now - new Date ( w . linear_created_at ) . getTime ( ) ) / 86_400_000 ) : null ,
834+ // Conexión con código
835+ source : ( w . source as 'pr' | 'commit' | null ) ?? null ,
836+ pr : w . pr_number && w . repo ? { repo : w . repo , number : w . pr_number , url : `https://github.com/${ w . repo } /pull/${ w . pr_number } ` } : null ,
837+ authors : act ?. authors ?? [ ] ,
838+ reviewers : act ?. reviewers ?? [ ] ,
839+ merger,
840+ } ;
841+ } )
788842 . sort ( ( a , b ) => ( PRIO_ORDER [ a . priority ?? '' ] ?? 9 ) - ( PRIO_ORDER [ b . priority ?? '' ] ?? 9 ) ) ;
789843
790844 // Agregaciones para los KPIs/gráficas (sobre el conjunto filtrado).
791845 const byState = countMap ( tickets , ( t ) => t . stateName ) ;
792846 const byPriority = countMap ( tickets , ( t ) => t . priority ?? 'sin prioridad' ) ;
793847 const byProject = countMap ( tickets , ( t ) => t . projectName ?? 'sin proyecto' ) ;
848+ const bySource = countMap ( tickets , ( t ) => t . source ?? 'linear' ) ;
849+
850+ // Top revisores: quién aparece más como reviewer (no es el assignee; hoy invisible).
851+ const revMap = new Map < string , { name : string ; avatarUrl : string | null ; count : number } > ( ) ;
852+ tickets . forEach ( ( t ) =>
853+ t . reviewers . forEach ( ( r ) => {
854+ const k = r . login ?? r . name ;
855+ if ( ! revMap . has ( k ) ) revMap . set ( k , { name : r . name , avatarUrl : r . avatarUrl , count : 0 } ) ;
856+ revMap . get ( k ) ! . count ++ ;
857+ } ) ,
858+ ) ;
859+ const topReviewers = [ ...revMap . values ( ) ] . sort ( ( a , b ) => b . count - a . count ) ;
794860
795- // Developers involucrados (con avatar): cuántos tickets tiene cada uno en el conjunto.
796- const devMap = new Map < string , { name : string ; avatarUrl : string | null ; count : number } > ( ) ;
797- tickets . forEach ( ( t ) => {
798- if ( ! t . assignee ) return ;
799- const k = t . assignee . name ;
800- if ( ! devMap . has ( k ) ) devMap . set ( k , { name : t . assignee . name , avatarUrl : t . assignee . avatarUrl , count : 0 } ) ;
801- devMap . get ( k ) ! . count ++ ;
802- } ) ;
803- const developers = [ ...devMap . values ( ) ] . sort ( ( a , b ) => b . count - a . count ) ;
861+ // Insight de atribución: tickets cuyo merger no es ninguno de los autores (ver squash/merge).
862+ const attributionMismatch = tickets . filter (
863+ ( t ) => t . merger && t . authors . length > 0 && ! t . authors . some ( ( a ) => samePerson ( a , t . merger ! ) ) ,
864+ ) . length ;
865+ // Trabajo cerrado sin PR vinculado (no trazable a código).
866+ const withoutPr = tickets . filter ( ( t ) => [ 'completed' , 'done' ] . includes ( t . state ) && ! t . pr ) . length ;
804867
805868 // Resumen por categoría, SIEMPRE sobre todos los tickets del filtro (ignora el scope), para
806869 // que los KPIs muestren abiertos / en curso / completados sin importar el toggle.
@@ -810,6 +873,18 @@ export async function getTickets(f: TicketFilters) {
810873 if ( f . priority ) sq = sq . eq ( 'priority' , f . priority ) ;
811874 const { data : allRows } = await sq . limit ( 2000 ) ;
812875 const all = ( allRows ?? [ ] ) as any [ ] ;
876+
877+ // Developers involucrados: sobre el conjunto COMPLETO (no los 500 visibles), para que no
878+ // dependa del scope/orden. Antes se calculaba sobre `tickets` y devs con tickets viejos
879+ // quedaban fuera del corte de 500 en scope "todos".
880+ const devMap = new Map < string , { name : string ; avatarUrl : string | null ; count : number } > ( ) ;
881+ all . forEach ( ( w ) => {
882+ if ( ! w . assignee_dev_id ) return ;
883+ const name = devName . get ( w . assignee_dev_id ) ?? '—' ;
884+ if ( ! devMap . has ( name ) ) devMap . set ( name , { name, avatarUrl : devAvatar . get ( w . assignee_dev_id ) ?? null , count : 0 } ) ;
885+ devMap . get ( name ) ! . count ++ ;
886+ } ) ;
887+ const developers = [ ...devMap . values ( ) ] . sort ( ( a , b ) => b . count - a . count ) ;
813888 const summary = {
814889 total : all . length ,
815890 open : all . filter ( ( w ) => OPEN_STATES . includes ( w . state ) ) . length ,
@@ -827,7 +902,11 @@ export async function getTickets(f: TicketFilters) {
827902 byState,
828903 byPriority,
829904 byProject,
905+ bySource,
830906 developers,
907+ topReviewers,
908+ attributionMismatch,
909+ withoutPr,
831910 tickets,
832911 } ;
833912}
0 commit comments