Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidWiseman committed Oct 29, 2023
1 parent 8418ef9 commit 084bb03
Show file tree
Hide file tree
Showing 116 changed files with 821 additions and 1,059 deletions.
6 changes: 1 addition & 5 deletions DBADash.Test/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ public static void RunProcess(ProcessStartInfo psi)
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

using var p = Process.Start(psi);
if (p == null)
{
throw new Exception("Process is NULL");
}
using var p = Process.Start(psi) ?? throw new Exception("Process is NULL");
p.WaitForExit();
var output = p.StandardOutput.ReadToEnd();
var error = p.StandardError.ReadToEnd();
Expand Down
2 changes: 1 addition & 1 deletion DBADash/CollectionConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public string GetMaintenanceCron()
}

[JsonIgnore]
public List<DBADashConnection> SecondaryDestinationConnections { get; set; } = new List<DBADashConnection>();
public List<DBADashConnection> SecondaryDestinationConnections { get; set; } = new();

// Required for serialization
public string[] SecondaryDestinations
Expand Down
41 changes: 19 additions & 22 deletions DBADash/ConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public class ConnectionInfo
public bool IsRDS => ComputerNetBIOSName.StartsWith("EC2AMAZ-");

// Extended events only supported on Standard and Enterprise editions for RDS
public bool IsXESupported => IsRDS && !(EngineEdition == DatabaseEngineEdition.Standard || EngineEdition == DatabaseEngineEdition.Enterprise) ? false : GetXESupported(MajorVersion);
public bool IsXESupported => (!IsRDS || EngineEdition is DatabaseEngineEdition.Standard or DatabaseEngineEdition.Enterprise) && GetXESupported(MajorVersion);

public static int GetMajorVersion(string ProductVersion)
{
return Int32.Parse(ProductVersion[..ProductVersion.IndexOf('.')]);
return int.Parse(ProductVersion[..ProductVersion.IndexOf('.')]);
}

public static bool GetXESupported(string ProductVersion)
Expand All @@ -47,28 +47,25 @@ public static bool GetXESupported(int MajorVersion)
public static ConnectionInfo GetConnectionInfo(string connectionString)
{
var connectionInfo = new ConnectionInfo();
using (var cn = new SqlConnection(connectionString))
using (var cmd = new SqlCommand("SELECT SERVERPROPERTY('EngineEdition'),SERVERPROPERTY('ProductVersion'),DB_NAME(),@@SERVERNAME,SERVERPROPERTY('ComputerNamePhysicalNetBIOS')", cn))
using var cn = new SqlConnection(connectionString);
using var cmd = new SqlCommand("SELECT SERVERPROPERTY('EngineEdition'),SERVERPROPERTY('ProductVersion'),DB_NAME(),@@SERVERNAME,SERVERPROPERTY('ComputerNamePhysicalNetBIOS')", cn);
cn.Open();
using var rdr = cmd.ExecuteReader();
rdr.Read();
connectionInfo.EngineEditionValue = rdr.GetInt32(0);
try
{
cn.Open();
using (var rdr = cmd.ExecuteReader())
{
rdr.Read();
connectionInfo.EngineEditionValue = rdr.GetInt32(0);
try
{
connectionInfo.EngineEdition = (DatabaseEngineEdition)connectionInfo.EngineEditionValue;
}
catch
{
connectionInfo.EngineEdition = DatabaseEngineEdition.Unknown;
}
connectionInfo.ProductVersion = rdr.GetString(1);
connectionInfo.DatabaseName = rdr.GetString(2);
connectionInfo.ServerName = rdr.GetString(3);
connectionInfo.ComputerNetBIOSName = rdr.IsDBNull(4) ? "" : rdr.GetString(4); /* ComputerNamePhysicalNetBIOS is NULL for AzureDB */
}
connectionInfo.EngineEdition = (DatabaseEngineEdition)connectionInfo.EngineEditionValue;
}
catch
{
connectionInfo.EngineEdition = DatabaseEngineEdition.Unknown;
}
connectionInfo.ProductVersion = rdr.GetString(1);
connectionInfo.DatabaseName = rdr.GetString(2);
connectionInfo.ServerName = rdr.GetString(3);
connectionInfo.ComputerNetBIOSName = rdr.IsDBNull(4) ? "" : rdr.GetString(4); /* ComputerNamePhysicalNetBIOS is NULL for AzureDB */

return connectionInfo;
}
}
Expand Down
18 changes: 9 additions & 9 deletions DBADash/Creds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ namespace DBADash
{
internal class Creds
{
private static string userKeyFolder => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DBADash");
private static string UserKeyFolder => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DBADash");

[SupportedOSPlatform("windows")]
public static void SetPassword(string target, string password)
{
string fileName = target + ".key";
string filePath = Path.Combine(userKeyFolder, fileName);
Directory.CreateDirectory(userKeyFolder);
var fileName = target + ".key";
var filePath = Path.Combine(UserKeyFolder, fileName);
Directory.CreateDirectory(UserKeyFolder);
File.WriteAllText(filePath, password.UserEncryptString());
}

[SupportedOSPlatform("windows")]
public static void Remove(string target)
{
string fileName = target + ".key";
string filePath = Path.Combine(userKeyFolder, fileName);
var fileName = target + ".key";
var filePath = Path.Combine(UserKeyFolder, fileName);
File.Delete(filePath);
}

[SupportedOSPlatform("windows")]
public static string GetPassword(string target)
{
string fileName = target + ".key";
string filePath = Path.Combine(userKeyFolder, fileName);
var fileName = target + ".key";
var filePath = Path.Combine(UserKeyFolder, fileName);
if (File.Exists(filePath))
{
string password = File.ReadAllText(filePath);
var password = File.ReadAllText(filePath);
return password.UserDecryptString();
}
else
Expand Down
2 changes: 1 addition & 1 deletion DBADash/DBADashConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public string MasterConnectionString
}
}

public DBADashConnection MasterConnection() => new DBADashConnection(MasterConnectionString);
public DBADashConnection MasterConnection() => new(MasterConnectionString);

public string EncryptedConnectionString
{
Expand Down
24 changes: 12 additions & 12 deletions DBADash/DBADashSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ public enum IOCollectionLevels

public CollectionSchedules CollectionSchedules
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL ? collectionSchedules : null;
get => SourceConnection is { Type: ConnectionType.SQL } ? collectionSchedules : null;
set => collectionSchedules = value;
}

public PlanCollectionThreshold RunningQueryPlanThreshold
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL ? runningQueryPlanThreshold : null;
get => SourceConnection is { Type: ConnectionType.SQL } ? runningQueryPlanThreshold : null;
set => runningQueryPlanThreshold = value;
}

