From 3e0683420aee5f6010f9f864c1a99c4ec6b55b88 Mon Sep 17 00:00:00 2001 From: Eddasol Date: Tue, 3 Dec 2024 10:19:41 +0100 Subject: [PATCH] Allow missions where most tags don't have area --- .../api/Controllers/MissionSchedulingController.cs | 11 +++++++++-- backend/api/Services/EchoService.cs | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/backend/api/Controllers/MissionSchedulingController.cs b/backend/api/Controllers/MissionSchedulingController.cs index ffdd27ae..b5007e17 100644 --- a/backend/api/Controllers/MissionSchedulingController.cs +++ b/backend/api/Controllers/MissionSchedulingController.cs @@ -252,11 +252,18 @@ [FromBody] ScheduledMissionQuery scheduledMissionQuery logger.LogWarning($"Mission {missionDefinition.Name} has tags on more than one deck. The decks are: {joinedMissionDeckNames}."); } - Area? area = null; - area = missionAreas.GroupBy(i => i).OrderByDescending(grp => grp.Count()).Select(grp => grp.Key).First(); + var sortedAreas = missionAreas.GroupBy(i => i).OrderByDescending(grp => grp.Count()).Select(grp => grp.Key); + var area = sortedAreas.First(); + if (area == null && sortedAreas.Count() > 1) + { + logger.LogWarning($"Most common area in mission {missionDefinition.Name} is null. Will use second most common area."); + area = sortedAreas.Skip(1).First(); + + } if (area == null) { + logger.LogError($"Mission {missionDefinition.Name} doesn't have any tags with valid area."); return NotFound($"No area found for mission '{missionDefinition.Name}'."); } diff --git a/backend/api/Services/EchoService.cs b/backend/api/Services/EchoService.cs index 559196c6..65ddb308 100644 --- a/backend/api/Services/EchoService.cs +++ b/backend/api/Services/EchoService.cs @@ -119,11 +119,18 @@ public async Task> GetTasksForMission(string missionSourceId) logger.LogWarning($"Mission {echoMission.Name} has tags on more than one deck. The decks are: {joinedMissionDeckNames}."); } - Area? area = null; - area = missionAreas.GroupBy(i => i).OrderByDescending(grp => grp.Count()).Select(grp => grp.Key).First(); + var sortedAreas = missionAreas.GroupBy(i => i).OrderByDescending(grp => grp.Count()).Select(grp => grp.Key); + var area = sortedAreas.First(); + if (area == null && sortedAreas.Count() > 1) + { + logger.LogWarning($"Most common area in mission {echoMission.Name} is null. Will use second most common area."); + area = sortedAreas.Skip(1).First(); + + } if (area == null) { + logger.LogError($"Mission {echoMission.Name} doesn't have any tags with valid area."); return null; }