Skip to content
Merged
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
23 changes: 14 additions & 9 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private async Task RunQueryAsync(QuickQuery? quick, bool refresh = false)

if (channels.Length == 0)
{
StatusText.Text = Localization.Text("SelectChannel");
SetOperationStatus(() => Localization.Text("SelectChannel"));
return;
}

Expand All @@ -121,6 +121,7 @@ private async Task RunQueryAsync(QuickQuery? quick, bool refresh = false)
SetOperationStatus(() => quick is null
? Localization.Text("Querying")
: Localization.Format("QueryingNamed", Localization.Text(quick.Name)));
var queryCompleted = false;

try
{
Expand All @@ -142,7 +143,7 @@ void ShowFirstBatch(IReadOnlyList<EventRow> batch)
var filtered = batch.Where(row => EventQuery.Matches(row, criteria)).ToArray();
Dispatcher.BeginInvoke(() =>
{
if (token.IsCancellationRequested)
if (!CanShowFirstBatch(_operationCancellation, operation, queryCompleted, token))
return;
previewRows.AddRange(filtered);
EventsGrid.ItemsSource = ProblemGrouping.Group(previewRows);
Expand Down Expand Up @@ -171,6 +172,7 @@ void ShowFirstBatch(IReadOnlyList<EventRow> batch)
token.ThrowIfCancellationRequested();
var groups = ProblemGrouping.Group(rows);
token.ThrowIfCancellationRequested();
queryCompleted = true;
_rows = rows;
_allRows = allRows;
_groups = groups;
Expand All @@ -180,27 +182,26 @@ void ShowFirstBatch(IReadOnlyList<EventRow> batch)
var firstError = results.Select(result => result.Error).FirstOrDefault(error => error is not null);
AdminButton.Visibility = results.Any(result => result.RequiresAdmin) ? Visibility.Visible : Visibility.Collapsed;
StatusText.ToolTip = string.Join(Environment.NewLine, results.Select(result => result.Error).OfType<string>());
StatusText.Text = Localization.Format("QuerySummary", allRows.Length, rows.Length,
rows.Count(row => row.Level == "Critical"), rows.Count(row => row.Level == "Error"),
rows.Count(row => row.Level == "Warning"), groups.Count) +
(errors > 0 ? Localization.Format("SkippedChannels", errors, firstError) : "");
SetOperationStatus(() => Localization.Format("QuerySummary", allRows.Length, rows.Length,
rows.Count(row => row.Level == "Critical"), rows.Count(row => row.Level == "Error"),
rows.Count(row => row.Level == "Warning"), groups.Count) +
(errors > 0 ? Localization.Format("SkippedChannels", errors, firstError) : ""));
}
catch (OperationCanceledException)
{
if (ReferenceEquals(_operationCancellation, operation))
StatusText.Text = Localization.Text("QueryCancelled");
SetOperationStatus(() => Localization.Text("QueryCancelled"));
}
catch (Exception exception)
{
if (ReferenceEquals(_operationCancellation, operation))
StatusText.Text = exception.Message;
SetOperationStatus(() => exception.Message);
}
finally
{
if (ReferenceEquals(_operationCancellation, operation))
{
_operationCancellation = null;
_operationStatus = null;
operation.Dispose();
SearchButton.IsEnabled = true;
CancelButton.IsEnabled = false;
Expand All @@ -209,6 +210,10 @@ void ShowFirstBatch(IReadOnlyList<EventRow> batch)
}
}

internal static bool CanShowFirstBatch(CancellationTokenSource? current, CancellationTokenSource operation,
bool queryCompleted, CancellationToken token) =>
!token.IsCancellationRequested && ReferenceEquals(current, operation) && !queryCompleted;

private async void Window_Drop(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetData(DataFormats.FileDrop) is not string[] { Length: > 0 } files ||
Expand Down
18 changes: 17 additions & 1 deletion tests/EventFast.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
("XPath/time/level filters", TestXPath),
("Keyword and Event ID filters", TestFilters),
("Provider-aware quick filters", TestQuickFilters),
("Query first-batch lifecycle", TestQueryFirstBatchLifecycle),
("Grouping/classifier/sorting", TestGrouping),
("Problem summary", TestProblemSummary),
("Event details layout", TestEventDetailsLayout),
Expand Down Expand Up @@ -296,6 +297,17 @@ static void TestQuickFilters()
ProblemClassifier.Classify("Fake-USB-Provider", 7) == "Fake-USB-Provider + Event ID 7");
}

static void TestQueryFirstBatchLifecycle()
{
using var operation = new CancellationTokenSource();
using var newerOperation = new CancellationTokenSource();
Assert(MainWindow.CanShowFirstBatch(operation, operation, false, operation.Token));
Assert(!MainWindow.CanShowFirstBatch(operation, operation, true, operation.Token));
Assert(!MainWindow.CanShowFirstBatch(newerOperation, operation, false, operation.Token));
operation.Cancel();
Assert(!MainWindow.CanShowFirstBatch(operation, operation, false, operation.Token));
}

static void TestGrouping()
{
var now = DateTime.Now;
Expand Down Expand Up @@ -652,7 +664,11 @@ eventsGrid.SelectedItem is ProblemGroup &&
if (searchEnabled && !status.Contains("Querying", StringComparison.Ordinal))
{
var eventIds = eventsGrid.Items.Cast<ProblemGroup>().Select(group => group.EventId).ToArray();
Assert(eventIds.SequenceEqual(eventIds.Order()));
Assert(eventIds.SequenceEqual(eventIds.Order()) && status.StartsWith("Scanned", StringComparison.Ordinal));
window.ApplyLanguage("zh-TW", persist: false);
var localizedStatus = ((TextBlock)window.FindName("StatusText")).Text;
Assert(localizedStatus.StartsWith("掃描", StringComparison.Ordinal) &&
!localizedStatus.Contains("背景查詢中", StringComparison.Ordinal));
timer.Stop();
window.Close();
app.Shutdown();
Expand Down
Loading