From 39ee3263a716e17d02b867570b49f2dd117f9b21 Mon Sep 17 00:00:00 2001 From: Pepijn van Oort Date: Fri, 16 Feb 2024 14:52:48 +0100 Subject: [PATCH] bit of code simplification --- FarmmapsKPI/KPIApplication.cs | 233 ++++++++++++++++++---------------- 1 file changed, 122 insertions(+), 111 deletions(-) diff --git a/FarmmapsKPI/KPIApplication.cs b/FarmmapsKPI/KPIApplication.cs index 92be0ab..312c002 100644 --- a/FarmmapsKPI/KPIApplication.cs +++ b/FarmmapsKPI/KPIApplication.cs @@ -396,42 +396,46 @@ namespace FarmmapsKPI { if (kpio.id != kpioPrevious.id) { + KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values; if (kpio.id == "B1") { //Make a new dataList = new line to be written - dataList = new List { }; //Fill the datalist with this kpi - dataList.Add(kpio.parentName); - dataList.Add(cropfieldItem.Code); - dataList.Add(kpio.data.area); - dataList.Add(kpio.data.cropTypeCode); - dataList.Add(kpio.data.cropTypeName); - dataList.Add(kpio.id); - dataList.Add(kpio.quantity); // in KPI output quantity is what we call KPIvariable in headerlist of csv file - dataList.Add(kpio.value); - dataList.Add(kpio.unit); - dataList.Add(kpio.targetValue); - dataList.Add(kpio.thresholdValue); + dataList = new List + { + kpio.parentName, + cropfieldItem.Code, + kpio.data.area, + kpio.data.cropTypeCode, + kpio.data.cropTypeName, + kpio.id, + kpio.quantity, // in KPI output quantity is what we call KPIvariable in headerlist of csv file + kpio.value, + kpio.unit, + kpio.targetValue, + kpio.thresholdValue + }; //Write the datalist to a line to the streamwrieter sw for the output csv file sw.WriteLine(string.Join(",", dataList)); - KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values; foreach (string elementName in kpio.B1elements) { // get B1element from the element called values - dataList = new List { }; string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); - dataList.Add(kpio.parentName); - dataList.Add(cropfieldItem.Code); - dataList.Add(kpio.data.area); - dataList.Add(kpio.data.cropTypeCode); - dataList.Add(kpio.data.cropTypeName); - dataList.Add(kpio.id); - dataList.Add(elementName); // specific output variable name for B1 element - dataList.Add(elementValue); // specific output value name for B1 element - dataList.Add(kpio.unit); - dataList.Add(""); - dataList.Add(""); + dataList = new List + { + kpio.parentName, + cropfieldItem.Code, + kpio.data.area, + kpio.data.cropTypeCode, + kpio.data.cropTypeName, + kpio.id, + elementName, // specific output variable name for B1 element + elementValue, // specific output value name for B1 element + kpio.unit, + "", + "" + }; //Write the datalist to a line to the streamwrieter sw for the output csv file sw.WriteLine(string.Join(",", dataList)); } @@ -439,39 +443,42 @@ namespace FarmmapsKPI else if (kpio.id == "B2") { //Make a new dataList = new line to be written - dataList = new List { }; - //Fill the datalist with this kpi - dataList.Add(kpio.parentName); - dataList.Add(cropfieldItem.Code); - dataList.Add(kpio.data.area); - dataList.Add(kpio.data.cropTypeCode); - dataList.Add(kpio.data.cropTypeName); - dataList.Add(kpio.id); - dataList.Add(kpio.quantity); // in KPI output quantity is what we call KPIvariable in headerlist of csv file - dataList.Add(kpio.value); - dataList.Add(kpio.unit); - dataList.Add(kpio.targetValue); - dataList.Add(kpio.thresholdValue); + dataList = new List + { + //Fill the datalist with this kpi + kpio.parentName, + cropfieldItem.Code, + kpio.data.area, + kpio.data.cropTypeCode, + kpio.data.cropTypeName, + kpio.id, + kpio.quantity, // in KPI output quantity is what we call KPIvariable in headerlist of csv file + kpio.value, + kpio.unit, + kpio.targetValue, + kpio.thresholdValue + }; //Write the datalist to a line to the streamwrieter sw for the output csv file sw.WriteLine(string.Join(",", dataList)); - KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values; foreach (string elementName in kpio.B2elements) { // get B1element from the element called values - dataList = new List { }; string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); - dataList.Add(kpio.parentName); - dataList.Add(cropfieldItem.Code); - dataList.Add(kpio.data.area); - dataList.Add(kpio.data.cropTypeCode); - dataList.Add(kpio.data.cropTypeName); - dataList.Add(kpio.id); - dataList.Add(elementName); // specific output variable name for B2 element - dataList.Add(elementValue); // specific output value name for B2 element - dataList.Add(kpio.unit); - dataList.Add(""); - dataList.Add(""); + dataList = new List + { + kpio.parentName, + cropfieldItem.Code, + kpio.data.area, + kpio.data.cropTypeCode, + kpio.data.cropTypeName, + kpio.id, + elementName, // specific output variable name for B2 element + elementValue, // specific output value name for B2 element + kpio.unit, + "", + "" + }; //Write the datalist to a line to the streamwrieter sw for the output csv file sw.WriteLine(string.Join(",", dataList)); } @@ -480,39 +487,41 @@ namespace FarmmapsKPI else if (kpio.id == "C1") //TtD { //Make a new dataList = new line to be written - dataList = new List { }; - //Fill the datalist with this kpi - dataList.Add(kpio.parentName); - dataList.Add(cropfieldItem.Code); - dataList.Add(kpio.data.area); - dataList.Add(kpio.data.cropTypeCode); - dataList.Add(kpio.data.cropTypeName); - dataList.Add(kpio.id); - dataList.Add(kpio.quantity); // in KPI output quantity is what we call KPIvariable in headerlist of csv file - dataList.Add(kpio.value); - dataList.Add(kpio.unit); - dataList.Add(kpio.targetValue); - dataList.Add(kpio.thresholdValue); + dataList = new List + { + kpio.parentName, + cropfieldItem.Code, + kpio.data.area, + kpio.data.cropTypeCode, + kpio.data.cropTypeName, + kpio.id, + kpio.quantity, // in KPI output quantity is what we call KPIvariable in headerlist of csv file + kpio.value, + kpio.unit, + kpio.targetValue, + kpio.thresholdValue + }; //Write the datalist to a line to the streamwrieter sw for the output csv file sw.WriteLine(string.Join(",", dataList)); - KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values; foreach (string elementName in kpio.C1elements) { // get C1element from the element called values - dataList = new List { }; string elementValue = (string)kPIelementsOfBalance.GetType().GetProperty(elementName).GetValue(kPIelementsOfBalance, null); - dataList.Add(kpio.parentName); - dataList.Add(cropfieldItem.Code); - dataList.Add(kpio.data.area); - dataList.Add(kpio.data.cropTypeCode); - dataList.Add(kpio.data.cropTypeName); - dataList.Add(kpio.id); - dataList.Add(elementName); // specific output variable name for C1 element - dataList.Add(elementValue); // specific output value name for C1 element - dataList.Add(kpio.unit); - dataList.Add(""); - dataList.Add(""); + dataList = new List + { + kpio.parentName, + cropfieldItem.Code, + kpio.data.area, + kpio.data.cropTypeCode, + kpio.data.cropTypeName, + kpio.id, + elementName, // specific output variable name for C1 element + elementValue, // specific output value name for C1 element + kpio.unit, + "", + "" + }; //Write the datalist to a line to the streamwrieter sw for the output csv file sw.WriteLine(string.Join(",", dataList)); } @@ -527,26 +536,27 @@ namespace FarmmapsKPI { if (mbp_KPIvariable == "aquaticLife") { mbp_KPIvalue = e.aquaticLife; } else if (mbp_KPIvariable == "groundWater") { mbp_KPIvalue = e.groundWater; } else if (mbp_KPIvariable == "soilLife") { mbp_KPIvalue = e.soilLife; } else { mbp_KPIvalue = ""; } //Make a new dataList = new line to be written - dataList = new List { }; - //Fill the datalist with this kpi - dataList.Add(kpio.parentName); - dataList.Add(cropfieldItem.Code); - dataList.Add(kpio.data.area); - dataList.Add(kpio.data.cropTypeCode); - dataList.Add(kpio.data.cropTypeName); - dataList.Add(kpio.id); //"E1" - dataList.Add(kpio.quantity); // "mbp" - dataList.Add(""); // not here the value - dataList.Add(""); // not here KPIunit for this indicator - dataList.Add(""); // not here KPItargetvalue for this indicator - dataList.Add(""); // not here KPIthresholdValue for this indicator - dataList.Add(e.productCode); - dataList.Add(e.productName); - dataList.Add(e.quantity); - dataList.Add(e.unitCode); - dataList.Add(e.date); - dataList.Add(mbp_KPIvariable); - dataList.Add(mbp_KPIvalue); + dataList = new List + { + kpio.parentName, + cropfieldItem.Code, + kpio.data.area, + kpio.data.cropTypeCode, + kpio.data.cropTypeName, + kpio.id, //"E1" + kpio.quantity, // "mbp" + "", // not here the value + "", // not here KPIunit for this indicator + "", // not here KPItargetvalue for this indicator + "", // not here KPIthresholdValue for this indicator + e.productCode, + e.productName, + e.quantity, + e.unitCode, + e.date, + mbp_KPIvariable, + mbp_KPIvalue + }; //Write the datalist to a line to the streamwrieter sw for the output csv file sw.WriteLine(string.Join(",", dataList)); } @@ -554,20 +564,21 @@ namespace FarmmapsKPI } else { - //Make a new dataList = new line to be written - dataList = new List { }; - //Fill the datalist with this kpi - dataList.Add(kpio.parentName); - dataList.Add(cropfieldItem.Code); - dataList.Add(kpio.data.area); - dataList.Add(kpio.data.cropTypeCode); - dataList.Add(kpio.data.cropTypeName); - dataList.Add(kpio.id); - dataList.Add(kpio.quantity); // in KPI output quantity is what we call KPIvariable in headerlist of csv file - dataList.Add(kpio.value); - dataList.Add(kpio.unit); - dataList.Add(kpio.targetValue); - dataList.Add(kpio.thresholdValue); + //Any other KPI, example A1 or D1, with just 1 record to write + dataList = new List + { + kpio.parentName, + cropfieldItem.Code, + kpio.data.area, + kpio.data.cropTypeCode, + kpio.data.cropTypeName, + kpio.id, + kpio.quantity, + kpio.value, + kpio.unit, + kpio.targetValue, + kpio.thresholdValue + }; //Write the datalist to a line to the streamwrieter sw for the output csv file sw.WriteLine(string.Join(",", dataList)); }