[AZ-371] Refactor C18: magic numbers to ProcessingConfig/MapConfig

Promotes 8 operational levers into config keys with defaults that match
the prior source literals byte-for-byte:
  ProcessingConfig: RegionProcessingTimeoutSeconds (300),
  RouteProcessingPollIntervalSeconds (5),
  MaxRoutePointSpacingMeters (200), LatLonTolerance (0.0001).
  MapConfig: TileSizePixels (256), AllowedZoomLevels ([15..19]),
  RetryBaseDelaySeconds (1), RetryMaxDelaySeconds (30).

Sites updated: RegionService, RouteProcessingService,
RoutePointGraphBuilder, RouteValidator, RouteService 4-arg ctor,
RouteImageRenderer, GoogleMapsDownloaderV2, TileService. Closes LF-2 by
forwarding HttpContext.RequestAborted from GetTileByLatLon into the
downloader. appsettings.json gains the 8 new keys at default values.

Tests: 141 / 141 unit + 5 / 5 smoke green. New ConfigDefaultsTests pins
defaults to original literals; new TileService unit test asserts CT
identity from caller to downloader (AZ-371 AC-3).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-05-11 03:30:07 +03:00
parent ee42b1716b
commit 1dcd089d39
21 changed files with 404 additions and 68 deletions
@@ -18,15 +18,16 @@ namespace SatelliteProvider.Services.RouteManagement;
// AC-2 (pixel-for-pixel identical output for existing scenarios).
public class RouteImageRenderer
{
private const int TileSizePixels = 256;
private readonly StorageConfig _storageConfig;
private readonly int _tileSizePixels;
private readonly ILogger<RouteImageRenderer> _logger;
public RouteImageRenderer(IOptions<StorageConfig> storageConfig, ILogger<RouteImageRenderer> logger)
public RouteImageRenderer(IOptions<StorageConfig> storageConfig, IOptions<MapConfig> mapConfig, ILogger<RouteImageRenderer> logger)
{
ArgumentNullException.ThrowIfNull(storageConfig);
ArgumentNullException.ThrowIfNull(mapConfig);
_storageConfig = storageConfig.Value;
_tileSizePixels = mapConfig.Value.TileSizePixels;
_logger = logger;
}
@@ -69,7 +70,7 @@ public class RouteImageRenderer
var stitcher = new TileGridStitcher();
var result = await stitcher.StitchAsync(
placements,
TileSizePixels,
_tileSizePixels,
deduplicateByTileCoords: true,
swallowTileLoadErrors: true,
cancellationToken);
@@ -97,10 +98,10 @@ public class RouteImageRenderer
foreach (var (geoMinX, geoMinY, geoMaxX, geoMaxY) in geofencePolygonBounds)
{
var x1 = (geoMinX - minX) * TileSizePixels;
var y1 = (geoMinY - minY + 1) * TileSizePixels;
var x2 = (geoMaxX - minX + 2) * TileSizePixels - 1;
var y2 = (geoMaxY - minY + 1) * TileSizePixels - 1;
var x1 = (geoMinX - minX) * _tileSizePixels;
var y1 = (geoMinY - minY + 1) * _tileSizePixels;
var x2 = (geoMaxX - minX + 2) * _tileSizePixels - 1;
var y2 = (geoMaxY - minY + 1) * _tileSizePixels - 1;
x1 = Math.Max(0, Math.Min(x1, imageWidth - 1));
y1 = Math.Max(0, Math.Min(y1, imageHeight - 1));
@@ -121,8 +122,8 @@ public class RouteImageRenderer
var geoPoint = new GeoPoint(point.Latitude, point.Longitude);
var (tileX, tileY) = GeoUtils.WorldToTilePos(geoPoint, zoomLevel);
var pixelX = (tileX - minX) * TileSizePixels + TileSizePixels / 2;
var pixelY = (tileY - minY) * TileSizePixels + TileSizePixels / 2;
var pixelX = (tileX - minX) * _tileSizePixels + _tileSizePixels / 2;
var pixelY = (tileY - minY) * _tileSizePixels + _tileSizePixels / 2;
if (pixelX >= 0 && pixelX < imageWidth && pixelY >= 0 && pixelY < imageHeight)
{
@@ -1,3 +1,5 @@
using Microsoft.Extensions.Options;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.DTO;
using SatelliteProvider.Common.Utils;
@@ -5,7 +7,13 @@ namespace SatelliteProvider.Services.RouteManagement;
public class RoutePointGraphBuilder
{
public const double MaxPointSpacingMeters = 200.0;
private readonly double _maxPointSpacingMeters;
public RoutePointGraphBuilder(IOptions<ProcessingConfig> processingConfig)
{
ArgumentNullException.ThrowIfNull(processingConfig);
_maxPointSpacingMeters = processingConfig.Value.MaxRoutePointSpacingMeters;
}
public RoutePointGraph Build(IReadOnlyList<RoutePoint> userPoints)
{
@@ -53,7 +61,7 @@ public class RoutePointGraphBuilder
var next = userPoints[segmentIndex + 1];
var nextGeo = new GeoPoint(next.Latitude, next.Longitude);
var intermediates = GeoUtils.CalculateIntermediatePoints(currentGeo, nextGeo, MaxPointSpacingMeters);
var intermediates = GeoUtils.CalculateIntermediatePoints(currentGeo, nextGeo, _maxPointSpacingMeters);
foreach (var intermediateGeo in intermediates)
{
@@ -1,5 +1,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.DTO;
using SatelliteProvider.Common.Interfaces;
using SatelliteProvider.Common.Utils;
@@ -26,7 +28,7 @@ public class RouteProcessingService : BackgroundService
private readonly RegionFileCleaner _regionFileCleaner;
private readonly RouteRegionMatcher _routeRegionMatcher;
private readonly ILogger<RouteProcessingService> _logger;
private readonly TimeSpan _checkInterval = TimeSpan.FromSeconds(5);
private readonly TimeSpan _checkInterval;
public RouteProcessingService(
IRouteRepository routeRepository,
@@ -37,6 +39,7 @@ public class RouteProcessingService : BackgroundService
RouteImageRenderer routeImageRenderer,
TilesZipBuilder tilesZipBuilder,
RegionFileCleaner regionFileCleaner,
IOptions<ProcessingConfig> processingConfig,
ILogger<RouteProcessingService> logger)
{
_routeRepository = routeRepository;
@@ -48,6 +51,7 @@ public class RouteProcessingService : BackgroundService
_tilesZipBuilder = tilesZipBuilder;
_regionFileCleaner = regionFileCleaner;
_routeRegionMatcher = new RouteRegionMatcher();
_checkInterval = TimeSpan.FromSeconds(processingConfig.Value.RouteProcessingPollIntervalSeconds);
_logger = logger;
}
@@ -1,4 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.DTO;
using SatelliteProvider.Common.Interfaces;
using SatelliteProvider.DataAccess.Models;
@@ -19,10 +21,11 @@ public class RouteService : IRouteService
public RouteService(
IRouteRepository routeRepository,
IRegionService regionService,
IOptions<ProcessingConfig> processingConfig,
ILogger<RouteService> logger)
: this(routeRepository, regionService, logger,
new RouteValidator(),
new RoutePointGraphBuilder(),
new RouteValidator(processingConfig),
new RoutePointGraphBuilder(processingConfig),
new GeofenceGridCalculator(),
new RouteResponseMapper())
{
@@ -1,9 +1,19 @@
using Microsoft.Extensions.Options;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.DTO;
namespace SatelliteProvider.Services.RouteManagement;
public class RouteValidator
{
private readonly double _latLonTolerance;
public RouteValidator(IOptions<ProcessingConfig> processingConfig)
{
ArgumentNullException.ThrowIfNull(processingConfig);
_latLonTolerance = processingConfig.Value.LatLonTolerance;
}
public void Validate(CreateRouteRequest request)
{
ArgumentNullException.ThrowIfNull(request);
@@ -39,7 +49,7 @@ public class RouteValidator
}
}
private static void ValidatePolygon(GeofencePolygon polygon, List<string> errors)
private void ValidatePolygon(GeofencePolygon polygon, List<string> errors)
{
if (polygon.NorthWest is null || polygon.SouthEast is null)
{
@@ -50,8 +60,8 @@ public class RouteValidator
var nw = polygon.NorthWest;
var se = polygon.SouthEast;
if ((Math.Abs(nw.Lat) < 0.0001 && Math.Abs(nw.Lon) < 0.0001) ||
(Math.Abs(se.Lat) < 0.0001 && Math.Abs(se.Lon) < 0.0001))
if ((Math.Abs(nw.Lat) < _latLonTolerance && Math.Abs(nw.Lon) < _latLonTolerance) ||
(Math.Abs(se.Lat) < _latLonTolerance && Math.Abs(se.Lon) < _latLonTolerance))
{
errors.Add("Geofence polygon coordinates cannot be (0,0)");
}