member_webapp/@overflow/probe/component/probe-list.component.ts
crusader c47bb4fad5 ing
2018-06-04 19:03:59 +09:00

103 lines
2.8 KiB
TypeScript

import { Component, OnInit, Output, EventEmitter, Input, OnDestroy } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, Subscription, of } from 'rxjs';
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 { ProbeHost, Probe } from '@overflow/commons-typescript/model/probe';
import { ProbeHostService } from '../service/probe-host.service';
import { ProbeSubscriber, ProbeNotify } from '../subscriber/probe.subscriber';
@Component({
selector: 'of-probe-list',
templateUrl: './probe-list.component.html',
})
export class ProbeListComponent implements OnInit, OnDestroy {
@Output() select = new EventEmitter<ProbeHost>();
probeHosts: ProbeHost[];
pending$: Observable<boolean>;
error$: Observable<any>;
probeSubscription: Subscription;
constructor(
private store: Store<any>,
private probeHostService: ProbeHostService,
private probeSubscriber: ProbeSubscriber,
) {
this.probeSubscription = null;
}
ngOnInit(): void {
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;
}),
catchError(error => {
this.error$ = of(error);
return of();
})
)
),
tap(() => {
this.pending$ = of(false);
}),
take(1),
).subscribe();
this.probeSubscription = this.probeSubscriber.observable().pipe(
tap((probeNotify: ProbeNotify) => {
const probeHosts = [];
this.probeHosts.forEach(probeHost => {
const n = probeNotify.params;
if (probeHost.probe.id === n.id) {
probeHost.probe = n;
}
probeHosts.push(probeHost);
});
this.probeHosts = probeHosts;
}
),
).subscribe();
}
ngOnDestroy(): void {
if (null !== this.probeSubscription) {
this.probeSubscription.unsubscribe();
}
}
onProbeSelect(event) {
this.select.emit(event.data);
}
getUptime(probe: Probe) {
// if (!probe.connectDate) {
// return 'Not Connected.';
// }
// const connectDate = new Date(probe.connectDate);
// const hours = Math.abs(new Date().getTime() - connectDate.getTime());
// return this.convertUptimeString(hours);
}
convertUptimeString(hours: number) {
if (hours === 0) {
return 'Less than an hour.';
} else {
return 'About ' + hours;
}
}
}