removed incorrect duplicate KPIs

This commit is contained in:
ttenden 2024-05-27 13:52:15 +02:00
parent fcf6b2af51
commit 6d6b5b1b7a

View File

@ -89,7 +89,7 @@ namespace FarmmapsKPI
TimeSpan tsTotalEstimated; TimeSpan tsTotalEstimated;
//Per default just 1 run per field. For debugging check if when we run multiple times do we get (should be) always the same output? //Per default just 1 run per field. For debugging check if when we run multiple times do we get (should be) always the same output?
int nrun = 5; int nrun = 1;
for (int run = 1; run <= nrun; run++) { for (int run = 1; run <= nrun; run++) {
for (int i = 0; i < fieldsInputs.Count; i++) for (int i = 0; i < fieldsInputs.Count; i++)
//for (int i = 3; i < 4; i++) // for testing //for (int i = 3; i < 4; i++) // for testing
@ -223,16 +223,16 @@ namespace FarmmapsKPI
// We need that because for operations, you need to provide the area on which the operation was applied // We need that because for operations, you need to provide the area on which the operation was applied
// And if we put that to the crop area, then we neatly get everything on a per ha basis // And if we put that to the crop area, then we neatly get everything on a per ha basis
_logger.LogInformation($"Getting polygon area (ha))"); _logger.LogInformation($"Getting polygon area (ha))");
List<Item> KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3); List<Item> KPIItemsArea = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3);
trycnt = 1; trycnt = 1;
targetKPIitemsCount = 3; // here for the area we need at least 3, but not more than that targetKPIitemsCount = 3; // here for the area we need at least 3, but not more than that
while (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries) while (KPIItemsArea.Count < targetKPIitemsCount & trycnt < maxtries)
{ {
KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem,3); KPIItemsArea = await _generalService.GetKpiItemsForCropField(cropfieldItem,3);
_logger.LogInformation($"Found {KPIItems.Count} KPI items"); _logger.LogInformation($"Found {KPIItemsArea.Count} KPI items");
trycnt++; trycnt++;
} }
kpio = JsonConvert.DeserializeObject<KPIOutput>(KPIItems[0].Data.ToString()); kpio = JsonConvert.DeserializeObject<KPIOutput>(KPIItemsArea[0].Data.ToString());
string area_ha = kpio.data.area; string area_ha = kpio.data.area;
// turn the area into a JObject for later merging with operation data; // turn the area into a JObject for later merging with operation data;
string strJarea = JsonConvert.SerializeObject(new { area = area_ha }); string strJarea = JsonConvert.SerializeObject(new { area = area_ha });
@ -383,10 +383,8 @@ namespace FarmmapsKPI
//throw new Exception(String.Format($"run {run}: totalNferiliserInput != totalNferiliserCropfield?!")); //throw new Exception(String.Format($"run {run}: totalNferiliserInput != totalNferiliserCropfield?!"));
} }
//Now get the KPIs for this cropfield, mounted with operations & cropyield //Now get the KPIs for this cropfield, mounted with operations & cropyield
//Note sometimes the KPIItems.Count is already for some crazy reason greater than or equal to targetKPIitemsCount List<Item> KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3);
//But that would have strange results, since that was from above before adding the crop recordings. We want to do at least one new call -> 'while (trycnt == 0 || '
_logger.LogInformation($"GetKpiItemsForCropField('{cropfieldItem.Code}')"); _logger.LogInformation($"GetKpiItemsForCropField('{cropfieldItem.Code}')");
//Pesticide KPI's D1 and E1 are retreived from API's of CTBG and CLM and may take a bit longer to retrieve //Pesticide KPI's D1 and E1 are retreived from API's of CTBG and CLM and may take a bit longer to retrieve
targetKPIitemsCount = 8; //if we know we should be getting 8 KPI items (A1,B1,B2,C1,D1,E1,F1,F2) targetKPIitemsCount = 8; //if we know we should be getting 8 KPI items (A1,B1,B2,C1,D1,E1,F1,F2)
@ -394,17 +392,16 @@ namespace FarmmapsKPI
bool boolAquaticLife = false; bool boolAquaticLife = false;
_logger.LogInformation($"Firing calls GetKpiItemsForCropField() until we have {targetKPIitemsCount} KPIitems, but don't keep firing forever, stop after {maxtries} calls"); _logger.LogInformation($"Firing calls GetKpiItemsForCropField() until we have {targetKPIitemsCount} KPIitems, but don't keep firing forever, stop after {maxtries} calls");
_logger.LogInformation($"Before we start:"); _logger.LogInformation($"Before we start:");
_logger.LogInformation($"* KPIItems.Count = {KPIItems.Count}"); //_logger.LogInformation($"* KPIItems.Count = {KPIItems.Count}");
_logger.LogInformation($"* trycnt = {trycnt}"); _logger.LogInformation($"* trycnt = {trycnt}");
_logger.LogInformation($"* boolAquaticLife = {boolAquaticLife}"); _logger.LogInformation($"* boolAquaticLife = {boolAquaticLife}");
//additional criterion for while loop: check if it really contains the E1 mbp elements. //additional criterion for while loop: check if it really contains the E1 mbp elements.
//while (trycnt == 0 || ((KPIItems.Count < targetKPIitemsCount || boolAquaticLife == false) & trycnt < maxtries)) //while (trycnt == 0 || ((KPIItems.Count < targetKPIitemsCount || boolAquaticLife == false) & trycnt < maxtries))
//normal while loop
targetKPIitemsCount = 8; //if we know we should be getting 8 KPI items (A1,B1,B2,C1,D1,E1,F1,F2) targetKPIitemsCount = 8; //if we know we should be getting 8 KPI items (A1,B1,B2,C1,D1,E1,F1,F2)
while (trycnt == 0 || (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries)) while (trycnt == 0 || (KPIItems.Count < targetKPIitemsCount & trycnt < maxtries))
{ {
_logger.LogInformation($"Call nr {trycnt + 1}"); _logger.LogInformation($"Call nr {trycnt + 1}");
KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 3); //number after comma is how many seconds per try KPIItems = await _generalService.GetKpiItemsForCropField(cropfieldItem, 30); //number after comma is how many seconds per try, should be between 30 seconds and 5 minutes
_logger.LogInformation($"Found {KPIItems.Count} KPI items"); _logger.LogInformation($"Found {KPIItems.Count} KPI items");
//boolAquaticLife = GetBoolAquaticLife(KPIItems); //boolAquaticLife = GetBoolAquaticLife(KPIItems);
trycnt ++; trycnt ++;
@ -425,14 +422,56 @@ namespace FarmmapsKPI
_logger.LogInformation($"Downloaded file {KPIItemPathJson}"); _logger.LogInformation($"Downloaded file {KPIItemPathJson}");
_logger.LogInformation($""); _logger.LogInformation($"");
//Remove duplicate KPI items
List<Item> KPIItemsClean = new List<Item>();
Item itemi;
string id, idNext;
double value, valueNext;
value = 0;
id = "";
for (int i = 0; i < KPIItems.Count; i++)
{
itemi = KPIItems[i];
idNext = JsonConvert.DeserializeObject<KPIOutput>(itemi.Data.ToString()).id;
valueNext = Convert.ToDouble(JsonConvert.DeserializeObject<KPIOutput>(itemi.Data.ToString()).value); //hoe doe ik dit voor een getal?
if (idNext != null)
{
if (id != idNext)
{
KPIItemsClean.Add(itemi);
}
else
{
if (valueNext >= value)
{
//Remove the previous element from this list with same id but wrong value (valueNext > value) or duplicate value (valueNext == value). Presumes list is always sorted by kpiid, e.g. we may havce "
KPIItemsClean.RemoveAt(i - 1);
KPIItemsClean.Add(itemi);
}
else
{
//Previous element was correct and already added so do nothing here
}
}
id = idNext;
value = valueNext;
}
}
////Order again from A zo Z.
//List<Item> KPIItemsCleanSorted = new List<Item>();
//for (int i = KPIItemsClean.Count - 1; i >= 0; i--)
//{
// KPIItemsCleanSorted.Add(KPIItemsClean[i]);
//}
//Write to the csv file that collects all KPI's for all the crop fields //Write to the csv file that collects all KPI's for all the crop fields
List<string> dataList; List<string> dataList;
foreach (Item item in KPIItems) foreach (Item item in KPIItemsClean)
{ {
kpio = JsonConvert.DeserializeObject<KPIOutput>(item.Data.ToString()); kpio = JsonConvert.DeserializeObject<KPIOutput>(item.Data.ToString());
if (kpio.id != null) if (kpio.id != null)
{
if (kpio.id != kpioPrevious.id)
{ {
KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values; KPIelementsOfBalance kPIelementsOfBalance = kpio.data.values;
if (kpio.id == "A1") //TtD if (kpio.id == "A1") //TtD
@ -684,11 +723,8 @@ namespace FarmmapsKPI
//Write the datalist to a line to the streamwrieter sw for the output csv file //Write the datalist to a line to the streamwrieter sw for the output csv file
sw.WriteLine(string.Join(",", dataList)); sw.WriteLine(string.Join(",", dataList));
} }
} }
} }
kpioPrevious = kpio;
}
////Total N applied from input ////Total N applied from input
//double totalNapplied = 0.0; //double totalNapplied = 0.0;