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
    body.classList.remove("page_heading_active");
45
  }
46

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

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

    
60

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

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

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

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

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

    
96

    
97
  storedJobs () {
98
    this.valService.getStoredJobsNew().subscribe(
99
      jobs => this.jobs = jobs,
100
      error => console.log(error.status),
101
      () => {
102
        console.log('Also hit getStoredJobsNew and got:');
103
        console.log(this.jobs);
104
      }
105
    );
106
  }
107

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

    
152
  getResultImage(status: string) {
153
    // if (status === 'ongoing') {
154
    //   return `../../../assets/imgs/icon_colours-question.jpg`;
155
    // } else if (status === 'successful') {
156
    //     return `../../../assets/imgs/icon_colours-check.jpg`;
157
    // } else {
158
    //   return `../../../assets/imgs/icon_colours-x.jpg`;
159
    // }
160

    
161
    if (status === 'ongoing') {
162
      return `../../../assets/imgs/icon_colours-question.jpg`;
163
    } else if (status === 'successful') {
164
      return `check_circle`;
165
    } else {
166
      return `../../../assets/imgs/icon_colours-x.jpg`;
167
    }
168
  }
169

    
170
  resubmitJob (id: string) {
171
    this.valService.reSubmitJobForValidation(id).subscribe(
172
      res => this.successMessage = `The job with id ${id} was successfully resubmitted`,
173
      error => {
174
        this.failureMessage = `Could not resubmit the job with id ${id}`;
175
        console.log(error);
176
      }
177
    );
178
    this.getJobs();
179
  }
180

    
181
}
182

    
(12-12/17)