Skip to content

Commit

Permalink
Add option to filter and compact history list for current action
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Oct 29, 2024
1 parent 26dd7f1 commit 9e0fa97
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 45 deletions.
35 changes: 35 additions & 0 deletions src/ScriptRunner/ScriptRunner.GUI/ViewModels/ExecutionLogAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using DynamicData;

namespace ScriptRunner.GUI.ViewModels;

public record ExecutionLogAction(DateTime Timestamp, string Source, string Name, Dictionary<string, string> Parameters)
{
[JsonIgnore]
public InlineCollection ParametersDescription => new InlineCollection()
{
new Run("["),

Parameters.SelectMany((x,i) =>
{
var value = x.Value?.StartsWith("!!vault:") == true ? "*****" : x.Value;
return new[]
{
new Run($"{x.Key} = "),
new Run(value)
{
Foreground = Brushes.LightGreen,
},
new Run( i< Parameters.Count -1?", ":"")
};
}),
new Run("]"),
};

public string ParametersDescriptionString() => string.Join(", ", Parameters.OrderBy(x=>x.Key).Select(x => $"{x.Key} = {x.Value}"));
};
79 changes: 47 additions & 32 deletions src/ScriptRunner/ScriptRunner.GUI/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
using System.Reactive.Linq;
using System.Security.Principal;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Documents;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Media;
using Avalonia.Threading;
using CliWrap;
using DynamicData;
Expand Down Expand Up @@ -206,6 +203,7 @@ public bool ShowNewVersionAvailable

public MainWindowViewModel(ParamsPanelFactory paramsPanelFactory, VaultProvider vaultProvider)
{
CompactedHistoryForCurrent = true;
this._configRepositoryUpdater = new ConfigRepositoryUpdater(new CliRepositoryClient(command =>
{
var tcs = new TaskCompletionSource<CliCommandOutputs>();
Expand Down Expand Up @@ -294,16 +292,40 @@ public MainWindowViewModel(ParamsPanelFactory paramsPanelFactory, VaultProvider
h => this.ExecutionLog.CollectionChanged -= h)
.Select(_ => Unit.Default) // We don't care about the event args; we just want to know something changed.
.StartWith(Unit.Default) // To ensure initial population.
.CombineLatest(this.WhenAnyValue(x => x.SelectedAction).Where(x => x != null),
.CombineLatest(
this.WhenAnyValue(
x => x.SelectedAction,
x=>x.CompactedHistoryForCurrent,
x=>x.TermForCurrentHistoryFilter
).Where(x => x.Item1 != null).Throttle(TimeSpan.FromMilliseconds(200)),
(_, selectedAction) => selectedAction)
.Select(selectedAction =>
.Select(data =>
{
return this.ExecutionLog
.Where(y => y.Source == selectedAction.SourceName && y.Name == selectedAction.Name);
var (selectedAction, compacted, term) = data;
var filtered = this.ExecutionLog.Where(y => y.Source == selectedAction!.SourceName && y.Name == selectedAction.Name);

if (compacted)
{
filtered = filtered.GroupBy(x => x.ParametersDescriptionString(), (key, group) => group.First());
}

if (string.IsNullOrWhiteSpace(term) == false)
{
filtered = filtered.Where(x => x.Parameters.Values.Any(p => p?.Contains(term, StringComparison.InvariantCultureIgnoreCase) == true));
}

return filtered;
})
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.ExecutionLogForCurrent, out _executionLogForCurrent);


this.WhenAnyValue(x => x.SelectedAction)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(s =>
{
TermForCurrentHistoryFilter = "";
});


_appUpdateScheduler = new RealTimeScheduler(TimeSpan.FromDays(1), TimeSpan.FromHours(1), async () =>
{
Expand All @@ -323,6 +345,23 @@ public MainWindowViewModel(ParamsPanelFactory paramsPanelFactory, VaultProvider
BuildUi();
}

private bool _compactedHistoryForCurrent;

public bool CompactedHistoryForCurrent
{
get => _compactedHistoryForCurrent;
set => this.RaiseAndSetIfChanged(ref _compactedHistoryForCurrent, value);
}

private string _termForCurrentHistoryFilter;

public string TermForCurrentHistoryFilter
{
get => _termForCurrentHistoryFilter;
set => this.RaiseAndSetIfChanged(ref _termForCurrentHistoryFilter, value);
}


private async Task RefreshInfoAbouAppUpdates()
{
var isNewerVersion = await appUpdater.CheckIsNewerVersionAvailable();
Expand Down Expand Up @@ -922,30 +961,6 @@ private static string[] SplitCommand(string command)
}
}

public record ExecutionLogAction(DateTime Timestamp, string Source, string Name, Dictionary<string, string> Parameters)
{
[JsonIgnore]
public InlineCollection ParametersDescription => new InlineCollection()
{
new Run("["),

Parameters.SelectMany((x,i) =>
{
var value = x.Value?.StartsWith("!!vault:") == true ? "*****" : x.Value;
return new[]
{
new Run($"{x.Key} = "),
new Run(value)
{
Foreground = Brushes.LightGreen,
},
new Run( i< Parameters.Count -1?", ":"")
};
}),
new Run("]"),
};
};

public record RecentAction(ActionId ActionId, DateTime Timestamp);

public record ActionId(string SourceName, string ActionName, string ParameterSet);
Expand Down
30 changes: 17 additions & 13 deletions src/ScriptRunner/ScriptRunner.GUI/Views/ActionDetailsSection.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,23 @@
</Border>
</TabItem>
<TabItem Header="History" IsSelected="{Binding !SelectedAction.HasDocs}">
<ListBox SelectedItem="{Binding SelectedRecentExecution}" ItemsSource="{Binding ExecutionLogForCurrent}">
<ListBox.ItemTemplate>


<DataTemplate x:DataType="viewModels:ExecutionLogAction">
<Grid ColumnDefinitions="Auto, *" RowDefinitions="Auto, *">
<avalonia:Icon Value="fas fa-clock" Margin="0,3,0,0" VerticalAlignment="Top"/>
<TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" TextWrapping="Wrap" Margin="10,0" Text="{Binding Timestamp, StringFormat={}{0:s}}"></TextBlock>
<SelectableTextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Padding="0, 5" Inlines="{Binding ParametersDescription}" TextWrapping="Wrap" IsHitTestVisible="False"></SelectableTextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid RowDefinitions="Auto,*">
<StackPanel Orientation="Vertical">
<TextBox Watermark="Search" Text="{Binding TermForCurrentHistoryFilter}"></TextBox>
<CheckBox IsChecked="{Binding CompactedHistoryForCurrent}" ToolTip.Tip="Show latest execution with the same parameters">Compacted</CheckBox>
</StackPanel>
<ListBox Grid.Row="1" SelectedItem="{Binding SelectedRecentExecution}" ItemsSource="{Binding ExecutionLogForCurrent}">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="viewModels:ExecutionLogAction">
<Grid ColumnDefinitions="Auto, *" RowDefinitions="Auto, *">
<avalonia:Icon Value="fas fa-clock" Margin="0,3,0,0" VerticalAlignment="Top"/>
<TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" TextWrapping="Wrap" Margin="10,0" Text="{Binding Timestamp, StringFormat={}{0:s}}"></TextBlock>
<SelectableTextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Padding="0, 5" Inlines="{Binding ParametersDescription}" TextWrapping="Wrap" IsHitTestVisible="False"></SelectableTextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</TabItem>
</TabControl>

Expand Down

0 comments on commit 9e0fa97

Please sign in to comment.