private async Task <(IEnumerable <AnalyzeSentimentResult> sentimentResults, IEnumerable <RecognizePiiEntitiesResult> piiResults, IEnumerable <string> errors)> GetOperationResults(int index, string operationId) { var errors = new List <string>(); var sentimentResults = new List <AnalyzeSentimentResult>(); var piiResults = new List <RecognizePiiEntitiesResult>(); try { Log.LogInformation($"Sending text analytics request for document chunk with id {index}."); using var cts = new CancellationTokenSource(); cts.CancelAfter(RequestTimeout); var textAnalyticsOperation = new AnalyzeActionsOperation(operationId, TextAnalyticsClient); await textAnalyticsOperation.UpdateStatusAsync().ConfigureAwait(false); if (!textAnalyticsOperation.HasCompleted) { throw new InvalidOperationException("Tried to get result of not completed text analytics request."); } await foreach (var documentsInPage in textAnalyticsOperation.GetValuesAsync()) { foreach (var piiResult in documentsInPage.RecognizePiiEntitiesResults) { if (piiResult.HasError) { errors.Add($"PII recognition failed with error: {piiResult.Error.Message}"); } piiResults.AddRange(piiResult.DocumentsResults); } foreach (var sentimentResult in documentsInPage.AnalyzeSentimentResults) { if (sentimentResult.HasError) { errors.Add($"Sentiment analysis failed with error: {sentimentResult.Error.Message}"); } sentimentResults.AddRange(sentimentResult.DocumentsResults); } } } catch (OperationCanceledException operationCanceledException) { throw new TransientFailureException($"Operation was canceled after {RequestTimeout.TotalSeconds} seconds.", operationCanceledException); } // do not catch throttling errors, rather throw and retry catch (RequestFailedException e) when(e.Status != 429) { errors.Add($"Text analytics request failed with error: {e.Message}"); } return(sentimentResults, piiResults, errors); }
/// <summary> /// Checks for all text analytics requests that were marked as running if they have completed and sets a new state accordingly. /// <param name="audioFileInfos"></param> /// <returns>True if all requests completed, else false.</returns> /// </summary> public async Task <bool> TextAnalyticsRequestsCompleted(IEnumerable <AudioFileInfo> audioFileInfos) { var runningTextAnalyticsRequests = new List <TextAnalyticsRequest>(); if (audioFileInfos.Where(audioFileInfo => audioFileInfo.TextAnalyticsRequests.AudioLevelRequests != null).Any()) { runningTextAnalyticsRequests.AddRange(audioFileInfos.SelectMany(audioFileInfo => audioFileInfo.TextAnalyticsRequests.AudioLevelRequests).Where(text => text.Status == TextAnalyticsRequestStatus.Running)); } if (audioFileInfos.Where(audioFileInfo => audioFileInfo.TextAnalyticsRequests.UtteranceLevelRequests != null).Any()) { runningTextAnalyticsRequests.AddRange(audioFileInfos.SelectMany(audioFileInfo => audioFileInfo.TextAnalyticsRequests.UtteranceLevelRequests).Where(text => text.Status == TextAnalyticsRequestStatus.Running)); } var textAnalyticsRequestCompleted = true; foreach (var textAnalyticsJob in runningTextAnalyticsRequests) { var operation = new AnalyzeActionsOperation(textAnalyticsJob.Id, TextAnalyticsClient); using var cts = new CancellationTokenSource(); cts.CancelAfter(RequestTimeout); await operation.UpdateStatusAsync(cts.Token).ConfigureAwait(false); if (operation.HasCompleted) { textAnalyticsJob.Status = TextAnalyticsRequestStatus.Completed; } else { textAnalyticsRequestCompleted = false; } } return(textAnalyticsRequestCompleted); }
public void AnalyzeOperationWithGenericError() { using var stream = new MemoryStream(Encoding.UTF8.GetBytes(@" { ""displayName"": ""AnalyzeOperationBatchWithErrorTest"", ""jobId"": ""75d521bc-c2aa-4d8a-aabe-713e72d53a2d"", ""lastUpdateDateTime"": ""2021-03-03T22:39:37Z"", ""createdDateTime"": ""2021-03-03T22:39:36Z"", ""expirationDateTime"": ""2021-03-04T22:39:36Z"", ""status"": ""failed"", ""errors"": [ { ""code"": ""InternalServerError"", ""message"": ""Some error"" } ], ""tasks"": { ""details"": { ""name"": ""AnalyzeOperationBatchWithErrorTest"", ""lastUpdateDateTime"": ""2021-03-03T22:39:37Z"" }, ""completed"": 0, ""failed"": 1, ""inProgress"": 0, ""total"": 1, ""entityRecognitionTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ] } }")); var mockResponse = new MockResponse(200); mockResponse.ContentStream = stream; var mockTransport = new MockTransport(new[] { mockResponse }); var client = CreateTestClient(mockTransport); var documents = new List <string> { "Elon Musk is the CEO of SpaceX and Tesla." }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>() { new ExtractKeyPhrasesAction() }, DisplayName = "AnalyzeOperationBatchWithErrorTest" }; var operation = new AnalyzeActionsOperation("75d521bc-c2aa-4d8a-aabe-713e72d53a2d", client); RequestFailedException ex = Assert.ThrowsAsync <RequestFailedException>(async() => await operation.UpdateStatusAsync()); Assert.AreEqual("InternalServerError", ex.ErrorCode); Assert.IsTrue(ex.Message.Contains("Some error")); }
public async Task AnalyzeOperationWithActionsError() { using var stream = new MemoryStream(Encoding.UTF8.GetBytes(@" { ""displayName"": ""AnalyzeOperationBatchWithErrorTest"", ""jobId"": ""75d521bc-c2aa-4d8a-aabe-713e72d53a2d"", ""lastUpdateDateTime"": ""2021-03-03T22:39:37Z"", ""createdDateTime"": ""2021-03-03T22:39:36Z"", ""expirationDateTime"": ""2021-03-04T22:39:36Z"", ""status"": ""succeeded"", ""errors"": [ { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/entityRecognitionPiiTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/entityRecognitionTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/keyPhraseExtractionTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/entityLinkingTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/sentimentAnalysisTasks/0"" } ], ""tasks"": { ""details"": { ""name"": ""AnalyzeOperationBatchWithErrorTest"", ""lastUpdateDateTime"": ""2021-03-03T22:39:37Z"" }, ""completed"": 0, ""failed"": 5, ""inProgress"": 0, ""total"": 5, ""entityRecognitionTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""entityRecognitionPiiTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""keyPhraseExtractionTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""entityLinkingTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""sentimentAnalysisTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ] } }")); var mockResponse = new MockResponse(200); mockResponse.ContentStream = stream; var mockTransport = new MockTransport(new[] { mockResponse }); var client = CreateTestClient(mockTransport); var documents = new List <string> { "Elon Musk is the CEO of SpaceX and Tesla." }; TextAnalyticsActions batchActions = new TextAnalyticsActions() { ExtractKeyPhrasesActions = new List <ExtractKeyPhrasesAction>() { new ExtractKeyPhrasesAction() }, RecognizeEntitiesActions = new List <RecognizeEntitiesAction>() { new RecognizeEntitiesAction() }, RecognizePiiEntitiesActions = new List <RecognizePiiEntitiesAction>() { new RecognizePiiEntitiesAction() }, RecognizeLinkedEntitiesActions = new List <RecognizeLinkedEntitiesAction>() { new RecognizeLinkedEntitiesAction() }, AnalyzeSentimentActions = new List <AnalyzeSentimentAction>() { new AnalyzeSentimentAction() }, DisplayName = "AnalyzeOperationBatchWithErrorTest" }; var operation = new AnalyzeActionsOperation("75d521bc-c2aa-4d8a-aabe-713e72d53a2d", client); await operation.UpdateStatusAsync(); Assert.AreEqual(5, operation.ActionsFailed); Assert.AreEqual(0, operation.ActionsSucceeded); Assert.AreEqual(0, operation.ActionsInProgress); Assert.AreEqual(5, operation.ActionsTotal); //Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); RecognizeEntitiesActionResult entitiesActionsResults = resultCollection.RecognizeEntitiesResults.FirstOrDefault(); ExtractKeyPhrasesActionResult keyPhrasesActionsResults = resultCollection.ExtractKeyPhrasesResults.FirstOrDefault(); RecognizePiiEntitiesActionResult piiActionsResults = resultCollection.RecognizePiiEntitiesResults.FirstOrDefault(); RecognizeLinkedEntitiesActionResult entityLinkingActionsResults = resultCollection.RecognizeLinkedEntitiesResults.FirstOrDefault(); AnalyzeSentimentActionResult analyzeSentimentActionsResults = resultCollection.AnalyzeSentimentResults.FirstOrDefault(); Assert.IsTrue(entitiesActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => entitiesActionsResults.DocumentsResults.GetType()); Assert.IsTrue(keyPhrasesActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => keyPhrasesActionsResults.DocumentsResults.GetType()); Assert.IsTrue(piiActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => piiActionsResults.DocumentsResults.GetType()); Assert.IsTrue(entityLinkingActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => entityLinkingActionsResults.DocumentsResults.GetType()); Assert.IsTrue(analyzeSentimentActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => analyzeSentimentActionsResults.DocumentsResults.GetType()); }
public async Task AnalyzeOperationWithActionsError() { using var stream = new MemoryStream(Encoding.UTF8.GetBytes(@" { ""displayName"": ""AnalyzeOperationBatchWithErrorTest"", ""jobId"": ""75d521bc-c2aa-4d8a-aabe-713e72d53a2d"", ""lastUpdateDateTime"": ""2021-03-03T22:39:37Z"", ""createdDateTime"": ""2021-03-03T22:39:36Z"", ""expirationDateTime"": ""2021-03-04T22:39:36Z"", ""status"": ""succeeded"", ""errors"": [ { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/entityRecognitionPiiTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/entityRecognitionTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/keyPhraseExtractionTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/entityLinkingTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/sentimentAnalysisTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/extractiveSummarizationTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/customEntityRecognitionTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/customSingleClassificationTasks/0"" }, { ""code"": ""InvalidRequest"", ""message"": ""Some error"", ""target"": ""#/tasks/customMultiClassificationTasks/0"" } ], ""tasks"": { ""details"": { ""name"": ""AnalyzeOperationBatchWithErrorTest"", ""lastUpdateDateTime"": ""2021-03-03T22:39:37Z"" }, ""completed"": 0, ""failed"": 9, ""inProgress"": 0, ""total"": 9, ""entityRecognitionTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""entityRecognitionPiiTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""keyPhraseExtractionTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""entityLinkingTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""sentimentAnalysisTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""extractiveSummarizationTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""customEntityRecognitionTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""customSingleClassificationTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ], ""customMultiClassificationTasks"": [ { ""lastUpdateDateTime"": ""2021-03-03T22:39:37.1716697Z"", ""taskName"": ""something"", ""state"": ""failed"" } ] } }")); var mockResponse = new MockResponse(200); mockResponse.ContentStream = stream; var mockTransport = new MockTransport(new[] { mockResponse }); var client = CreateTestClient(mockTransport); AnalyzeActionsOperation operation = CreateOperation(client); await operation.UpdateStatusAsync(); Assert.AreEqual(9, operation.ActionsFailed); Assert.AreEqual(0, operation.ActionsSucceeded); Assert.AreEqual(0, operation.ActionsInProgress); Assert.AreEqual(9, operation.ActionsTotal); //Take the first page AnalyzeActionsResult resultCollection = operation.Value.ToEnumerableAsync().Result.FirstOrDefault(); RecognizeEntitiesActionResult entitiesActionsResults = resultCollection.RecognizeEntitiesResults.FirstOrDefault(); ExtractKeyPhrasesActionResult keyPhrasesActionsResults = resultCollection.ExtractKeyPhrasesResults.FirstOrDefault(); RecognizePiiEntitiesActionResult piiActionsResults = resultCollection.RecognizePiiEntitiesResults.FirstOrDefault(); RecognizeLinkedEntitiesActionResult entityLinkingActionsResults = resultCollection.RecognizeLinkedEntitiesResults.FirstOrDefault(); AnalyzeSentimentActionResult analyzeSentimentActionsResults = resultCollection.AnalyzeSentimentResults.FirstOrDefault(); ExtractSummaryActionResult extractSummaryActionsResults = resultCollection.ExtractSummaryResults.FirstOrDefault(); RecognizeCustomEntitiesActionResult recognizeCustomEntitiesActionResults = resultCollection.RecognizeCustomEntitiesResults.FirstOrDefault(); SingleCategoryClassifyActionResult singleCategoryClassifyActionResult = resultCollection.SingleCategoryClassifyResults.FirstOrDefault(); MultiCategoryClassifyActionResult multiCategoryClassifyActionResult = resultCollection.MultiCategoryClassifyResults.FirstOrDefault(); Assert.IsTrue(entitiesActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => entitiesActionsResults.DocumentsResults.GetType()); Assert.IsTrue(keyPhrasesActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => keyPhrasesActionsResults.DocumentsResults.GetType()); Assert.IsTrue(piiActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => piiActionsResults.DocumentsResults.GetType()); Assert.IsTrue(entityLinkingActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => entityLinkingActionsResults.DocumentsResults.GetType()); Assert.IsTrue(analyzeSentimentActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => analyzeSentimentActionsResults.DocumentsResults.GetType()); Assert.IsTrue(extractSummaryActionsResults.HasError); Assert.Throws <InvalidOperationException>(() => extractSummaryActionsResults.DocumentsResults.GetType()); Assert.IsTrue(recognizeCustomEntitiesActionResults.HasError); Assert.Throws <InvalidOperationException>(() => recognizeCustomEntitiesActionResults.DocumentsResults.GetType()); Assert.IsTrue(singleCategoryClassifyActionResult.HasError); Assert.Throws <InvalidOperationException>(() => singleCategoryClassifyActionResult.DocumentsResults.GetType()); Assert.IsTrue(multiCategoryClassifyActionResult.HasError); Assert.Throws <InvalidOperationException>(() => multiCategoryClassifyActionResult.DocumentsResults.GetType()); }