vo.component.ts 2.93 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
import { Component, OnInit, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';

4
import { DialogService } from '../dialogues/dialog.service';
Lukas Burgey's avatar
Lukas Burgey committed
5
6
7
8
9
10
11
12
13
14
15
import { UserService } from '../user.service';
import * as t from '../types/types.module';

@Component({
  selector: 'app-vo',
  templateUrl: './vo.component.html',
  styleUrls: ['./vo.component.css']
})
export class VoComponent implements OnInit {

  @Input() group: t.Group;
16
  @Input() deployment: t.Deployment;
Lukas Burgey's avatar
Lukas Burgey committed
17
18
19
20

  constructor(
    public userService: UserService,
    public http: HttpClient,
21
    public dialog: DialogService,
Lukas Burgey's avatar
Lukas Burgey committed
22
23
24
  ) { }

  ngOnInit() {
25
26
  }

27
28
29
30
31
32
33
34
35
36
37
38
  public sites(): t.Site[] {
    if (this.deployment) {
      return this.deployment.sites;
    }
    return this.userService.getServices(this.group).map(
      (s: t.Service) => {
        if (s.site.length  == 1) {
          return s.site[0];
        } else {
          console.log("Group service is provided by more than one site!")
          return undefined
        }
Lukas Burgey's avatar
Lukas Burgey committed
39
40
      }
    );
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  }

  public servicesBySite(site: t.Site): t.Service[] {
    if (this.deployment) {
      return this.deployment.services.filter(
        (s: t.Service) => {
          return s.site.some(
            (ss: t.Site) => {
              return ss.id === site.id;
            }
          );
        }
      );
    }
    return this.userService.getServices(this.group);
Lukas Burgey's avatar
Lukas Burgey committed
56
57
  }

58
  public services(): t.Service[] {
Lukas Burgey's avatar
Lukas Burgey committed
59
    if (this.deployment) {
60
      return this.deployment.services;
Lukas Burgey's avatar
Lukas Burgey committed
61
    }
62
    return this.userService.getServices(this.group);
Lukas Burgey's avatar
Lukas Burgey committed
63
64
  }

65
  public taskItem(site: t.Site, key: t.SSHKeyRef): t.DeploymentStateItem {
66
67
68
69
70
71
72
73
74
75
76
77
78
    if (this.deployment == undefined) {
      return undefined;
    }

    const deploymentState = this.deployment.states.find(
      state => {
        return state.key.id === key.id
      }
    );
    if (deploymentState) {
      return deploymentState.state_items.find(
        item => {
          return item.site.id === site.id
Lukas Burgey's avatar
Lukas Burgey committed
79
80
81
82
83
84
85
86
87
88
        }
      );
    }
  }

  public taskState(site: t.Site, key: t.SSHKeyRef): string {
    let item = this.taskItem(site, key);
    if (item) {
      return item.state;
    }
89
    return "unknown";
Lukas Burgey's avatar
Lukas Burgey committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  }

  public changeDeployment(key: t.SSHKeyRef, action: string) {
    const body = {
      'type': action,
      'key': key.id,
      'group': this.group.id,
    };
    return this.http.post('/backend/api/deployments', body).subscribe(
      (newDep: t.Deployment) => {
        // update the deployment
        this.deployment = newDep;
      },
      (err) => {
        console.log(err);
        this.userService.update();
      }
    );
  }

110
111
112
113
114
115
116
117
118
119
  public isDeployed(key: t.SSHKeyRef): boolean {
    if (this.deployment == undefined) {
      return false;
    }

    return this.deployment.ssh_keys.some(k => {
      return k.id === key.id;
    });
  }

Lukas Burgey's avatar
Lukas Burgey committed
120
121
122
123
124
125
126
127
128

  public deploymentChange(key: t.SSHKeyRef) {
    if (!this.isDeployed(key)) {
      this.changeDeployment(key, 'add');
    } else {
      this.changeDeployment(key, 'remove');
    }
  }
}