Fix returned status codes depending on scenario, provide informative logs

Extend logic of encpoints to provide more informative status codes on different scenarios (not found, bad request, etc). Update logging to write structured and informative logs
This commit is contained in:
dsokolovrudakov
2025-02-20 00:05:33 +02:00
parent 21a6a22a9e
commit 9329d76ab6
8 changed files with 89 additions and 79 deletions

View File

@@ -32,17 +32,21 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
[HttpGet("hive/{hiveId}")]
public async Task<IActionResult> GetHive(string hiveId)
{
var hiveModel = await _communicationControlService.GetHiveModel(hiveId);
var hiveExists = await _communicationControlService.IsHiveConnected(hiveId);
if (!hiveExists)
{
_logger.LogWarning("Failed to get Hive for HiveID: {hiveId}", hiveId);
return NotFound($"Hive with HiveID: {hiveId} is not found");
}
var hiveModel = await _communicationControlService.GetHiveModel(hiveId);
return Ok(hiveModel);
}
[HttpGet("hive")]
public async Task<IActionResult> GetHives()
{
var hives = await _communicationControlService.GetAllHives();
return Ok(hives);
}
@@ -59,6 +63,7 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
if (request?.Hives == null || !request.Hives.Any())
return BadRequest("No hive IDs provided.");
_logger.LogInformation("Hive moving request accepted by enpdoint. Request: {@request}", request);
foreach (var id in request.Hives)
{
_ = Task.Run(async () =>
@@ -69,7 +74,7 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
}
catch (Exception ex)
{
_logger.LogError(ex, $"Failed to send control signal for HiveID: {id}");
_logger.LogError(ex, "Failed to send control signal for HiveID: {id} \n Request: {@request}", id, request);
}
});
}

View File

@@ -11,10 +11,12 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
public class HiveController : Controller
{
private readonly ICommunicationControlService _communicationControlService;
private readonly ILogger<HiveController> _logger;
public HiveController(ICommunicationControlService communicationControlService)
public HiveController(ICommunicationControlService communicationControlService, ILogger<HiveController> logger)
{
_communicationControlService = communicationControlService;
_logger = logger;
}
[HttpPost("connect")]
@@ -28,6 +30,13 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
HiveSchema = request.HiveSchema
};
bool isConnected = await _communicationControlService.IsHiveConnected(request.HiveID);
if (isConnected)
{
_logger.LogError("Hive with HiveID: {hiveId} already connected. Request: {@request}", request.HiveID, request);
return BadRequest($"Hive with HiveID: {request.HiveID} already connected");
}
var hiveOperationalArea = await _communicationControlService.ConnectHive(hiveModel);
var connectResponse = new HiveConnectResponse
{
@@ -36,6 +45,7 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
};
return Ok(connectResponse);
}
[HttpPost("telemetry")]
@@ -51,13 +61,22 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
Timestamp = DateTime.Now
};
var telemetryUpdateTimestamp = await _communicationControlService.AddTelemetry(hiveTelemetryModel);
var telemetryResponse = new HiveTelemetryResponse
bool isHiveConnected = await _communicationControlService.IsHiveConnected(request.HiveID);
if (isHiveConnected)
{
Timestamp = telemetryUpdateTimestamp
};
var telemetryUpdateTimestamp = await _communicationControlService.AddTelemetry(hiveTelemetryModel);
var telemetryResponse = new HiveTelemetryResponse
{
Timestamp = telemetryUpdateTimestamp
};
return Ok(telemetryResponse);
return Ok(telemetryResponse);
}
else
{
_logger.LogWarning("Failed to write telemetry. Hive with HiveID: {hiveId} is not connected. Request: {@request}", request.HiveID, request);
return NotFound($"Failed to write telemetry. Hive with HiveID: {request.HiveID} is not connected");
}
}
}

View File

@@ -12,7 +12,5 @@ namespace DevOpsProject.CommunicationControl.API.DI
return serviceCollection;
}
}
}