Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Mar 10, 2022
1 parent 68065b7 commit f5a484d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
31 changes: 21 additions & 10 deletions src/app/Shared/Services/NotificationChannel.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
import { AlertVariant } from '@patternfly/react-core';
import { concatMap, distinctUntilChanged, filter } from 'rxjs/operators';
import { AuthMethod, LoginService, SessionState } from './Login.service';
import { Target } from './Target.service';
import { TargetDiscoveryEvent } from './Targets.service';

export enum NotificationCategory {
WsClientActivity = 'WsClientActivity',
Expand Down Expand Up @@ -75,22 +77,31 @@ interface ReadyState {

export const messageKeys = new Map([
[
// explicitly configure this category with a null mapper.
// This is a special case because we do not want to display an alert,
// the Targets.service already handles this
NotificationCategory.TargetJvmDiscovery, {
variant: AlertVariant.info,
title: 'Target JVM Discovery',
},
],
[
// explicitly configure this category with a null mapper.
// explicitly configure this category with a null message body mapper.
// This is a special case because this is generated client-side,
// not sent by the backend
NotificationCategory.GrafanaConfiguration, {
title: 'Grafana Configuration',
},
],
[
NotificationCategory.TargetJvmDiscovery, {
variant: AlertVariant.info,
title: 'Target JVM Discovery',
body: v => {
const evt: TargetDiscoveryEvent = v.message.event;
const target: Target = evt.serviceRef;
switch (evt.kind) {
case 'FOUND':
return `Target "${target.alias}" appeared (${target.connectUrl})"`;
case 'LOST':
return `Target "${target.alias}" disappeared (${target.connectUrl})"`;
default:
return `Received a notification with category ${NotificationCategory.TargetJvmDiscovery} and unrecognized kind ${evt.kind}`;
}
}
} as NotificationMessageMapper,
],
[
NotificationCategory.WsClientActivity, {
variant: AlertVariant.info,
Expand Down
6 changes: 1 addition & 5 deletions src/app/Shared/Services/Targets.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class TargetsService {
constructor(
private readonly api: ApiService,
private readonly notifications: Notifications,
private readonly login: LoginService,
login: LoginService,
notificationChannel: NotificationChannel,
) {
login.getSessionState().pipe(
Expand All @@ -68,18 +68,14 @@ export class TargetsService {
notificationChannel.messages(NotificationCategory.TargetJvmDiscovery)
.subscribe(v => {
const evt: TargetDiscoveryEvent = v.message.event;
const target: Target = evt.serviceRef;
switch (evt.kind) {
case 'FOUND':
this._targets$.next(_.unionBy(this._targets$.getValue(), [evt.serviceRef], t => t.connectUrl));
notifications.info('Target Appeared', `Target "${target.alias}" appeared (${target.connectUrl})"`, NotificationCategory.TargetJvmDiscovery);
break;
case 'LOST':
this._targets$.next(_.filter(this._targets$.getValue(), t => t.connectUrl !== evt.serviceRef.connectUrl));
notifications.info('Target Disappeared', `Target "${target.alias}" disappeared (${target.connectUrl})"`, NotificationCategory.TargetJvmDiscovery);
break;
default:
notifications.danger(`Invalid Message Received`, `Received a notification with category ${NotificationCategory.TargetJvmDiscovery} and unrecognized kind ${evt.kind}`);
break;
}
});
Expand Down

0 comments on commit f5a484d

Please sign in to comment.