1515 * limitations under the License.
1616 */
1717
18- import { Component , EventEmitter , Input , OnChanges , Output , SimpleChanges } from '@angular/core' ;
18+ import { Component , EventEmitter , Input , OnChanges , Output , SimpleChanges , computed , inject , input , signal } from '@angular/core' ;
1919import { MatDialog } from '@angular/material/dialog' ;
2020
21+ import { Span } from '../../core/models/Trace' ;
2122import { TraceChartComponent } from './trace-chart/trace-chart.component' ;
2223import { MatButtonToggleGroup , MatButtonToggle } from '@angular/material/button-toggle' ;
2324import { FormsModule } from '@angular/forms' ;
2425import { MatList , MatListItem } from '@angular/material/list' ;
2526import { KeyValuePipe } from '@angular/common' ;
27+ import { InvocIdPipe } from './invoc-id.pipe' ;
2628
2729@Component ( {
2830 selector : 'app-event-tab' ,
@@ -35,29 +37,36 @@ import { KeyValuePipe } from '@angular/common';
3537 MatList ,
3638 MatListItem ,
3739 KeyValuePipe ,
40+ InvocIdPipe ,
3841 ] ,
3942} )
40- export class EventTabComponent implements OnChanges {
41- @Input ( ) eventsMap = new Map < string , any > ( ) ;
43+ export class EventTabComponent {
44+ readonly eventsMap = input < Map < string , any > > ( new Map < string , any > ( ) ) ;
45+ readonly traceData = input < Span [ ] > ( [ ] ) ;
4246 @Output ( ) selectedEvent = new EventEmitter < string > ( ) ;
43- @Input ( ) traceData : any [ ] = [ ] ;
44- llmRequest : any = undefined ;
45- llmResponse : any = undefined ;
46- llmRequestKey = 'gcp.vertex.agent.llm_request' ;
47- llmResponseKey = 'gcp.vertex.agent.llm_response' ;
48- isDetailsPanelOpen = false ;
49- view = 'events' ;
50- invocTraces = new Map < string , any [ ] > ( ) ;
47+ private readonly dialog = inject ( MatDialog ) ;
5148
52- constructor ( private dialog : MatDialog ) { }
53-
54- ngOnChanges ( changes : SimpleChanges ) : void {
55- if ( ' traceData' in changes ) {
56- this . prcessTraceDataToInvocTrace ( ) ;
49+ readonly view = signal < string > ( 'events' ) ;
50+ readonly isTraceView = computed ( ( ) => this . view ( ) === 'trace' ) ;
51+ readonly spansByTraceId = computed ( ( ) => {
52+ if ( ! this . traceData || this . traceData . length == 0 ) {
53+ return new Map < string , Span [ ] > ( ) ;
5754 }
58- }
55+ return this . traceData ( ) . reduce ( ( map , span ) => {
56+ const key = span . trace_id ;
57+ const group = map . get ( key ) ;
58+ if ( group ) {
59+ span . invoc_id = span . attributes ?. [ 'gcp.vertex.agent.invocation_id' ] ;
60+ group . push ( span ) ;
61+ group . sort ( ( a : Span , b : Span ) => a . start_time - b . start_time ) ;
62+ } else {
63+ map . set ( key , [ span ] ) ;
64+ }
65+ return map ;
66+ } , new Map < string , Span [ ] > ( ) ) ;
67+ } ) ;
5968
60- showJson : boolean [ ] = Array ( this . eventsMap . size ) . fill ( false ) ;
69+ showJson : boolean [ ] = Array ( this . eventsMap ( ) . size ) . fill ( false ) ;
6170
6271 toggleJson ( index : number ) {
6372 this . showJson [ index ] = ! this . showJson [ index ] ;
@@ -67,46 +76,24 @@ export class EventTabComponent implements OnChanges {
6776 this . selectedEvent . emit ( key ) ;
6877 }
6978
70- isTraceView ( ) {
71- return this . view == 'trace' ;
72- }
73-
7479 mapOrderPreservingSort = ( a : any , b : any ) : number => 0 ;
7580
76- prcessTraceDataToInvocTrace ( ) {
77- if ( ! this . traceData || this . traceData . length == 0 ) {
78- return ;
79- }
80- this . invocTraces = this . traceData . reduce ( ( map , item ) => {
81- const key = item . trace_id ;
82- const group = map . get ( key ) ;
83- if ( group ) {
84- group . push ( item ) ;
85- group . sort ( ( a : any , b : any ) => a . start_time - b . start_time ) ;
86- } else {
87- map . set ( key , [ item ] ) ;
88- }
89- return map ;
90- } , new Map < string , any [ ] > ( ) ) ;
91- }
92-
93- findInvocIdFromTraceId ( traceId : string ) {
94- const group = this . invocTraces . get ( traceId ) ;
95- return group
96- ?. find (
81+ findInvocId ( spans : Span [ ] ) {
82+ return spans
83+ . find (
9784 item => item . attributes !== undefined &&
9885 'gcp.vertex.agent.invocation_id' in item . attributes )
99- . attributes [ 'gcp.vertex.agent.invocation_id' ]
86+ ? .attributes [ 'gcp.vertex.agent.invocation_id' ]
10087 }
10188
10289 openDialog ( traceId : string ) : void {
90+ const spans = this . spansByTraceId ( ) . get ( traceId ) ;
91+ if ( ! spans ) return ;
92+
10393 const dialogRef = this . dialog . open ( TraceChartComponent , {
10494 width : 'auto' ,
10595 maxWidth : '90vw' ,
106- data : {
107- spans : this . invocTraces . get ( traceId ) ,
108- invocId : this . findInvocIdFromTraceId ( traceId )
109- } ,
96+ data : { spans, invocId : this . findInvocId ( spans ) } ,
11097 } ) ;
11198 }
11299}
0 commit comments