1111namespace SxgEvalPlatformApi . RequestHandlers
1212{
1313 /// <summary>
14- /// Request handler for evaluation result operations with caching support
14+ /// Request handler for evaluation fetchRequestStatus operations with caching support
1515 /// </summary>
1616 public class EvaluationResultRequestHandler : IEvaluationResultRequestHandler
1717 {
@@ -101,20 +101,45 @@ public EvaluationResultRequestHandler(
101101 // Save to blob storage first (write to backend store)
102102 await _blobService . WriteBlobContentAsync ( containerName , evalSummaryFileName , evalResultSummary . ToString ( ) ) ;
103103
104- _logger . LogInformation ( "Successfully saved evaluation result summary for EvalRunId: {EvalRunId} to {BlobPath}, Saved by: {CallerEmail}" ,
104+ _logger . LogInformation ( "Successfully saved evaluation fetchRequestStatus summary for EvalRunId: {EvalRunId} to {BlobPath}, Saved by: {CallerEmail}" ,
105105 evalRunId , $ "{ containerName } /{ evalSummaryFileName } ", callerEmail ) ;
106106
107- //Write the same to service bus
108- await _messagePublisher . SendMessageAsync ( "evalresults" , evalResultDataset . ToString ( ) ) ;
109107
110- _logger . LogInformation ( "Successfully pushed evaluation result details for EvalRunId: {EvalRunId} to the downstream" ,
111- evalRunId ) ;
112-
113108 await _blobService . WriteBlobContentAsync ( containerName , evalDatasetFileName , evalResultDataset . ToString ( ) ) ;
114109
115- _logger . LogInformation ( "Successfully saved evaluation result dataset for EvalRunId: {EvalRunId} to {BlobPath}, Saved by: {CallerEmail}" ,
110+ _logger . LogInformation ( "Successfully saved evaluation fetchRequestStatus dataset for EvalRunId: {EvalRunId} to {BlobPath}, Saved by: {CallerEmail}" ,
116111 evalRunId , $ "{ containerName } /{ evalDatasetFileName } ", callerEmail ) ;
117112
113+ if ( _configHelper . GetEnablePublishingEvalResultsToDataPlatform ( ) )
114+ {
115+ //Write the same to service bus
116+ var ( evalResultData , fetchRequestStatus ) = await GetEvaluationResultFromStorageAsync ( evalRunId ) ;
117+ if ( fetchRequestStatus . IsSuccessful == false || evalResultData == null )
118+ {
119+ _logger . LogWarning ( "Failed to retrieve evaluation results for EvalRunId: {EvalRunId} after saving. Skipping publishing to downstream." ,
120+ evalRunId ) ;
121+ return ( null , new APIRequestProcessingResultDto
122+ {
123+ IsSuccessful = false ,
124+ Message = "Failed to retrieve evaluation results after saving. Cannot publish to downstream." ,
125+ StatusCode = System . Net . HttpStatusCode . InternalServerError
126+ } ) ;
127+ }
128+ //Serialize the evalResultData to JSON string
129+ var evalResultDataJson = JsonSerializer . Serialize ( evalResultData , new JsonSerializerOptions
130+ {
131+ PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
132+ WriteIndented = true
133+ } ) ;
134+ await _messagePublisher . SendMessageAsync ( "evalresults" , evalResultDataJson ) ;
135+ _logger . LogInformation ( "Successfully pushed eval results to Data Platform for EvalRunId: {EvalRunId}." ,
136+ evalRunId ) ;
137+ }
138+
139+
140+ _logger . LogInformation ( "Successfully pushed evaluation fetchRequestStatus details for EvalRunId: {EvalRunId} to the downstream" ,
141+ evalRunId ) ;
142+
118143 // Delete enriched dataset file after successfully saving evaluation results
119144 await DeleteEnrichedDatasetAsync ( evalRunId , containerName ) ;
120145
@@ -243,7 +268,7 @@ public EvaluationResultRequestHandler(
243268 // Check if at least one blob exists
244269 if ( ! summaryExists && ! datasetExists )
245270 {
246- _logger . LogInformation ( $ "No evaluation result blobs found for EvalRunId: { evalRunId } in container: { containerName } . Tried paths: { evalSummaryBlobPath } , { evalResultDatasetPath } .") ;
271+ _logger . LogInformation ( $ "No evaluation fetchRequestStatus blobs found for EvalRunId: { evalRunId } in container: { containerName } . Tried paths: { evalSummaryBlobPath } , { evalResultDatasetPath } .") ;
247272 return ( null , new APIRequestProcessingResultDto
248273 {
249274 IsSuccessful = false ,
@@ -255,7 +280,7 @@ public EvaluationResultRequestHandler(
255280 // Combine the results from both blobs
256281 var combinedResults = new Dictionary < string , object > ( ) ;
257282
258- // Add summary data if available
283+ // Add summary evalResultData if available
259284 if ( ! string . IsNullOrEmpty ( summaryContent ) )
260285 {
261286 try
@@ -270,7 +295,7 @@ public EvaluationResultRequestHandler(
270295 }
271296 }
272297
273- // Add dataset data if available
298+ // Add dataset evalResultData if available
274299 if ( ! string . IsNullOrEmpty ( datasetContent ) )
275300 {
276301 try
@@ -342,224 +367,7 @@ public async Task<IList<EvalRunDto>> GetEvalRunsByAgentIdAsync(string agentId, D
342367 throw ;
343368 }
344369 }
345-
346-
347-
348- /// <summary>
349- /// Get evaluation results for a specific agent within a date range with caching support
350- /// </summary>
351- //public async Task<IList<EvaluationResultResponseDto>> GetEvaluationResultsByDateRangeAsync(string agentId, DateTime startDateTime, DateTime endDateTime)
352- //{
353- // try
354- // {
355- // _logger.LogInformation("Retrieving evaluation results for AgentId: {AgentId} between {StartDateTime} and {EndDateTime}",
356- // agentId, startDateTime, endDateTime);
357-
358-
359- // // If not in cache, fetch from storage
360- // // Get all evaluation runs for the agent
361- // var evalRuns = await _evalRunTableService.GetEvalRunsByAgentIdAndDateFilterAsync(agentId, startDateTime, endDateTime);
362-
363- // // Filter runs within the date range and only include completed ones
364- // var filteredRuns = evalRuns
365- // .Where(run => run.StartedDatetime.HasValue &&
366- // run.StartedDatetime.Value >= startDateTime &&
367- // run.StartedDatetime.Value <= endDateTime &&
368- // string.Equals(run.Status, EvalRunStatus.EvalRunCompleted, StringComparison.OrdinalIgnoreCase))
369- // .ToList();
370-
371- // _logger.LogInformation("Found {Count} completed evaluation runs within date range for AgentId: {AgentId}",
372- // filteredRuns.Count, agentId);
373-
374- // var results = new List<EvaluationResultResponseDto>();
375-
376- // foreach (var evalRun in filteredRuns)
377- // {
378- // try
379- // {
380- // // Get evaluation results for each run (this will use caching from GetEvaluationResultByIdAsync)
381- // var result = await GetEvaluationResultByIdAsync(evalRun.EvalRunId);
382-
383- // if (result.Success)
384- // {
385- // results.Add(result);
386- // }
387- // else
388- // {
389- // _logger.LogWarning("Could not retrieve results for EvalRunId: {EvalRunId}. Reason: {Message}",
390- // evalRun.EvalRunId, result.Message);
391- // }
392- // }
393- // catch (Exception ex)
394- // {
395- // _logger.LogError(ex, "Error retrieving results for EvalRunId: {EvalRunId}", evalRun.EvalRunId);
396- // // Continue with other runs instead of failing completely
397- // }
398- // }
399-
400- // // Cache the result (cache for 20 minutes for aggregated queries)
401- // await _cacheManager.SetAsync(cacheKey, results, TimeSpan.FromMinutes(20));
402- // _logger.LogDebug("Cached evaluation results by date range for AgentId: {AgentId}", agentId);
403-
404- // _logger.LogInformation("Successfully retrieved {Count} evaluation results for AgentId: {AgentId}",
405- // results.Count, agentId);
406-
407- // return results;
408- // }
409- // catch (Exception ex)
410- // {
411- // _logger.LogError(ex, "Error retrieving evaluation results for AgentId: {AgentId} between {StartDateTime} and {EndDateTime}",
412- // agentId, startDateTime, endDateTime);
413- // throw;
414- // }
415- //}
416-
417- /// <summary>
418- /// Invalidate related caches when data is modified
419- /// </summary>
420- //private async Task InvalidateRelatedCaches(string agentId)
421- //{
422- // try
423- // {
424- // // We can't easily invalidate wildcard patterns, so we'll log this for now
425- // // In a production system, you might want to implement cache tagging or use a more sophisticated cache invalidation strategy
426- // _logger.LogDebug("Invalidating related caches for AgentId: {AgentId}", agentId);
427-
428- // // For now, we just log the invalidation need
429- // // In a more sophisticated implementation, you could:
430- // // 1. Use cache tags to track related cache entries
431- // // 2. Store a list of active cache keys and iterate through them
432- // // 3. Use Redis SCAN command to find matching patterns (for Redis cache)
433-
434- // var statistics = await _cacheManager.GetStatisticsAsync();
435- // _logger.LogDebug("Cache statistics after potential invalidation - Type: {CacheType}, Items: {ItemCount}",
436- // statistics.CacheType, statistics.ItemCount);
437- // }
438- // catch (Exception ex)
439- // {
440- // _logger.LogWarning(ex, "Error invalidating related caches for AgentId: {AgentId}", agentId);
441- // // Don't throw - cache invalidation failure shouldn't break the main operation
442- // }
443- //}
444-
445- // ... rest of the existing methods remain the same (GetEvaluationResultByIdFallbackAsync, etc)
446-
447- /// <summary>
448- /// Fallback method to retrieve evaluation results using the original logic
449- /// </summary>
450- //private async Task<EvaluationResultResponseDto> GetEvaluationResultByIdFallbackAsync(Guid evalRunId, EvalRunDto evalRun, EvalRunTableEntity evalRunEntity)
451- //{
452- // try
453- // {
454- // _logger.LogInformation("Using fallback method to retrieve evaluation results for EvalRunId: {EvalRunId}", evalRunId);
455-
456- // // Handle container name and blob path with support for folder structure
457- // string containerName;
458- // string blobPath;
459-
460- // if (!string.IsNullOrEmpty(evalRunEntity.ContainerName))
461- // {
462- // // New format: use stored container name and blob path
463- // containerName = evalRunEntity.ContainerName;
464- // // If BlobFilePath is a folder (ends with /), look for evaluation results file dynamically
465- // if (!string.IsNullOrEmpty(evalRunEntity.BlobFilePath) && evalRunEntity.BlobFilePath.EndsWith('/'))
466- // {
467- // // Search for evaluation results files in the folder
468- // var blobs = await _blobService.ListBlobsAsync(containerName, evalRunEntity.BlobFilePath);
469- // var evaluationResultBlob = blobs.FirstOrDefault(b =>
470- // b.Contains("evaluation_results_") && b.EndsWith(".json"));
471-
472- // if (evaluationResultBlob != null)
473- // {
474- // blobPath = evaluationResultBlob;
475- // }
476- // else
477- // {
478- // // Fallback to looking for results.json for backward compatibility
479- // blobPath = $"{evalRunEntity.BlobFilePath}results.json";
480- // }
481- // }
482- // else
483- // {
484- // blobPath = evalRunEntity.BlobFilePath ?? $"evalresults/{evalRunId}/results.json";
485- // }
486- // }
487- // else
488- // {
489- // // Backward compatibility: parse the old format
490- // containerName = CommonUtils.TrimAndRemoveSpaces(evalRunEntity.AgentId);
491- // // Try to find the evaluation results file dynamically first
492- // var folderPath = $"evalresults/{evalRunId}/";
493- // var blobs = await _blobService.ListBlobsAsync(containerName, folderPath);
494- // var evaluationResultBlob = blobs.FirstOrDefault(b =>
495- // b.Contains("evaluation_results_") && b.EndsWith(".json"));
496-
497- // if (evaluationResultBlob != null)
498- // {
499- // blobPath = evaluationResultBlob;
500- // }
501- // else
502- // {
503- // // Fallback to the old hardcoded name
504- // blobPath = $"evalresults/{evalRunId}/results.json";
505- // }
506- // }
507-
508- // // Check if blob exists
509- // var blobExists = await _blobService.BlobExistsAsync(containerName, blobPath);
510- // if (!blobExists)
511- // {
512- // _logger.LogInformation("Evaluation results not found for EvalRunId: {EvalRunId} in path {BlobPath}. " +
513- // "This could mean the evaluation run hasn't completed yet or something went wrong.",
514- // evalRunId, $"{containerName}/{blobPath}");
515-
516- // return new EvaluationResultResponseDto
517- // {
518- // Success = false,
519- // Message = "Evaluation results not found. This could mean the evaluation run hasn't completed yet or something went wrong during the evaluation process.",
520- // EvalRunId = evalRunId
521- // };
522- // }
523-
524- // // Read blob content
525- // var jsonContent = await _blobService.ReadBlobContentAsync(containerName, blobPath);
526-
527- // if (string.IsNullOrEmpty(jsonContent))
528- // {
529- // _logger.LogWarning("Empty evaluation results content for EvalRunId: {EvalRunId}", evalRunId);
530- // return new EvaluationResultResponseDto
531- // {
532- // Success = false,
533- // Message = "Evaluation results are empty",
534- // EvalRunId = evalRunId
535- // };
536- // }
537-
538- // // Deserialize the content using the storage model
539- // var evaluationResult = JsonSerializer.Deserialize<StoredEvaluationResultDto>(jsonContent);
540-
541- // _logger.LogInformation("Successfully retrieved evaluation results using fallback method for EvalRunId: {EvalRunId}", evalRunId);
542-
543- // return new EvaluationResultResponseDto
544- // {
545- // Success = true,
546- // Message = "Evaluation results retrieved successfully (fallback)",
547- // EvalRunId = evalRunId,
548- // FileName = evaluationResult?.FileName ?? Path.GetFileName(blobPath),
549- // EvaluationRecords = evaluationResult?.EvaluationResults
550- // };
551- // }
552- // catch (Exception ex)
553- // {
554- // _logger.LogError(ex, "Error in fallback method for EvalRunId: {EvalRunId}", evalRunId);
555- // return new EvaluationResultResponseDto
556- // {
557- // Success = false,
558- // Message = "Failed to retrieve evaluation results using fallback method",
559- // EvalRunId = evalRunId
560- // };
561- // }
562- //}
370+
563371
564372 /// <summary>
565373 /// Delete enriched dataset file for the given evaluation run
0 commit comments