launcher.component.ts 4.91 KB
Newer Older
Chris Hines's avatar
Chris Hines committed
1
import { Component, OnInit, NgModule, Inject, Input } from '@angular/core';
Chris Hines's avatar
Chris Hines committed
2
3
import { MatTableDataSource, MatDialog, MatDialogRef, MAT_DIALOG_DATA, MatDialogModule, MatDialogConfig } from '@angular/material';

Chris Hines's avatar
Chris Hines committed
4
5
import { Location } from '@angular/common';
// import { HttpClientModule, HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
Chris Hines's avatar
Chris Hines committed
6
import { Observable, Subject } from 'rxjs';
Chris Hines's avatar
Chris Hines committed
7
import { Subscription } from 'rxjs';
Chris Hines's avatar
Chris Hines committed
8
9
import { fromEvent } from 'rxjs';
import { timer } from 'rxjs/observable/timer';
Chris Hines's avatar
Chris Hines committed
10
import { repeat, takeUntil, skip } from 'rxjs/operators';
Chris Hines's avatar
Chris Hines committed
11
import { Router, NavigationStart } from '@angular/router';
Chris Hines's avatar
Chris Hines committed
12

Chris Hines's avatar
Chris Hines committed
13
14
15
import {Strudelapp} from '../strudelapp';
import { StrudelappsService } from '../strudelapps.service';
import { TesService } from '../tes.service';
Chris Hines's avatar
Chris Hines committed
16
import {BackendSelectionService } from '../backend-selection.service';
Chris Hines's avatar
Chris Hines committed
17
import { AuthorisationService } from '../authorisation.service';
Chris Hines's avatar
Chris Hines committed
18
import { Identity } from '../identity';
19
import { Computesite, Health } from '../computesite';
Chris Hines's avatar
Chris Hines committed
20
import { LogoutdialogComponent } from '../logoutdialog/logoutdialog.component';
21

Chris Hines's avatar
Chris Hines committed
22
import { SshAuthzServer } from '../identity';
Chris Hines's avatar
Chris Hines committed
23
import { ComputesitesService } from '../computesites.service';
24
25
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

Chris Hines's avatar
Chris Hines committed
26

27
28


Chris Hines's avatar
Chris Hines committed
29
30
31
32
33
34
export interface Quota {
  resource: string;
  usage: string;
  quota: string;
}

Chris Hines's avatar
Chris Hines committed
35
36
37
38
39
40
41
42
43



@Component({
  selector: 'app-launcher',
  templateUrl: './launcher.component.html',
  styleUrls: ['./launcher.component.css']
})
export class LauncherComponent implements OnInit {
44
  public strudelapps: Strudelapp[];
Chris Hines's avatar
Chris Hines committed
45
  @Input() menuopen: boolean;
Chris Hines's avatar
Chris Hines committed
46

Chris Hines's avatar
Chris Hines committed
47
  public appSubject: BehaviorSubject<Strudelapp>;
48
  public authorised: boolean;
Chris Hines's avatar
Chris Hines committed
49
50
  //public identity: Identity;
  public identitySubject: BehaviorSubject<Identity>;
Chris Hines's avatar
Chris Hines committed
51

52
  public identities: Identity[];
Chris Hines's avatar
Chris Hines committed
53
54
55
  public sshauthzservers: SshAuthzServer[];
  private launchwindow: any;
  private launchwindowWatcher: any;
Chris Hines's avatar
Chris Hines committed
56
  private subscriptions: Subscription[];
57
58
  //public quotas: BehaviorSubject<any[]>;
  displayedColumns: string[] = [];
Chris Hines's avatar
Chris Hines committed
59

Chris Hines's avatar
Chris Hines committed
60
61

