// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; namespace Spludlow { public class SpeedProfile { private DateTime StartTime; private DataTable ResultsTable; private DataRow CurrentRow = null; private int StartColumn; public SpeedProfile(string tableName) //, string[] varibleNames) { this.ResultsTable = new DataTable(tableName); this.ResultsTable.Columns.Add("SampleId", typeof(int)); this.ResultsTable.Columns["SampleId"].AutoIncrement = true; this.ResultsTable.Columns.Add("SampleName", typeof(string)); this.StartColumn = this.ResultsTable.Columns.Count; //if (varibleNames != null) // test speed difenrce //{ // foreach (string varibleName in varibleNames) // this.ResultsTable.Columns.Add(varibleName, typeof(TimeSpan)); //} this.Reset(); } public void Reset() { this.StartTime = DateTime.Now; } public void NewSample() { this.NewSample(""); } public void NewSample(string sampleName) { this.CurrentRow = null; this.GetCurrent(); this.CurrentRow["SampleName"] = sampleName; this.Reset(); } public void Submit(string varibleName) { DateTime endTime = DateTime.Now; this.GetCurrent(); if (this.ResultsTable.Columns.Contains(varibleName) == false) this.ResultsTable.Columns.Add(varibleName, typeof(TimeSpan)); this.CurrentRow[varibleName] = endTime - this.StartTime; this.Reset(); } //public void Next() //{ // this.CurrentRow = null; // this.GetCurrent(); // this.Reset(); //} public void LogReport(string subject) { LogResults("R", subject); } public void LogFinish(string subject) { LogResults("F", subject); } private void LogResults(string logType, string subject) { if (this.ResultsTable.Rows.Count == 0) { Spludlow.Log.Report(subject + ", Total Took empty"); return; } this.ResultsTable.Columns.Add("TOTAL", typeof(TimeSpan)); foreach (DataRow row in this.ResultsTable.Rows) { TimeSpan totalSpan = TimeSpan.Zero; for (int colIndex = this.StartColumn; colIndex < (this.ResultsTable.Columns.Count - 1); ++colIndex) { if (row.IsNull(colIndex) == false) totalSpan += (TimeSpan)row[colIndex]; } row["TOTAL"] = totalSpan; } if (this.ResultsTable.Rows.Count > 1) { DataRow totalRow = this.ResultsTable.NewRow(); totalRow["SampleName"] = "TOTAL"; for (int coloumnIndex = this.StartColumn; coloumnIndex < this.ResultsTable.Columns.Count; ++coloumnIndex) totalRow[coloumnIndex] = TimeSpan.Zero; foreach (DataRow row in this.ResultsTable.Rows) { for (int colIndex = this.StartColumn; colIndex < this.ResultsTable.Columns.Count; ++colIndex) { TimeSpan rowSpan = TimeSpan.Zero; if (row.IsNull(colIndex) == false) rowSpan = (TimeSpan)row[colIndex]; totalRow[colIndex] = rowSpan + (TimeSpan)totalRow[colIndex]; } } this.ResultsTable.Rows.Add(totalRow); } DataRow lastRow = Spludlow.Data.ADO.LastRow(this.ResultsTable); string totalTook = Spludlow.Text.TimeTook((TimeSpan)lastRow["TOTAL"]); Spludlow.Log.Submit(logType, subject + ", Speed Profile Logged Took:" + totalTook, new object[] { this.ResultsTable }); } private void GetCurrent() { if (this.CurrentRow == null) { this.CurrentRow = this.ResultsTable.NewRow(); for (int coloumnIndex = this.StartColumn; coloumnIndex < this.ResultsTable.Columns.Count; ++coloumnIndex) this.CurrentRow[coloumnIndex] = TimeSpan.Zero; this.ResultsTable.Rows.Add(this.CurrentRow); } } } }