summaryrefslogtreecommitdiff
path: root/gae/frontend/src/app/menu/build/build.component.ts
blob: e149f878938eadfbf0b1e3c44066945347154662 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatSnackBar, MatTableDataSource, PageEvent } from '@angular/material';

import { Build } from '../../model/build';
import { BuildService } from './build.service';
import { FilterComponent } from '../../shared/filter/filter.component';
import { FilterItem } from '../../model/filter_item';
import { MenuBaseClass } from '../menu_base';


/** Component that handles build menu. */
@Component({
  selector: 'app-build',
  templateUrl: './build.component.html',
  providers: [ BuildService ],
  styleUrls: ['./build.component.scss'],
})
export class BuildComponent extends MenuBaseClass implements OnInit {
  columnTitles = [
    '_index',
    'artifact_type',
    'manifest_branch',
    'build_id',
    'build_target',
    'build_type',
    'signed'];
  dataSource = new MatTableDataSource<Build>();
  pageEvent: PageEvent;
  appliedFilters: FilterItem[];

  @ViewChild(FilterComponent) filterComponent: FilterComponent;

  constructor(private buildService: BuildService,
              public snackBar: MatSnackBar) {
    super(snackBar);
  }

  ngOnInit(): void {
    this.filterComponent.setSelectorList(Build);
    this.getCount();
    this.getBuilds(this.pageSize, this.pageSize * this.pageIndex);
  }

  /** Gets a total count of builds. */
  getCount(observer = this.getDefaultCountObservable()) {
    const filterJSON = (this.appliedFilters) ? JSON.stringify(this.appliedFilters) : '';
    this.buildService.getCount(filterJSON).subscribe(observer);
  }

  /** Gets builds.
   * @param size A number, at most this many results will be returned.
   * @param offset A Number of results to skip.
   */
  getBuilds(size = 0, offset = 0) {
    this.loading = true;
    const filterJSON = (this.appliedFilters) ? JSON.stringify(this.appliedFilters) : '';
    this.buildService.getBuilds(size, offset, filterJSON, '', '')
      .subscribe(
        (response) => {
          this.loading = false;
          if (this.count >= 0) {
            let length = 0;
            if (response.builds) {
              length = response.builds.length;
            }
            const total = length + offset;
            if (response.has_next) {
              if (length !== this.pageSize) {
                this.showSnackbar('Received unexpected number of entities.');
              } else if (this.count <= total) {
                this.getCount();
              }
            } else {
              if (this.count !== total) {
                if (length !== this.count) {
                  this.getCount();
                } else if (this.count > total) {
                  const countObservable = this.getDefaultCountObservable([
                    () => {
                      this.pageIndex = Math.floor(this.count / this.pageSize);
                      this.getBuilds(this.pageSize, this.pageSize * this.pageIndex);
                    }
                  ]);
                  this.getCount(countObservable);
                }
              }
            }
          }
          this.dataSource.data = response.builds;
        },
        (error) => this.showSnackbar(`[${error.status}] ${error.name}`)
      );
  }

  /** Hooks a page event and handles properly. */
  onPageEvent(event: PageEvent) {
    this.pageSize = event.pageSize;
    this.pageIndex = event.pageIndex;
    this.getBuilds(this.pageSize, this.pageSize * this.pageIndex);
    return event;
  }

  /** Applies a filter and get entities with it. */
  applyFilters(filters) {
    this.pageIndex = 0;
    this.appliedFilters = filters;
    this.getCount();
    this.getBuilds(this.pageSize, this.pageSize * this.pageIndex);
  }
}