vo-data.component.ts 3.05 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 { LanguageService } from '../language.service';
Lukas Burgey's avatar
Lukas Burgey committed
8
import { DialogService } from '../dialogues/dialog.service';
Lukas Burgey's avatar
Lukas Burgey committed
9
import { VO, Site, Service, Deployment, DeploymentState }  from '../types/types.module';
10 11 12 13 14 15 16 17

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

18
  @Input() vo: VO;
19

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

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

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

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

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

        // generate the sites from the services
        this.services.map(
Lukas Burgey's avatar
Lukas Burgey committed
51
          (s: Service) => this.sites.set(s.site.id, s.site)
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
        (services: Service[]) => services.filter(
Lukas Burgey's avatar
Lukas Burgey committed
84 85
          (service: Service) => service.site.id === site.id
        ),
86
      ),
87 88 89
    );
  }

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

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