• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { Component, OnInit } from '@angular/core';
17import { MatSnackBar, MatTableDataSource, PageEvent } from '@angular/material';
18
19import { AppService } from '../../appservice';
20import { Host } from '../../model/host';
21import { Lab } from '../../model/lab';
22import { LabService } from './lab.service';
23import { MenuBaseClass } from '../menu_base';
24
25/** Component that handles lab and host menu. */
26@Component({
27  selector: 'app-lab',
28  templateUrl: './lab.component.html',
29  providers: [ LabService ],
30  styleUrls: ['./lab.component.scss'],
31})
32export class LabComponent extends MenuBaseClass implements OnInit {
33  labColumnTitles = [
34    '_index',
35    'name',
36    'owner',
37    'admin',
38    'hostCount',
39  ];
40  hostColumnTitles = [
41    '_index',
42    'name',
43    'hostname',
44    'ip',
45    'host_equipment',
46    'vtslab_version',
47  ];
48  labCount = -1;
49  labPageIndex = 0;
50
51  constructor(private labService: LabService,
52              appService: AppService,
53              snackBar: MatSnackBar) {
54    super(appService, snackBar);
55  }
56
57  labDataSource = new MatTableDataSource<Lab>();
58  hostDataSource = new MatTableDataSource<Host>();
59
60  ngOnInit(): void {
61    // For labs and hosts, it does not use query pagination.
62    this.getHosts();
63  }
64
65  /** Gets hosts.
66   * @param size A number, at most this many results will be returned.
67   * @param offset A Number of results to skip.
68   */
69  getHosts(size = 0, offset = 0) {
70    this.loading = true;
71    // Labs will not use filter for query.
72    const filterJSON = '';
73    this.labService.getLabs(size, offset, filterJSON, '', '')
74      .subscribe(
75        (response) => {
76          this.loading = false;
77          if (response.labs) {
78            this.count = response.labs.length;
79            this.hostDataSource.data = response.labs;
80            this.setLabs(response.labs);
81          }
82        },
83        (error) => this.showSnackbar(`[${error.status}] ${error.name}`)
84      );
85  }
86
87  /** Sets labs from given hosts.
88   * @param hosts A list of Host instances.
89   */
90  setLabs(hosts: Host[]) {
91    if (hosts == null || hosts.length === 0) { return; }
92    const labMap = new Map();
93    hosts.forEach(function(host) {
94      if (labMap.has(host.name)) {
95        labMap.get(host.name).hosts.push(host);
96      } else {
97        labMap.set(host.name, {name: host.name, owner: host.owner, admin: host.admin, hosts: [host]});
98      }
99    });
100    const labs: Lab[] = [];
101    labMap.forEach((value) => labs.push(value));
102    this.labDataSource.data = labs;
103  }
104}
105