vo-data.component.ts 3.09 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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import * as t from '../types/types.module';

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

  @Input() group: t.Group;

  private services: t.Service[];
  private services$ = new BehaviorSubject<t.Service[]>([]);
  public services$$: Observable<t.Service[]>;

  private sites: Map<number, t.Site> = new Map();
  private sites$ = new BehaviorSubject<t.Site[]>([]);
  public sites$$: Observable<t.Site[]>;

  private deployment$ = <BehaviorSubject<t.Deployment>> new BehaviorSubject(new Object);
  public deployment$$: Observable<t.Deployment>;

  constructor(
    public userService: UserService,
Lukas Burgey's avatar
Lukas Burgey committed
32
    public dialog: DialogService,
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  ) {
  }

  ngOnInit(): void {
    this.userService.subscribeSpecific(this.userService.serviceSelector).subscribe(
      (services: t.Service[]) => {
        // filter by the group of this vo
        this.services = services.filter(
          (s: t.Service) => s.groups.some(
            (g: t.Group) => this.group.name == g.name
          ),
        );
        this.services$.next(this.services);

        // generate the sites from the services
        this.services.map(
          (s: t.Service) => s.site.forEach(
            (site: t.Site) => this.sites.set(site.id, site)
          )
        );

        let uniqueSites = []
        this.sites.forEach(
          site => uniqueSites.push(site)
        );
        this.sites$.next(uniqueSites);
      }
    );
    this.userService.subscribeGroupDeployment(this.group).subscribe(
      (dep: t.Deployment) => {
        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();
  }

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

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

  public changeDeployment(action: string): void {
    this.userService.changeDeployment(action, this.group).subscribe(
      (dep: t.Deployment) => {
        this.deployment$.next(dep);
      }
    );
  }
}