member_webapp/@overflow/infra/component/infra-map.component.ts
2018-06-07 15:10:17 +09:00

116 lines
3.0 KiB
TypeScript

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 { PageParams } from '@overflow/commons-typescript/model/commons/PageParams';
import { Page } from '@overflow/commons-typescript/model/commons/Page';
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<boolean>;
error$: Observable<any>;
tabs: MenuItem[];
probeHosts: ProbeHost[];
probeTabIdx: number;
@Output() discovery = new EventEmitter();
constructor(
private store: Store<any>,
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<Infra>) => {
this.infras = infraPage.content;
console.log(this.infras);
}),
catchError(error => {
this.error$ = of(error);
return of();
}),
tap(() => {
this.pending$ = of(false);
}),
take(1),
).subscribe();
}
}