|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Erik Darling, Darling Data LLC |
| 3 | + * |
| 4 | + * This file is part of the SQL Server Performance Monitor. |
| 5 | + * |
| 6 | + * Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Collections.ObjectModel; |
| 12 | +using System.ComponentModel; |
| 13 | +using System.Linq; |
| 14 | +using System.Windows; |
| 15 | +using System.Windows.Media; |
| 16 | +using PerformanceMonitorDashboard.Models; |
| 17 | +using PerformanceMonitorDashboard.Services; |
| 18 | + |
| 19 | +namespace PerformanceMonitorDashboard |
| 20 | +{ |
| 21 | + public partial class ExcludedDatabasesDialog : Window |
| 22 | + { |
| 23 | + private readonly ServerManager _serverManager; |
| 24 | + private readonly ServerConnection _server; |
| 25 | + private ObservableCollection<DatabaseExclusionItem> _items = new(); |
| 26 | + |
| 27 | + public bool ExclusionsModified { get; private set; } |
| 28 | + |
| 29 | + public ExcludedDatabasesDialog(ServerManager serverManager, ServerConnection server) |
| 30 | + { |
| 31 | + InitializeComponent(); |
| 32 | + _serverManager = serverManager; |
| 33 | + _server = server; |
| 34 | + HeaderText.Text = $"Excluded Databases — {server.DisplayNameWithIntent}"; |
| 35 | + Loaded += async (_, _) => await LoadAsync(); |
| 36 | + } |
| 37 | + |
| 38 | + private async System.Threading.Tasks.Task LoadAsync() |
| 39 | + { |
| 40 | + StatusText.Text = "Loading databases…"; |
| 41 | + DatabasesItemsControl.ItemsSource = null; |
| 42 | + |
| 43 | + try |
| 44 | + { |
| 45 | + var liveDatabases = await _serverManager.GetUserDatabasesAsync(_server); |
| 46 | + var existingExclusions = await _serverManager.GetCollectorDatabaseExclusionsAsync(_server); |
| 47 | + |
| 48 | + var liveSet = new HashSet<string>(liveDatabases, StringComparer.OrdinalIgnoreCase); |
| 49 | + |
| 50 | + _items = new ObservableCollection<DatabaseExclusionItem>(); |
| 51 | + |
| 52 | + /* Live databases: sortable, checkable */ |
| 53 | + foreach (var name in liveDatabases.OrderBy(n => n, StringComparer.OrdinalIgnoreCase)) |
| 54 | + { |
| 55 | + _items.Add(new DatabaseExclusionItem |
| 56 | + { |
| 57 | + Name = name, |
| 58 | + DisplayName = name, |
| 59 | + IsExcluded = existingExclusions.Contains(name, StringComparer.OrdinalIgnoreCase), |
| 60 | + IsEnabled = true, |
| 61 | + IsStale = false |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + /* Stale entries: in exclusion list but not present on the server. Show greyed, disabled, pre-checked. */ |
| 66 | + foreach (var name in existingExclusions |
| 67 | + .Where(n => !liveSet.Contains(n)) |
| 68 | + .OrderBy(n => n, StringComparer.OrdinalIgnoreCase)) |
| 69 | + { |
| 70 | + _items.Add(new DatabaseExclusionItem |
| 71 | + { |
| 72 | + Name = name, |
| 73 | + DisplayName = $"{name} (missing)", |
| 74 | + IsExcluded = true, |
| 75 | + IsEnabled = false, |
| 76 | + IsStale = true |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + DatabasesItemsControl.ItemsSource = _items; |
| 81 | + StatusText.Text = $"{liveDatabases.Count} database(s) on this server, {existingExclusions.Count} currently excluded."; |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + StatusText.Text = $"Failed to load: {ex.Message}"; |
| 86 | + MessageBox.Show(this, |
| 87 | + $"Could not read database list from '{_server.DisplayNameWithIntent}':\n\n{ex.Message}", |
| 88 | + "Load Failed", |
| 89 | + MessageBoxButton.OK, |
| 90 | + MessageBoxImage.Error); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private async void Save_Click(object sender, RoutedEventArgs e) |
| 95 | + { |
| 96 | + /* Collect every checked item (live + stale). Stale ones can't be unchecked, so they stay if they were excluded. */ |
| 97 | + var checkedNames = _items |
| 98 | + .Where(i => i.IsExcluded) |
| 99 | + .Select(i => i.Name) |
| 100 | + .ToList(); |
| 101 | + |
| 102 | + Cursor = System.Windows.Input.Cursors.Wait; |
| 103 | + try |
| 104 | + { |
| 105 | + await _serverManager.SaveCollectorDatabaseExclusionsAsync(_server, checkedNames); |
| 106 | + ExclusionsModified = true; |
| 107 | + DialogResult = true; |
| 108 | + } |
| 109 | + catch (Exception ex) |
| 110 | + { |
| 111 | + MessageBox.Show(this, |
| 112 | + $"Failed to save exclusions on '{_server.DisplayNameWithIntent}':\n\n{ex.Message}", |
| 113 | + "Save Failed", |
| 114 | + MessageBoxButton.OK, |
| 115 | + MessageBoxImage.Error); |
| 116 | + } |
| 117 | + finally |
| 118 | + { |
| 119 | + Cursor = null; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void Cancel_Click(object sender, RoutedEventArgs e) |
| 124 | + { |
| 125 | + DialogResult = false; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public class DatabaseExclusionItem : INotifyPropertyChanged |
| 130 | + { |
| 131 | + public string Name { get; set; } = ""; |
| 132 | + public string DisplayName { get; set; } = ""; |
| 133 | + private bool _isExcluded; |
| 134 | + public bool IsExcluded |
| 135 | + { |
| 136 | + get => _isExcluded; |
| 137 | + set { _isExcluded = value; OnPropertyChanged(nameof(IsExcluded)); } |
| 138 | + } |
| 139 | + public bool IsEnabled { get; set; } = true; |
| 140 | + public bool IsStale { get; set; } |
| 141 | + |
| 142 | + public Brush ForegroundBrush => IsStale |
| 143 | + ? (Brush)Application.Current.FindResource("ForegroundMutedBrush") |
| 144 | + : (Brush)Application.Current.FindResource("ForegroundBrush"); |
| 145 | + |
| 146 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 147 | + private void OnPropertyChanged(string propertyName) |
| 148 | + => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 149 | + } |
| 150 | +} |
0 commit comments