launcher.component.ts 4.58 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[];
Chris Hines's avatar
Chris Hines committed
57
  public quotas: BehaviorSubject<any[]>;
Chris Hines's avatar
Chris Hines committed
58
  displayedColumns: string[] = ['pu','pq'];
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);
Chris Hines's avatar
Chris Hines committed
74
      this.quotas = new BehaviorSubject<any[]>([]);
Chris Hines's avatar
Chris Hines committed
75
76
  }

77
78
79
80
  countErrors(a: Health[], b: Health[]) {
    var count: number = 0
    var h: Health;
    for (h of a) {
Chris Hines's avatar
Chris Hines committed
81
      if (h.stat == 'error' || h.stat == 'warn') {
82
83
84
85
        count++;
      }
    }
    for (h of b) {
Chris Hines's avatar
Chris Hines committed
86
      if (h.stat == 'error' || h.stat == 'warn') {
87
88
89
90
91
92
        count++;
      }
    }
    return count;
  }

Chris Hines's avatar
Chris Hines committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  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';
  }

  quotaClass(quota) {
    if (quota.stat == 'error' || quota.stat == 'warn') {
      return 'health-warn';
    } else {
      return 'health-ok';
    }
  }
116

Chris Hines's avatar
Chris Hines committed
117
118
119
120
  navLogin(o) {
    if (o.length == 0) {
      this.router.navigate(['/login']);
    }
Chris Hines's avatar
Chris Hines committed
121
122
  }

Chris Hines's avatar
Chris Hines committed
123
124
125
126
  getHealth(o) {
    for (let id of o) {
      this.tesService.getHealthAlerts(id);
    }
127
128
129
130
131
132
  }

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

Chris Hines's avatar
Chris Hines committed
133
134
  ngOnInit() {
    this.strudelapps = [];
Chris Hines's avatar
Chris Hines committed
135

136
  }
Chris Hines's avatar
Chris Hines committed
137

Chris Hines's avatar
Chris Hines committed
138
139
140
141
  ngOnDestroy() {
    for (let s of this.subscriptions) {
      s.unsubscribe();
    }
142
143
  }

Chris Hines's avatar
Chris Hines committed
144
145
146
  selectId(id: Identity) {
    this.identitySubject.next(id);
    this.appSubject.next(null);
Chris Hines's avatar
Chris Hines committed
147
148
149
150
151
152
153
    if (id != null) {
      // 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)
      id.accountalerts.pipe(takeUntil(this.identitySubject.pipe(skip(1)))).subscribe((msgs) => { this.quotas.next(msgs.filter(q => q.type =='quota')) });
    }
154
  }
Chris Hines's avatar
Chris Hines committed
155

Chris Hines's avatar
Chris Hines committed
156
157
  selectApp(app: Strudelapp) {
    this.appSubject.next(app);
158
159
  }

Chris Hines's avatar
Chris Hines committed
160

Chris Hines's avatar
Chris Hines committed
161
}