Skip to content

Commit

Permalink
Adding Validator Management Tool
Browse files Browse the repository at this point in the history
  • Loading branch information
MichielOda committed Dec 29, 2023
1 parent d09bba2 commit a44bab0
Show file tree
Hide file tree
Showing 50 changed files with 12,526 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Skyline.DataMiner.CICD.Validators.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.Testing", "Common.Te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProtocolTests.SchemaGenerator", "ProtocolTests.SchemaGenerator\ProtocolTests.SchemaGenerator.csproj", "{A3278AF2-05A5-4953-9746-2821B4D07740}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Validator Management Tool", "Validator Management Tool\Validator Management Tool.csproj", "{82F5E0F7-A112-489E-9480-D4ECD9FACB4E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +59,10 @@ Global
{A3278AF2-05A5-4953-9746-2821B4D07740}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3278AF2-05A5-4953-9746-2821B4D07740}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3278AF2-05A5-4953-9746-2821B4D07740}.Release|Any CPU.Build.0 = Release|Any CPU
{82F5E0F7-A112-489E-9480-D4ECD9FACB4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{82F5E0F7-A112-489E-9480-D4ECD9FACB4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{82F5E0F7-A112-489E-9480-D4ECD9FACB4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{82F5E0F7-A112-489E-9480-D4ECD9FACB4E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 8 additions & 0 deletions Validator Management Tool/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Application x:Class="Validator_Management_Tool.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="../Views/MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
11 changes: 11 additions & 0 deletions Validator Management Tool/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Validator_Management_Tool
{
using System.Windows;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
26 changes: 26 additions & 0 deletions Validator Management Tool/BindableBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Validator_Management_Tool
{
using System.ComponentModel;
using System.Runtime.CompilerServices;

public class BindableBase : INotifyPropertyChanged
{
protected virtual void SetProperty<T>(ref T member, T val, [CallerMemberName] string propertyName = null)
{
if (Equals(member, val))
{
return;
}

member = val;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
}
244 changes: 244 additions & 0 deletions Validator Management Tool/Common/ExportManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
namespace Validator_Management_Tool.Common
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using Microsoft.Win32;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using Skyline.DataMiner.CICD.Validators.Common.Model;
using Validator_Management_Tool.Model;

/// <summary>
/// Static class that has methods to export the error messages.
/// </summary>
public static class ExportManager
{
/// <summary>
/// Used in Jenkins pipeline (exportErrorMessages.ps1).
/// </summary>
public static void ExportToExcelJenkins(Version version)
{
try
{
// Retrieve checks
var checks = new List<Check>();
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Serialization.Serializer.ReadXml(Path.Combine(directory, Settings.XmlPath));

foreach (var check in Serialization.Serializer.GetChecks())
{
checks.Add(check);
}

string fileName = Settings.ExportFile + " - " + version.ToString().Replace('.', '_');
CreateWorksheet(checks, Path.Combine(directory, fileName));
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

/// <summary>
/// Creates a chosen file with all the error messages in,
/// after it asked the location of the file with a save file dialog.
/// </summary>
/// <param name="checks">The list of checks for which a file has to be created.</param>
public static void ExportToExcel(ICollection<Check> checks)
{
SaveFileDialog saveFileDialog = new SaveFileDialog
{
DefaultExt = ".xlsx",
Filter = "Excel Worksheet (*.xlsx)|*.xlsx",
FileName = Settings.ExportPath + Settings.ExportFile,
};

if (saveFileDialog.ShowDialog() == true)
{
string extension = saveFileDialog.FileName.Split(new char[] { '.' })[1];
string filename = saveFileDialog.FileName.Replace("." + extension, String.Empty).Split(new char[] { '\\' }).Last();
Settings.ExportPath = saveFileDialog.FileName.Replace(filename + "." + extension, String.Empty);
Settings.ExportFile = filename;

// Export to Excel worksheet
CreateWorksheet(checks, saveFileDialog.FileName);

MessageBox.Show("Generating Done!", "Generation Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
}
}

/// <summary>
/// Creates a Excel Worksheet in the specified location with all the error messages from the list.
/// </summary>
/// <param name="checks">List of checks that has to be written to the file.</param>
/// <param name="filename">Path of the file that has to be created.</param>
private static void CreateWorksheet(ICollection<Check> checks, string filename)
{
if (!Path.HasExtension(filename))
{
filename = filename + ".xlsx";
}

IWorkbook workbook = new XSSFWorkbook();

CreateWorksheetSplit(workbook, checks, "All Error Messages");
CreateWorksheetSplit(workbook, checks.Where(x => x.Source == Source.Validator).ToList(), "Validator");
CreateWorksheetSplit(workbook, checks.Where(x => x.Source == Source.MajorChangeChecker).ToList(), "Major Change Checker");

using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
workbook.Write(fs, false);
}

workbook.Close();
}

private static void CreateWorksheetSplit(IWorkbook workbook, ICollection<Check> checks, string sheetName)
{
try
{
ISheet excelWorkSheet = workbook.CreateSheet(sheetName);

IRow headerRow = excelWorkSheet.CreateRow(0);

// Add table headers going cell by cell.
headerRow.CreateCell(0).SetCellValue("Full ID");
headerRow.CreateCell(1).SetCellValue("Category");
headerRow.CreateCell(2).SetCellValue("Namespace");
headerRow.CreateCell(3).SetCellValue("Check Name");
headerRow.CreateCell(4).SetCellValue("Error Message Name");
headerRow.CreateCell(5).SetCellValue("Description");
headerRow.CreateCell(6).SetCellValue("Severity");
headerRow.CreateCell(7).SetCellValue("Certainty");
headerRow.CreateCell(8).SetCellValue("Fix Impact");
headerRow.CreateCell(9).SetCellValue("Has Code Fix");
headerRow.CreateCell(10).SetCellValue("Source");
headerRow.CreateCell(11).SetCellValue("Details");
headerRow.CreateCell(12).SetCellValue("Example Code");
headerRow.CreateCell(13).SetCellValue("How To Fix");

int rowCount = 2;

var sortedChecks = checks.OrderBy(c => c.CategoryId).ThenBy(c => c.CheckId).ThenBy(c => c.ErrorId);
foreach (var check in sortedChecks)
{
IRow row = excelWorkSheet.CreateRow(rowCount);

row.CreateCell(0).SetCellValue(check.FullId);
row.CreateCell(1).SetCellValue(check.Category.ToString());
row.CreateCell(2).SetCellValue(SimplifyNamespace(check));
row.CreateCell(3).SetCellValue(check.CheckName);
row.CreateCell(4).SetCellValue(check.Name);

string description;
try
{
description = check.Description;
for (int i = 0; i < check.Parameters.Count; i++)
{
var param = check.Parameters[i];
string oldValue = String.Format("{{{0}}}", i);

string newValue;
if (String.IsNullOrWhiteSpace(param.Value))
{
newValue = String.Format("{{{0}}}", param.Text);
}
else
{
// No need to add the braces as it's a hard-coded value anyway.
newValue = String.Format("{0}", param.Value);
}

description = description.Replace(oldValue, newValue);
}
}
catch (IndexOutOfRangeException)
{
description = check.Description;
}

row.CreateCell(5).SetCellValue(description);
row.CreateCell(6).SetCellValue(check.Severity.ToString());
row.CreateCell(7).SetCellValue(check.Certainty.ToString());
row.CreateCell(8).SetCellValue(check.FixImpact.ToString());
row.CreateCell(9).SetCellValue(check.HasCodeFix);
row.CreateCell(10).SetCellValue(check.Source.ToString());
row.CreateCell(11).SetCellValue(check.Details);
row.CreateCell(12).SetCellValue(check.ExampleCode);
row.CreateCell(13).SetCellValue(check.HowToFix);

rowCount++;
}

excelWorkSheet.AutoSizeColumn(0);
excelWorkSheet.AutoSizeColumn(1);
excelWorkSheet.AutoSizeColumn(2);
excelWorkSheet.AutoSizeColumn(3);
excelWorkSheet.AutoSizeColumn(4);
excelWorkSheet.AutoSizeColumn(5);
excelWorkSheet.AutoSizeColumn(6);
excelWorkSheet.AutoSizeColumn(7);
excelWorkSheet.AutoSizeColumn(8);
excelWorkSheet.AutoSizeColumn(9);
excelWorkSheet.AutoSizeColumn(10);
excelWorkSheet.AutoSizeColumn(11);
excelWorkSheet.AutoSizeColumn(12);
excelWorkSheet.AutoSizeColumn(13);

excelWorkSheet.SetAutoFilter(new CellRangeAddress(0, rowCount, 0, 13));
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
}

private static string SimplifyNamespace(Check check)
{
List<string> nsParts = check.Namespace.Split('.').ToList();

List<string> simpleNs = new List<string>();
bool found = false;
string catString = check.Category.ToString();
foreach (string item in nsParts)
{
string partToCheck = item;
if (found)
{
simpleNs.Add(partToCheck);
continue;
}

if (check.Category == Category.ParameterGroup && String.Equals(partToCheck, "ParameterGroups"))
{
partToCheck = "ParameterGroup";
}

if (String.Equals(catString, partToCheck, StringComparison.OrdinalIgnoreCase))
{
found = true;
}
}

string ns;
if (simpleNs.Count == 0)
{
ns = nsParts.Last();
}
else
{
ns = String.Join(".", simpleNs);
}

return ns;
}
}
}
Loading

0 comments on commit a44bab0

Please sign in to comment.