site.service.ts 9.52 KB
Newer Older
Jie Yuan's avatar
Jie Yuan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import {EventEmitter, Injectable} from '@angular/core';
import { Site } from '../../models/Site';
import {HostName} from '../../models/HostName';
import {ServiceComponent} from '../../models/ServiceComponent';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Subscription} from 'rxjs/Subscription';
import {AuthService} from '../Auth/auth.service';
import {SidebarComponent} from '../../components/sidebar/sidebar.component';
import {SharedService} from '../../../app/sharedService';

@Injectable()
export class SiteService {

  /** Old way with http **/
  //private URL = 'http://svmon-dev.scc.kit.edu:8080';
  /** New https way **/
  //private URL = 'https://svmon-dev.scc.kit.edu:8443';
  //private URL = 'https://localhost:8443/api';

  siteList: Site[];
21
  siteNamesList: string[];
Jie Yuan's avatar
Jie Yuan committed
22
23
  /** Site that is currently shown on the site page **/
  currentSelectedSite: Site;
24
  currentSelectedSiteName: string;
Jie Yuan's avatar
Jie Yuan committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  siteSubscription: EventEmitter<Site>;
  /** Subscription to know if the user is using another source of the sites **/
  siteSourceSubscription: EventEmitter<Site[]>;

  constructor(private httpClient: HttpClient, private authService: AuthService, private sharedService: SharedService) {
    //this.getSiteListFromGOCDB();
    //this.getSiteListFromDPMT();
    this.siteSubscription = new EventEmitter();
    this.siteSourceSubscription = new EventEmitter<Site[]>();
    console.log('from site service constructoe');
  }

  /**
   * 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');
57
    
Jie Yuan's avatar
Jie Yuan committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    const promise = new Promise(
      (resolve, reject) => {
        /** Gets the sites from the source specified (GOCDB or DPMT) **/
        const getUrl = this.sharedService.siteURL + '/all';
        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(
           (sitesList: Site[]) => {
             this.siteList = sitesList;
72
             sessionStorage.setItem('JsonSiteListALL', JSON.stringify(this.siteList));
Jie Yuan's avatar
Jie Yuan committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
             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) **/
89
90
91
92
93
94
      let getUrl;
      if (source === 'all') {
        getUrl = this.sharedService.siteURL + '/all';
      } else {
        getUrl = this.sharedService.siteURL + '/' + source + '/all';
      }
Jie Yuan's avatar
Jie Yuan committed
95
96
97
98
99
100
101
102
103
104
      if ( sessionStorage.getItem('authHeader') !== '') {
          const headers = new HttpHeaders( true ? {
           'Authorization' : sessionStorage.getItem('authHeader'),
           'Content-Type' : 'application/json'
         } : {}
         );
         this.httpClient.get<Site[]>(getUrl , {headers : headers })
         .subscribe(
           (sitesList: Site[]) => {
             this.siteList = sitesList;
105
106
             const tmp = 'JsonSiteList' + source.toUpperCase();
             sessionStorage.setItem(tmp, JSON.stringify(this.siteList));
Jie Yuan's avatar
Jie Yuan committed
107
108
109
110
111
112
113
114
115
116
             resolve(this.siteList);
           },
           (error) => {
             reject(error);
           }
         );
        }
    });
    return promise;
  }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  

  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[]) => {
             this.siteNamesList = nameList;
             const tmp = 'JsonSiteNameList' + source.toUpperCase();
             sessionStorage.setItem(tmp, JSON.stringify(this.siteNamesList));
             resolve(this.siteNamesList);
           },
           (error) => {
             reject(error);
           }
         );
        }
    });
    return promise;
  }
152
153


