From b131ba01a810e2bce3aca5fb0f58d758e98e68aa Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Fri, 8 Jan 2016 13:51:54 +0100 Subject: [PATCH] Batch results written into the built in Results database. --- Results/DB.cs | 71 +++++++++++++++++++ .../BenchControl/Sequences/MainSeq.cs | 28 ++++---- .../BenchControl/Sequences/SequenceBase.cs | 12 +++- 3 files changed, 96 insertions(+), 15 deletions(-) diff --git a/Results/DB.cs b/Results/DB.cs index a9f63e1ea..3ddea4101 100644 --- a/Results/DB.cs +++ b/Results/DB.cs @@ -9,6 +9,7 @@ using log4net; using NHibernate; using NHibernate.Cfg; using NHibernate.Tool.hbm2ddl; +using Results.Entities; namespace Results { @@ -180,5 +181,75 @@ namespace Results ComponentsList = session.CreateQuery("FROM Components").List(); WaterMeterDataList = session.CreateQuery("FROM WaterMeterData").List(); } + + + /// + /// Update TestData, Components and WaterMeterData fo tests and water meters in batch results + /// with existing data in static lists TestDataList, ComponentsList and WaterMeterDataList. + /// + /// Batch results + /// true when any data modiifed + public static bool UpdateBatchData(Entities.Batch batch) + { + ISession session = DB.CreateSession(); + + if (session == null) return false; + + foreach (var tr in batch.TestRslts) + { + /// Search in TestDataList and update test data in the new batch + TestData td = TestData.UpdateList(TestDataList, tr.TestData); + tr.TestData = td; + + /// Search in ComponentsList and update components in the new batch + Components cd = Components.UpdateList(ComponentsList, tr.Components); + tr.Components = cd; + } + + foreach (var wm in batch.WaterMeters) + { + /// Search in WaterMeterDataList and update water meter data in the new batch + WaterMeterData wmd = WaterMeterData.UpdateList(WaterMeterDataList, wm.WaterMeterData); + wm.WaterMeterData = wmd; + } + + return true; /// TODO: Really check for modifications + } + + + public static bool SaveNewBatch(Entities.Batch batch) + { + ISession session = DB.CreateSession(); + if (session == null) return false; + + TestDataList = session.CreateQuery("FROM TestData").List(); + ComponentsList = session.CreateQuery("FROM Components").List(); + WaterMeterDataList = session.CreateQuery("FROM WaterMeterData").List(); + + UpdateBatchData(batch); + + ITransaction transaction = session.BeginTransaction(); + if (transaction == null) return false; + + try + { + session.SaveOrUpdate(TestDataList); + session.SaveOrUpdate(ComponentsList); + session.SaveOrUpdate(WaterMeterDataList); + + session.SaveOrUpdate(batch); + + transaction.Commit(); + } + catch (Exception exc) + { + transaction.Rollback(); + log.FatalFormat("Saving results of batch #{0} into the results DB failed: {1}", batch.BatchNr, exc.Message); + return false; + } + + return true; + } + } } diff --git a/TestBenchFramework/BenchControl/Sequences/MainSeq.cs b/TestBenchFramework/BenchControl/Sequences/MainSeq.cs index ea5c9d448..a282b39a4 100644 --- a/TestBenchFramework/BenchControl/Sequences/MainSeq.cs +++ b/TestBenchFramework/BenchControl/Sequences/MainSeq.cs @@ -664,19 +664,21 @@ namespace TBF.BenchControl.Sequences } while (e.Contains(Event.Busy)); - /// TODO: Reimplement !!!!!!!!!!! - ///// - ///// Save to database and to 'summary results' logger - ///// - //IList sortedResults = Utils.GetSortedTestResults(StateMachine.Procedure, results, 0); - //foreach (var rslt in sortedResults) - //{ - // if (rslt != null) - // { - // Config.FluentCommon.SaveToDb(StateMachine.WtSession, rslt); - // summaryResults.Info(TestResult2CsvLine(rslt)); - // } - //} + bool resultsSent = !e.Contains(Event.Error); + ProcessData.BatchRslts.Batch.RsltsSent = resultsSent; + + /// + /// Save results to the built in 'Results' database + /// + Results.DB.SaveNewBatch(ProcessData.BatchRslts.Batch); + + /// + /// Save results to 'summary results' logger + /// + foreach (var tr in ProcessData.BatchRslts.Batch.TestRslts) + { + summaryResults.Info(TestResult2CsvLine(tr)); + } Program.LocalSettings.BatchNr++; diff --git a/TestBenchFramework/BenchControl/Sequences/SequenceBase.cs b/TestBenchFramework/BenchControl/Sequences/SequenceBase.cs index 1889e2375..150c4647e 100644 --- a/TestBenchFramework/BenchControl/Sequences/SequenceBase.cs +++ b/TestBenchFramework/BenchControl/Sequences/SequenceBase.cs @@ -692,6 +692,14 @@ namespace TBF.BenchControl.Sequences { Results.Entities.TestRslt tstRslt = ProcessData.BatchRslts.GetTestRslt(testName, part); + if (tstRslt == null) return string.Empty; + + return TestResult2CsvLine(tstRslt); + } + + + protected string TestResult2CsvLine(Results.Entities.TestRslt tstRslt) + { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(tstRslt.StartTime); @@ -776,7 +784,7 @@ namespace TBF.BenchControl.Sequences { if (!ProcessData.BatchRslts.WaterMeters[i].Compound()) { - Results.Entities.MeterTestRslt mtrRslt = ProcessData.BatchRslts.GetMeterTestRslt(testName, i, CompoundMeterId.Single); + Results.Entities.MeterTestRslt mtrRslt = ProcessData.BatchRslts.GetMeterTestRslt(tstRslt.Name(), i, CompoundMeterId.Single); if (mtrRslt != null) { @@ -810,7 +818,7 @@ namespace TBF.BenchControl.Sequences { for (byte b = (byte)CompoundMeterId.CompoundMain; b <= (byte)CompoundMeterId.Compound; b++) { - Results.Entities.MeterTestRslt mtrRslt = ProcessData.BatchRslts.GetMeterTestRslt(testName, i, (CompoundMeterId)b); + Results.Entities.MeterTestRslt mtrRslt = ProcessData.BatchRslts.GetMeterTestRslt(tstRslt.Name(), i, (CompoundMeterId)b); if (mtrRslt != null) {