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
    body.classList.remove("landing");
46
    body.classList.add("dashboard");
47
  }
48

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

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

    
62

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

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

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

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

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

    
98

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

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

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

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

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

    
183
}
184

    
(6-6/11)