Jie Yuan's avatar
Jie Yuan committed
154
155
  setSelectedSite(newSelectedSite: Site) {
    this.currentSelectedSite = newSelectedSite;
156
    sessionStorage.setItem('currentSelectedSite', JSON.stringify(this.currentSelectedSite));
Jie Yuan's avatar
Jie Yuan committed
157
158
    this.siteSubscription.emit(newSelectedSite);
  }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
 
  setSelectedSiteName(newSelectedSiteName: string) {
    const tmp = sessionStorage.getItem('currentSelectedSiteName');
    if ( newSelectedSiteName !== undefined && newSelectedSiteName !== null && newSelectedSiteName !== tmp){
      this.currentSelectedSiteName = tmp;
      sessionStorage.setItem('currentSelectedSiteName',  this.currentSelectedSiteName);
    }
  }


  

  setCurrentSelectedSiteBySiteName()  {
    if (this.currentSelectedSiteName !== null && this.currentSelectedSiteName !== undefined) {
      sessionStorage.setItem('currentSelectedSiteName', this.currentSelectedSiteName);
      this.currentSelectedSiteName = null;
    }
    const tmp = sessionStorage.getItem('currentSelectedSiteName');
    if ( tmp !== null && tmp !== '' && tmp !== undefined) {
      this.getASite(tmp).then( (asite: Site) => {
        this.currentSelectedSite = asite;
        sessionStorage.setItem('currentSelectedSite', JSON.stringify(this.currentSelectedSite));
        this.siteSubscription.emit(asite);
      }
      )
      .catch(
        errot => {
          console.log('can not get a site');
        }
      )
    }
  }
191

Jie Yuan's avatar
Jie Yuan committed
192
  getCurrentSelectedSite() {
193
    if (this.currentSelectedSite !== null && this.currentSelectedSite !== undefined) {
Jie Yuan's avatar
Jie Yuan committed
194
195
196
197
198
199
      sessionStorage.setItem('currentSelectedSite', JSON.stringify(this.currentSelectedSite));
      this.currentSelectedSite = null;
    }
    const tmp = sessionStorage.getItem('currentSelectedSite');
    if ( tmp !== null && tmp !== '' && tmp !== undefined) {
      return JSON.parse(tmp);
200
    }
Jie Yuan's avatar
Jie Yuan committed
201
202
  }

Jie Yuan's avatar
Jie Yuan committed
203
204


Jie Yuan's avatar
Jie Yuan committed
205
206
207
208
209
210
211
212
213
  /**
   * 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; }
  }

  selectSourceSite(sourceSelected: string) {
Jie Yuan's avatar
Jie Yuan committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

    const tmp = 'JsonSiteList' + sourceSelected.toUpperCase();
    const sessionData = sessionStorage.getItem(tmp);
    if (sessionData !== null ) {
       const list = JSON.parse(sessionData);
       this.siteSourceSubscription.emit(list);
    } else {

      switch (sourceSelected) {
        case 'all': {
          this.getSiteList()
            .then(
              (serverList: Site[]) => {
                sessionStorage.setItem('JsonSiteListALL', JSON.stringify(serverList));
                this.siteSourceSubscription.emit(serverList);
              }
            )
            .catch(
              error => {
                console.log('Error while loading sites');
              }
            );
          break;
        }
        case 'gocdb': {
          this.getSiteListFromGOCDB()
            .then(
              (serverList: Site[]) => {
                sessionStorage.setItem('JsonSiteListGOCDB', JSON.stringify(serverList));
                this.siteSourceSubscription.emit(serverList);
              }
            )
            .catch(
              error => {
                console.log('Error while loading sites');
              }
            );
          break;
        }
        case 'dpmt': {
          this.getSiteListFromDPMT()
            .then(
              (serverList: Site[]) => {
                sessionStorage.setItem('JsonSiteListDPMT', JSON.stringify(serverList));
                this.siteSourceSubscription.emit(serverList);
              }
            )
            .catch(
              error => {
                console.log('Error while loading sites');
              }
            );
          break;
        }
Jie Yuan's avatar
Jie Yuan committed
268
      }
Jie Yuan's avatar
Jie Yuan committed
269

Jie Yuan's avatar
Jie Yuan committed
270
271
    }
  }
Jie Yuan's avatar
Jie Yuan committed
272
273


274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304



  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;

  }


Jie Yuan's avatar
Jie Yuan committed
305
}