From 9d691dfed11946a7a4210009d4acc777b550a670 Mon Sep 17 00:00:00 2001
From: Pepijn van Oort <pepijn.vanoort@wur.nl>
Date: Wed, 22 Nov 2023 16:16:59 +0100
Subject: [PATCH] read & write KPI "E1" = milieu belastingspunten ("mbp")

---
 FarmmapsKPI/KPIApplication.cs   | 103 +++++++++++++++++++++++---------
 FarmmapsKPI/Models/KPIoutput.cs |  13 ++++
 2 files changed, 89 insertions(+), 27 deletions(-)

diff --git a/FarmmapsKPI/KPIApplication.cs b/FarmmapsKPI/KPIApplication.cs
index dc3299d..61960ed 100644
--- a/FarmmapsKPI/KPIApplication.cs
+++ b/FarmmapsKPI/KPIApplication.cs
@@ -71,7 +71,8 @@ namespace FarmmapsKPI
             StreamWriter sw;
             string KPIItemCsv = Path.GetFileNameWithoutExtension(fnKPIinput) + "_Items.csv";
             string KPIItemPathCsv = Path.Combine(downloadFolder, KPIItemCsv);
-            List<string> headerList = new List<string> { "parentName", "area_ha", "cropTypeCode", "cropTypeName", "KPIid", "KPIvariable", "KPIvalue", "KPIunit", "KPItargetvalue", "KPIthresholdValue" };
+            List<string> headerList = new List<string> { "parentName", "area_ha", "cropTypeCode", "cropTypeName", "KPIid", "KPIvariable", "KPIvalue", "KPIunit", "KPItargetvalue", "KPIthresholdValue",
+                "mbp_productCode","mbp_productName","mbp_quantity","mbp_unitCode","mbp_date","mbp_KPIvariable","mbp_KPIvalue"};
             //Create a new csv file. Means if existing then overwritten !!!
             sw = new StreamWriter(KPIItemPathCsv);
             sw.WriteLine($"FarmmapsKPI backend calculations on input file '{fnKPIinput}' downloaded on {DateTime.Now} with the FarmmapsKPI application in the FarmmapsApSamples.sln");
@@ -86,8 +87,8 @@ namespace FarmmapsKPI
 
             for (int i = 0; i < fieldsInputs.Count; i++)
             //for (int i = 7; i < 8; i++)