public string SchemaSnapshotDBs
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL ? schemaSnapshotDBs : string.Empty;
get => SourceConnection is { Type: ConnectionType.SQL } ? schemaSnapshotDBs : string.Empty;
set => schemaSnapshotDBs = value;
}

Expand All @@ -61,7 +61,7 @@ public string SchemaSnapshotDBs
[JsonIgnore]
public bool PlanCollectionEnabled
{
get => RunningQueryPlanThreshold != null && RunningQueryPlanThreshold.PlanCollectionEnabled;
get => RunningQueryPlanThreshold is { PlanCollectionEnabled: true };
set => RunningQueryPlanThreshold = value ? PlanCollectionThreshold.DefaultThreshold : null;
}

Expand Down Expand Up @@ -136,47 +136,47 @@ public static string GenerateFileName(string connection)
[DefaultValue(false)]
public bool NoWMI
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL && noWMI;
get => SourceConnection is { Type: ConnectionType.SQL } && noWMI;
set => noWMI = value;
}

public Int32 SlowQuerySessionMaxMemoryKB
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL ? slowQuerySessionMaxMemoryKB : 0;
get => SourceConnection is { Type: ConnectionType.SQL } ? slowQuerySessionMaxMemoryKB : 0;
set => slowQuerySessionMaxMemoryKB = value;
}

public Int32 SlowQueryTargetMaxMemoryKB
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL ? slowQueryTargetMaxMemoryKB : 0;
get => SourceConnection is { Type: ConnectionType.SQL } ? slowQueryTargetMaxMemoryKB : 0;
set => slowQueryTargetMaxMemoryKB = value;
}

[DefaultValue(true)]
public bool UseDualEventSession
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL ? useDualXESesion : false;
get => SourceConnection is { Type: ConnectionType.SQL } && useDualXESesion;
set => useDualXESesion = value;
}

[DefaultValue(-1)]
public Int32 SlowQueryThresholdMs
public int SlowQueryThresholdMs
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL ? slowQueryThresholdMs : -1;
get => SourceConnection is { Type: ConnectionType.SQL } ? slowQueryThresholdMs : -1;
set => slowQueryThresholdMs = value;
}

[DefaultValue(false)]
public bool PersistXESessions
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL && persistXESessions; set => persistXESessions = value;
get => SourceConnection is { Type: ConnectionType.SQL } && persistXESessions; set => persistXESessions = value;
}

private bool _collectSessionWaits = true;

public bool CollectSessionWaits
{
get => SourceConnection != null && SourceConnection.Type == ConnectionType.SQL && _collectSessionWaits; set => _collectSessionWaits = value;
get => SourceConnection is { Type: ConnectionType.SQL } && _collectSessionWaits; set => _collectSessionWaits = value;
}

