Skip to content

Commit e714f0c

Browse files
committed
feat(api-checker): enhance API availability checks and improve loading indicators
Signed-off-by: Manuel Abascal <mjabascal10@gmail.com>
1 parent a99780e commit e714f0c

File tree

9 files changed

+75
-49
lines changed

9 files changed

+75
-49
lines changed

frontend/src/app/app.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ export class AppComponent implements OnInit {
111111
)
112112
.subscribe();
113113

114-
this.appLoading.style.display = 'none';
114+
if (this.appLoading && this.appLoading.parentNode) {
115+
this.appLoading.parentNode.removeChild(this.appLoading);
116+
}
115117
}
116118

117119
@HostListener('window', ['$event'])
@@ -141,10 +143,8 @@ export class AppComponent implements OnInit {
141143
break;
142144
}
143145
}
144-
this.apiServiceCheckerService.setOnlineStatus(true);
145146
}, error => {
146147
this.offline = true;
147-
this.apiServiceCheckerService.checkApiAvailability();
148148
});
149149
}
150150

frontend/src/app/app.module.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@ import {AlertIncidentStatusChangeBehavior} from './shared/behaviors/alert-incide
3636
import {GettingStartedBehavior} from './shared/behaviors/getting-started.behavior';
3737
import {NavBehavior} from './shared/behaviors/nav.behavior';
3838
import {NewAlertBehavior} from './shared/behaviors/new-alert.behavior';
39-
import {AppVersionService} from './shared/services/version/app-version.service';
4039
import {TimezoneFormatService} from './shared/services/utm-timezone.service';
40+
import {AppVersionService} from './shared/services/version/app-version.service';
4141
import {UtmSharedModule} from './shared/utm-shared.module';
4242

