Commit 0f929907 authored by Jie Yuan's avatar Jie Yuan
Browse files

add site short service

parent d684b3bc
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
import { TestBed } from '@angular/core/testing';

import { SiteShortService } from './site-short.service';

describe('SiteShortService', () => {
  beforeEach(() => TestBed.configureTestingModule({}));

  it('should be created', () => {
    const service: SiteShortService = TestBed.get(SiteShortService);
    expect(service).toBeTruthy();
  });
});
+344 −0
Original line number Diff line number Diff line
import {EventEmitter, Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {HttpHeaders} from '@angular/common/http';
import { Router } from '@angular/router';

import { SiteShort } from './model/site-short';
import {SharedService} from './shared-service';
import {AuthService} from './auth.service';
import {HostName} from './model/hostname';
import {ServiceComponent} from './model/service-component';
import {Site} from './model/site';

@Injectable()
export class SiteShortService  {



  siteList: SiteShort[];
  /** Site that is currently shown on the site page **/
 
  siteSubscription: EventEmitter<SiteShort>;
  /** Subscription to know if the user is using another source of the sites **/
  siteSourceSubscription: EventEmitter<SiteShort[]>;

  //hosts: HostName[];
  currentSite: string;
  currentHost: string;
  currentService: string;
  currentServiceComp: ServiceComponent;
  currentSelectedSiteName: string;
  currentSelectedSite: SiteShort;
  currentSelectedLongSite: Site;




  constructor(private httpClient: HttpClient, private authService: AuthService, private sharedService: SharedService, private router: Router) {
    this.siteSubscription = new EventEmitter();
    this.siteSourceSubscription = new EventEmitter<SiteShort[]>();
    
  }

  setCurrentSite(siteName: string) {
    if( siteName === '' || siteName === undefined) {
      this.currentSite = '';
       sessionStorage.removeItem('currentSite');
       this.router.navigate(['dashboard']);
    }
    
    this.currentSite = siteName;
    sessionStorage.setItem('currentSite', this.currentSite );
  }

  setCurrentHost(siteName: string, hostName: string) {
    if( siteName === '' || siteName === undefined)  {
      this.currentSite = '';
      
       sessionStorage.removeItem('currentSite');
       this.router.navigate(['dashboard']);
       }
    
    this.currentSite = siteName;
    this.currentHost = hostName;
    sessionStorage.setItem('currentSite', this.currentSite );
  }




  setCurrentServiceComponent(sp: ServiceComponent){
    this.currentServiceComp = sp;
  }


  getHostsShort() {
    return this.currentSelectedSite.hostNameInfos;
  }




  getServiceComponentsForAHost() {

    console.log('Fetching service lists from host  ' + this.currentHost );
    const promise = new Promise(
      (resolve, reject) => {
        /** Gets the sites from the source specified (GOCDB or DPMT) **/
        const getUrl = this.sharedService.serviceCompURL  + this.currentHost + '/all';
        if ( sessionStorage.getItem('authHeader') !== '') {
          const headers = new HttpHeaders( sessionStorage.getItem('authHeader') ? {
           'Authorization' : sessionStorage.getItem('authHeader'),
          'Content-Type' : 'application/json'
         } : {}
         );
         this.httpClient.get<ServiceComponent[]>(getUrl , {headers : headers })
         .subscribe(
           (list: ServiceComponent[]) => {
             resolve(list);
           },
           (error) => {
             reject(error);
           }
         );
        }
      });
    return promise;
  }




  getASite(sitename: string) {
    console.log('fetch a site with its name');
    const promise = new Promise(
      (resolve, reject) => {
        /** Gets the sites from the source specified (GOCDB or DPMT) **/
        const getUrl = this.sharedService.siteURL + '/' + sitename;
        if ( sessionStorage.getItem('authHeader') !== '') {
          const headers = new HttpHeaders( sessionStorage.getItem('authHeader') ? {
           'Authorization' : sessionStorage.getItem('authHeader'),
          'Content-Type' : 'application/json'
         } : {}
         );
         this.httpClient.get<Site>(getUrl , {headers : headers })
         .subscribe(
           (asite: Site) => {
             resolve(asite);
           },
           (error) => {
             reject(error);
           }
         );
        }
      });
    return promise;

  }



  /**
   * Gets all the servers list from GOCDB
   */
  getSiteListFromGOCDB() {
    return this.getSiteListFromSource('gocdb');
  }

  /**
   * Gets all the servers list from DPMT
   */
  getSiteListFromDPMT() {
    return this.getSiteListFromSource('dpmt');
  }

  /**
   * Gets ALL the server from the backend
   * @returns {Promise<any>}
   */
  getSiteList() {
      console.log('Fetching server lists from backend');
    const promise = new Promise(
      (resolve, reject) => {
        /** Gets the sites from the source specified (GOCDB or DPMT) **/
        const getUrl = this.sharedService.siteShortURL + '/all';
        if ( sessionStorage.getItem('authHeader') !== '') {
          const headers = new HttpHeaders( sessionStorage.getItem('authHeader') ? {
           'Authorization' : sessionStorage.getItem('authHeader'),
          'Content-Type' : 'application/json'
         } : {}
         );
         this.httpClient.get<SiteShort[]>(getUrl , {headers : headers })
         .subscribe(
           (sitesList: SiteShort[]) => {
             this.siteList = sitesList;
             sessionStorage.setItem('JsonSiteShortListALL', JSON.stringify(this.siteList));
             resolve(this.siteList);
           },
           (error) => {
             reject(error);
           }
         );
        }
      });
    return promise;
}

  getSiteListFromSource(source: String) {
    console.log('Fetching server lists from backend with source');
    const promise = new Promise(
      (resolve, reject) => {
      /** Gets the sites from the source specified (GOCDB or DPMT) **/
      const getUrl = this.sharedService.siteShortURL + '/' + source + '/all';
      if ( sessionStorage.getItem('authHeader') !== '') {
          const headers = new HttpHeaders( true ? {
           'Authorization' : sessionStorage.getItem('authHeader'),
           'Content-Type' : 'application/json'
         } : {}
         );
         this.httpClient.get<SiteShort[]>(getUrl , {headers : headers })
         .subscribe(
           (sitesList: SiteShort[]) => {
             this.siteList = sitesList;
            
             resolve(this.siteList);
           },
           (error) => {
             reject(error);
           }
         );
        }
    });
    return promise;
  }



  setSelectedSite(newSelectedSite: SiteShort) {
    this.currentSelectedSite = newSelectedSite;
    this.siteSubscription.emit(newSelectedSite);
  }
  getCurrentSelectedSite() {
    return this.currentSelectedSite;
  }

  /**
   * Returns true if the user can enter to the dashboard/site url
   * @returns {boolean}
   */
  canEnterToSite(): boolean {
    if (this.currentSelectedSite != null) {return true; } else {return false; }
  }

  getHosts() {
    console.log('Fetching host lists from ' + this.currentSite);
    const promise = new Promise(
      (resolve, reject) => {
        /** Gets the sites from the source specified (GOCDB or DPMT) **/
        const getUrl = this.sharedService.hostsURL + '/' + this.currentSite;
        if ( sessionStorage.getItem('authHeader') !== '') {
          const headers = new HttpHeaders( sessionStorage.getItem('authHeader') ? {
           'Authorization' : sessionStorage.getItem('authHeader'),
          'Content-Type' : 'application/json'
         } : {}
         );
         this.httpClient.get<HostName[]>(getUrl , {headers : headers })
         .subscribe(
           (list: HostName[]) => {
             resolve(list);
           },
           (error) => {
             reject(error);
           }
         );
        }
      });
    return promise;
  }


  selectSourceSite(sourceSelected: string) {
    switch (sourceSelected) {
      case 'all': {
        this.getSiteList()
          .then(
            (serverList: SiteShort[]) => {
              this.siteSourceSubscription.emit(serverList);
            }
          )
          .catch(
            error => {
              console.log('Error while loading sites');
            }
          );
        break;
      }
      case 'gocdb': {
        this.getSiteListFromGOCDB()
          .then(
            (serverList: SiteShort[]) => {
              this.siteSourceSubscription.emit(serverList);
            }
          )
          .catch(
            error => {
              console.log('Error while loading sites');
            }
          );
        break;
      }
      case 'dpmt': {
        this.getSiteListFromDPMT()
          .then(
            (serverList: SiteShort[]) => {
              this.siteSourceSubscription.emit(serverList);
            }
          )
          .catch(
            error => {
              console.log('Error while loading sites');
            }
          );
        break;
      }
    }
  }



 

  getSiteNameListFromSource(source: String) {
    console.log('Fetching server name lists from backend with source');
    const promise = new Promise(
      (resolve, reject) => {
      /** Gets the sites from the source specified (GOCDB or DPMT) **/
      let getUrl;
      if (source === 'all') {
        getUrl = this.sharedService.siteURL + '/siteNames/all';
      } else {
        getUrl = this.sharedService.siteURL + '/siteNames/' + source + '/all';
      }
      if ( sessionStorage.getItem('authHeader') !== '') {
          const headers = new HttpHeaders( true ? {
           'Authorization' : sessionStorage.getItem('authHeader'),
           'Content-Type' : 'application/json'
         } : {}
         );
         this.httpClient.get<string[]>(getUrl , {headers : headers })
         .subscribe(
           (nameList: string[]) => {
             
             resolve(nameList);
           },
           (error) => {
             reject(error);
           }
         );
        }
    });
    return promise;
  }


}
 No newline at end of file