  constructor( public dialog: MatDialog,
Chris Hines's avatar
Chris Hines committed
62
                public tesService: TesService,
Chris Hines's avatar
Chris Hines committed
63
                public backendSelectionService: BackendSelectionService,
Chris Hines's avatar
Chris Hines committed
64
                public authService: AuthorisationService,
Chris Hines's avatar
Chris Hines committed
65
                public computeSitesService: ComputesitesService,
Chris Hines's avatar
Chris Hines committed
66
                private router: Router,
67
                ) {
Chris Hines's avatar
Chris Hines committed
68
69
70
71
72
73
      this.subscriptions = [];
      this.subscriptions.push(this.authService.sshAuthzServers.subscribe(o => {this.updateSshAuthZServers(o)}));
      this.subscriptions.push(this.computeSitesService.appidentities.subscribe(o => this.navLogin(o)));
      this.subscriptions.push(this.computeSitesService.appidentities.subscribe(o => this.getHealth(o)));
      this.appSubject = new BehaviorSubject<Strudelapp>(null);
      this.identitySubject = new BehaviorSubject<Identity>(null);
74
    //this.quotas = new BehaviorSubject<any[]>([]);
Chris Hines's avatar
Chris Hines committed
75
76
  }

77
78
79
  countErrors(a: Health[], b: Health[]) {
    var count: number = 0
    var h: Health;
80
81
82
83
84
85
86
87
    if (a != null) {
      for (h of a) {
        if (h.stat == 'error' || h.stat == 'warn') {
          count++;
        }
        if(!isNaN(parseInt(h.stat))) {
          count = count + parseInt(h.stat);
        }
88
      }
89
    }
90
91
92
93
94
95
96
97
    if (b != null) {
      for (h of b) {
        if (h.stat == 'error' || h.stat == 'warn') {
          count++;
        }
        if(!isNaN(parseInt(h.stat))) {
          count = count + parseInt(h.stat);
        }
98
      }
99
100
101
102
    }
    return count;
  }

103
104
105
106
  calculateCols(data) {
    return data.map(function(r) { return r.key });
  }

Chris Hines's avatar
Chris Hines committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  humanKBytes(n: number) {
    if (n > 1024*1024*1024) {
      let v = n/1024/1024/1024;
      return v.toFixed(0)+'TB';
    }
    if (n > 1024*1024) {
      let v = n/1024/1024;
      return v.toFixed(0)+'GB';
    }
    if (n > 1024) {
      let v = n/1024;
      return v.toFixed(0)+'MB';
    }
    return '0 MB';
  }

123
124
  rowClass(row) {
    if (row.stat == 'error' || row.stat == 'warn') {
Chris Hines's avatar
Chris Hines committed
125
126
127
128
129
      return 'health-warn';
    } else {
      return 'health-ok';
    }
  }
130

Chris Hines's avatar
Chris Hines committed
131
132
133
134
  navLogin(o) {
    if (o.length == 0) {
      this.router.navigate(['/login']);
    }
Chris Hines's avatar
Chris Hines committed
135
136
  }

Chris Hines's avatar
Chris Hines committed
137
138
139
140
  getHealth(o) {
    for (let id of o) {
      this.tesService.getHealthAlerts(id);
    }
141
142
143
144
145
146
  }

  updateSshAuthZServers(o) {
    this.sshauthzservers = o;
  }

Chris Hines's avatar
Chris Hines committed
147
148
  ngOnInit() {
    this.strudelapps = [];
149
  }
Chris Hines's avatar
Chris Hines committed
150

Chris Hines's avatar
Chris Hines committed
151
152
153
154
  ngOnDestroy() {
    for (let s of this.subscriptions) {
      s.unsubscribe();
    }
155
156
  }

Chris Hines's avatar
Chris Hines committed
157
158
159
  selectId(id: Identity) {
    this.identitySubject.next(id);
    this.appSubject.next(null);
160
161

    //if (id != null) {
Chris Hines's avatar
Chris Hines committed
162
163
164
165
      // We will subscribe to account alerts until the id changes (in which case we will subscribe to account alerts
      // on the next id
      // BUT
      // since identitySubject is a behaviorsubject and already has a value, we must skip(1)
166
167
    //  id.accountalerts.pipe(takeUntil(this.identitySubject.pipe(skip(1)))).subscribe((msgs) => { this.quotas.next(msgs.filter(q => q.type =='quota')) });
    //}
168
  }
Chris Hines's avatar
Chris Hines committed
169

Chris Hines's avatar
Chris Hines committed
170
171
  selectApp(app: Strudelapp) {
    this.appSubject.next(app);
172
173
  }

Chris Hines's avatar
Chris Hines committed
174

Chris Hines's avatar
Chris Hines committed
175
}