Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 19 additions & 30 deletions APSIM.POStats.Shared/Collector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,25 @@ private static List<Table> GetTablesFromFile(string apsimxFileName)
Data = new List<VariableData>()
});
}
var matchFields = GetMatchColumnNamesForTable(apsimxFileName, tableName)
.Where(f => f != "SimulationName");

List<string> matchFields = new List<string>();
IEnumerable<string> fields = ["field_match_one", "field_match_two", "field_match_three", "field_match_four"];
using (SqliteCommand cmd = new SqliteCommand($"SELECT * FROM _PredictedObserved WHERE name = '{tableName}'", db))
{
using (SqliteDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
reader.Read();
foreach(string field in fields)
{
string match = reader[field].ToString();
if (!string.IsNullOrEmpty(match) && match != "SimulationName")
matchFields.Add(match);
}
}
}
}
Comment thread
ric394 marked this conversation as resolved.

string selectSQL = $"SELECT * FROM {tableName}";
using (SqliteCommand cmd = new SqliteCommand(selectSQL, db))
Expand Down Expand Up @@ -227,34 +244,6 @@ private static Dictionary<long, string> GetSimulationNameIdPairs(SqliteConnectio
return returnPairs;
}

/// <summary>
/// Get the column names that were used to match the predicted/observed data in a table.
/// </summary>
/// <param name="apsimxFileName">The .apsimx file that contains the match column names.</param>
/// <param name="tableName">The name of the table.</param>
/// <returns></returns>
private static IEnumerable<string> GetMatchColumnNamesForTable(string apsimxFileName, string tableName)
{
var options = new JsonDocumentOptions { AllowTrailingCommas = true };
using (JsonDocument document = JsonDocument.Parse(File.ReadAllText(apsimxFileName), options))
{
var rootNode = new JsonProxyForModel(document.RootElement);
var predictedObservedModel = rootNode.ChildrenRecursively
.FirstOrDefault(child => child.Type == "PredictedObserved" &&
child.Name == tableName);
if (predictedObservedModel == null)
throw new Exception($"Cannot find predicted observed table {tableName} in file {apsimxFileName}");

var matchElements = new List<string>()
{
predictedObservedModel.GetPropertyValue("FieldNameUsedForMatch"),
predictedObservedModel.GetPropertyValue("FieldName2UsedForMatch"),
predictedObservedModel.GetPropertyValue("FieldName3UsedForMatch")
};
return matchElements.Where(element => element != null);
}
}

/// <summary>
/// Create a label for the current row of a SqliteDataReader
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions APSIM.POStats.Tests/CollectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public void TearDown()
[Test]
public void TestEnsureNormalCollectorOperationWorks()
{
SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_PredictedObserved",
"Name,field_match_one,field_match_two,field_match_three,field_match_four" + Environment.NewLine +
"PO1,Date,,," + Environment.NewLine
));
Comment thread
ric394 marked this conversation as resolved.
SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_Simulations",
"ID,Name" + Environment.NewLine +
" 1,Sim1" + Environment.NewLine
Expand Down Expand Up @@ -154,6 +158,11 @@ public void EnsureObservedStringColumnsAreIgnored()
[Test]
public void EnsureObservedStringValuesInRowsAreIgnored()
{
SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_PredictedObserved",
"Name,field_match_one,field_match_two,field_match_three,field_match_four" + Environment.NewLine +
"PO1,Date,,," + Environment.NewLine
));

SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_Simulations",
"ID,Name" + Environment.NewLine +
" 1,Sim1" + Environment.NewLine
Expand Down Expand Up @@ -182,6 +191,11 @@ public void EnsureObservedStringValuesInRowsAreIgnored()
[Test]
public void EnsurePOTableNotUnderDataStoreIsFound()
{
SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_PredictedObserved",
"Name,field_match_one,field_match_two,field_match_three,field_match_four" + Environment.NewLine +
"PO1,Date,,," + Environment.NewLine
));

SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_Simulations",
"ID,Name" + Environment.NewLine +
" 1,Sim1" + Environment.NewLine
Expand All @@ -208,6 +222,11 @@ public void EnsurePOTableNotUnderDataStoreIsFound()
[Test]
public void EnsureCollectorFindsIntegers()
{
SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_PredictedObserved",
"Name,field_match_one,field_match_two,field_match_three,field_match_four" + Environment.NewLine +
"PO1,Date,,," + Environment.NewLine
));

SqliteUtilities.CreateTable(database, DataTableUtilities.FromCSV("_Simulations",
"ID,Name" + Environment.NewLine +
" 1,Sim1" + Environment.NewLine
Expand Down
Loading