New dashboard look & feel
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / xds-config.service.ts
1 import { Injectable } from '@angular/core';
2 import { Observable } from 'rxjs/Observable';
3 import { Subject } from 'rxjs/Subject';
4 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
5
6 import { AlertService, IAlert } from '../services/alert.service';
7 import { XDSAgentService, IAgentStatus, IXDServerCfg } from '../../@core-xds/services/xdsagent.service';
8
9 import 'rxjs/add/operator/publish';
10 import 'rxjs/add/operator/map';
11 import 'rxjs/add/operator/catch';
12
13
14 @Injectable()
15 export class XDSConfigService {
16
17   // Conf$: Observable<IXdsConfig>;
18   xdsServers: IXDServerCfg[];
19
20   // private confSubject: BehaviorSubject<IXdsConfig>;
21   // private confStore: IXdsConfig;
22
23   private _curServer: IXDServerCfg = { id: '', url: '', connRetry: 0, connected: false };
24   private curServer$ = new Subject<IXDServerCfg>();
25
26   constructor(
27     private alert: AlertService,
28     private xdsAgentSvr: XDSAgentService,
29   ) {
30     /*
31     this.confSubject = <BehaviorSubject<IXdsConfig>>new BehaviorSubject(this.confStore);
32     this.Conf$ = this.confSubject.asObservable();
33     */
34
35     // Update servers list
36     this.xdsAgentSvr.XdsConfig$.subscribe(cfg => {
37       if (!cfg || cfg.servers.length < 1) {
38         return;
39       }
40       this.xdsServers = cfg.servers;
41       this._updateCurServer();
42     });
43   }
44
45   onCurServer(): Observable<IXDServerCfg> {
46     return this.curServer$.publish().refCount();
47   }
48
49   getCurServer(): IXDServerCfg {
50     return Object.assign({}, this._curServer);
51   }
52
53   setCurServer(svr: IXDServerCfg): Observable<IXDServerCfg> {
54     const curSvr = this._getCurServer();
55
56     if (!curSvr.connected || curSvr.url !== svr.url) {
57       return this.xdsAgentSvr.setServerUrl(curSvr.id, svr.url, svr.connRetry)
58         .map(cfg => this._updateCurServer())
59         .catch(err => {
60           this._curServer.connected = false;
61           this.curServer$.next(Object.assign({}, this._curServer));
62           return Observable.throw(err);
63         });
64     } else {
65       if (curSvr.connRetry !== svr.connRetry) {
66         return this.xdsAgentSvr.setServerRetry(curSvr.id, svr.connRetry)
67           .map(cfg => this._updateCurServer())
68           .catch(err => {
69             this.curServer$.next(Object.assign({}, this._curServer));
70             return Observable.throw(err);
71           });
72       }
73     }
74     return Observable.of(curSvr);
75   }
76
77   private _updateCurServer() {
78     this._curServer = this._getCurServer();
79     this.curServer$.next(Object.assign({}, this._curServer));
80   }
81
82   private _getCurServer(url?: string): IXDServerCfg {
83     if (!this.xdsServers) {
84       return this._curServer;
85     }
86
87     // Init the 1st time
88     let svrUrl = url || this._curServer.url;
89     if (this._curServer.url === '' && this.xdsServers.length > 0) {
90       svrUrl = this.xdsServers[0].url;
91     }
92
93     const svr = this.xdsServers.filter(s => s.url === svrUrl);
94     return svr[0];
95   }
96
97 }