-                {
-                    watch.Restart();
+            {
+                watch.Restart();
                 input = fieldsInputs[i];
                 _logger.LogInformation(string.Format($"// FarmmapsKPI: Downloading KPI's for field {i + 1} out of {fieldsInputs.Count} to single csv file {KPIItemPathCsv}"));
                 try
@@ -116,6 +117,9 @@ namespace FarmmapsKPI
             List<Item> crprecChildren;
             KPIOutput kpio;
             KPIOutput kpioPrevious = new KPIOutput();   //creates a new empty
+            //If KPI E1 is calculated, write these sub kpi's to output
+            string[] mbp_KPIvariables = new string[] { "aquaticLife", "groundWater", "soilLife" };
+            string mbp_KPIvalue;
 
             string downloadFolder = input.DownloadFolder;
             if (string.IsNullOrEmpty(downloadFolder)) {
@@ -209,18 +213,24 @@ namespace FarmmapsKPI
             string strJarea = JsonConvert.SerializeObject(new { area = area_ha });
             JObject Jarea = JObject.Parse(strJarea);
 
-            //Retreiving BOFEK. A cropfield has 1 soil
-            //Have a look at the cropfieldChildren before and after running this task, see one child (i.e. soil) has been added)
-            cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
-            _logger.LogInformation("Get BOFEK for field");
-            Item bofekItem = await _generalService.RunBofekTask(cropfieldItem);
-            if (bofekItem == null)
+            if (useExistingCropfieldWithChildren == false)
             {
-                _logger.LogError("Something went wrong while obtaining the BOFEK data");
-                return;
+                //Retreiving BOFEK. A cropfield has 1 soil
+                //Have a look at the cropfieldChildren before and after running this task, see one child (i.e. soil) has been added)
+                //cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
+                _logger.LogInformation("Get BOFEK for field");
+                Item bofekItem = await _generalService.RunBofekTask(cropfieldItem);
+                if (bofekItem == null)
+                {
+                    _logger.LogError("Something went wrong while obtaining the BOFEK data");
+                    return;
+                }
+                //cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
+            }
+            else
+            {
+                _logger.LogInformation("For existing cropfield we assume it already has BOFEK soil data");
             }
-            cropfieldChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
-
 
             // A cropfield has 1 crop recording and the crop recording has 0:many operations
             //So first at the crop recording
@@ -331,9 +341,10 @@ namespace FarmmapsKPI
             int targetKPIitemsCount = 8;   //if we know we should be getting 8
             int maxtries = 5;   // but don't keep on trying forever; there is a maximum number of tries
             int trycnt = 0;
+
             _logger.LogInformation($"Firing calls GetKpiItemsForCropField() until we have {targetKPIitemsCount}, but don't keep firing forever, stop after {maxtries} calls");
             while (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries) {
-                KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 30);
+                KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 5);
                 _logger.LogInformation($"Found {KPIItems.Count} KPI items");
                 trycnt ++;
             }
@@ -356,24 +367,62 @@ namespace FarmmapsKPI
             List<string> dataList; 
             foreach (Item item in KPIItems)
             {
-                dataList = new List<string> { };
                 kpio = JsonConvert.DeserializeObject<KPIOutput>(item.Data.ToString());
-                //Seems sometimes duplicate KPI items are returned. So check that here and only write if this kpio is different from previous
                 if (kpio.id != null)
                 {
                     if (kpio.id != kpioPrevious.id)
                     {
-                        dataList.Add(kpio.parentName);
-                        dataList.Add(kpio.data.area);
-                        dataList.Add(kpio.data.cropTypeCode);
-                        dataList.Add(kpio.data.cropTypeName);
-                        dataList.Add(kpio.id);
-                        dataList.Add(kpio.quantity);
-                        dataList.Add(kpio.value);
-                        dataList.Add(kpio.unit);
-                        dataList.Add(kpio.targetValue);
-                        dataList.Add(kpio.thresholdValue);
-                        sw.WriteLine(string.Join(",", dataList));
+                        if (kpio.id != "E1")
+                        {
+                            //Make a new dataList = new line to be written
+                            dataList = new List<string> { }; 
+                            //Fill the datalist with this kpi
+                            dataList.Add(kpio.parentName);
+                            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);
+                            //Write the datalist to a line to the streamwrieter sw for the output csv file
+                            sw.WriteLine(string.Join(",", dataList));
+                        }
+                        else
+                        {
+                            //for E1, environmentMeasureData is an array of elements with 1 per pesticide. Inside each element there are multiple mbp_KPIvariables to be written
+                            foreach (KPIenvironmentMeasureData e in kpio.data.environmentMeasureData)
+                            {
+                                foreach (string mbp_KPIvariable in mbp_KPIvariables)
+                                {
+                                    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<string> { };
+                                    //Fill the datalist with this kpi
+                                    dataList.Add(kpio.parentName);
+                                    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);
+                                    //Write the datalist to a line to the streamwrieter sw for the output csv file
+                                    sw.WriteLine(string.Join(",", dataList));
+                                }
+                            }
+                        }
                     }
                 }
                 kpioPrevious = kpio;
diff --git a/FarmmapsKPI/Models/KPIoutput.cs b/FarmmapsKPI/Models/KPIoutput.cs
index 5cf2acd..2a64b40 100644
--- a/FarmmapsKPI/Models/KPIoutput.cs
+++ b/FarmmapsKPI/Models/KPIoutput.cs
@@ -21,5 +21,18 @@ namespace FarmmapsKPI.Models
         public string area { get; set; }
         public string cropTypeCode { get; set; }
         public string cropTypeName { get; set; }
+        public KPIenvironmentMeasureData[] environmentMeasureData { get; set; }
+
+    }
+    public class KPIenvironmentMeasureData
+    {
+        public string date { get; set; }
+        public string productCode { get; set; }
+        public string productName { get; set; }
+        public string quantity { get; set; }
+        public string unitCode { get; set; }
+        public string soilLife { get; set; }
+        public string aquaticLife { get; set; }
+        public string groundWater { get; set; }
     }
 }
\ No newline at end of file