1- import { AfterViewInit , ChangeDetectorRef , Component , OnInit } from '@angular/core' ;
1+ import { AfterViewInit , ChangeDetectorRef , Component , OnDestroy , OnInit } from '@angular/core' ;
22import { DomSanitizer } from '@angular/platform-browser' ;
33import { ActivatedRoute } from '@angular/router' ;
44import { NgxSpinnerService } from 'ngx-spinner' ;
5+ import { combineLatest , from , of , Subject } from 'rxjs' ;
6+ import { filter , map , switchMap , takeUntil , tap } from 'rxjs/operators' ;
57import { AccountService } from '../../core/auth/account.service' ;
68import { Account } from '../../core/user/account.model' ;
79import { DashboardBehavior } from '../../shared/behaviors/dashboard.behavior' ;
@@ -21,7 +23,7 @@ import {UtmRenderVisualization} from '../shared/services/utm-render-visualizatio
2123 templateUrl : './dashboard-export-pdf.component.html' ,
2224 styleUrls : [ './dashboard-export-pdf.component.scss' ]
2325} )
24- export class DashboardExportPdfComponent implements OnInit , AfterViewInit {
26+ export class DashboardExportPdfComponent implements OnInit , OnDestroy {
2527 dashboardId : number ;
2628 dashboardName : string ;
2729 visualizationRender : UtmDashboardVisualizationType [ ] ;
@@ -36,6 +38,7 @@ export class DashboardExportPdfComponent implements OnInit, AfterViewInit {
3638 filtersValues : ElasticFilterType [ ] = [ ] ;
3739 filterTime : { from : string , to : string } ;
3840 cover : string ;
41+ destroy$ = new Subject < void > ( ) ;
3942
4043 constructor ( private activatedRoute : ActivatedRoute ,
4144 private accountService : AccountService ,
@@ -48,14 +51,7 @@ export class DashboardExportPdfComponent implements OnInit, AfterViewInit {
4851 private cdr : ChangeDetectorRef ) {
4952 }
5053
51- ngAfterViewInit ( ) : void {
52- this . cdr . detectChanges ( ) ;
53- this . themeChangeBehavior . $themeReportCover . subscribe ( img => {
54- this . cover = img ;
55- } ) ;
56- }
57-
58- ngOnInit ( ) {
54+ /*ngOnInit() {
5955 this.activatedRoute.queryParams.subscribe(params => {
6056 const queryParams = Object.entries(params).length > 0 ? params : null;
6157 if (queryParams) {
@@ -68,12 +64,7 @@ export class DashboardExportPdfComponent implements OnInit, AfterViewInit {
6864 this.accountService.identity().then(account => {
6965 this.account = account;
7066 });
71- /*window.addEventListener('beforeprint', (event) => {
72- this.printFormat = true;
73- });
74- window.addEventListener('afterprint', (event) => {
75- this.printFormat = false;
76- });*/
67+
7768 this.activatedRoute.params.subscribe(params => {
7869 this.dashboardId = params.id;
7970 if (this.dashboardId) {
@@ -93,8 +84,66 @@ export class DashboardExportPdfComponent implements OnInit, AfterViewInit {
9384 });
9485 }
9586 });
87+ }*/
88+
89+ ngOnInit ( ) {
90+
91+ this . themeChangeBehavior . $themeReportCover
92+ . pipe ( takeUntil ( this . destroy$ ) )
93+ . subscribe ( img => {
94+ this . cover = img ;
95+ } ) ;
96+
97+ const queryParams$ = this . activatedRoute . queryParams . pipe (
98+ map ( params => Object . keys ( params ) . length > 0 ? params : null ) ,
99+ switchMap ( params => {
100+ if ( ! params ) { return of ( null ) ; }
101+ return from ( parseQueryParamsToFilter ( params ) ) ;
102+ } ) ,
103+ tap ( filters => {
104+ if ( filters && filters . length > 0 ) {
105+ this . filtersValues = filters ;
106+ this . getTimeFilterValue ( ) ;
107+ }
108+ } )
109+ ) ;
110+
111+ const dashboardId$ = this . activatedRoute . params . pipe (
112+ map ( params => params . id ) ,
113+ tap ( id => this . dashboardId = id )
114+ ) ;
115+
116+ combineLatest ( [ queryParams$ , dashboardId$ ] )
117+ . pipe (
118+ filter ( ( [ filters , dashboardId ] ) => ! ! dashboardId ) ,
119+ switchMap ( ( [ filters , dashboardId ] ) => {
120+ const request = {
121+ page : 0 ,
122+ size : 10000 ,
123+ 'idDashboard.equals' : dashboardId ,
124+ sort : 'order,asc'
125+ } ;
126+ return this . utmRenderVisualization . query ( request ) ;
127+ } ) ,
128+ tap ( vis => {
129+ const visualizations = vis . body || [ ] ;
130+ this . dashboardName = visualizations [ 0 ] . dashboard . name ;
131+ this . dashboardDescription = visualizations [ 0 ] . dashboard . description ;
132+
133+ const filters = JSON . parse ( visualizations [ 0 ] . dashboard . filters ) ;
134+ this . dashboardFilters = filters ? filters : [ ] ;
135+
136+ this . applyTimeFilterToVisualizations ( visualizations ) ;
137+
138+ this . loadingVisualizations = false ;
139+ } )
140+ )
141+ . subscribe ( ) ;
142+
143+ this . accountService . identity ( ) . then ( account => this . account = account ) ;
96144 }
97145
146+
98147 setVisFilter ( ) : Promise < boolean > {
99148 return new Promise < boolean > ( resolve => {
100149 for ( const dashFilter of this . getFilterByIndexPattern ( ) ) {
@@ -151,7 +200,7 @@ export class DashboardExportPdfComponent implements OnInit, AfterViewInit {
151200 getTimeFilterValue ( ) {
152201 this . filterTime = {
153202 from : this . resolveFromDate ( this . getTime ( ) ) ,
154- to : this . resolveToDate ( this . getTime ( ) ) ,
203+ to : this . resolveFromDate ( this . getTime ( ) ) ,
155204 } ;
156205 }
157206
@@ -191,4 +240,24 @@ export class DashboardExportPdfComponent implements OnInit, AfterViewInit {
191240 showFilters ( ) : boolean {
192241 return this . filtersValues . filter ( value => value . field !== '@timestamp' ) . length > 0 ;
193242 }
243+
244+ applyTimeFilterToVisualizations ( visualizations : UtmDashboardVisualizationType [ ] ) {
245+ if ( ! this . filtersValues || this . filtersValues . length === 0 ) {
246+ this . visualizationRender = visualizations ;
247+ return ;
248+ }
249+
250+ this . visualizationRender = visualizations . map ( v => ( {
251+ ...v ,
252+ visualization : {
253+ ...v . visualization ,
254+ filterType : [ ...v . visualization . filterType , ... this . filtersValues ]
255+ }
256+ } ) ) ;
257+ }
258+
259+ ngOnDestroy ( ) {
260+ this . destroy$ . next ( ) ;
261+ this . destroy$ . complete ( ) ;
262+ }
194263}
0 commit comments