import { Component, Input, Output, EventEmitter, AfterContentInit, OnInit, OnDestroy } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { Observable, of, Subscription } from 'rxjs'; import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators'; import { ConfirmationService, Message } from 'primeng/primeng'; import { NoAuthProbe } from '@overflow/commons-typescript/model/noauth'; import { AuthSelector } from '@overflow/shared/auth/store'; import { DomainMember } from '@overflow/commons-typescript/model/domain'; import { NoAuthProbeService } from '../service/noauth-probe.service'; import { NoAuthProbeSubscriber, NoAuthProbeNotify } from '../subscriber/noauth-probe.subscriber'; @Component({ selector: 'of-noauth-probe-list', templateUrl: './noauth-probe-list.component.html', providers: [ConfirmationService] }) export class NoAuthProbeListComponent implements OnInit, OnDestroy { pending$: Observable; error$: Observable; noauthProbes: NoAuthProbe[]; noauthProbeSubscription: Subscription; constructor( private confirmationService: ConfirmationService, private store: Store, private noAuthProbeService: NoAuthProbeService, private noAuthProbeSubscriber: NoAuthProbeSubscriber, ) { this.noauthProbes = []; this.noauthProbeSubscription = null; } ngOnInit() { this.store.pipe( tap(() => { this.pending$ = of(true); }), select(AuthSelector.selectDomainMember), exhaustMap((domainMember: DomainMember) => this.noAuthProbeService.readAllByDomainID(domainMember.domain.id) .pipe( map((noauthProbes: NoAuthProbe[]) => { this.setNoauthProbes(noauthProbes); }), catchError(error => { this.setError$(error); return of(); }) ) ), tap(() => { this.pending$ = of(false); }), take(1), ).subscribe(); this.noauthProbeSubscription = this.noAuthProbeSubscriber.observable().pipe( tap((noAuthProbeNotify: NoAuthProbeNotify) => { const noauthProbes = []; this.noauthProbes.forEach(noauthProbe => { const n = noAuthProbeNotify.params; if (noauthProbe.id === n.id) { noauthProbes.push(n); } else { noauthProbes.push(noauthProbe); } }); this.setNoauthProbes(noauthProbes); } ), ).subscribe(); } ngOnDestroy(): void { if (null !== this.noauthProbeSubscription) { this.noauthProbeSubscription.unsubscribe(); } } onAcceptOrDeny(isAccept: boolean, selected: NoAuthProbe) { const title = isAccept ? 'Are you sure to accept this Probe?' : 'Are you sure to deny this Probe'; const message = isAccept ? 'Start collecting data as a Probe.' : 'It will be permanently deleted.'; this.confirmationService.confirm({ header: title, message: message, icon: isAccept ? 'fa-check' : 'fa fa-trash', accept: () => { if (isAccept) { this.noAuthProbeService.acceptNoAuthProbe(selected.id) .pipe( tap(() => { this.pending$ = of(true); }), map((noauthProbes: NoAuthProbe[]) => { this.setNoauthProbes(noauthProbes); }), catchError(error => { this.setError$(error); return of(); }), tap(() => { this.pending$ = of(false); }), take(1), ).subscribe(); } else { this.noAuthProbeService.denyNoauthProbe(selected.id) .pipe( tap(() => { this.pending$ = of(true); }), map((noauthProbes: NoAuthProbe[]) => { this.setNoauthProbes(noauthProbes); }), catchError(error => { this.setError$(error); return of(); }), tap(() => { this.pending$ = of(false); }), take(1), ).subscribe(); } }, reject: () => { } }); } private setNoauthProbes(noauthProbes: NoAuthProbe[]): void { if (null === noauthProbes) { return; } this.noauthProbes = noauthProbes; } private setError$(error: any): void { this.error$ = of(error); } getInfraHostInfo(infraHostMeta: string, type: string): string { const infraHost = JSON.parse(infraHostMeta); switch (type) { case 'name': return infraHost.host.name; case 'os': let os = infraHost.host.os; return os; case 'platform': return infraHost.host.platform; case 'platformFamily': return infraHost.host.platformFamily; case 'kernelVersion': return infraHost.host.kernelVersion; case 'hostID': return infraHost.host.hostID; default: return ''; } } getInfraNetworkInfo(infraHostMeta: string, type: string): string { const infraHost = JSON.parse(infraHostMeta); switch (type) { case 'name': return infraHost.network.name; case 'address': return infraHost.network.address; case 'gateway': return infraHost.network.gateway; case 'macAddress': return infraHost.network.macAddress; default: return ''; } } }