VICE / Client / src / app / server-data.service.ts
server-data.service.ts
Raw
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { BehaviorSubject, interval, timer } from 'rxjs';
import { AuthenticationService, AuthLevel } from './authentication.service';
import { environment } from './../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class ServerDataService {

  private vms = new BehaviorSubject("");
  vmList = this.vms.asObservable();

  private fws = new BehaviorSubject("");
  fwList = this.fws.asObservable();

  private systemInfo = new BehaviorSubject("");
  systemList = this.systemInfo.asObservable();

  private issue = new BehaviorSubject("");
  issueList = this.issue.asObservable();

  private workRqs = new BehaviorSubject("");
  workList = this.workRqs.asObservable();

  private updateTimer : any;
  private intervalNum = interval(100000);

  public locations = ["West US Azure DC", "US-West-1 AWS DC", "Milpitas", "Irvine"];

  constructor( private http: HttpClient, private auth: AuthenticationService ) {
    this.auth.status.subscribe( (auth) => {
      if(auth === AuthLevel.None) {
        if(this.updateTimer != undefined) {
          this.updateTimer.unsubscribe();
        }
      }
      else if(auth >= AuthLevel.Base && auth <= AuthLevel.Admin) {
        this.getDataFromServer();
        this.updateTimer = this.intervalNum.subscribe( () =>
        {this.getDataFromServer()});
      }
    });
  }


  getDataFromServer() {
    this.getVmData();
    this.getSystemData();
    this.getIssueData();
    this.getFwData();
    this.getWorkRqData();
    this.logData();
  }

  getVmData() {
    this.http.get(environment.ip + '/vm', {headers: this.auth.authHeader})
    .subscribe( (data:any) => {
      this.vms.next(data);
    });
  }

  getFwData() {
    this.http.get(environment.ip + '/fw', {headers: this.auth.authHeader})
    .subscribe( (data:any) => {
      this.fws.next(data);
    });
  }

  getSystemData() {
    this.http.get(environment.ip + '/systems', {headers: this.auth.authHeader})
    .subscribe( (data:any) => {
      this.systemInfo.next(data);
    });
  }

  getIssueData() {
    this.http.get(environment.ip + '/issue', {headers: this.auth.authHeader})
    .subscribe( (data:any) => {
      this.issue.next(data);
    });
  }

  getWorkRqData() {
    this.http.get(environment.ip + '/works', {headers: this.auth.authHeader})
    .subscribe( (data:any) => {
      this.workRqs.next(data);
    });
  }

  logData() {
    console.log(this.issueList);
    console.log(this.systemList);
    console.log(this.vmList);
    console.log(this.fwList);
    console.log(this.workList);
  }
}