vo-data.component.ts 3.07 KB
Newer Older
1
import { Component, OnInit, Input } from '@angular/core';
2
3
4

import { Observable, BehaviorSubject } from 'rxjs';
import { map } from 'rxjs/operators';
5
6

import { UserService } from '../user.service';
Lukas Burgey's avatar
Lukas Burgey committed
7
import { DialogService } from '../dialogues/dialog.service';
8
import { VO, Site, Service, Deployment, DeploymentStateItem }  from '../types/types.module';
9
10
11
12
13
14
15
16

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

17
  @Input() vo: VO;
18

19
20
21
  private services: Service[];
  private services$ = new BehaviorSubject<Service[]>([]);
  public services$$: Observable<Service[]>;
22

23
24
25
  private sites: Map<number, Site> = new Map();
  private sites$ = new BehaviorSubject<Site[]>([]);
  public sites$$: Observable<Site[]>;
26

27
28
  private deployment$ = <BehaviorSubject<Deployment>> new BehaviorSubject(new Object);
  public deployment$$: Observable<Deployment>;
29
30
31

  constructor(
    public userService: UserService,
Lukas Burgey's avatar
Lukas Burgey committed
32
    public dialog: DialogService,
33
34
35
36
37
  ) {
  }

  ngOnInit(): void {
    this.userService.subscribeSpecific(this.userService.serviceSelector).subscribe(
38
39
      (services: Service[]) => {
        // filter by the vo of this vo
40
        this.services = services.filter(
41
42
          (s: Service) => s.vos.some(
            (g: VO) => this.vo.name == g.name
43
44
45
46
47
48
          ),
        );
        this.services$.next(this.services);

        // generate the sites from the services
        this.services.map(
49
50
          (s: Service) => s.site.forEach(
            (site: Site) => this.sites.set(site.id, site)
51
52
53
54
55
56
57
58
59
60
          )
        );

        let uniqueSites = []
        this.sites.forEach(
          site => uniqueSites.push(site)
        );
        this.sites$.next(uniqueSites);
      }
    );
61
62
    this.userService.subscribeVODeployment(this.vo).subscribe(
      (dep: Deployment) => {
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        if (dep){
          this.deployment$.next(dep);
        }
      }
    );

    this.services$$ = this.services$.asObservable();
    this.sites$$ = this.sites$.asObservable();
    this.deployment$$ = this.deployment$.asObservable();
  }

  ngOnDestroy(): void {
    this.services$.complete();
    this.sites$.complete();
    this.deployment$.complete();
  }

80
  public servicesAtSite(site: Site): Observable<Service[]> {
81
82
    return this.services$$.pipe(
      map(
83
84
85
        (services: Service[]) => services.filter(
          (service: Service) => service.site.some(
            (s: Site) => s.id === site.id
86
          )
87
        )
88
      ),
89
90
91
    );
  }

92
  public subscribeStateItem(site: Site, service: Service): Observable<DeploymentStateItem> {
93
94
    return this.deployment$$.pipe(
      map(
95
        (dep: Deployment) => {
96
97
          if (dep.state_items) {
            return dep.state_items.find(
98
              (dsi: DeploymentStateItem) => dsi.site.id == site.id && dsi.service.id == service.id,
99
100
101
102
            )
          }
        },
      ),
103
104
105
106
    );
  }

  public changeDeployment(action: string): void {
107
108
    this.userService.changeDeployment(action, this.vo).subscribe(
      (dep: Deployment) => {
109
110
111
112
113
        this.deployment$.next(dep);
      }
    );
  }
}