Project

General

Profile

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

    
4

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

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

    
18

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

    
27
  jobTypes: string[];
28
  jobsOfUser: JobsOfUser;
29
  jobs: StoredJob[];
30

    
31
  itemsPerPage: number;
32
  currentPage: number;
33
  currentTotalJobs: number;
34
  totalPages: number;
35
  currentFilter: string;
36
  chosenJobType: string;
37

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

    
42
  ngOnInit() {
43
    this.loadTable();
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 < 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
    let 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.errorMessage = loadingUserJobsError;
135
      },
136
      () => {
137
        if (this.currentFilter == 'all') {
138
          this.currentTotalJobs = this.jobsOfUser.totalJobs;
139
        } else if (this.currentFilter == 'successful') {
140
          this.currentTotalJobs = this.jobsOfUser.totalJobsSuccessful;
141
        } else if (this.currentFilter == 'failed') {
142
          this.currentTotalJobs = this.jobsOfUser.totalJobsFailed;
143
        } else {
144
          this.currentTotalJobs = this.jobsOfUser.totalJobsOngoing;
145
        }
146
        this.totalPages = Math.ceil(this.currentTotalJobs / this.itemsPerPage);
147
        this.loadingMessage = '';
148
        if (!this.totalPages || !this.jobsOfUser.jobs) {
149
          this.infoMessage = noUserJobsFound;
150
          this.currentPage = -1;
151
        }
152
      }
153
    );
154
  }
155

    
156
  getResultImage(ended: string, error: string) {
157
    if (!ended) {
158
      return `../../../assets/imgs/icon_colours-question.jpg`;
159
    } else {
160
      if (error == 'no errors') {
161
        return `../../../assets/imgs/icon_colours-check.jpg`;
162
      } else {
163
        return `../../../assets/imgs/icon_colours-x.jpg`;
164
      }
165
    }
166
  }
167

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

    
179
}
180

    
(10-10/15)