Files
bom-local-service/Utilities/CacheHelper.cs
T
alex b30d76ddec Refactor configuration system and remove OpenAPI
- Remove OpenAPI/Swagger support and all related code
- Standardize configuration: remove hardcoded defaults, all defaults from appsettings.json
- Configuration values throw exceptions if missing (except Debug:Enabled defaults to false)
- Refactor CacheManagementService: extract ProcessLocationsAsync helper, remove duplicate code
- Combine UpdateStaggerSeconds and LocationStaggerSeconds into single LocationStaggerSeconds config
- Move config reads to constructors for better performance
- Update README and docker-compose.yml to reflect configuration changes
2025-12-23 21:03:21 +10:00

105 lines
4.4 KiB
C#

using BomLocalService.Models;
using Microsoft.Extensions.Configuration;
namespace BomLocalService.Utilities;
/// <summary>
/// Utility methods for cache folder validation and operations.
/// </summary>
public static class CacheHelper
{
/// <summary>
/// Gets the configured frame count for a data type from configuration.
/// </summary>
public static int GetFrameCountForDataType(IConfiguration configuration, CachedDataType dataType)
{
var dataTypeName = dataType.ToString();
var frameCountConfig = configuration.GetValue<int?>($"CachedDataTypes:{dataTypeName}:FrameCount");
if (!frameCountConfig.HasValue)
{
throw new InvalidOperationException($"CachedDataTypes:{dataTypeName}:FrameCount configuration is required. Set it in appsettings.json or via CACHEDDATATYPES__{dataTypeName.ToUpperInvariant()}__FRAMECOUNT environment variable.");
}
return frameCountConfig.Value;
}
/// <summary>
/// Checks if a cache folder has complete data for a specific data type.
/// </summary>
public static bool IsCacheFolderCompleteForDataType(string cacheFolderPath, CachedDataType dataType, IConfiguration configuration)
{
if (string.IsNullOrEmpty(cacheFolderPath) || !Directory.Exists(cacheFolderPath))
{
return false;
}
var dataTypeFolder = FilePathHelper.GetDataTypeFolderPath(cacheFolderPath, dataType);
if (!Directory.Exists(dataTypeFolder))
{
return false;
}
var expectedFrameCount = GetFrameCountForDataType(configuration, dataType);
for (int i = 0; i < expectedFrameCount; i++)
{
var framePath = FilePathHelper.GetFrameFilePath(cacheFolderPath, dataType, i);
if (!File.Exists(framePath))
{
return false;
}
}
// Check for frames.json in data type folder
var framesMetadataPath = FilePathHelper.GetFramesMetadataFilePath(cacheFolderPath, dataType);
if (!File.Exists(framesMetadataPath))
{
return false;
}
return true;
}
/// <summary>
/// Checks if a cache folder is complete (has radar data - for backward compatibility).
/// </summary>
public static bool IsCacheFolderComplete(string cacheFolderPath, IConfiguration configuration)
{
return IsCacheFolderCompleteForDataType(cacheFolderPath, CachedDataType.Radar, configuration);
}
/// <summary>
/// Calculates the estimated cache update duration in seconds.
/// This is used as a fallback when metrics-based estimation is not available yet.
/// Calculates based on frame count and configured wait times.
/// </summary>
public static int GetEstimatedUpdateDurationSeconds(IConfiguration configuration, CachedDataType dataType = CachedDataType.Radar)
{
// Calculate based on actual wait times and frame count
var frameCount = GetFrameCountForDataType(configuration, dataType);
var tileRenderWaitMsConfig = configuration.GetValue<int?>("Screenshot:TileRenderWaitMs");
if (!tileRenderWaitMsConfig.HasValue)
{
throw new InvalidOperationException("Screenshot:TileRenderWaitMs configuration is required. Set it in appsettings.json or via SCREENSHOT__TILERENDERWAITMS environment variable.");
}
var tileRenderWaitMs = tileRenderWaitMsConfig.Value;
var dynamicContentWaitMsConfig = configuration.GetValue<int?>("Screenshot:DynamicContentWaitMs");
if (!dynamicContentWaitMsConfig.HasValue)
{
throw new InvalidOperationException("Screenshot:DynamicContentWaitMs configuration is required. Set it in appsettings.json or via SCREENSHOT__DYNAMICCONTENTWAITMS environment variable.");
}
var dynamicContentWaitMs = dynamicContentWaitMsConfig.Value;
// Rough calculation:
// - Initial page load and navigation: ~10-15 seconds
// - Per frame: tileRenderWaitMs (default 5s) + overhead (~1-2s for clicking, waiting, etc.)
// - Final processing and metadata saving: ~5 seconds
var perFrameSeconds = (tileRenderWaitMs + 1500) / 1000.0; // Add 1.5s overhead per frame
var baseOverheadSeconds = 15; // Initial load + final processing
var estimatedSeconds = (int)Math.Ceiling(baseOverheadSeconds + (frameCount * perFrameSeconds));
return estimatedSeconds;
}
}