1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

fix: last seen now sorts nulls last (#5664)

Two changes were needed to sort better

1. Since we are still using `last seen` from `features` table for
backwards compatibility, we needed to add it to sort condition.
2. Nulls break the order, so now sorting nulls as last.
This commit is contained in:
Jaanus Sellin 2023-12-18 10:36:50 +02:00 committed by GitHub
parent 2da919fee9
commit f4268347da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -227,7 +227,6 @@ class FeatureSearchStore implements IFeatureSearchStore {
const sortByMapping = {
name: 'features.name',
type: 'features.type',
lastSeenAt: lastSeenQuery,
stale: 'features.stale',
};
@ -245,6 +244,12 @@ class FeatureSearchStore implements IFeatureSearchStore {
[envName],
)
.toString();
} else if (sortBy === 'lastSeenAt') {
rankingSql += `${this.db
.raw(
`coalesce(${lastSeenQuery}, features.last_seen_at) ${validatedSortOrder} nulls last`,
)
.toString()}, features.created_at asc, features.name asc`;
} else if (sortByMapping[sortBy]) {
rankingSql += `${this.db
.raw(`?? ${validatedSortOrder}`, [

View File

@ -1,5 +1,6 @@
import dbInit, { ITestDb } from '../../../test/e2e/helpers/database-init';
import {
insertLastSeenAt,
IUnleashTest,
setupAppWithAuth,
} from '../../../test/e2e/helpers/test-helper';
@ -392,6 +393,8 @@ test('should sort features', async () => {
await app.enableFeature('my_feature_c', 'default');
await app.favoriteFeature('my_feature_b');
await insertLastSeenAt('my_feature_c', db.rawDatabase, 'default');
const { body: ascName } = await sortFeatures({
sortBy: 'name',
sortOrder: 'asc',
@ -476,6 +479,20 @@ test('should sort features', async () => {
],
total: 3,
});
const { body: lastSeenAscSort } = await sortFeatures({
sortBy: 'lastSeenAt',
sortOrder: 'asc',
});
expect(lastSeenAscSort).toMatchObject({
features: [
{ name: 'my_feature_c' },
{ name: 'my_feature_a' },
{ name: 'my_feature_b' },
],
total: 3,
});
});
test('should sort features when feature names are numbers', async () => {
@ -541,7 +558,7 @@ test('should paginate correctly when using tags', async () => {
});
});
test('should not return duplicate entries when sorting by last seen', async () => {
test('should not return duplicate entries when sorting by environments', async () => {
await app.createFeature('my_feature_a');
await app.createFeature('my_feature_b');
await app.createFeature('my_feature_c');