public DBADashSource(string source)
Expand Down
2 changes: 1 addition & 1 deletion DBADash/DBCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ private void CollectPowerPlanWMI()

if (!string.IsNullOrEmpty(instanceId))
{
activePowerPlanGUID = Guid.Parse(instanceId.Substring(instanceId.Length - 38, 38));
activePowerPlanGUID = Guid.Parse(instanceId.AsSpan(instanceId.Length - 38, 38));
}
}
catch (Exception ex)
Expand Down
6 changes: 3 additions & 3 deletions DBADash/PlanCollectionThreshold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class PlanCollectionThreshold

public bool PlanCollectionEnabled => CPUThreshold != Int32.MaxValue || MemoryGrantThreshold != Int32.MaxValue || DurationThreshold != Int32.MaxValue;

public static PlanCollectionThreshold DefaultThreshold => new PlanCollectionThreshold() { CPUThreshold = 1000, DurationThreshold = 10000, MemoryGrantThreshold = 6400, CountThreshold = 2 };
public static PlanCollectionThreshold DefaultThreshold => new() { CPUThreshold = 1000, DurationThreshold = 10000, MemoryGrantThreshold = 6400, CountThreshold = 2 };

public static PlanCollectionThreshold PlanCollectionDisabledThreshold => new PlanCollectionThreshold();
public static PlanCollectionThreshold PlanCollectionDisabledThreshold => new();

public static PlanCollectionThreshold CollectAllPlansThreshold => new PlanCollectionThreshold() { CountThreshold = 0, CPUThreshold = 0, DurationThreshold = 0, MemoryGrantThreshold = 0 };
public static PlanCollectionThreshold CollectAllPlansThreshold => new() { CountThreshold = 0, CPUThreshold = 0, DurationThreshold = 0, MemoryGrantThreshold = 0 };
}
}
32 changes: 16 additions & 16 deletions DBADashGUI/AgentJobs/AgentJobThreshold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ namespace DBADashGUI.AgentJobs
public class AgentJobThreshold
{
public bool IsInherited;
public Int32 InstanceID { get; set; }
public int InstanceID { get; set; }
public Guid JobID { get; set; } = Guid.Empty;
public Int32? TimeSinceLastFailureWarning { get; set; }
public Int32? TimeSinceLastFailureCritical { get; set; }
public Int32? TimeSinceLastSucceededCritical { get; set; }
public Int32? TimeSinceLastSucceededWarning { get; set; }
public Int32? FailCount24HrsWarning { get; set; }
public Int32? FailCount24HrsCritical { get; set; }
public Int32? FailCount7DaysWarning { get; set; }
public Int32? FailCount7DaysCritical { get; set; }
public int? TimeSinceLastFailureWarning { get; set; }
public int? TimeSinceLastFailureCritical { get; set; }
public int? TimeSinceLastSucceededCritical { get; set; }
public int? TimeSinceLastSucceededWarning { get; set; }
public int? FailCount24HrsWarning { get; set; }
public int? FailCount24HrsCritical { get; set; }
public int? FailCount7DaysWarning { get; set; }
public int? FailCount7DaysCritical { get; set; }

public Int32? JobStepFails24HrsWarning { get; set; }
public Int32? JobStepFails24HrsCritical { get; set; }
public int? JobStepFails24HrsWarning { get; set; }
public int? JobStepFails24HrsCritical { get; set; }

public Int32? JobStepFails7DaysWarning { get; set; }
public Int32? JobStepFails7DaysCritical { get; set; }
public int? JobStepFails7DaysWarning { get; set; }
public int? JobStepFails7DaysCritical { get; set; }

public bool LastFailIsCritical { get; set; }
public bool LastFailIsWarning { get; set; }

public static AgentJobThreshold GetAgentJobThreshold(Int32 InstanceID, Guid JobID, string connectionString)
public static AgentJobThreshold GetAgentJobThreshold(int InstanceID, Guid JobID, string connectionString)
{
var threshold = new AgentJobThreshold
{
Expand Down Expand Up @@ -99,9 +99,9 @@ public void Save(string connectionString)
}
}

private static Int32? ColumnToNullableInt32(SqlDataReader rdr, string columnName)
private static int? ColumnToNullableInt32(SqlDataReader rdr, string columnName)
{
return rdr[columnName] == DBNull.Value ? null : (Int32?)rdr[columnName];
return rdr[columnName] == DBNull.Value ? null : (int?)rdr[columnName];
}
}
}
Loading

0 comments on commit 084bb03

Please sign in to comment.