vo-data.component.ts 2.96 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';
Lukas Burgey's avatar
Lukas Burgey committed
8
import { VO, Site, Service, Deployment, DeploymentState }  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(
Lukas Burgey's avatar
Lukas Burgey committed
49
          (s: Service) => this.sites.set(s.site.id, s.site)
50 51 52 53 54 55 56 57 58
        );

        let uniqueSites = []
        this.sites.forEach(
          site => uniqueSites.push(site)
        );
        this.sites$.next(uniqueSites);
      }
    );
59 60
    this.userService.subscribeVODeployment(this.vo).subscribe(
      (dep: Deployment) => {
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        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();
  }

78
  public servicesAtSite(site: Site): Observable<Service[]> {
79 80
    return this.services$$.pipe(
      map(
81
        (services: Service[]) => services.filter(
Lukas Burgey's avatar
Lukas Burgey committed
82 83
          (service: Service) => service.site.id === site.id
        ),
84
      ),
85 86 87
    );
  }

Lukas Burgey's avatar
Lukas Burgey committed
88
  public subscribeStateItem(site: Site, service: Service): Observable<DeploymentState> {
89 90
    return this.deployment$$.pipe(
      map(
91
        (dep: Deployment) => {
92 93
          if (dep.state_items) {
            return dep.state_items.find(
Lukas Burgey's avatar
Lukas Burgey committed
94
              (dsi: DeploymentState) => dsi.site.id == site.id && dsi.service.id == service.id,
95 96 97 98
            )
          }
        },
      ),
99 100 101 102
    );
  }

  public changeDeployment(action: string): void {
103 104
    this.userService.changeDeployment(action, this.vo).subscribe(
      (dep: Deployment) => {
105 106 107 108 109
        this.deployment$.next(dep);
      }
    );
  }
}