This Commit contains all the changes for the new feature for S.L in regards to ExcelStats of the GUI
This commit is contained in:
@@ -9,7 +9,7 @@ namespace AiQ_GUI
|
||||
// Reads camera model numbers and descriptions from the database, sorts them alphabetically by model number (except "AB12CD", which appears last), and formats each entry as "ModelNumber - Description".
|
||||
public static string[] ReadCamTypes()
|
||||
{
|
||||
List<Tuple<string, string>> modelTuples = new List<Tuple<string, string>>(30); // Preallocate list with estimated capacity to reduce internal resizing
|
||||
List<Tuple<string, string>> modelTuples = new(30); // Preallocate list with estimated capacity to reduce internal resizing
|
||||
using OleDbConnection conn = new(connString);
|
||||
|
||||
try
|
||||
@@ -100,6 +100,9 @@ namespace AiQ_GUI
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Expected universal data for the GUI, read from the database
|
||||
public class UniversalData
|
||||
{
|
||||
|
||||
@@ -66,28 +66,22 @@ namespace AiQ_GUI
|
||||
return "Spreadsheet not found";
|
||||
}
|
||||
|
||||
using XLWorkbook workbook = new XLWorkbook(FilePath);
|
||||
using XLWorkbook workbook = new(FilePath);
|
||||
IXLWorksheet worksheet = workbook.Worksheets.First();
|
||||
|
||||
// Start from row 1 and check downwards to find next empty row in column B
|
||||
int row = 1;
|
||||
while (!worksheet.Cell(row, 2).IsEmpty()) // Column B = Serial numbers
|
||||
{
|
||||
row++;
|
||||
}
|
||||
|
||||
// Safety check to avoid invalid index
|
||||
if (row <= 1)
|
||||
{
|
||||
return "Last serial number not found";
|
||||
}
|
||||
|
||||
// Generate new serial number from previous
|
||||
string lastSerialNumber = worksheet.Cell(row - 1, 2).GetString(); // Column B
|
||||
if (string.IsNullOrWhiteSpace(lastSerialNumber) || !lastSerialNumber.StartsWith("K"))
|
||||
{
|
||||
return "Invalid last serial number format";
|
||||
}
|
||||
|
||||
int NewSerialNumberInt = Convert.ToInt32(lastSerialNumber.Substring(1)) + 1;
|
||||
string newSerialNumber = "K" + NewSerialNumberInt;
|
||||
@@ -124,7 +118,7 @@ namespace AiQ_GUI
|
||||
return "Spreadsheet not found";
|
||||
}
|
||||
|
||||
using XLWorkbook workbook = new XLWorkbook(filePath);
|
||||
using XLWorkbook workbook = new(filePath);
|
||||
IXLWorksheet worksheet = workbook.Worksheets.First();
|
||||
|
||||
// Find the row with the matching serial number in column B
|
||||
@@ -136,8 +130,7 @@ namespace AiQ_GUI
|
||||
{
|
||||
// Update columns D-E
|
||||
worksheet.Cell(row, 4).Value = "Pre Test: " + DateTime.Now.ToString("dd/MM/yyyy");
|
||||
worksheet.Cell(row, 5).Value = Vers.version + " - " + Vers.revision + Environment.NewLine +
|
||||
Vers.buildtime + Environment.NewLine + Vers.proquint;
|
||||
worksheet.Cell(row, 5).Value = $"{Vers.version} - {Vers.revision}" + Environment.NewLine + Vers.buildtime + Environment.NewLine + Vers.proquint;
|
||||
// Update MAC to column H
|
||||
worksheet.Cell(row, 8).Value = Vers.MAC;
|
||||
|
||||
@@ -172,7 +165,7 @@ namespace AiQ_GUI
|
||||
return "Spreadsheet not found";
|
||||
}
|
||||
|
||||
using XLWorkbook workbook = new XLWorkbook(FilePath);
|
||||
using XLWorkbook workbook = new(FilePath);
|
||||
IXLWorksheet worksheet = workbook.Worksheets.First();
|
||||
|
||||
// Find the row with the matching serial number in column B
|
||||
@@ -224,15 +217,13 @@ namespace AiQ_GUI
|
||||
return "Spreadsheet not found";
|
||||
}
|
||||
|
||||
using XLWorkbook workbook = new XLWorkbook(FilePath);
|
||||
using XLWorkbook workbook = new(FilePath);
|
||||
IXLWorksheet worksheet = workbook.Worksheets.First();
|
||||
|
||||
// Find next free row by checking column C
|
||||
int row = 2;
|
||||
while (!worksheet.Cell(row, 3).IsEmpty()) // Column C
|
||||
{
|
||||
row++;
|
||||
}
|
||||
|
||||
// Write model, serial, date, and protectionKeyId to columns C–F
|
||||
worksheet.Cell(row, 3).Value = model; // Column C
|
||||
@@ -263,7 +254,7 @@ namespace AiQ_GUI
|
||||
return 0;
|
||||
}
|
||||
|
||||
using XLWorkbook workbook = new XLWorkbook(filePath);
|
||||
using XLWorkbook workbook = new(filePath);
|
||||
IXLWorksheet worksheet = workbook.Worksheets.First();
|
||||
|
||||
int row = 2; // Start from row 2
|
||||
@@ -277,10 +268,9 @@ namespace AiQ_GUI
|
||||
if (sheetSerial.Contains(serial) && sheetModel.Contains(model))
|
||||
{
|
||||
string rmaStr = worksheet.Cell(row, 1).GetString(); // Column A
|
||||
|
||||
if (int.TryParse(rmaStr, out int rmaNumber))
|
||||
{
|
||||
return rmaNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { /* Safe to ignore bad row */ }
|
||||
@@ -306,7 +296,7 @@ namespace AiQ_GUI
|
||||
return 0;
|
||||
}
|
||||
|
||||
using XLWorkbook workbook = new XLWorkbook(filePath);
|
||||
using XLWorkbook workbook = new(filePath);
|
||||
IXLWorksheet worksheet = workbook.Worksheets.First();
|
||||
|
||||
int row = 1;
|
||||
@@ -315,10 +305,9 @@ namespace AiQ_GUI
|
||||
try
|
||||
{
|
||||
string cellSerial = worksheet.Cell(row, 2).GetString();
|
||||
|
||||
if (cellSerial.Contains(serial))
|
||||
{
|
||||
return row;
|
||||
}
|
||||
}
|
||||
catch { /* Ignore malformed rows */ }
|
||||
|
||||
@@ -343,14 +332,12 @@ namespace AiQ_GUI
|
||||
return -1;
|
||||
}
|
||||
|
||||
using XLWorkbook workbook = new XLWorkbook(filePath);
|
||||
using XLWorkbook workbook = new(filePath);
|
||||
IXLWorksheet worksheet = workbook.Worksheets.First();
|
||||
|
||||
int row = 2; // Start from C2
|
||||
while (!worksheet.Cell(row, 3).IsEmpty()) // Column C = 3
|
||||
{
|
||||
row++;
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
@@ -368,7 +355,7 @@ namespace AiQ_GUI
|
||||
// 1. Ensure spreadsheet exists and has valid starting data
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
XLWorkbook workbook = new XLWorkbook();
|
||||
XLWorkbook workbook = new();
|
||||
IXLWorksheet ws = workbook.Worksheets.Add("Sheet1");
|
||||
|
||||
// Pre-fill first row with dummy data for serial
|
||||
@@ -379,7 +366,7 @@ namespace AiQ_GUI
|
||||
}
|
||||
|
||||
// 2. Run Pre-Test update
|
||||
Versions fakeVersion = new Versions
|
||||
Versions fakeVersion = new()
|
||||
{
|
||||
version = "1.6.4",
|
||||
revision = "bf16134",
|
||||
@@ -388,11 +375,11 @@ namespace AiQ_GUI
|
||||
MAC = "00:1A:2B:3C:4D:5E"
|
||||
};
|
||||
|
||||
string preTestResult = Excel.UpdateSpreadSheetPreTest(filePath, fakeVersion, "Fake Cam", "TestModel");
|
||||
string preTestResult = UpdateSpreadSheetPreTest(filePath, fakeVersion, "Fake Cam", "TestModel");
|
||||
Debug.WriteLine("Pre-Test Result: " + preTestResult);
|
||||
|
||||
// 3. Run Final Test update
|
||||
Diags fakeDiags = new Diags
|
||||
Diags fakeDiags = new()
|
||||
{
|
||||
serialNumber = preTestResult, // Newly generated serial
|
||||
licenses = new Licenses
|
||||
@@ -404,13 +391,13 @@ namespace AiQ_GUI
|
||||
}
|
||||
};
|
||||
|
||||
SSHData fakeSSH = new SSHData
|
||||
SSHData fakeSSH = new()
|
||||
{
|
||||
packages = "\r\nlibvaxtorocr10 - 8.4.20-1\r\nvaxtorocrdatacpu3 - 8.4.20-1",
|
||||
tailscale = true
|
||||
};
|
||||
|
||||
string finalTestResult = Excel.UpdateSpreadSheetFinalTest(filePath, fakeDiags, fakeSSH, 0);
|
||||
string finalTestResult = UpdateSpreadSheetFinalTest(filePath, fakeDiags, fakeSSH, 0);
|
||||
Debug.WriteLine("Final-Test Result: " + finalTestResult);
|
||||
|
||||
// // 4. Run Vaxtor update
|
||||
|
||||
247
Microsoft/StatsExcel.cs
Normal file
247
Microsoft/StatsExcel.cs
Normal file
@@ -0,0 +1,247 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace AiQ_GUI.Microsoft
|
||||
{
|
||||
internal class StatsExcel
|
||||
{
|
||||
private const string connString =
|
||||
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\Shared drives\MAV Production GUI's\AiQ\GUI's\AiQ_Final_Test.accdb;Persist Security Info=False;OLE DB Services=-1;";
|
||||
|
||||
private readonly string exportPath =
|
||||
@"C:\Users\BradleyBorn\OneDrive - MAV Systems Ltd\Desktop\AiQ_Test_Stats.xlsx";
|
||||
|
||||
public void ExportDatabaseToExcel()
|
||||
{
|
||||
try
|
||||
{
|
||||
MainForm.Instance.AddToActionsList("=== ExportDatabaseToExcel START ===");
|
||||
|
||||
using (OleDbConnection conn = new(connString))
|
||||
{
|
||||
conn.Open();
|
||||
MainForm.Instance.AddToActionsList("Connected to Access database.");
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
// ==================== MAIN STATS EXPORT ====================
|
||||
string selectColumns = @"
|
||||
ModelNumber,
|
||||
[Total Tests Run],
|
||||
[Pre Tests Passed],
|
||||
[Pre Tests Failed],
|
||||
[Final Tests Passed],
|
||||
[Final Tests Failed],
|
||||
[RMA Total Tests Run],
|
||||
[RMA Pre Tests Passed],
|
||||
[RMA Pre Tests Failed],
|
||||
[RMA Final Tests Passed],
|
||||
[RMA Final Tests Failed],
|
||||
[Diagnostic Failure],
|
||||
[Failed To Set Model Or Serial Number],
|
||||
[Camera Not In Test Tube],
|
||||
[Could Not Zoom Modules To 8000],
|
||||
[Could Not Zoom Modules To Full Wide],
|
||||
[Please Get RMA Number From Operations Team Before Continuing],
|
||||
[Please Get A Valid Vaxtor Product Key Before Continuing],
|
||||
[Visual Test Fail - Sleeve Not Aligned],
|
||||
[Visual Test Fail - Not All Front Screws Fitted],
|
||||
[Visual Test Fail - Not All Rear Screws Fitted],
|
||||
[Visual Test Fail - Unit rattles]";
|
||||
|
||||
string query = $@"
|
||||
SELECT
|
||||
{selectColumns}
|
||||
FROM AiQ";
|
||||
|
||||
OleDbDataAdapter adapter = new(query, conn);
|
||||
DataTable dataTable = new();
|
||||
adapter.Fill(dataTable);
|
||||
|
||||
if (dataTable.Rows.Count == 0)
|
||||
{
|
||||
MainForm.Instance.AddToActionsList("No data found in AiQ table.");
|
||||
return;
|
||||
}
|
||||
|
||||
string monthName = now.ToString("MMMM");
|
||||
string sheetName = $"{monthName} Stats";
|
||||
|
||||
MainForm.Instance.AddToActionsList($"Adding sheet: {sheetName}");
|
||||
|
||||
XLWorkbook workbook;
|
||||
if (System.IO.File.Exists(exportPath))
|
||||
{
|
||||
workbook = new XLWorkbook(exportPath);
|
||||
MainForm.Instance.AddToActionsList("Opened existing workbook.");
|
||||
}
|
||||
else
|
||||
{
|
||||
workbook = new XLWorkbook();
|
||||
MainForm.Instance.AddToActionsList("Created new workbook (file not found).");
|
||||
}
|
||||
|
||||
if (workbook.Worksheets.Contains(sheetName))
|
||||
{
|
||||
workbook.Worksheet(sheetName).Delete();
|
||||
MainForm.Instance.AddToActionsList($"Deleted old sheet: {sheetName}");
|
||||
}
|
||||
|
||||
var ws = workbook.Worksheets.Add(sheetName);
|
||||
ws.Cell(1, 1).InsertTable(dataTable, "AiQ_Stats", true);
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
ws.Cell(1, dataTable.Columns.Count + 2).Value = "Exported:";
|
||||
ws.Cell(2, dataTable.Columns.Count + 2).Value = now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
// ==================== DIAGS STATS EXPORT (CURRENT MONTH ONLY) ====================
|
||||
MainForm.Instance.AddToActionsList("Exporting DiagsStats for current month...");
|
||||
|
||||
DateTime monthStart = new DateTime(now.Year, now.Month, 1);
|
||||
DateTime nextMonth = monthStart.AddMonths(1);
|
||||
|
||||
string diagsQuery = @"
|
||||
SELECT
|
||||
[Date],
|
||||
[Model],
|
||||
[Red Diags Labels],
|
||||
[RhTxBxActions Contents]
|
||||
FROM DiagsStats
|
||||
WHERE [Date] >= ? AND [Date] < ?";
|
||||
|
||||
OleDbDataAdapter diagsAdapter = new(diagsQuery, conn);
|
||||
diagsAdapter.SelectCommand.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = monthStart });
|
||||
diagsAdapter.SelectCommand.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = nextMonth });
|
||||
|
||||
DataTable diagsTable = new();
|
||||
diagsAdapter.Fill(diagsTable);
|
||||
|
||||
if (diagsTable.Rows.Count > 0)
|
||||
{
|
||||
int startRow = ws.LastRowUsed().RowNumber() + 3;
|
||||
|
||||
ws.Cell(startRow, 1).Value = "Diags Stats (Current Month)";
|
||||
ws.Cell(startRow, 1).Style.Font.Bold = true;
|
||||
ws.Cell(startRow, 1).Style.Font.FontSize = 12;
|
||||
|
||||
ws.Cell(startRow + 1, 1).InsertTable(diagsTable, "Diags_Stats", true);
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
MainForm.Instance.AddToActionsList($"Added DiagsStats ({diagsTable.Rows.Count} rows) starting at row {startRow}.");
|
||||
}
|
||||
else
|
||||
{
|
||||
MainForm.Instance.AddToActionsList("No DiagsStats data found for current month.");
|
||||
}
|
||||
|
||||
// ==================== SAVE MAIN SHEET ====================
|
||||
workbook.SaveAs(exportPath);
|
||||
MainForm.Instance.AddToActionsList($"Added {sheetName} to workbook: {exportPath}");
|
||||
|
||||
workbook.Dispose();
|
||||
|
||||
// ==================== BACKUP TABLE (ALWAYS, BEFORE RESET) ====================
|
||||
string ts = now.ToString("yyyyMMdd_HHmmss");
|
||||
string backupTableName = $"Ztats_{ts}";
|
||||
|
||||
if (backupTableName.Length > 64)
|
||||
backupTableName = backupTableName.Substring(0, 64);
|
||||
|
||||
string backupSql = $@"
|
||||
SELECT
|
||||
{selectColumns}
|
||||
INTO [{backupTableName}]
|
||||
FROM AiQ";
|
||||
try
|
||||
{
|
||||
using (var cmdBackup = new OleDbCommand(backupSql, conn))
|
||||
{
|
||||
cmdBackup.ExecuteNonQuery();
|
||||
}
|
||||
MainForm.Instance.AddToActionsList($"Created backup table: [{backupTableName}]");
|
||||
}
|
||||
catch (Exception backupEx)
|
||||
{
|
||||
MainForm.Instance.AddToActionsList($"ERROR creating backup table [{backupTableName}]. Aborting reset to protect data. Details: {backupEx.Message}");
|
||||
MainForm.Instance.AddToActionsList("=== ExportDatabaseToExcel END (backup failed, no reset) ===");
|
||||
return;
|
||||
}
|
||||
|
||||
using (var tx = conn.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
string resetSql = @"
|
||||
UPDATE AiQ SET
|
||||
[Total Tests Run] = 0,
|
||||
[Pre Tests Passed] = 0,
|
||||
[Pre Tests Failed] = 0,
|
||||
[Final Tests Passed] = 0,
|
||||
[Final Tests Failed] = 0,
|
||||
[RMA Total Tests Run] = 0,
|
||||
[RMA Pre Tests Passed] = 0,
|
||||
[RMA Pre Tests Failed] = 0,
|
||||
[RMA Final Tests Passed] = 0,
|
||||
[RMA Final Tests Failed] = 0,
|
||||
[Diagnostic Failure] = 0,
|
||||
[Failed To Set Model Or Serial Number] = 0,
|
||||
[Camera Not In Test Tube] = 0,
|
||||
[Could Not Zoom Modules To 8000] = 0,
|
||||
[Could Not Zoom Modules To Full Wide] = 0,
|
||||
[Please Get RMA Number From Operations Team Before Continuing] = 0,
|
||||
[Please Get A Valid Vaxtor Product Key Before Continuing] = 0,
|
||||
[Visual Test Fail - Sleeve Not Aligned] = 0,
|
||||
[Visual Test Fail - Not All Front Screws Fitted] = 0,
|
||||
[Visual Test Fail - Not All Rear Screws Fitted] = 0,
|
||||
[Visual Test Fail - Unit rattles] = 0;";
|
||||
|
||||
using (var cmdReset = new OleDbCommand(resetSql, conn, tx))
|
||||
{
|
||||
int affected = cmdReset.ExecuteNonQuery();
|
||||
MainForm.Instance.AddToActionsList($"Zeroed counters on AiQ rows: {affected} row(s).");
|
||||
}
|
||||
|
||||
int updatedRows;
|
||||
using (var cmdUpd = new OleDbCommand("UPDATE UniversalData SET LastStatsRun = ?", conn, tx))
|
||||
{
|
||||
cmdUpd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = now });
|
||||
updatedRows = cmdUpd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
if (updatedRows == 0)
|
||||
{
|
||||
using (var cmdIns = new OleDbCommand("INSERT INTO UniversalData (LastStatsRun) VALUES (?)", conn, tx))
|
||||
{
|
||||
cmdIns.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = now });
|
||||
cmdIns.ExecuteNonQuery();
|
||||
MainForm.Instance.AddToActionsList("Inserted LastStatsRun row.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MainForm.Instance.AddToActionsList("Updated LastStatsRun successfully.");
|
||||
}
|
||||
|
||||
tx.Commit();
|
||||
MainForm.Instance.AddToActionsList("Reset committed.");
|
||||
}
|
||||
catch (Exception resetEx)
|
||||
{
|
||||
tx.Rollback();
|
||||
MainForm.Instance.AddToActionsList($"ERROR during reset, rolled back. Details: {resetEx.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MainForm.Instance.AddToActionsList("=== ExportDatabaseToExcel END ===");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MainForm.Instance.AddToActionsList($"ERROR exporting stats: {ex.Message}");
|
||||
MainForm.Instance.AddToActionsList($"Stack Trace: {ex.StackTrace}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,8 @@ namespace AiQ_GUI
|
||||
|
||||
Properties.Settings.Default.FirstRun = false;
|
||||
Properties.Settings.Default.Save();
|
||||
|
||||
Application.Restart(); // Restart to not be in admin mode
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user