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 } from '@angular/material';
18
19import { AppService } from '../../appservice';
20import { BuildService } from "../build/build.service";
21import { MenuBaseClass } from "../menu_base";
22import { ScheduleService } from "../schedule/schedule.service";
23
24/** Component that handles dashboard. */
25@Component({
26  selector: 'app-dashboard',
27  templateUrl: './dashboard.component.html',
28  providers: [ BuildService, ScheduleService ],
29  styleUrls: ['./dashboard.component.scss']
30})
31export class DashboardComponent extends MenuBaseClass implements OnInit {
32  lastBuildUpdateTime: any = '---';
33  lastScheduleUpdateTime: any = '---';
34
35  constructor(private buildService: BuildService,
36              private scheduleService: ScheduleService,
37              appService: AppService,
38              snackBar: MatSnackBar) {
39    super(appService, snackBar);
40  }
41
42  ngOnInit(): void {
43    this.getLatestBuild();
44    this.getLastestSchedule();
45  }
46
47  /** Fetches the most recently updated build and gets timestamp from it. */
48  getLatestBuild() {
49    this.lastBuildUpdateTime = '---';
50    this.buildService.getBuilds(1, 0, '', 'timestamp', 'desc')
51      .subscribe(
52        (response) => {
53          if (response.builds) {
54            this.lastBuildUpdateTime = response.builds[0].timestamp;
55          }
56        },
57        (error) => this.showSnackbar(`[${error.status}] ${error.name}`)
58      );
59  }
60
61  /** Fetches the most recently updated schedule and gets timestamp from it. */
62  getLastestSchedule() {
63    this.lastScheduleUpdateTime = '---';
64    this.scheduleService.getSchedules(1, 0, '', 'timestamp', 'desc')
65      .subscribe(
66        (response) => {
67          if (response.schedules) {
68            this.lastScheduleUpdateTime = response.schedules[0].timestamp;
69          }
70        },
71        (error) => this.showSnackbar(`[${error.status}] ${error.name}`)
72      );
73  }
74}
75