chore: use strings for severities instead of numbers

Signed-off-by: Marek Libra <marek.libra@gmail.com>
This commit is contained in:
Marek Libra
2024-03-13 14:09:42 +01:00
parent dff7a7e9e2
commit 883dfde5bf
12 changed files with 33 additions and 60 deletions
+1 -1
View File
@@ -3,4 +3,4 @@
'@backstage/plugin-notifications': patch
---
all notifications can be marked and filtered by severity critical, high, normal or low, the default is 'normal'
All notifications can be marked and filtered by severity critical, high, normal or low, the default is 'normal'
+1 -1
View File
@@ -2,4 +2,4 @@
'@backstage/plugin-airbrake': patch
---
added an optional ESLint rule - no-top-level-material-ui-4-imports - which has an auto fix function to migrate the imports and using it migrated the imports
Added an optional ESLint rule - no-top-level-material-ui-4-imports - which has an auto fix function to migrate the imports and using it migrated the imports.
@@ -1,28 +0,0 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
exports.up = async function up(knex) {
await knex.schema.alterTable('notification', table => {
table.tinyint('severity').notNullable().alter();
// we do not need to migrate data since there are not real deployments so far
});
};
exports.down = async function down(knex) {
await knex.schema.alterTable('notification', table => {
table.string('severity').nullable().alter();
});
};
@@ -14,10 +14,7 @@
* limitations under the License.
*/
import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils';
import {
DatabaseNotificationsStore,
getNumericSeverity,
} from './DatabaseNotificationsStore';
import { DatabaseNotificationsStore } from './DatabaseNotificationsStore';
import { Knex } from 'knex';
import {
Notification,
@@ -300,7 +297,7 @@ describe.each(databases.eachSupportedId())(
it('normal', async () => {
const normal = await storage.getNotifications({
user,
minimalSeverity: getNumericSeverity('normal'),
minimumSeverity: 'normal',
});
expect(normal.map(idOnly)).toEqual([id1, id2, id3, id4]);
});
@@ -308,7 +305,7 @@ describe.each(databases.eachSupportedId())(
it('critical', async () => {
const critical = await storage.getNotifications({
user,
minimalSeverity: getNumericSeverity('critical'),
minimumSeverity: 'critical',
});
expect(critical.length).toBe(1);
expect(critical.at(0)?.id).toEqual(id3);
@@ -317,7 +314,7 @@ describe.each(databases.eachSupportedId())(
it('high', async () => {
const high = await storage.getNotifications({
user,
minimalSeverity: getNumericSeverity('high'),
minimumSeverity: 'high',
});
expect(high.map(idOnly)).toEqual([id3, id4]);
});
@@ -325,7 +322,7 @@ describe.each(databases.eachSupportedId())(
it('low', async () => {
const low = await storage.getNotifications({
user,
minimalSeverity: getNumericSeverity('low'),
minimumSeverity: 'low',
});
expect(low.map(idOnly)).toEqual([id1, id2, id3, id4, id5]);
});
@@ -39,9 +39,13 @@ const severities: NotificationSeverity[] = [
'normal',
'low',
];
export const getNumericSeverity = (severity: string): Number => {
const idx = severities.indexOf(severity as NotificationSeverity);
return idx >= 0 ? idx : 2 /* normal */;
export const normalizeSeverity = (input?: string): NotificationSeverity => {
let lower = (input ?? 'normal').toLowerCase() as NotificationSeverity;
if (severities.indexOf(lower) < 0) {
lower = 'normal';
}
return lower;
};
/** @internal */
@@ -84,7 +88,7 @@ export class DatabaseNotificationsStore implements NotificationsStore {
description: row.description,
link: row.link,
topic: row.topic,
severity: severities[row.severity],
severity: row.severity,
scope: row.scope,
icon: row.icon,
},
@@ -101,7 +105,7 @@ export class DatabaseNotificationsStore implements NotificationsStore {
link: notification.payload?.link,
title: notification.payload?.title,
description: notification.payload?.description,
severity: getNumericSeverity(notification.payload?.severity ?? 'normal'),
severity: normalizeSeverity(notification.payload?.severity),
scope: notification.payload?.scope,
saved: notification.saved,
read: notification.read,
@@ -169,8 +173,10 @@ export class DatabaseNotificationsStore implements NotificationsStore {
query.whereNull('notification.saved');
} // or match both if undefined
if (options.minimalSeverity !== undefined) {
query.where('notification.severity', '<=', options.minimalSeverity);
if (options.minimumSeverity !== undefined) {
const idx = severities.indexOf(options.minimumSeverity);
const equalOrHigher = severities.slice(0, idx + 1);
query.whereIn('notification.severity', equalOrHigher);
}
return query;
@@ -260,7 +266,7 @@ export class DatabaseNotificationsStore implements NotificationsStore {
link: notification.payload.link,
topic: notification.payload.topic,
updated: new Date(),
severity: getNumericSeverity(notification.payload?.severity ?? 'normal'),
severity: normalizeSeverity(notification.payload?.severity),
read: null,
});
@@ -16,6 +16,7 @@
import {
Notification,
NotificationSeverity,
NotificationStatus,
} from '@backstage/plugin-notifications-common';
@@ -32,7 +33,7 @@ export type NotificationGetOptions = {
read?: boolean;
saved?: boolean;
createdAfter?: Date;
minimalSeverity?: Number;
minimumSeverity?: NotificationSeverity;
};
/** @internal */
@@ -18,7 +18,7 @@ import express, { Request } from 'express';
import Router from 'express-promise-router';
import {
DatabaseNotificationsStore,
getNumericSeverity,
normalizeSeverity,
NotificationGetOptions,
} from '../database';
import { v4 as uuid } from 'uuid';
@@ -239,7 +239,7 @@ export async function createRouter(
opts.createdAfter = new Date(sinceEpoch);
}
if (req.query.minimal_severity) {
opts.minimalSeverity = getNumericSeverity(
opts.minimumSeverity = normalizeSeverity(
req.query.minimal_severity.toString(),
);
}
+1 -1
View File
@@ -27,7 +27,7 @@ export type GetNotificationsOptions = {
createdAfter?: Date;
sort?: 'created' | 'topic' | 'origin';
sortOrder?: 'asc' | 'desc';
minimalSeverity?: NotificationSeverity;
minimumSeverity?: NotificationSeverity;
};
// @public (undocumented)
@@ -35,7 +35,7 @@ export type GetNotificationsOptions = {
createdAfter?: Date;
sort?: 'created' | 'topic' | 'origin';
sortOrder?: 'asc' | 'desc';
minimalSeverity?: NotificationSeverity;
minimumSeverity?: NotificationSeverity;
};
/** @public */
@@ -67,8 +67,8 @@ export class NotificationsClient implements NotificationsApi {
if (options?.createdAfter !== undefined) {
queryString.append('created_after', options.createdAfter.toISOString());
}
if (options?.minimalSeverity !== undefined) {
queryString.append('minimal_severity', options.minimalSeverity);
if (options?.minimumSeverity !== undefined) {
queryString.append('minimal_severity', options.minimumSeverity);
}
const urlSegment = `?${queryString}`;
@@ -251,12 +251,10 @@ export const NotificationsFilters = ({
<Grid item xs={12}>
<FormControl fullWidth variant="outlined" size="small">
<InputLabel id="notifications-filter-severity">
Minimal severity
</InputLabel>
<InputLabel id="notifications-filter-severity">Severity</InputLabel>
<Select
label="Minimal severity"
label="Severity"
labelId="notifications-filter-severity"
value={severity}
onChange={handleOnSeverityChanged}
@@ -46,8 +46,7 @@ export const NotificationsPage = () => {
const [sorting, setSorting] = React.useState<SortBy>(
SortByOptions.newest.sortBy,
);
const [severity, setSeverity] =
React.useState<NotificationSeverity>('normal');
const [severity, setSeverity] = React.useState<NotificationSeverity>('low');
const { error, value, retry, loading } = useNotificationsApi(
api => {
@@ -55,7 +54,7 @@ export const NotificationsPage = () => {
search: containsText,
limit: pageSize,
offset: pageNumber * pageSize,
minimalSeverity: severity,
minimumSeverity: severity,
...(sorting || {}),
};
if (unreadOnly !== undefined) {