From b9efeb46b28cd92836da6f0aba5416f0a605d1da Mon Sep 17 00:00:00 2001 From: ionite34 Date: Thu, 28 Dec 2023 13:01:59 +0800 Subject: [PATCH] Add find local model from index debug command --- .../Settings/MainSettingsViewModel.cs | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/StabilityMatrix.Avalonia/ViewModels/Settings/MainSettingsViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Settings/MainSettingsViewModel.cs index bf241a12..7f39fb44 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Settings/MainSettingsViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Settings/MainSettingsViewModel.cs @@ -48,6 +48,7 @@ using StabilityMatrix.Core.Extensions; using StabilityMatrix.Core.Helper; using StabilityMatrix.Core.Helper.HardwareInfo; using StabilityMatrix.Core.Models; +using StabilityMatrix.Core.Models.Database; using StabilityMatrix.Core.Models.FileInterfaces; using StabilityMatrix.Core.Models.Settings; using StabilityMatrix.Core.Python; @@ -754,6 +755,71 @@ public partial class MainSettingsViewModel : PageViewModelBase } #endregion + #region Debug Commands + + public CommandItem[] DebugCommands => [new CommandItem(DebugFindLocalModelFromIndexCommand)]; + + [RelayCommand] + private async Task DebugFindLocalModelFromIndex() + { + var textFields = new TextBoxField[] + { + new() { Label = "Blake3 Hash" }, + new() { Label = "SharedFolderType" } + }; + + var dialog = DialogHelper.CreateTextEntryDialog("Find Local Model", "", textFields); + + if (await dialog.ShowAsync() == ContentDialogResult.Primary) + { + Func>> modelGetter; + + if (textFields.ElementAtOrDefault(0)?.Text is { } hash && !string.IsNullOrWhiteSpace(hash)) + { + modelGetter = () => modelIndexService.FindByHashAsync(hash); + } + else if (textFields.ElementAtOrDefault(1)?.Text is { } type && !string.IsNullOrWhiteSpace(type)) + { + modelGetter = () => modelIndexService.FindAsync(Enum.Parse(type)); + } + else + { + return; + } + + var timer = Stopwatch.StartNew(); + + var result = (await modelGetter()).ToImmutableArray(); + + timer.Stop(); + + if (result.Length != 0) + { + await DialogHelper + .CreateMarkdownDialog( + string.Join( + "\n\n", + result.Select( + (model, i) => + $"[{i + 1}] {model.RelativePath.ToRepr()} " + + $"({model.DisplayModelName}, {model.DisplayModelVersion})" + ) + ), + $"Found Models ({CodeTimer.FormatTime(timer.Elapsed)})" + ) + .ShowAsync(); + } + else + { + await DialogHelper + .CreateMarkdownDialog(":(", $"No models found ({CodeTimer.FormatTime(timer.Elapsed)})") + .ShowAsync(); + } + } + } + + #endregion + #region Info Section public void OnVersionClick()