Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
waitForAsync,
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import {
NavigationEnd,
Router,
} from '@angular/router';
import { APP_CONFIG } from '@dspace/config/app-config.interface';
import { SystemWideAlertDataService } from '@dspace/core/data/system-wide-alert-data.service';
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
import { SystemWideAlert } from '@dspace/core/shared/system-wide-alert.model';
Expand All @@ -16,11 +21,13 @@ import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote
import { TranslateModule } from '@ngx-translate/core';
import { utcToZonedTime } from 'date-fns-tz';
import { getTestScheduler } from 'jasmine-marbles';
import { of } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';

import { SystemWideAlertBannerComponent } from './system-wide-alert-banner.component';



describe('SystemWideAlertBannerComponent', () => {
let comp: SystemWideAlertBannerComponent;
let fixture: ComponentFixture<SystemWideAlertBannerComponent>;
Expand Down Expand Up @@ -53,6 +60,8 @@ describe('SystemWideAlertBannerComponent', () => {
providers: [
{ provide: SystemWideAlertDataService, useValue: systemWideAlertDataService },
{ provide: NotificationsService, useValue: new NotificationsServiceStub() },
{ provide: Router, useValue: { events: of(new NavigationEnd(0, '/test', '/test')) } },
{ provide: APP_CONFIG, useValue: { systemWideAlert: { refreshIntervalMs: 300000 } } },
],
}).compileComponents();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import {
OnInit,
PLATFORM_ID,
} from '@angular/core';
import {
NavigationEnd,
Router,
} from '@angular/router';
import {
APP_CONFIG,
AppConfig,
} from '@dspace/config/app-config.interface';
import { PaginatedList } from '@dspace/core/data/paginated-list.model';
import { SystemWideAlertDataService } from '@dspace/core/data/system-wide-alert-data.service';
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
Expand All @@ -29,7 +37,9 @@ import {
import {
filter,
map,
startWith,
switchMap,
throttleTime,
} from 'rxjs/operators';

/**
Expand Down Expand Up @@ -73,20 +83,29 @@ export class SystemWideAlertBannerComponent implements OnInit, OnDestroy {

constructor(
@Inject(PLATFORM_ID) protected platformId: any,
@Inject(APP_CONFIG) protected appConfig: AppConfig,
protected systemWideAlertDataService: SystemWideAlertDataService,
protected notificationsService: NotificationsService,
protected router: Router,
) {
}

ngOnInit() {
this.subscriptions.push(this.systemWideAlertDataService.searchBy('active').pipe(
getAllSucceededRemoteDataPayload(),
map((payload: PaginatedList<SystemWideAlert>) => payload.page),
filter((page) => isNotEmpty(page)),
map((page) => page[0]),
).subscribe((alert: SystemWideAlert) => {
this.systemWideAlert$.next(alert);
}));
this.subscriptions.push(
this.router.events.pipe(
filter(event => event instanceof NavigationEnd),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to spam that endpoint a lot, instead I would only trigger it every certain amount of minutes. We could make this interval configurable as well similar to the auth.ui.timeUntilIdle configuration

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @alexandrevryghem, thanks for the feedback! I've addressed this by adding a throttleTime operator to limit how often the endpoint is called during navigation. The interval is now configurable via appConfig.systemWideAlert.refreshIntervalMs, similar to auth.ui.timeUntilIdle, with a default of 5 minutes.

startWith(null),
throttleTime(this.appConfig.systemWideAlert?.refreshIntervalMs ?? 5 * 60 * 1000),
switchMap(() =>
this.systemWideAlertDataService.searchBy('active', null, false, true),
),
getAllSucceededRemoteDataPayload(),
map((payload: PaginatedList<SystemWideAlert>) => payload.page),
map((page) => isNotEmpty(page) ? page[0] : null),
).subscribe((alert: SystemWideAlert) => {
this.systemWideAlert$.next(alert);
}),
);

this.subscriptions.push(this.systemWideAlert$.pipe(
switchMap((alert: SystemWideAlert) => {
Expand All @@ -100,7 +119,6 @@ export class SystemWideAlertBannerComponent implements OnInit, OnDestroy {
} else {
return EMPTY;
}

}
}
// Reset the countDown times to 0 and return EMPTY to prevent unnecessary countdown calculations
Expand All @@ -120,7 +138,6 @@ export class SystemWideAlertBannerComponent implements OnInit, OnDestroy {
*/
private setTimeDifference(countdownTo: string) {
const date = zonedTimeToUtc(countdownTo, 'UTC');

const timeDifference = date.getTime() - new Date().getTime();
this.allocateTimeUnits(timeDifference);
}
Expand Down
3 changes: 3 additions & 0 deletions src/config/app-config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ interface AppConfig extends Config {
searchResult: SearchResultConfig;
addToAnyPlugin: AddToAnyPluginConfig;
cms: CmsMetadata;
systemWideAlert?: {
refreshIntervalMs: number;
};
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/config/default-app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ export class DefaultAppConfig implements AppConfig {
},
};

// System-wide alert settings
systemWideAlert = {
// How often (in milliseconds) the system-wide alert is refreshed during navigation.
// Prevents spamming the endpoint on every route change.
refreshIntervalMs: 5 * 60 * 1000, // 5 minutes
};

// Form settings
form: FormConfig = {
spellCheck: true,
Expand Down
Loading