| | | 1 | | using System.Net; |
| | | 2 | | using System.Net.Http.Headers; |
| | | 3 | | using System.Text; |
| | | 4 | | using EHonda.KicktippAi.Core; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 7 | | using Microsoft.Extensions.FileProviders; |
| | | 8 | | using Microsoft.Extensions.Http.Resilience; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | using OpenTelemetry.Exporter; |
| | | 11 | | using OpenTelemetry.Resources; |
| | | 12 | | using OpenTelemetry.Trace; |
| | | 13 | | using OpenAiIntegration; |
| | | 14 | | using Orchestrator.Commands.Operations.CollectContext; |
| | | 15 | | using Orchestrator.Infrastructure.Factories; |
| | | 16 | | using Orchestrator.Infrastructure.Langfuse; |
| | | 17 | | using Orchestrator.Services; |
| | | 18 | | |
| | | 19 | | namespace Orchestrator.Infrastructure; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Extension methods for registering Orchestrator services in dependency injection. |
| | | 23 | | /// </summary> |
| | | 24 | | public static class ServiceRegistrationExtensions |
| | | 25 | | { |
| | | 26 | | public const string OpenAiHttpClientName = "openai"; |
| | | 27 | | |
| | | 28 | | private const string LangfuseIngestionVersionHeaderName = "x-langfuse-ingestion-version"; |
| | | 29 | | private const string LangfuseIngestionVersionHeaderValue = "4"; |
| | 1 | 30 | | private static readonly Uri FifaApiBaseAddress = new("https://api.fifa.com/api/v3/"); |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Registers all shared infrastructure services (factories, logging). |
| | | 34 | | /// </summary> |
| | | 35 | | /// <remarks> |
| | | 36 | | /// This method is idempotent - calling it multiple times has no additional effect. |
| | | 37 | | /// </remarks> |
| | | 38 | | public static IServiceCollection AddOrchestratorInfrastructure( |
| | | 39 | | this IServiceCollection services, |
| | | 40 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 41 | | { |
| | | 42 | | // Add logging (idempotent via TryAdd internally) |
| | 1 | 43 | | services.AddLogging(builder => |
| | 1 | 44 | | { |
| | 1 | 45 | | builder.AddSimpleConsole(options => |
| | 1 | 46 | | { |
| | 1 | 47 | | options.SingleLine = true; |
| | 1 | 48 | | options.IncludeScopes = false; |
| | 1 | 49 | | options.TimestampFormat = null; |
| | 1 | 50 | | options.ColorBehavior = Microsoft.Extensions.Logging.Console.LoggerColorBehavior.Enabled; |
| | 1 | 51 | | }); |
| | 1 | 52 | | builder.SetMinimumLevel(minimumLogLevel); |
| | 1 | 53 | | }); |
| | | 54 | | |
| | | 55 | | // Add memory cache for Kicktipp client |
| | 1 | 56 | | services.AddMemoryCache(); |
| | | 57 | | |
| | | 58 | | // Add HTTP client factory for Kicktipp |
| | 1 | 59 | | services.AddHttpClient(); |
| | 1 | 60 | | services.AddOpenAiHttpClientIfMissing(); |
| | | 61 | | |
| | 1 | 62 | | if (!services.Any(descriptor => descriptor.ServiceType == typeof(ILangfusePublicApiClient))) |
| | | 63 | | { |
| | 1 | 64 | | services.AddLangfusePublicApiClient(); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // Register factories (idempotent) |
| | 1 | 68 | | services.TryAddSingleton<IFirebaseServiceFactory, FirebaseServiceFactory>(); |
| | 1 | 69 | | services.TryAddSingleton<IKicktippClientFactory, KicktippClientFactory>(); |
| | 1 | 70 | | services.TryAddSingleton<IOpenAiServiceFactory, OpenAiServiceFactory>(); |
| | 1 | 71 | | services.TryAddSingleton<IContextProviderFactory, ContextProviderFactory>(); |
| | 1 | 72 | | services.TryAddSingleton<TimeProvider>(TimeProvider.System); |
| | 1 | 73 | | services.TryAddTransient<MatchOutcomeCollectionService>(); |
| | | 74 | | |
| | | 75 | | // Register Langfuse/OTel tracing (no-op if credentials are absent) |
| | 1 | 76 | | services.AddLangfuseTracing(); |
| | | 77 | | |
| | 1 | 78 | | return services; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static IServiceCollection AddOpenAiHttpClientIfMissing(this IServiceCollection services) |
| | | 82 | | { |
| | 1 | 83 | | if (services.Any(descriptor => descriptor.ServiceType == typeof(OpenAiHttpClientRegistrationMarker))) |
| | | 84 | | { |
| | 1 | 85 | | return services; |
| | | 86 | | } |
| | | 87 | | |
| | 1 | 88 | | services.TryAddSingleton<OpenAiHttpClientRegistrationMarker>(); |
| | | 89 | | |
| | 1 | 90 | | var clientBuilder = services.AddHttpClient(OpenAiHttpClientName, client => |
| | 1 | 91 | | { |
| | 1 | 92 | | // OpenAI timeout ownership stays in ResponsesClientOptions.NetworkTimeout and |
| | 1 | 93 | | // the .NET HTTP resilience pipeline. Keeping HttpClient.Timeout infinite |
| | 1 | 94 | | // avoids a third timeout source racing those mechanisms. |
| | 1 | 95 | | client.Timeout = Timeout.InfiniteTimeSpan; |
| | 1 | 96 | | }); |
| | | 97 | | |
| | 1 | 98 | | clientBuilder.AddStandardResilienceHandler().Configure(options => |
| | 1 | 99 | | { |
| | 1 | 100 | | var defaultCircuitBreakerShouldHandle = options.CircuitBreaker.ShouldHandle; |
| | 1 | 101 | | |
| | 1 | 102 | | options.Retry.DisableForUnsafeHttpMethods(); |
| | 1 | 103 | | options.CircuitBreaker.ShouldHandle = async args => |
| | 1 | 104 | | { |
| | 1 | 105 | | if (!await defaultCircuitBreakerShouldHandle(args).ConfigureAwait(false)) |
| | 1 | 106 | | { |
| | 0 | 107 | | return false; |
| | 1 | 108 | | } |
| | 1 | 109 | | |
| | 1 | 110 | | return args.Outcome.Result?.StatusCode is not HttpStatusCode.RequestTimeout |
| | 1 | 111 | | and not HttpStatusCode.TooManyRequests; |
| | 1 | 112 | | }; |
| | 1 | 113 | | options.AttemptTimeout.Timeout = TimeSpan.FromMinutes(15); |
| | 1 | 114 | | options.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(30); |
| | 1 | 115 | | options.TotalRequestTimeout.Timeout = TimeSpan.FromMinutes(15); |
| | 1 | 116 | | }); |
| | | 117 | | |
| | 1 | 118 | | return services; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | internal static IHttpClientBuilder AddLangfusePublicApiClient(this IServiceCollection services) |
| | | 122 | | { |
| | 1 | 123 | | services.TryAddTransient<LangfuseRetryLoggingHandler>(); |
| | | 124 | | |
| | 1 | 125 | | var clientBuilder = services.AddHttpClient<ILangfusePublicApiClient, LangfusePublicApiClient>((_, client) => |
| | 1 | 126 | | { |
| | 1 | 127 | | var baseUrl = (Environment.GetEnvironmentVariable("LANGFUSE_BASE_URL") ?? "https://cloud.langfuse.com").Trim |
| | 1 | 128 | | client.BaseAddress = new Uri($"{baseUrl}/api/public/"); |
| | 1 | 129 | | client.Timeout = Timeout.InfiniteTimeSpan; |
| | 1 | 130 | | client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| | 1 | 131 | | |
| | 1 | 132 | | var publicKey = Environment.GetEnvironmentVariable("LANGFUSE_PUBLIC_KEY"); |
| | 1 | 133 | | var secretKey = Environment.GetEnvironmentVariable("LANGFUSE_SECRET_KEY"); |
| | 1 | 134 | | if (!string.IsNullOrWhiteSpace(publicKey) && !string.IsNullOrWhiteSpace(secretKey)) |
| | 1 | 135 | | { |
| | 0 | 136 | | var authorization = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{publicKey}:{secretKey}")); |
| | 0 | 137 | | client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorization); |
| | 1 | 138 | | } |
| | 1 | 139 | | }); |
| | | 140 | | |
| | 1 | 141 | | clientBuilder.AddStandardResilienceHandler().Configure(options => |
| | 1 | 142 | | { |
| | 1 | 143 | | var defaultShouldHandle = options.Retry.ShouldHandle; |
| | 1 | 144 | | var defaultCircuitBreakerShouldHandle = options.CircuitBreaker.ShouldHandle; |
| | 1 | 145 | | options.Retry.DisableForUnsafeHttpMethods(); |
| | 1 | 146 | | var safeMethodShouldHandle = options.Retry.ShouldHandle; |
| | 1 | 147 | | |
| | 1 | 148 | | options.Retry.ShouldHandle = async args => |
| | 1 | 149 | | { |
| | 1 | 150 | | if (await safeMethodShouldHandle(args).ConfigureAwait(false)) |
| | 1 | 151 | | { |
| | 1 | 152 | | return true; |
| | 1 | 153 | | } |
| | 1 | 154 | | |
| | 1 | 155 | | if (!await defaultShouldHandle(args).ConfigureAwait(false)) |
| | 1 | 156 | | { |
| | 1 | 157 | | return false; |
| | 1 | 158 | | } |
| | 1 | 159 | | |
| | 1 | 160 | | var response = args.Outcome.Result; |
| | 1 | 161 | | var request = response?.RequestMessage; |
| | 1 | 162 | | return response?.StatusCode == HttpStatusCode.TooManyRequests |
| | 1 | 163 | | && request is not null |
| | 1 | 164 | | && ShouldRetryUnsafeLangfuseRateLimit(request); |
| | 1 | 165 | | }; |
| | 1 | 166 | | |
| | 1 | 167 | | options.Retry.DelayGenerator = static args => |
| | 1 | 168 | | { |
| | 1 | 169 | | var response = args.Outcome.Result; |
| | 1 | 170 | | if (response is null) |
| | 1 | 171 | | { |
| | 0 | 172 | | return new ValueTask<TimeSpan?>((TimeSpan?)null); |
| | 1 | 173 | | } |
| | 1 | 174 | | |
| | 1 | 175 | | var retryMetadata = LangfuseRetryAfterUtility.GetRetryAfterMetadata(response.Headers); |
| | 1 | 176 | | return new ValueTask<TimeSpan?>(retryMetadata.RetryAfterDelay); |
| | 1 | 177 | | }; |
| | 1 | 178 | | options.CircuitBreaker.ShouldHandle = async args => |
| | 1 | 179 | | { |
| | 1 | 180 | | if (!await defaultCircuitBreakerShouldHandle(args).ConfigureAwait(false)) |
| | 1 | 181 | | { |
| | 1 | 182 | | return false; |
| | 1 | 183 | | } |
| | 1 | 184 | | |
| | 1 | 185 | | return args.Outcome.Result?.StatusCode != HttpStatusCode.TooManyRequests; |
| | 1 | 186 | | }; |
| | 1 | 187 | | options.AttemptTimeout.Timeout = TimeSpan.FromSeconds(45); |
| | 1 | 188 | | options.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(2); |
| | 1 | 189 | | options.TotalRequestTimeout.Timeout = TimeSpan.FromMinutes(4); |
| | 1 | 190 | | }); |
| | | 191 | | |
| | | 192 | | // Keep the retry logging handler inside the resilience pipeline so every attempted request is visible. |
| | 1 | 193 | | clientBuilder.AddHttpMessageHandler<LangfuseRetryLoggingHandler>(); |
| | | 194 | | |
| | 1 | 195 | | return clientBuilder; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | private static bool ShouldRetryUnsafeLangfuseRateLimit(HttpRequestMessage request) |
| | | 199 | | { |
| | 1 | 200 | | var absolutePath = request.RequestUri?.AbsolutePath ?? string.Empty; |
| | | 201 | | |
| | 1 | 202 | | if (string.Equals(request.Method.Method, HttpMethod.Post.Method, StringComparison.OrdinalIgnoreCase)) |
| | | 203 | | { |
| | 1 | 204 | | return absolutePath.EndsWith("/api/public/scores", StringComparison.OrdinalIgnoreCase) |
| | 1 | 205 | | || absolutePath.EndsWith("/scores", StringComparison.OrdinalIgnoreCase) |
| | 1 | 206 | | || absolutePath.EndsWith("/api/public/dataset-items", StringComparison.OrdinalIgnoreCase) |
| | 1 | 207 | | || absolutePath.EndsWith("/dataset-items", StringComparison.OrdinalIgnoreCase) |
| | 1 | 208 | | || absolutePath.EndsWith("/api/public/dataset-run-items", StringComparison.OrdinalIgnoreCase) |
| | 1 | 209 | | || absolutePath.EndsWith("/dataset-run-items", StringComparison.OrdinalIgnoreCase); |
| | | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | if (string.Equals(request.Method.Method, HttpMethod.Delete.Method, StringComparison.OrdinalIgnoreCase)) |
| | | 213 | | { |
| | 0 | 214 | | return absolutePath.Contains("/api/public/datasets/", StringComparison.OrdinalIgnoreCase) |
| | 0 | 215 | | && absolutePath.Contains("/runs/", StringComparison.OrdinalIgnoreCase); |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | return false; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Registers the OpenTelemetry tracing pipeline with the Langfuse OTLP endpoint. |
| | | 223 | | /// If <c>LANGFUSE_PUBLIC_KEY</c> or <c>LANGFUSE_SECRET_KEY</c> are not set, |
| | | 224 | | /// no pipeline is registered and all <see cref="System.Diagnostics.ActivitySource.StartActivity(string)"/> |
| | | 225 | | /// calls return <c>null</c> (graceful degradation). |
| | | 226 | | /// </summary> |
| | | 227 | | /// <remarks> |
| | | 228 | | /// <para> |
| | | 229 | | /// Uses the standard <c>AddOpenTelemetry()</c> API from <c>OpenTelemetry.Extensions.Hosting</c>, |
| | | 230 | | /// which registers the <see cref="TracerProvider"/> as a DI-managed singleton and an |
| | | 231 | | /// <see cref="Microsoft.Extensions.Hosting.IHostedService"/> that triggers provider construction. |
| | | 232 | | /// Since this app uses Spectre.Console.Cli (no <c>IHost</c>), the <see cref="TypeRegistrar"/> |
| | | 233 | | /// manually starts hosted services after building the <c>ServiceProvider</c>. |
| | | 234 | | /// </para> |
| | | 235 | | /// <para> |
| | | 236 | | /// This method is idempotent — multiple calls have no additional effect. |
| | | 237 | | /// </para> |
| | | 238 | | /// </remarks> |
| | | 239 | | public static IServiceCollection AddLangfuseTracing(this IServiceCollection services) |
| | | 240 | | { |
| | | 241 | | // Idempotency: skip if tracing has already been registered. |
| | 1 | 242 | | if (_langfuseTracingRegistered) |
| | 1 | 243 | | return services; |
| | | 244 | | |
| | 1 | 245 | | var publicKey = Environment.GetEnvironmentVariable("LANGFUSE_PUBLIC_KEY"); |
| | 1 | 246 | | var secretKey = Environment.GetEnvironmentVariable("LANGFUSE_SECRET_KEY"); |
| | | 247 | | |
| | 1 | 248 | | if (string.IsNullOrEmpty(publicKey) || string.IsNullOrEmpty(secretKey)) |
| | | 249 | | { |
| | | 250 | | // No credentials — skip OTel registration entirely |
| | 1 | 251 | | return services; |
| | | 252 | | } |
| | | 253 | | |
| | 1 | 254 | | _langfuseTracingRegistered = true; |
| | | 255 | | |
| | 1 | 256 | | var baseUrl = Environment.GetEnvironmentVariable("LANGFUSE_BASE_URL") ?? "https://cloud.langfuse.com"; |
| | 1 | 257 | | var headers = BuildLangfuseOtlpHeaders(publicKey, secretKey); |
| | | 258 | | |
| | | 259 | | // NOTE: Setting options.Endpoint programmatically sets AppendSignalPathToEndpoint = false, |
| | | 260 | | // so the full URL including /v1/traces must be provided. |
| | 1 | 261 | | services.AddOpenTelemetry() |
| | 0 | 262 | | .ConfigureResource(r => r.AddService("KicktippAi")) |
| | 1 | 263 | | .WithTracing(tracing => tracing |
| | 1 | 264 | | .AddSource(Telemetry.Source.Name) |
| | 1 | 265 | | .AddProcessor(new LangfuseBaggageSpanProcessor()) |
| | 1 | 266 | | .AddOtlpExporter(options => |
| | 1 | 267 | | { |
| | 0 | 268 | | options.Endpoint = new Uri($"{baseUrl}/api/public/otel/v1/traces"); |
| | 0 | 269 | | options.Protocol = OtlpExportProtocol.HttpProtobuf; |
| | 0 | 270 | | options.Headers = headers; |
| | 0 | 271 | | })); |
| | | 272 | | |
| | 1 | 273 | | return services; |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | internal static string BuildLangfuseOtlpHeaders(string publicKey, string secretKey) |
| | | 277 | | { |
| | 1 | 278 | | var authorization = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{publicKey}:{secretKey}")); |
| | 1 | 279 | | return string.Join(",", |
| | 1 | 280 | | $"Authorization=Basic {authorization}", |
| | 1 | 281 | | $"{LangfuseIngestionVersionHeaderName}={LangfuseIngestionVersionHeaderValue}"); |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | private static bool _langfuseTracingRegistered; |
| | | 285 | | |
| | | 286 | | private sealed class OpenAiHttpClientRegistrationMarker |
| | | 287 | | { |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Registers services specific to the ListKpiCommand. |
| | | 292 | | /// </summary> |
| | | 293 | | /// <remarks> |
| | | 294 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 295 | | /// </remarks> |
| | | 296 | | public static IServiceCollection AddListKpiCommandServices( |
| | | 297 | | this IServiceCollection services, |
| | | 298 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 299 | | { |
| | 1 | 300 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 301 | | |
| | | 302 | | // ListKpiCommand only needs Firebase factory (uses IKpiRepository) |
| | | 303 | | // No command-specific keyed services needed - factory pattern handles runtime config |
| | | 304 | | |
| | 1 | 305 | | return services; |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | /// <summary> |
| | | 309 | | /// Service key for the KPI documents file provider. |
| | | 310 | | /// </summary> |
| | | 311 | | public const string KpiDocumentsFileProviderKey = "kpi-documents"; |
| | | 312 | | |
| | | 313 | | /// <summary> |
| | | 314 | | /// Service key for the transfers documents file provider. |
| | | 315 | | /// </summary> |
| | | 316 | | public const string TransfersDocumentsFileProviderKey = "transfers-documents"; |
| | | 317 | | |
| | | 318 | | /// <summary> |
| | | 319 | | /// Registers services specific to the UploadKpiCommand. |
| | | 320 | | /// </summary> |
| | | 321 | | /// <remarks> |
| | | 322 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 323 | | /// </remarks> |
| | | 324 | | public static IServiceCollection AddUploadKpiCommandServices( |
| | | 325 | | this IServiceCollection services, |
| | | 326 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 327 | | { |
| | 1 | 328 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 329 | | |
| | | 330 | | // UploadKpiCommand only needs Firebase factory (uses IKpiRepository) |
| | | 331 | | // Register keyed file provider for KPI documents directory |
| | 1 | 332 | | services.TryAddKeyedSingleton<IFileProvider>( |
| | 1 | 333 | | KpiDocumentsFileProviderKey, |
| | 0 | 334 | | (_, _) => SolutionRelativeFileProvider.Create("kpi-documents")); |
| | | 335 | | |
| | 1 | 336 | | return services; |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | /// <summary> |
| | | 340 | | /// Registers services specific to the CostCommand. |
| | | 341 | | /// </summary> |
| | | 342 | | /// <remarks> |
| | | 343 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 344 | | /// </remarks> |
| | | 345 | | public static IServiceCollection AddCostCommandServices( |
| | | 346 | | this IServiceCollection services, |
| | | 347 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 348 | | { |
| | 1 | 349 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 350 | | |
| | | 351 | | // CostCommand needs Firebase factory (uses IPredictionRepository, FirestoreDb) |
| | | 352 | | // No command-specific keyed services needed - factory pattern handles runtime config |
| | | 353 | | |
| | 1 | 354 | | return services; |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | /// <summary> |
| | | 358 | | /// Registers services specific to the MatchdayCommand. |
| | | 359 | | /// </summary> |
| | | 360 | | /// <remarks> |
| | | 361 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 362 | | /// </remarks> |
| | | 363 | | public static IServiceCollection AddMatchdayCommandServices( |
| | | 364 | | this IServiceCollection services, |
| | | 365 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 366 | | { |
| | 1 | 367 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 368 | | |
| | | 369 | | // MatchdayCommand needs all factories: |
| | | 370 | | // - Firebase (IPredictionRepository, IContextRepository) |
| | | 371 | | // - Kicktipp (IKicktippClient) |
| | | 372 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 373 | | // Factory pattern handles runtime config based on settings |
| | | 374 | | |
| | 1 | 375 | | return services; |
| | | 376 | | } |
| | | 377 | | |
| | | 378 | | /// <summary> |
| | | 379 | | /// Registers services specific to the RandomMatchCommand. |
| | | 380 | | /// </summary> |
| | | 381 | | /// <remarks> |
| | | 382 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 383 | | /// </remarks> |
| | | 384 | | public static IServiceCollection AddRandomMatchCommandServices( |
| | | 385 | | this IServiceCollection services, |
| | | 386 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 387 | | { |
| | 1 | 388 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 389 | | |
| | | 390 | | // RandomMatchCommand needs the same factories as MatchdayCommand: |
| | | 391 | | // - Firebase (IPredictionRepository, IContextRepository) |
| | | 392 | | // - Kicktipp (IKicktippClient) |
| | | 393 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 394 | | |
| | 1 | 395 | | return services; |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | /// <summary> |
| | | 399 | | /// Registers services specific to the BonusCommand. |
| | | 400 | | /// </summary> |
| | | 401 | | /// <remarks> |
| | | 402 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 403 | | /// </remarks> |
| | | 404 | | public static IServiceCollection AddBonusCommandServices( |
| | | 405 | | this IServiceCollection services, |
| | | 406 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 407 | | { |
| | 1 | 408 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 409 | | |
| | | 410 | | // BonusCommand needs all factories: |
| | | 411 | | // - Firebase (IPredictionRepository, IKpiRepository) |
| | | 412 | | // - Kicktipp (IKicktippClient) |
| | | 413 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 414 | | |
| | 1 | 415 | | return services; |
| | | 416 | | } |
| | | 417 | | |
| | | 418 | | /// <summary> |
| | | 419 | | /// Registers services specific to the VerifyMatchdayCommand. |
| | | 420 | | /// </summary> |
| | | 421 | | public static IServiceCollection AddVerifyMatchdayCommandServices( |
| | | 422 | | this IServiceCollection services, |
| | | 423 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 424 | | { |
| | 1 | 425 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 426 | | |
| | | 427 | | // VerifyMatchdayCommand needs: |
| | | 428 | | // - Firebase (IPredictionRepository, IContextRepository) |
| | | 429 | | // - Kicktipp (IKicktippClient) |
| | | 430 | | |
| | 1 | 431 | | return services; |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | private static IServiceCollection AddFifaRankingSourceServicesIfMissing(this IServiceCollection services) |
| | | 435 | | { |
| | 1 | 436 | | services.TryAddTransient<IFifaRankingSource, FifaRankingSource>(); |
| | | 437 | | |
| | 1 | 438 | | if (services.Any(descriptor => descriptor.ServiceType == typeof(IFifaRankingApiClient))) |
| | | 439 | | { |
| | 1 | 440 | | return services; |
| | | 441 | | } |
| | | 442 | | |
| | 1 | 443 | | services.AddHttpClient<IFifaRankingApiClient, FifaRankingApiClient>(client => |
| | 1 | 444 | | { |
| | 0 | 445 | | client.BaseAddress = FifaApiBaseAddress; |
| | 0 | 446 | | client.Timeout = Timeout.InfiniteTimeSpan; |
| | 0 | 447 | | client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| | 0 | 448 | | }); |
| | | 449 | | |
| | 1 | 450 | | return services; |
| | | 451 | | } |
| | | 452 | | |
| | | 453 | | private static IServiceCollection AddWm26LineupSourceServicesIfMissing(this IServiceCollection services) |
| | | 454 | | { |
| | 1 | 455 | | services.TryAddTransient<IWm26LineupSource, Wm26LineupSource>(); |
| | | 456 | | |
| | 1 | 457 | | if (!services.Any(descriptor => descriptor.ServiceType == typeof(IWm26TransfermarktDuckDbProvider))) |
| | | 458 | | { |
| | 1 | 459 | | services.AddHttpClient<IWm26TransfermarktDuckDbProvider, Wm26TransfermarktDuckDbProvider>(); |
| | | 460 | | } |
| | | 461 | | |
| | 1 | 462 | | return services; |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | /// <summary> |
| | | 466 | | /// Registers services specific to the VerifyBonusCommand. |
| | | 467 | | /// </summary> |
| | | 468 | | public static IServiceCollection AddVerifyBonusCommandServices( |
| | | 469 | | this IServiceCollection services, |
| | | 470 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 471 | | { |
| | 1 | 472 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 473 | | |
| | | 474 | | // VerifyBonusCommand needs: |
| | | 475 | | // - Firebase (IPredictionRepository, IKpiRepository) |
| | | 476 | | // - Kicktipp (IKicktippClient) |
| | | 477 | | |
| | 1 | 478 | | return services; |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | /// <summary> |
| | | 482 | | /// Registers services specific to the CollectContextKicktippCommand. |
| | | 483 | | /// </summary> |
| | | 484 | | public static IServiceCollection AddCollectContextKicktippCommandServices( |
| | | 485 | | this IServiceCollection services, |
| | | 486 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 487 | | { |
| | 1 | 488 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 489 | | |
| | | 490 | | // CollectContextKicktippCommand needs: |
| | | 491 | | // - Firebase (IContextRepository, IMatchOutcomeRepository) |
| | | 492 | | // - Kicktipp (IKicktippClient) |
| | | 493 | | |
| | 1 | 494 | | return services; |
| | | 495 | | } |
| | | 496 | | |
| | | 497 | | /// <summary> |
| | | 498 | | /// Registers services specific to the CollectContextFifaCommand. |
| | | 499 | | /// </summary> |
| | | 500 | | public static IServiceCollection AddCollectContextFifaCommandServices( |
| | | 501 | | this IServiceCollection services, |
| | | 502 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 503 | | { |
| | 1 | 504 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 505 | | |
| | 1 | 506 | | services.AddFifaRankingSourceServicesIfMissing(); |
| | | 507 | | |
| | | 508 | | // CollectContextFifaCommand needs Firebase (IContextRepository, IKpiRepository) |
| | | 509 | | // and the public FIFA rankings API. |
| | | 510 | | |
| | 1 | 511 | | return services; |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | /// <summary> |
| | | 515 | | /// Registers services specific to the CollectContextLineupsCommand. |
| | | 516 | | /// </summary> |
| | | 517 | | public static IServiceCollection AddCollectContextLineupsCommandServices( |
| | | 518 | | this IServiceCollection services, |
| | | 519 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 520 | | { |
| | 1 | 521 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | 1 | 522 | | services.AddWm26LineupSourceServicesIfMissing(); |
| | | 523 | | |
| | | 524 | | // CollectContextLineupsCommand needs Firebase (IContextRepository, IKpiRepository) |
| | | 525 | | // and the Transfermarkt DuckDB snapshot. |
| | | 526 | | |
| | 1 | 527 | | return services; |
| | | 528 | | } |
| | | 529 | | |
| | | 530 | | /// <summary> |
| | | 531 | | /// Registers services specific to the CollectContextDevCommand. |
| | | 532 | | /// </summary> |
| | | 533 | | public static IServiceCollection AddCollectContextDevCommandServices( |
| | | 534 | | this IServiceCollection services, |
| | | 535 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 536 | | { |
| | 1 | 537 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | 1 | 538 | | services.AddFifaRankingSourceServicesIfMissing(); |
| | 1 | 539 | | services.AddWm26LineupSourceServicesIfMissing(); |
| | | 540 | | |
| | | 541 | | // CollectContextDevCommand composes the Kicktipp, FIFA, and lineup collection paths. |
| | | 542 | | |
| | 1 | 543 | | return services; |
| | | 544 | | } |
| | | 545 | | |
| | | 546 | | /// <summary> |
| | | 547 | | /// Registers services specific to WM26 recent-history date-map commands. |
| | | 548 | | /// </summary> |
| | | 549 | | public static IServiceCollection AddWm26RecentHistoryCommandServices( |
| | | 550 | | this IServiceCollection services, |
| | | 551 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 552 | | { |
| | 1 | 553 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 554 | | |
| | | 555 | | // WM26 recent-history commands need Firebase (IContextRepository). |
| | | 556 | | |
| | 1 | 557 | | return services; |
| | | 558 | | } |
| | | 559 | | |
| | | 560 | | /// <summary> |
| | | 561 | | /// Registers services specific to the ContextChangesCommand. |
| | | 562 | | /// </summary> |
| | | 563 | | public static IServiceCollection AddContextChangesCommandServices( |
| | | 564 | | this IServiceCollection services, |
| | | 565 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 566 | | { |
| | 1 | 567 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 568 | | |
| | | 569 | | // ContextChangesCommand only needs Firebase (IContextRepository) |
| | | 570 | | |
| | 1 | 571 | | return services; |
| | | 572 | | } |
| | | 573 | | |
| | | 574 | | /// <summary> |
| | | 575 | | /// Registers services specific to the UploadTransfersCommand. |
| | | 576 | | /// </summary> |
| | | 577 | | public static IServiceCollection AddUploadTransfersCommandServices( |
| | | 578 | | this IServiceCollection services, |
| | | 579 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 580 | | { |
| | 1 | 581 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 582 | | |
| | | 583 | | // UploadTransfersCommand needs Firebase (IContextRepository) |
| | | 584 | | // Register keyed file provider for transfers documents directory |
| | 1 | 585 | | services.TryAddKeyedSingleton<IFileProvider>( |
| | 1 | 586 | | TransfersDocumentsFileProviderKey, |
| | 0 | 587 | | (_, _) => SolutionRelativeFileProvider.Create("transfers-documents")); |
| | | 588 | | |
| | 1 | 589 | | return services; |
| | | 590 | | } |
| | | 591 | | |
| | | 592 | | /// <summary> |
| | | 593 | | /// Registers services specific to the UploadContextCommand. |
| | | 594 | | /// </summary> |
| | | 595 | | public static IServiceCollection AddUploadContextCommandServices( |
| | | 596 | | this IServiceCollection services, |
| | | 597 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 598 | | { |
| | 1 | 599 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 600 | | |
| | | 601 | | // UploadContextCommand needs Firebase (IContextRepository). |
| | | 602 | | |
| | 1 | 603 | | return services; |
| | | 604 | | } |
| | | 605 | | |
| | | 606 | | /// <summary> |
| | | 607 | | /// Registers services specific to the CopyFirestoreContextCommand. |
| | | 608 | | /// </summary> |
| | | 609 | | public static IServiceCollection AddCopyFirestoreContextCommandServices( |
| | | 610 | | this IServiceCollection services, |
| | | 611 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 612 | | { |
| | 1 | 613 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 614 | | |
| | | 615 | | // CopyFirestoreContextCommand needs Firebase (IContextRepository, IKpiRepository). |
| | | 616 | | |
| | 1 | 617 | | return services; |
| | | 618 | | } |
| | | 619 | | |
| | | 620 | | /// <summary> |
| | | 621 | | /// Registers services specific to the AnalyzeMatchDetailedCommand. |
| | | 622 | | /// </summary> |
| | | 623 | | public static IServiceCollection AddAnalyzeMatchDetailedCommandServices( |
| | | 624 | | this IServiceCollection services, |
| | | 625 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 626 | | { |
| | 1 | 627 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 628 | | |
| | | 629 | | // AnalyzeMatchDetailedCommand needs: |
| | | 630 | | // - Firebase (IContextRepository) |
| | | 631 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 632 | | |
| | 1 | 633 | | return services; |
| | | 634 | | } |
| | | 635 | | |
| | | 636 | | /// <summary> |
| | | 637 | | /// Registers services specific to the AnalyzeMatchComparisonCommand. |
| | | 638 | | /// </summary> |
| | | 639 | | public static IServiceCollection AddAnalyzeMatchComparisonCommandServices( |
| | | 640 | | this IServiceCollection services, |
| | | 641 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 642 | | { |
| | 1 | 643 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 644 | | |
| | | 645 | | // AnalyzeMatchComparisonCommand needs: |
| | | 646 | | // - Firebase (IContextRepository) |
| | | 647 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 648 | | |
| | 1 | 649 | | return services; |
| | | 650 | | } |
| | | 651 | | |
| | | 652 | | /// <summary> |
| | | 653 | | /// Registers all command services. Useful for production setup. |
| | | 654 | | /// </summary> |
| | | 655 | | public static IServiceCollection AddAllCommandServices( |
| | | 656 | | this IServiceCollection services, |
| | | 657 | | LogLevel minimumLogLevel = LogLevel.Information) |
| | | 658 | | { |
| | | 659 | | // Infrastructure is added by each command method, but we call it first for clarity |
| | 1 | 660 | | services.AddOrchestratorInfrastructure(minimumLogLevel); |
| | | 661 | | |
| | | 662 | | // Register all command-specific services |
| | 1 | 663 | | services.AddListKpiCommandServices(minimumLogLevel); |
| | 1 | 664 | | services.AddUploadKpiCommandServices(minimumLogLevel); |
| | 1 | 665 | | services.AddCostCommandServices(minimumLogLevel); |
| | 1 | 666 | | services.AddMatchdayCommandServices(minimumLogLevel); |
| | 1 | 667 | | services.AddRandomMatchCommandServices(minimumLogLevel); |
| | 1 | 668 | | services.AddBonusCommandServices(minimumLogLevel); |
| | 1 | 669 | | services.AddVerifyMatchdayCommandServices(minimumLogLevel); |
| | 1 | 670 | | services.AddVerifyBonusCommandServices(minimumLogLevel); |
| | 1 | 671 | | services.AddCollectContextKicktippCommandServices(minimumLogLevel); |
| | 1 | 672 | | services.AddCollectContextFifaCommandServices(minimumLogLevel); |
| | 1 | 673 | | services.AddCollectContextLineupsCommandServices(minimumLogLevel); |
| | 1 | 674 | | services.AddCollectContextDevCommandServices(minimumLogLevel); |
| | 1 | 675 | | services.AddWm26RecentHistoryCommandServices(minimumLogLevel); |
| | 1 | 676 | | services.AddContextChangesCommandServices(minimumLogLevel); |
| | 1 | 677 | | services.AddUploadTransfersCommandServices(minimumLogLevel); |
| | 1 | 678 | | services.AddUploadContextCommandServices(minimumLogLevel); |
| | 1 | 679 | | services.AddCopyFirestoreContextCommandServices(minimumLogLevel); |
| | 1 | 680 | | services.AddAnalyzeMatchDetailedCommandServices(minimumLogLevel); |
| | 1 | 681 | | services.AddAnalyzeMatchComparisonCommandServices(minimumLogLevel); |
| | | 682 | | |
| | 1 | 683 | | return services; |
| | | 684 | | } |
| | | 685 | | } |