11import { Component , OnDestroy , OnInit } from '@angular/core' ;
22import { environment } from '@app/environment' ;
33import { Subscription } from 'rxjs' ;
4- import isEqual from 'lodash-es/isEqual' ;
54import {
65 ConfirmDialogService ,
76 DIALOG_RESP ,
8- ExponentialBackoff ,
97 NamespaceService ,
108 ActionEvent ,
119 DashboardState ,
1210 ToolbarButton ,
11+ PollerService ,
1312} from 'kubeflow' ;
1413
1514import { KWABackendService } from 'src/app/services/backend.service' ;
@@ -18,7 +17,7 @@ import {
1817 ExperimentsProcessed ,
1918} from '../../models/experiment.model' ;
2019import { experimentsTableConfig } from './config' ;
21- import { getDeleteDialogConfig } from './delete-modal-config' ;
20+ import { generateDeleteConfig } from './delete-modal-config' ;
2221import { Router } from '@angular/router' ;
2322
2423@Component ( {
@@ -27,50 +26,67 @@ import { Router } from '@angular/router';
2726 styleUrls : [ './experiments.component.scss' ] ,
2827} )
2928export class ExperimentsComponent implements OnInit , OnDestroy {
30- experiments : ExperimentsProcessed = [ ] ;
31- currNamespace : string ;
32- config = experimentsTableConfig ;
3329 env = environment ;
34- dashboardDisconnectedState = DashboardState . Disconnected ;
3530
36- private subs = new Subscription ( ) ;
37- private poller : ExponentialBackoff ;
31+ nsSub = new Subscription ( ) ;
32+ pollSub = new Subscription ( ) ;
3833
39- private rawData : Experiment [ ] = [ ] ;
34+ currNamespace : string | string [ ] ;
35+ config = experimentsTableConfig ;
36+ experiments : ExperimentsProcessed = [ ] ;
4037
41- buttons : ToolbarButton [ ] = [
42- new ToolbarButton ( {
43- text : `New Experiment` ,
44- icon : 'add' ,
45- stroked : true ,
46- fn : ( ) => {
47- this . router . navigate ( [ '/new' ] ) ;
48- } ,
49- } ) ,
50- ] ;
38+ dashboardDisconnectedState = DashboardState . Disconnected ;
39+
40+ private newExperimentButton = new ToolbarButton ( {
41+ text : $localize `New Experiment` ,
42+ icon : 'add' ,
43+ stroked : true ,
44+ fn : ( ) => {
45+ this . router . navigate ( [ '/new' ] ) ;
46+ } ,
47+ } ) ;
48+
49+ buttons : ToolbarButton [ ] = [ this . newExperimentButton ] ;
5150
5251 constructor (
5352 private backend : KWABackendService ,
5453 private confirmDialog : ConfirmDialogService ,
5554 private router : Router ,
5655 public ns : NamespaceService ,
56+ public poller : PollerService ,
5757 ) { }
5858
59- ngOnInit ( ) {
60- this . startExperimentsPolling ( ) ;
61-
59+ ngOnInit ( ) : void {
6260 // Reset the poller whenever the selected namespace changes
63- this . subs . add (
64- this . ns . getSelectedNamespace ( ) . subscribe ( nameSpace => {
65- this . currNamespace = nameSpace ;
66- this . poller . reset ( ) ;
67- } ) ,
68- ) ;
61+ this . nsSub = this . ns . getSelectedNamespace2 ( ) . subscribe ( ns => {
62+ this . currNamespace = ns ;
63+ this . poll ( ns ) ;
64+ this . newExperimentButton . namespaceChanged ( ns , $localize `Experiment` ) ;
65+ } ) ;
66+ }
67+
68+ ngOnDestroy ( ) {
69+ this . nsSub . unsubscribe ( ) ;
70+ this . pollSub . unsubscribe ( ) ;
6971 }
7072
71- ngOnDestroy ( ) : void {
72- this . subs . unsubscribe ( ) ;
73- this . poller . stop ( ) ;
73+ public poll ( ns : string | string [ ] ) {
74+ this . pollSub . unsubscribe ( ) ;
75+ this . experiments = [ ] ;
76+
77+ const request = this . backend . getExperiments ( ns ) ;
78+
79+ this . pollSub = this . poller . exponential ( request ) . subscribe ( experiments => {
80+ this . experiments = experiments . map ( row => {
81+ return {
82+ ...row ,
83+ link : {
84+ text : row . name ,
85+ url : `/experiment/${ row . name } ` ,
86+ } ,
87+ } ;
88+ } ) ;
89+ } ) ;
7490 }
7591
7692 trackByFn ( index : number , experiment : Experiment ) {
@@ -81,76 +97,38 @@ export class ExperimentsComponent implements OnInit, OnDestroy {
8197 const exp = a . data as Experiment ;
8298 switch ( a . action ) {
8399 case 'delete' :
84- this . onDeleteExperiment ( exp . name ) ;
100+ this . deleteClicked ( exp ) ;
85101 break ;
86102 }
87103 }
88104
89- onDeleteExperiment ( name : string ) {
90- const deleteDialogConfig = getDeleteDialogConfig ( name , this . currNamespace ) ;
91- const ref = this . confirmDialog . open ( name , deleteDialogConfig ) ;
105+ deleteClicked ( exp : Experiment ) {
106+ const deleteDialogConfig = generateDeleteConfig ( exp . name ) ;
107+ const ref = this . confirmDialog . open ( exp . name , deleteDialogConfig ) ;
92108
93109 const delSub = ref . componentInstance . applying$ . subscribe ( applying => {
94110 if ( ! applying ) {
95111 return ;
96112 }
97113
98114 // Close the open dialog only if the DELETE request succeeded
99- this . backend . deleteExperiment ( name , this . currNamespace ) . subscribe ( {
100- next : _ => {
101- this . poller . reset ( ) ;
115+ this . backend . deleteExperiment ( exp . name , exp . namespace ) . subscribe (
116+ res => {
102117 ref . close ( DIALOG_RESP . ACCEPT ) ;
103118 } ,
104- error : err => {
105- const errorMsg = err ;
106- deleteDialogConfig . error = errorMsg ;
119+ err => {
120+ deleteDialogConfig . error = err ;
107121 ref . componentInstance . applying$ . next ( false ) ;
108122 } ,
109- } ) ;
123+ ) ;
110124
111125 // DELETE request has succeeded
112126 ref . afterClosed ( ) . subscribe ( res => {
113127 delSub . unsubscribe ( ) ;
114128 if ( res !== DIALOG_RESP . ACCEPT ) {
115129 return ;
116130 }
117-
118- this . poller . reset ( ) ;
119131 } ) ;
120132 } ) ;
121133 }
122-
123- private startExperimentsPolling ( ) {
124- this . poller = new ExponentialBackoff ( { interval : 1000 , retries : 3 } ) ;
125-
126- // Poll for new data and reset the poller if different data is found
127- this . subs . add (
128- this . poller . start ( ) . subscribe ( ( ) => {
129- if ( ! this . currNamespace ) {
130- return ;
131- }
132-
133- this . backend
134- . getExperiments ( this . currNamespace )
135- . subscribe ( experiments => {
136- if ( isEqual ( this . rawData , experiments ) ) {
137- return ;
138- }
139-
140- this . experiments = experiments . map ( row => {
141- return {
142- ...row ,
143- link : {
144- text : row . name ,
145- url : `/experiment/${ row . namespace } /${ row . name } ` ,
146- } ,
147- } ;
148- } ) ;
149-
150- this . rawData = experiments ;
151- this . poller . reset ( ) ;
152- } ) ;
153- } ) ,
154- ) ;
155- }
156134}
0 commit comments