43-
export function initTimezoneFormat(
44-
timezoneService: TimezoneFormatService,
45-
apiChecker: ApiServiceCheckerService,
46-
appVersionService: AppVersionService
47-
) {
48-
return () =>
49-
new Promise<void>((resolve, reject) => {
43+
export function initTimezoneFormat(apiChecker: ApiServiceCheckerService,
44+
timezoneService: TimezoneFormatService,
45+
appVersionService: AppVersionService) {
46+
return () => {
47+
apiChecker.init();
48+
return new Promise<void>((resolve, reject) => {
5049
apiChecker.isOnlineApi$
5150
.pipe(first(val => val === true))
5251
.subscribe({
@@ -59,6 +58,8 @@ export function initTimezoneFormat(
5958
error: reject
6059
});
6160
});
61+
};
62+
6263
}
6364

6465
@NgModule({
@@ -135,8 +136,8 @@ export function initTimezoneFormat(
135136
provide: APP_INITIALIZER,
136137
useFactory: initTimezoneFormat,
137138
deps: [
138-
TimezoneFormatService,
139139
ApiServiceCheckerService,
140+
TimezoneFormatService,
140141
AppVersionService
141142
],
142143
multi: true

frontend/src/app/compliance/compliance-reports-view/components/compliance-print-view/compliance-print-view.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div *ngIf="!preparingPrint" class="report-loading"></div>
1+
<div *ngIf="!preparingPrint" class="report-loaded"></div>
22

33
<h3 class="standard-prev text-center mb-4">
44
<strong>Compliance Assessment</strong>
Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {HttpClient} from '@angular/common/http';
1+
import {HttpClient, HttpResponse} from '@angular/common/http';
22
import {Injectable} from '@angular/core';
3-
import {BehaviorSubject, interval, Observable, Subject} from 'rxjs';
4-
import {first, takeUntil} from 'rxjs/operators';
3+
import {BehaviorSubject, interval, Observable, of, Subject, Subscription} from 'rxjs';
4+
import {catchError, distinctUntilChanged, filter, first, map, takeUntil, tap} from 'rxjs/operators';
55
import {SERVER_API_URL} from '../../app.constants';
66

77
@Injectable({
@@ -10,37 +10,56 @@ import {SERVER_API_URL} from '../../app.constants';
1010
export class ApiServiceCheckerService {
1111

1212
public resourceUrl = SERVER_API_URL + 'api/ping';
13-
private retryInterval = 3000;
14-
private isOnline: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
15-
public isOnlineApi$: Observable<boolean> = this.isOnline.asObservable();
16-
private stopInterval$: Subject<boolean> = new Subject<boolean>();
13+
private retryInterval = 5000;
1714

18-
constructor(private http: HttpClient) {
15+
private isOnline = new BehaviorSubject<boolean>(false);
16+
public isOnlineApi$ = this.isOnline.asObservable();
17+
18+
private stopInterval$ = new Subject<void>();
19+
private intervalSub?: Subscription;
20+
21+
constructor(private http: HttpClient) { }
22+
23+
init() {
1924
this.checkApiAvailability();
25+
26+
this.isOnlineApi$
27+
.pipe(distinctUntilChanged())
28+
.subscribe(isOnline => {
29+
if (!isOnline) {
30+
this.startCheckApiIsOnline();
31+
} else {
32+
this.stopChecking();
33+
}
34+
});
2035
}
2136

22-
checkApiAvailability() {
23-
interval(this.retryInterval)
37+
private checkApiAvailability() {
38+
this.checkApiStatus()
39+
.subscribe(status => this.isOnline.next(status));
40+
}
41+
42+
private checkApiStatus(): Observable<boolean> {
43+
return this.http.get(this.resourceUrl, { observe: 'response' }).pipe(
44+
map(res => res.status === 200),
45+
catchError(() => of(false))
46+
);
47+
}
48+
49+
private startCheckApiIsOnline() {
50+
if (this.intervalSub) { return; }
51+
52+
this.intervalSub = interval(this.retryInterval)
2453
.pipe(takeUntil(this.stopInterval$))
2554
.subscribe(() => {
26-
this.http
27-
.get(this.resourceUrl, { observe: 'response' })
28-
.pipe(first())
29-
.subscribe(
30-
resp => {
31-
if (resp.status === 200) {
32-
this.isOnline.next(true);
33-
this.stopInterval$.next(true);
34-
} else {
35-
this.isOnline.next(false);
36-
}
37-
},
38-
err => this.isOnline.next(false)
39-
);
40-
});
55+
this.checkApiStatus().
56+
subscribe(status => this.isOnline.next(status));
57+
});
4158
}
4259

43-
setOnlineStatus(status: boolean) {
44-
this.isOnline.next(status);
60+
private stopChecking() {
61+
this.stopInterval$.next();
62+
this.intervalSub.unsubscribe();
63+
this.intervalSub = undefined;
4564
}
4665
}

frontend/src/app/dashboard/dashboard-export-pdf/dashboard-export-pdf.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</button>
1414
</div>
1515
</div>
16-
<div *ngIf="!preparingPrint" class="report-loading"></div>
16+
<div *ngIf="!preparingPrint" class="report-loaded"></div>
1717
<div class="d-flex justify-content-center align-items-start w-100 m-0 d-print-block">
1818
<div class="compliance-front-page">
1919
<div *ngIf="account" class="prepared print-front-align">

frontend/src/app/dashboard/dashboard-export-pdf/dashboard-export-pdf.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ export class DashboardExportPdfComponent implements OnInit, AfterViewInit {
143143
}
144144

145145
onVisualizationLoaded() {
146-
this.preparingPrint = false;
147-
console.log('onVisualizationLoaded');
146+
setTimeout(() => {
147+
this.preparingPrint = false;
148+
});
148149
}
149150

150151
getTimeFilterValue() {

frontend/src/app/dashboard/dashboard-overview/dashboard-overview.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ <h5 class="card-title label-header mb-0 text-uppercase label-header">Overview</h
1313
</button>
1414
</div>
1515
</div>
16-
<div *ngIf="!preparingPrint" class="report-loading"></div>
16+
<div *ngIf="!preparingPrint" class="report-loaded"></div>
1717
<app-utm-report-header *ngIf="pdfExport" [reportName]="'UTMSTACK Overview'"></app-utm-report-header>
18-
<div class="h-100 overflow-hidden pb-3" [ngClass]="{'mt-3':pdfExport}" id="utmDashboardAlert">
18+
<div class="h-100 overflow-hidden pb-3 d-print-block" [ngClass]="{'mt-3':pdfExport}" id="utmDashboardAlert">
1919

20-
<div class="d-flex overflow-auto flex-column h-100 m-h-0 px-1">
20+
<div class="d-flex overflow-auto flex-column h-100 m-h-0 px-1 d-print-block">
2121

2222
<div class="w-100 border-bottom-grey-100 border-bottom-1 mb-3 pb-2">
2323
<label class="font-weight-bold text-uppercase">Alerts</label>

frontend/src/app/dashboard/dashboard-overview/dashboard-overview.component.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@
1616
.card-header-chart {
1717
padding-right: 0 !important;
1818
}
19+
20+
@media print {
21+
.d-print-block {
22+
display: block !important;
23+
overflow: visible !important;
24+
}
25+
}

frontend/src/app/dashboard/shared/render-visualization-print/render-visualization-print.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ export class RenderVisualizationPrintComponent implements OnInit {
3838
this.runList += 1;
3939
if (this.runList === this.visualizationRender.length) {
4040
this.loadingVisualizations = true;
41-
console.log('All the visualizations data has loaded, waiting for rendering');
42-
setTimeout(() => this.visualizationLoaded.emit(true), 3000);
43-
console.log('All the visualizations now has rendered');
41+
this.visualizationLoaded.emit(true);
4442
}
4543
}
4644
}

0 commit comments

Comments
 (0)