This commit is contained in:
Anton Martynenko
2025-11-19 18:27:35 +01:00
parent 48ebad0609
commit 9048a7b3ec
6 changed files with 56 additions and 54 deletions
@@ -30,13 +30,6 @@ public class RegionRepository : IRegionRepository
WHERE id = @Id"; WHERE id = @Id";
var region = await connection.QuerySingleOrDefaultAsync<RegionEntity>(sql, new { Id = id }); var region = await connection.QuerySingleOrDefaultAsync<RegionEntity>(sql, new { Id = id });
if (region != null)
{
_logger.LogInformation("RegionRepository - Read region {RegionId} from DB: Lat={Lat:F12}, Lon={Lon:F12}, Status={Status}",
id, region.Latitude, region.Longitude, region.Status);
}
return region; return region;
} }
@@ -71,9 +64,6 @@ public class RegionRepository : IRegionRepository
@CreatedAt, @UpdatedAt) @CreatedAt, @UpdatedAt)
RETURNING id"; RETURNING id";
_logger.LogInformation("RegionRepository - Inserting region {RegionId} to DB: Lat={Lat:F12}, Lon={Lon:F12}, Status={Status}",
region.Id, region.Latitude, region.Longitude, region.Status);
return await connection.ExecuteScalarAsync<Guid>(sql, region); return await connection.ExecuteScalarAsync<Guid>(sql, region);
} }
@@ -44,14 +44,6 @@ public class RouteRepository : IRouteRepository
ORDER BY sequence_number"; ORDER BY sequence_number";
var points = (await connection.QueryAsync<RoutePointEntity>(sql, new { RouteId = routeId })).ToList(); var points = (await connection.QueryAsync<RoutePointEntity>(sql, new { RouteId = routeId })).ToList();
if (points.Any())
{
_logger.LogInformation("RouteRepository - Read {Count} points from DB for route {RouteId}. First: Lat={Lat:F12}, Lon={Lon:F12}, Last: Lat={LastLat:F12}, Lon={LastLon:F12}",
points.Count, routeId, points[0].Latitude, points[0].Longitude,
points[^1].Latitude, points[^1].Longitude);
}
return points; return points;
} }
@@ -82,13 +74,6 @@ public class RouteRepository : IRouteRepository
@PointType, @SegmentIndex, @DistanceFromPrevious, @CreatedAt)"; @PointType, @SegmentIndex, @DistanceFromPrevious, @CreatedAt)";
var pointsList = points.ToList(); var pointsList = points.ToList();
if (pointsList.Any())
{
_logger.LogInformation("RouteRepository - Inserting {Count} points to DB. First: Lat={Lat:F12}, Lon={Lon:F12}, Last: Lat={LastLat:F12}, Lon={LastLon:F12}",
pointsList.Count, pointsList[0].Latitude, pointsList[0].Longitude,
pointsList[^1].Latitude, pointsList[^1].Longitude);
}
await connection.ExecuteAsync(sql, pointsList); await connection.ExecuteAsync(sql, pointsList);
} }
@@ -28,13 +28,6 @@ public class TileRepository : ITileRepository
WHERE id = @Id"; WHERE id = @Id";
var tile = await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new { Id = id }); var tile = await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new { Id = id });
if (tile != null)
{
_logger.LogInformation("TileRepository - Read tile {TileId} from DB: Lat={Lat:F12}, Lon={Lon:F12}, Zoom={Zoom}",
id, tile.Latitude, tile.Longitude, tile.ZoomLevel);
}
return tile; return tile;
} }
@@ -116,9 +109,6 @@ public class TileRepository : ITileRepository
updated_at = EXCLUDED.updated_at updated_at = EXCLUDED.updated_at
RETURNING id"; RETURNING id";
_logger.LogInformation("TileRepository - Inserting tile {TileId} to DB: Lat={Lat:F12}, Lon={Lon:F12}, Zoom={Zoom}",
tile.Id, tile.Latitude, tile.Longitude, tile.ZoomLevel);
return await connection.ExecuteScalarAsync<Guid>(sql, tile); return await connection.ExecuteScalarAsync<Guid>(sql, tile);
} }
@@ -24,21 +24,21 @@ class Program
Console.WriteLine("✓ API is ready"); Console.WriteLine("✓ API is ready");
Console.WriteLine(); Console.WriteLine();
// await TileTests.RunGetTileByLatLonTest(httpClient); await TileTests.RunGetTileByLatLonTest(httpClient);
// await RegionTests.RunRegionProcessingTest_200m_Zoom18(httpClient); await RegionTests.RunRegionProcessingTest_200m_Zoom18(httpClient);
// await RegionTests.RunRegionProcessingTest_400m_Zoom17(httpClient); await RegionTests.RunRegionProcessingTest_400m_Zoom17(httpClient);
// await RegionTests.RunRegionProcessingTest_500m_Zoom18(httpClient); await RegionTests.RunRegionProcessingTest_500m_Zoom18(httpClient);
// await RouteTests.RunSimpleRouteTest(httpClient); await RouteTests.RunSimpleRouteTest(httpClient);
// await RouteTests.RunRouteWithRegionProcessingAndStitching(httpClient); await RouteTests.RunRouteWithRegionProcessingAndStitching(httpClient);
await RouteTests.RunComplexRouteWithStitching(httpClient); await RouteTests.RunComplexRouteWithStitching(httpClient);
await RouteTests.RunComplexRouteWithStitchingAndGeofences(httpClient); await RouteTests.RunComplexRouteWithStitchingAndGeofences(httpClient);
// await RouteTests.RunExtendedRouteEast(httpClient); await RouteTests.RunExtendedRouteEast(httpClient);
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("========================="); Console.WriteLine("=========================");
@@ -47,13 +47,9 @@ public class RegionProcessingService : BackgroundService
if (workerId > 1) if (workerId > 1)
{ {
var startupDelay = Random.Shared.Next(100, 500); var startupDelay = Random.Shared.Next(100, 500);
_logger.LogInformation("Region worker {WorkerId} starting with {Delay}ms delay to reduce contention",
workerId, startupDelay);
await Task.Delay(startupDelay, stoppingToken); await Task.Delay(startupDelay, stoppingToken);
} }
_logger.LogInformation("Region worker {WorkerId} started", workerId);
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
try try
@@ -62,13 +58,7 @@ public class RegionProcessingService : BackgroundService
if (request != null) if (request != null)
{ {
_logger.LogInformation("Worker {WorkerId}: Dequeued region request {RegionId}",
workerId, request.Id);
await _regionService.ProcessRegionAsync(request.Id, stoppingToken); await _regionService.ProcessRegionAsync(request.Id, stoppingToken);
_logger.LogInformation("Worker {WorkerId}: Completed region {RegionId}",
workerId, request.Id);
} }
} }
catch (OperationCanceledException) catch (OperationCanceledException)
@@ -80,8 +70,6 @@ public class RegionProcessingService : BackgroundService
_logger.LogError(ex, "Worker {WorkerId}: Error processing region request", workerId); _logger.LogError(ex, "Worker {WorkerId}: Error processing region request", workerId);
} }
} }
_logger.LogInformation("Region worker {WorkerId} stopped", workerId);
} }
} }
@@ -318,6 +318,8 @@ public class RouteProcessingService : BackgroundService
await _routeRepository.UpdateRouteAsync(route); await _routeRepository.UpdateRouteAsync(route);
await CleanupRegionFilesAsync(regionIds, cancellationToken);
_logger.LogInformation("Route {RouteId} maps processing completed successfully", routeId); _logger.LogInformation("Route {RouteId} maps processing completed successfully", routeId);
} }
catch (Exception ex) catch (Exception ex)
@@ -327,6 +329,53 @@ public class RouteProcessingService : BackgroundService
} }
} }
private async Task CleanupRegionFilesAsync(IEnumerable<Guid> regionIds, CancellationToken cancellationToken)
{
foreach (var regionId in regionIds)
{
var region = await _regionRepository.GetByIdAsync(regionId);
if (region == null) continue;
if (!string.IsNullOrEmpty(region.CsvFilePath) && File.Exists(region.CsvFilePath))
{
try
{
File.Delete(region.CsvFilePath);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to delete region CSV file: {FilePath}", region.CsvFilePath);
}
}
if (!string.IsNullOrEmpty(region.SummaryFilePath) && File.Exists(region.SummaryFilePath))
{
try
{
File.Delete(region.SummaryFilePath);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to delete region summary file: {FilePath}", region.SummaryFilePath);
}
}
var readyDir = _storageConfig.ReadyDirectory;
var stitchedImagePath = Path.Combine(readyDir, $"region_{regionId}_stitched.jpg");
if (File.Exists(stitchedImagePath))
{
try
{
File.Delete(stitchedImagePath);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to delete region stitched image: {FilePath}", stitchedImagePath);
}
}
}
}
private async Task GenerateRouteCsvAsync( private async Task GenerateRouteCsvAsync(
string filePath, string filePath,
IEnumerable<TileInfo> tiles, IEnumerable<TileInfo> tiles,