import { Component, OnInit, Input } from '@angular/core'; import { Observable, BehaviorSubject } from 'rxjs'; import { map } from 'rxjs/operators'; import { UserService } from '../user.service'; import { DialogService } from '../dialogues/dialog.service'; import { VO, Site, Service, Deployment, DeploymentStateItem } 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() vo: VO; private services: Service[]; private services$ = new BehaviorSubject([]); public services$$: Observable; private sites: Map = new Map(); private sites$ = new BehaviorSubject([]); public sites$$: Observable; private deployment$ = > new BehaviorSubject(new Object); public deployment$$: Observable; constructor( public userService: UserService, public dialog: DialogService, ) { } ngOnInit(): void { this.userService.subscribeSpecific(this.userService.serviceSelector).subscribe( (services: Service[]) => { // filter by the vo of this vo this.services = services.filter( (s: Service) => s.vos.some( (g: VO) => this.vo.name == g.name ), ); this.services$.next(this.services); // generate the sites from the services this.services.map( (s: Service) => s.site.forEach( (site: Site) => this.sites.set(site.id, site) ) ); let uniqueSites = [] this.sites.forEach( site => uniqueSites.push(site) ); this.sites$.next(uniqueSites); } ); this.userService.subscribeVODeployment(this.vo).subscribe( (dep: 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: Site): Observable { return this.services$$.pipe( map( (services: Service[]) => services.filter( (service: Service) => service.site.some( (s: Site) => s.id === site.id ) ) ), ); } public subscribeStateItem(site: Site, service: Service): Observable { return this.deployment$$.pipe( map( (dep: Deployment) => { if (dep.state_items) { return dep.state_items.find( (dsi: DeploymentStateItem) => dsi.site.id == site.id && dsi.service.id == service.id, ) } }, ), ); } public changeDeployment(action: string): void { this.userService.changeDeployment(action, this.vo).subscribe( (dep: Deployment) => { this.deployment$.next(dep); } ); } }