Project

General

Profile

1
import { Component, OnInit } from '@angular/core';
2
import { jobTypes } from '../../domain/job-types';
3

    
4
import { MonitorService } from '../../services/monitor.service';
5
import { AuthenticationService } from '../../services/authentication.service';
6
import { JobsOfUser, StoredJob } from '../../domain/typeScriptClasses';
7
import { ValidatorService } from '../../services/validator.service';
8
import { loadingUserJobs, loadingUserJobsError, noUserJobsFound } from '../../domain/shared-messages';
9
import { URLParameter } from '../../domain/url-parameter';
10

    
11
@Component ({
12
  selector: 'app-compatibility-validation-history',
13
  templateUrl: 'compatibility-validation-history.component.html'
14
})
15

    
16

    
17
export class CompatibilityValidationHistoryComponent  implements OnInit {
18
  userEmail: string;
19
  loadingMessage: string;
20
  errorMessage: string;
21
  infoMessage: string;
22
  successMessage: string;
23
  failureMessage: string;
24

    
25
  jobTypes: string[];
26
  jobsOfUser: JobsOfUser;
27
  jobs: StoredJob[];
28

    
29
  itemsPerPage: number;
30
  currentPage: number;
31
  currentTotalJobs: number;
32
  totalPages: number;
33
  currentFilter: string;
34
  chosenJobType: string;
35

    
36
  constructor(private authService: AuthenticationService,
37
              private monitorService: MonitorService,
38
              private valService: ValidatorService) {}
39

    
40
  ngOnInit() {
41
    this.loadTable();
42
    let body = document.getElementsByTagName('body')[0];
43
    body.classList.remove("top_bar_active");   //remove the class
44
  }
45

    
46
  loadTable() {
47
    // initialize
48
    this.userEmail = this.authService.getUserEmail();
49
    this.jobTypes = jobTypes;
50
    this.itemsPerPage = 10;
51
    this.currentPage = 0;
52
    this.currentFilter = 'all';
53
    this.chosenJobType = '';
54

    
55
    // call API and get all jobs:
56
    this.getJobs();
57
  }
58

    
59

    
60
  getJobType(type: string) {
61
    this.chosenJobType = type;
62
    this.currentPage = 0;
63
    this.getJobs();
64
  }
65

    
66
  filterJobs(filter: string) {
67
    this.currentFilter = filter;
68
    this.currentPage = 0;
69
    console.log(`requesting ${this.currentFilter} jobs`);
70
    this.getJobs();
71
  }
72

    
73
  getItemsPerPage(num: number) {
74
    this.itemsPerPage = num;
75
    this.currentPage = 0;
76
    this.getJobs();
77
  }
78

    
79
  goToNextPage() {
80
    if ( (this.currentPage + 1) < this.totalPages ) {
81
      this.currentPage++;
82
      console.log(`Get me page ${this.currentPage}!`);
83
      this.getJobs();
84
    }
85
  }
86

    
87
  goToPreviousPage() {
88
    if (this.currentPage > 0) {
89
      this.currentPage--;
90
      console.log(`Get me page ${this.currentPage}!`);
91
      this.getJobs();
92
    }
93
  }
94

    
95

    
96
  storedJobs () {
97
    this.valService.getStoredJobsNew('ant.lebesis@gmail.com',
98
                                    'Compatibility Test',
99
                                    '0',
100
                                    '10',
101
                                    '2018-02-01',
102
                                    '2018-02-28',
103
                                    'successful').subscribe(
104
      jobs => this.jobs = jobs,
105
      error => console.log(error.status),
106
      () => {
107
        console.log('Also hit getStoredJobsNew and got:');
108
        console.log(this.jobs);
109
      }
110
    );
111
  }
112

    
113
  getJobs() {
114
    this.loadingMessage = loadingUserJobs;
115
    this.errorMessage = '';
116
    this.infoMessage = '';
117
    this.successMessage = '';
118
    this.failureMessage = '';
119
    const params: URLParameter[] = [];
120
    params.push({key: 'user', value: [this.userEmail]});
121
    if ( this.chosenJobType ) {
122
      params.push({key: 'jobType', value: [this.chosenJobType]});
123
    }
124
    params.push({key: 'offset', value: [( (this.currentPage) * this.itemsPerPage).toString()]});
125
    params.push({key: 'limit', value: [this.itemsPerPage.toString()]});
126
    /*  can also add dateFrom and dateTo if needed */
127
    params.push({key: 'validationStatus', value: [this.currentFilter]});
128
    params.push({key: 'includeJobsTotal', value: ['true']});
129
    this.monitorService.getJobsOfUser(params).subscribe(
130
      jobs => this.jobsOfUser = jobs,
131
      error => {
132
        console.log(`The API returned ${error.status}`);
133
        this.loadingMessage = '';
134
        this.jobsOfUser = null;
135
        this.errorMessage = loadingUserJobsError;
136
      },
137
      () => {
138
        if (this.currentFilter === 'all') {
139
          this.currentTotalJobs = this.jobsOfUser.totalJobs;
140
        } else if (this.currentFilter === 'successful') {
141
          this.currentTotalJobs = this.jobsOfUser.totalJobsSuccessful;
142
        } else if (this.currentFilter === 'failed') {
143
          this.currentTotalJobs = this.jobsOfUser.totalJobsFailed;
144
        } else {
145
          this.currentTotalJobs = this.jobsOfUser.totalJobsOngoing;
146
        }
147
        this.totalPages = Math.ceil(this.currentTotalJobs / this.itemsPerPage);
148
        this.loadingMessage = '';
149
        if (!this.totalPages || !this.jobsOfUser.jobs) {
150
          this.infoMessage = noUserJobsFound;
151
          this.currentPage = -1;
152
        }
153
      }
154
    );
155
  }
156

    
157
  getResultImage(status: string) {
158
    // if (status === 'ongoing') {
159
    //   return `../../../assets/imgs/icon_colours-question.jpg`;
160
    // } else if (status === 'successful') {
161
    //     return `../../../assets/imgs/icon_colours-check.jpg`;
162
    // } else {
163
    //   return `../../../assets/imgs/icon_colours-x.jpg`;
164
    // }
165

    
166
    if (status === 'ongoing') {
167
      return `../../../assets/imgs/icon_colours-question.jpg`;
168
    } else if (status === 'successful') {
169
      return `check_circle`;
170
    } else {
171
      return `../../../assets/imgs/icon_colours-x.jpg`;
172
    }
173
  }
174

    
175
  resubmitJob (id: string, email: string) {
176
    this.valService.reSubmitJobForValidation(id, email).subscribe(
177
      res => this.successMessage = `The job with id ${id} was successfully resubmitted`,
178
      error => {
179
        this.failureMessage = `Could not resubmit the job with id ${id}`;
180
        console.log(error);
181
      }
182
    );
183
    this.getJobs();
184
  }
185

    
186
}
187

    
(12-12/17)