job.component.ts 2.81 KB
Newer Older
Chris Hines's avatar
Chris Hines committed
1
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
Chris Hines's avatar
Chris Hines committed
2
3
4
import { Job } from '../job';
import {TesService} from '../tes.service';
import { StrudelappsService } from '../strudelapps.service';
Chris Hines's avatar
Chris Hines committed
5
6
import { timer, Subscription } from 'rxjs';
import { repeat } from 'rxjs/operators';
7
import { AppAction, Strudelapp} from '../strudelapp';
Chris Hines's avatar
Chris Hines committed
8
9
10
11
12
13

@Component({
  selector: 'app-job',
  templateUrl: './job.component.html',
  styleUrls: ['./job.component.css']
})
Chris Hines's avatar
Chris Hines committed
14
export class JobComponent implements OnInit, OnDestroy {
Chris Hines's avatar
Chris Hines committed
15
16

  @Input() jobdata: Job;
17
  public available: Boolean;
Chris Hines's avatar
Chris Hines committed
18
  public nocancel: Boolean;
19
20
  public resources: string;
  public timeremaining: string;
Chris Hines's avatar
Chris Hines committed
21
  private updatesub: Subscription;
Chris Hines's avatar
Chris Hines committed
22
23
24
  constructor(private tesService: TesService, private strudelAppsService: StrudelappsService) {


25

Chris Hines's avatar
Chris Hines committed
26
27
}

Chris Hines's avatar
Chris Hines committed
28
29
30
31

  ngOnDestroy() {
    this.updatesub.unsubscribe();
  }
Chris Hines's avatar
Chris Hines committed
32
  ngOnInit() {
Chris Hines's avatar
Chris Hines committed
33
    this.updatesub = timer(1000).pipe(repeat()).subscribe(() => this.updateFields());
34
35
  }

36
37
38
39
40
41
42
43
44
  parseDate(date) {
    const parsed = Date.parse(date);
    if (!isNaN(parsed)) {
      return parsed;
    }

      return Date.parse(date.replace(/-/g, '/').replace(/[a-z]+/gi, ' '));
  }

45
  updateFields() {
Chris Hines's avatar
Chris Hines committed
46
47
48
49
50
    if (this.jobdata.state == "RUNNING") {
      this.available = true;
    } else {
      this.available = false;
    }
Chris Hines's avatar
Chris Hines committed
51
52
53
54
55
56
    if (this.jobdata.app != null) {
      if (this.jobdata.app.startscript === null) {
        this.nocancel = true;
      } else {
        this.nocancel = false;
      }
Chris Hines's avatar
Chris Hines committed
57
58
59
    } else {
      this.nocancel = false;
    }
60
61
62
    if (this.jobdata.state == "Finished") {
      this.nocancel = true;
    }
63
    this.resources="";
64
65
66
67
68
69
70
    if (this.jobdata.cpus != undefined) {
        this.resources = this.resources+"CPUs: "+this.jobdata.cpus;
    }
    if (this.jobdata.mem != undefined) {
        this.resources = this.resources+" Mem: "+this.jobdata.mem;
    }
    if (this.jobdata.endtime != undefined) {
71
        let end = this.parseDate(this.jobdata.endtime);
72
73
74
        let remaining = end.valueOf() - Date.now().valueOf();
        this.timeremaining = "Time remaining: "+this.secondsToHms(remaining/1000);
    } else {
75
      console.log('jobdata endtime is undefined');
76
    }
77
78
79
80
81
82
  }

  secondsToHms(d: number) {
    var h: number = Math.floor(d / 3600);
    var m: number = Math.floor(d % 3600 / 60);
    var s: number = Math.floor(d % 3600 % 60);
Chris Hines's avatar
Chris Hines committed
83

84
85
86
    var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
    var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes ") : "";
    return hDisplay + mDisplay ; 
Chris Hines's avatar
Chris Hines committed
87
88
89
  }

  onCancel() {
90
    this.tesService.cancel(this.jobdata);
Chris Hines's avatar
Chris Hines committed
91
92
  }

93
94
95
  onConnect(action: AppAction) {
    console.log('called on connect');
    console.log(action)
Chris Hines's avatar
Chris Hines committed
96
    // Before connecting we must resolve what type of app we are connecting to
Chris Hines's avatar
Chris Hines committed
97
    // this.jobdata.app = this.strudelAppsService.getApp(this.jobdata.name);
98
    this.tesService.connect(this.jobdata,action);
Chris Hines's avatar
Chris Hines committed
99
100
101
  }

}