Search: order results newest first in every group
Chat hits sort by the chat's last use whatever field matched, and run, step, field, artifact and pipeline hits sort by their own date, before the result cap applies so the cap drops the oldest. Each kind's row scan also walks newest first. Score still reports the match tier. The app sorts both groups newest first as well.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../data/search_repository.dart';
|
||||
import 'search_models.dart';
|
||||
import 'search_order.dart';
|
||||
import 'search_query.dart';
|
||||
|
||||
/// Wording for a server whose search does not know the `chat` kind (it
|
||||
@@ -51,9 +52,11 @@ class SearchChatSlice {
|
||||
truncated = false;
|
||||
error = unsupportedChatSearchCopy;
|
||||
} else {
|
||||
results = response.results
|
||||
.where((hit) => hit['kind'] == SearchKind.chat.wire)
|
||||
.toList(growable: false);
|
||||
results = searchHitsNewestFirst(
|
||||
response.results
|
||||
.where((hit) => hit['kind'] == SearchKind.chat.wire)
|
||||
.toList(growable: false),
|
||||
);
|
||||
truncated = response.truncated;
|
||||
}
|
||||
} catch (failure) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import '../../../core/api_gateway.dart';
|
||||
import '../data/search_repository.dart';
|
||||
import 'search_chat_slice.dart';
|
||||
import 'search_models.dart';
|
||||
import 'search_order.dart';
|
||||
import 'search_query.dart';
|
||||
import 'search_row.dart';
|
||||
|
||||
@@ -246,7 +247,7 @@ class OmniSearchController extends ChangeNotifier {
|
||||
_notify();
|
||||
return;
|
||||
}
|
||||
_results = response.results;
|
||||
_results = searchHitsNewestFirst(response.results);
|
||||
_truncated = response.truncated;
|
||||
_scannedSteps = response.scannedSteps;
|
||||
_scannedBytes = response.scannedBytes;
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// Search hits newest first, the order the server sends them in. The app
|
||||
/// sorts again so each displayed group stays newest first even from an older
|
||||
/// server that ranked by match tier. Equal or missing dates keep the server's
|
||||
/// order; a hit without a date sorts last.
|
||||
List<JsonMap> searchHitsNewestFirst(List<JsonMap> hits) {
|
||||
final indexed = [
|
||||
for (var i = 0; i < hits.length; i++) (i, _hitMillis(hits[i]), hits[i]),
|
||||
];
|
||||
indexed.sort((a, b) {
|
||||
final byDate = b.$2.compareTo(a.$2);
|
||||
return byDate != 0 ? byDate : a.$1.compareTo(b.$1);
|
||||
});
|
||||
return [for (final entry in indexed) entry.$3];
|
||||
}
|
||||
|
||||
/// A chat hit's last-used time (`chat.updated_at`, epoch milliseconds), else
|
||||
/// any hit's `recency` timestamp; -1 when neither is present.
|
||||
int _hitMillis(JsonMap hit) {
|
||||
final chat = hit['chat'];
|
||||
if (chat is Map) {
|
||||
final updated = chat['updated_at'];
|
||||
if (updated is num && updated > 0) return updated.toInt();
|
||||
}
|
||||
final recency = DateTime.tryParse('${hit['recency'] ?? ''}');
|
||||
return recency?.millisecondsSinceEpoch ?? -1;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_order.dart';
|
||||
|
||||
import 'search_test_support.dart';
|
||||
|
||||
JsonMap _run(int id, String recency, {int score = 1}) => {
|
||||
'kind': 'run',
|
||||
'target': 'run:$id',
|
||||
'score': score,
|
||||
'recency': recency,
|
||||
'run': {'id': id},
|
||||
};
|
||||
|
||||
JsonMap _chat(String id, int updatedAt, {String? snippet}) {
|
||||
final hit = chatHit(id, snippet: snippet);
|
||||
return {
|
||||
...hit,
|
||||
'chat': <String, dynamic>{...hit['chat'] as Map, 'updated_at': updatedAt},
|
||||
};
|
||||
}
|
||||
|
||||
List<Object?> _targets(List<JsonMap> hits) => [
|
||||
for (final hit in hits) hit['target'],
|
||||
];
|
||||
|
||||
void main() {
|
||||
test('hits sort newest first whatever their score; ties and undated hits '
|
||||
'keep the server order, undated last', () {
|
||||
final sorted = searchHitsNewestFirst([
|
||||
_run(1, '2026-01-01T00:00:00Z', score: 3),
|
||||
{
|
||||
'kind': 'run',
|
||||
'target': 'run:9',
|
||||
'run': {'id': 9},
|
||||
},
|
||||
_run(2, '2026-03-01T00:00:00Z'),
|
||||
_run(3, '2026-02-01T00:00:00Z'),
|
||||
_run(4, '2026-02-01T00:00:00Z'),
|
||||
]);
|
||||
expect(_targets(sorted), ['run:2', 'run:3', 'run:4', 'run:1', 'run:9']);
|
||||
});
|
||||
|
||||
test('chat hits sort by last use, not by matched field', () {
|
||||
final sorted = searchHitsNewestFirst([
|
||||
_chat('title_old', 1000),
|
||||
_chat('message_new', 3000, snippet: 'flaky'),
|
||||
_chat('dir_mid', 2000),
|
||||
]);
|
||||
expect(_targets(sorted), [
|
||||
'chat:message_new',
|
||||
'chat:dir_mid',
|
||||
'chat:title_old',
|
||||
]);
|
||||
});
|
||||
|
||||
test('the controller shows both groups newest first', () async {
|
||||
final gateway = QueuedGateway(holdChats: true);
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('flaky');
|
||||
await pumpEventQueue();
|
||||
gateway.succeed(
|
||||
0,
|
||||
envelope([
|
||||
_run(1, '2026-01-01T00:00:00Z', score: 3),
|
||||
_run(2, '2026-05-01T00:00:00Z'),
|
||||
]),
|
||||
);
|
||||
gateway.chatPending.single.complete(
|
||||
envelope([_chat('a', 1000), _chat('b', 5000, snippet: 'flaky')]),
|
||||
);
|
||||
await pumpEventQueue();
|
||||
expect(_targets(controller.shownResults), ['run:2', 'run:1']);
|
||||
expect(_targets(controller.shownChats), ['chat:b', 'chat:a']);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user