import { Component, OnInit, EventEmitter, Output, Input, OnChanges, SimpleChanges, } from '@angular/core'; import { ProbeHostService } from '@overflow/probe/service/probe-host.service'; import { Store, select } from '@ngrx/store'; import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators'; import { AuthSelector } from '@overflow/shared/auth/store'; import { DomainMember } from '@overflow/commons-typescript/model/domain'; import { Observable, Subscription, of } from 'rxjs'; import { ProbeHost } from '@overflow/commons-typescript/model/probe'; import { MenuItem } from 'primeng/primeng'; import { InfraService } from '../service/infra.service'; import { Page, PageParams } from '@overflow/commons-typescript/core/model'; import { Infra } from '@overflow/commons-typescript/model/infra'; @Component({ selector: 'of-infra-map', templateUrl: './infra-map.component.html', providers: [ ProbeHostService ] }) export class InfraMapComponent implements OnInit { infras: Infra[]; pending$: Observable; error$: Observable; tabs: MenuItem[]; probeHosts: ProbeHost[]; probeTabIdx: number; @Output() discovery = new EventEmitter(); constructor( private store: Store, private probeHostService: ProbeHostService, private infraService: InfraService, ) { } ngOnInit(): void { this.getProbes(); } getProbes() { this.store.pipe( tap(() => { this.pending$ = of(true); }), select(AuthSelector.selectDomainMember), exhaustMap((domainMember: DomainMember) => this.probeHostService.readAllByDomainID(domainMember.domain.id) .pipe( map((probeHosts: ProbeHost[]) => { this.probeHosts = probeHosts; this.generateTabs(); this.getInfras(probeHosts[0].probe.id); }), catchError(error => { this.error$ = of(error); return of(); }) ) ), tap(() => { this.pending$ = of(false); }), take(1), ).subscribe(); } generateTabs() { this.tabs = []; this.probeHosts.forEach((ph, i) => { this.tabs.push({ label: ph.probe.cidr }); }); } onProbeChange(event) { this.getInfras(this.probeHosts[event.index].probe.id); } getInfras(probeID: number) { const pageParams: PageParams = { pageNo: 0, countPerPage: 99999, sortCol: 'id', sortDirection: 'descending' }; this.infraService.readAllByProbeID(probeID, pageParams) .pipe( tap(() => { this.pending$ = of(true); }), map((infraPage: Page) => { this.infras = infraPage.content; console.log(this.infras); }), catchError(error => { this.error$ = of(error); return of(); }), tap(() => { this.pending$ = of(false); }), take(1), ).subscribe(); } }