MdpIngestBatchStatusService.cs 6.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Globalization;
  2. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  3. public sealed class MdpIngestBatchStatusService(ISqlSugarClient db) : ITransient
  4. {
  5. public Task<MdpIngestBatchStatus?> FindAsync(MdpIngestSubmission caller, Guid batchId, CancellationToken cancellationToken) =>
  6. FindAsync(new BatchReadScope(batchId.ToString("D"), caller.TenantId, caller.FactoryId,
  7. caller.ConfigurationFactoryId ?? caller.FactoryId, caller.SourceId, caller.DatasetKey,
  8. caller.GrantId, caller.AccessKeyHash, caller.ConfigurationFactoryId.HasValue, null, null), cancellationToken);
  9. internal Task<MdpIngestBatchStatus?> FindForOperatorAsync(long tenantId, long configurationFactory, long datasetId,
  10. IReadOnlyCollection<long> authorizedFactories, Guid batchId, CancellationToken cancellationToken) =>
  11. FindAsync(new BatchReadScope(batchId.ToString("D"), tenantId, null, configurationFactory, null, null, null, null, false,
  12. datasetId, string.Join(',', authorizedFactories.Select(factory => factory.ToString(CultureInfo.InvariantCulture)))),
  13. cancellationToken);
  14. private async Task<MdpIngestBatchStatus?> FindAsync(BatchReadScope scope, CancellationToken cancellationToken)
  15. {
  16. cancellationToken.ThrowIfCancellationRequested();
  17. using var connection = MdpIngestDatabase.Open(db);
  18. var row = await connection.Ado.SqlQuerySingleAsync<BatchStatusRow>("""
  19. SELECT b.accepted_record_count AS AcceptedCount, b.source_schema_version AS SchemaVersion,
  20. b.received_at AS ReceivedAt, t.status AS WorkerStatus,
  21. t.last_error_code AS ErrorCode,
  22. GREATEST(COALESCE(t.updated_at_utc, b.received_at), COALESCE(e.acknowledged_at, b.received_at)) AS UpdatedAt,
  23. dispatch.status AS DispatchStatus,
  24. (SELECT COUNT(DISTINCT q.raw_event_id) FROM mdp_ingest_worker_quarantine q
  25. WHERE q.batch_id=b.batch_id AND q.tenant_id=b.tenant_id AND q.factory_id=b.factory_id
  26. AND q.source_id=b.source_id AND q.dataset_id=b.dataset_id) AS IsolatedCount,
  27. e.accepted_count AS ReceiverAcceptedCount
  28. FROM mdp_ingest_raw_batch b
  29. JOIN mdp_ingest_dataset d ON d.id=b.dataset_id AND d.tenant_id=b.tenant_id AND d.factory_id=@ConfigurationFactory
  30. JOIN mdp_purchase_active_factory f ON f.tenant_id=b.tenant_id AND f.factory_id=b.factory_id
  31. LEFT JOIN mdp_ingest_receive_task t ON t.batch_id=b.batch_id AND t.tenant_id=b.tenant_id
  32. AND t.factory_id=b.factory_id AND t.source_id=b.source_id AND t.dataset_id=b.dataset_id
  33. LEFT JOIN mdp_ingest_release_batch e ON e.batch_id=b.batch_id AND e.tenant_id=b.tenant_id
  34. AND e.factory_id=b.factory_id AND e.source_id=b.source_id AND e.dataset_id=b.dataset_id
  35. LEFT JOIN mdp_ingest_purchase_dispatch dispatch ON dispatch.batch_id=b.batch_id AND dispatch.tenant_id=b.tenant_id
  36. AND dispatch.factory_id=b.factory_id AND dispatch.source_id=b.source_id AND dispatch.dataset_id=b.dataset_id
  37. WHERE b.batch_id=@BatchId AND b.tenant_id=@TenantId
  38. AND ((b.dataset_id=@OperatorDatasetId AND FIND_IN_SET(CAST(b.factory_id AS CHAR), @AuthorizedFactories) > 0)
  39. OR (@OperatorDatasetId IS NULL AND b.accepted_record_count>0 AND b.factory_id=@FactoryId AND b.source_id=@SourceId AND d.dataset_key=@DatasetKey
  40. AND EXISTS (SELECT 1 FROM mdp_ingest_access_grant g
  41. WHERE g.id=@GrantId AND g.tenant_id=b.tenant_id AND g.factory_id=d.factory_id
  42. AND g.source_id=b.source_id AND g.dataset_key=d.dataset_key AND g.enabled=1
  43. AND g.use_batch_factory=@FromBatch AND g.access_key_hash=@AccessKeyHash)))
  44. """, new
  45. {
  46. scope.BatchId, scope.TenantId, scope.FactoryId, scope.ConfigurationFactory, scope.SourceId,
  47. scope.DatasetKey, scope.GrantId, scope.AccessKeyHash, scope.FromBatch, scope.OperatorDatasetId, scope.AuthorizedFactories
  48. });
  49. cancellationToken.ThrowIfCancellationRequested();
  50. if (row is null)
  51. return null;
  52. var receiverAccepted = row.ReceiverAcceptedCount;
  53. if (receiverAccepted is null && row.DispatchStatus == "DONE")
  54. receiverAccepted = row.AcceptedCount - row.IsolatedCount;
  55. return new MdpIngestBatchStatus(Guid.Parse(scope.BatchId), "accepted", row.SchemaVersion, row.AcceptedCount,
  56. row.WorkerStatus, row.IsolatedCount, receiverAccepted,
  57. receiverAccepted.HasValue ? "confirmed" : row.DispatchStatus switch
  58. {
  59. "FAILED" => "failed",
  60. "READY" => "awaiting_delivery",
  61. _ => "pending"
  62. },
  63. Utc(row.ReceivedAt), Utc(row.UpdatedAt), row.ErrorCode);
  64. }
  65. private static DateTimeOffset Utc(DateTime value) => new(DateTime.SpecifyKind(value, DateTimeKind.Utc));
  66. private sealed record BatchReadScope(string BatchId, long TenantId, long? FactoryId, long ConfigurationFactory,
  67. long? SourceId, string? DatasetKey, long? GrantId, string? AccessKeyHash, bool FromBatch, long? OperatorDatasetId, string? AuthorizedFactories);
  68. private sealed class BatchStatusRow
  69. {
  70. public int AcceptedCount { get; set; }
  71. public string SchemaVersion { get; set; } = "";
  72. public DateTime ReceivedAt { get; set; }
  73. public string? WorkerStatus { get; set; }
  74. public string? ErrorCode { get; set; }
  75. public DateTime UpdatedAt { get; set; }
  76. public string? DispatchStatus { get; set; }
  77. public int? ReceiverAcceptedCount { get; set; }
  78. public int IsolatedCount { get; set; }
  79. }
  80. }
  81. public sealed record MdpIngestBatchStatus(Guid BatchId, string ReceiveStatus, string SchemaVersion, int AcceptedRecordCount,
  82. string? WorkerStatus, int IsolatedRecordCount, int? ReceiverAcceptedRecordCount, string HandoffStatus,
  83. DateTimeOffset ReceivedAt, DateTimeOffset UpdatedAt, string? ErrorCode);