Skip to content

Commit 930bfa3

Browse files
authored
[Text Analytics] Fix a bug for returning stats for analyze (Azure#14143)
This PR fixes several issues: - when the user asks for statistics, they did not get it because the request parameter did not make it to the status endpoint. This PR fixes this issue by sending the `includeStatistics` parameter to the status endpoint - removes statistics from the top-level response because the service does not return it. See issue:Azure#14139 - the LRO's onProgress function was broken because the input callback was only applied when the LRO finished which defeats the purpose - the new core lib parameters (e.g. `onResponse`) were not being handled correctly - updates the samples with the includeStatistics parameter
1 parent 5d96175 commit 930bfa3

File tree

12 files changed

+1433
-65
lines changed

12 files changed

+1433
-65
lines changed

sdk/textanalytics/ai-text-analytics/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- `beginAnalyzeHealthcareEntities` entities now include `assertions` instead of `isNegated` which gives more context about the respective entity.
1010
- [Breaking] `beginAnalyzeHealthcareEntities` no longer returns `relatedEntities`.
1111
- `recognizePiiEntities` takes a new option, `categoriesFilter`, that specifies a list of Pii categories to return.
12+
- [Breaking] `statistics` is no longer part of `PagedAnalyzeBatchActionsResult`.
1213

1314
## 5.1.0-beta.4 (2021-02-10)
1415

sdk/textanalytics/ai-text-analytics/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ main();
500500

501501
## Known Issues
502502

503-
- Currently, the `beginAnalyze` API accepts `includeStatistics` in its options bag, a feature that was not yet supported by the service at the time of the current release. This feature is expected to be supported soon after the release.
504503
- `beginAnalyzeHealthcare` is still in gated preview and can not be used with AAD credentials. For more information, see (the Text Analytics for Health documentation)[https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/how-tos/text-analytics-for-health?tabs=ner#request-access-to-the-public-preview].
505504

506505
## Troubleshooting

sdk/textanalytics/ai-text-analytics/recordings/browsers/aad_textanalyticsclient_lros_analyze/recording_statistics.json

Lines changed: 708 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/textanalytics/ai-text-analytics/recordings/node/aad_textanalyticsclient_lros_analyze/recording_statistics.js

Lines changed: 597 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/textanalytics/ai-text-analytics/review/ai-text-analytics.api.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ export interface Opinion {
318318

319319
// @public
320320
export interface PagedAnalyzeBatchActionsResult extends PagedAsyncIterableAnalyzeBatchActionsResult {
321-
statistics?: TextDocumentBatchStatistics;
322321
}
323322

324323
// @public

sdk/textanalytics/ai-text-analytics/samples/javascript/beginAnalyzeBatchActions.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,22 @@ async function main() {
3333
recognizePiiEntitiesActions: [{ modelVersion: "latest" }],
3434
extractKeyPhrasesActions: [{ modelVersion: "latest" }]
3535
};
36-
const poller = await client.beginAnalyzeBatchActions(documents, actions);
36+
const poller = await client.beginAnalyzeBatchActions(documents, actions, "en", {
37+
includeStatistics: true
38+
});
39+
poller.onProgress(() => {
40+
console.log(
41+
`Number of actions still in progress: ${poller.getOperationState().actionsInProgressCount}`
42+
);
43+
});
44+
console.log(
45+
`The analyze batch actions operation was created on ${poller.getOperationState().createdOn}`
46+
);
47+
console.log(
48+
`The analyze batch actions operation results will expire on ${
49+
poller.getOperationState().expiresOn
50+
}`
51+
);
3752
const resultPages = await poller.pollUntilDone();
3853
for await (const page of resultPages) {
3954
const keyPhrasesAction = page.extractKeyPhrasesResults[0];
@@ -49,6 +64,8 @@ async function main() {
4964
console.error("\tError:", doc.error);
5065
}
5166
}
67+
console.log("Action statistics: ");
68+
console.log(JSON.stringify(keyPhrasesAction.results.statistics));
5269
}
5370

5471
const entitiesAction = page.recognizeEntitiesResults[0];
@@ -64,6 +81,8 @@ async function main() {
6481
console.error("\tError:", doc.error);
6582
}
6683
}
84+
console.log("Action statistics: ");
85+
console.log(JSON.stringify(entitiesAction.results.statistics));
6786
}
6887

6988
const piiEntitiesAction = page.recognizePiiEntitiesResults[0];
@@ -79,6 +98,8 @@ async function main() {
7998
console.error("\tError:", doc.error);
8099
}
81100
}
101+
console.log("Action statistics: ");
102+
console.log(JSON.stringify(piiEntitiesAction.results.statistics));
82103
}
83104
}
84105
}

sdk/textanalytics/ai-text-analytics/samples/typescript/src/beginAnalyzeBatchActions.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ export async function main() {
3333
recognizePiiEntitiesActions: [{ modelVersion: "latest" }],
3434
extractKeyPhrasesActions: [{ modelVersion: "latest" }]
3535
};
36-
const poller = await client.beginAnalyzeBatchActions(documents, actions);
36+
const poller = await client.beginAnalyzeBatchActions(documents, actions, "en", {
37+
includeStatistics: true
38+
});
3739
const resultPages = await poller.pollUntilDone();
40+
poller.onProgress(() => {
41+
console.log(
42+
`Number of actions still in progress: ${poller.getOperationState().actionsInProgressCount}`
43+
);
44+
});
3845
console.log(
39-
`The analyze batch actions operation created on ${
40-
poller.getOperationState().createdOn
41-
} finished process`
46+
`The analyze batch actions operation created on ${poller.getOperationState().createdOn}`
4247
);
4348
console.log(
4449
`The analyze batch actions operation results will expire on ${
@@ -60,6 +65,8 @@ export async function main() {
6065
console.error("\tError:", doc.error);
6166
}
6267
}
68+
console.log("Action statistics: ");
69+
console.log(JSON.stringify(keyPhrasesAction.results.statistics));
6370
}
6471

6572
const entitiesAction = page.recognizeEntitiesResults[0];
@@ -75,6 +82,8 @@ export async function main() {
7582
console.error("\tError:", doc.error);
7683
}
7784
}
85+
console.log("Action statistics: ");
86+
console.log(JSON.stringify(entitiesAction.results.statistics));
7887
}
7988

8089
const piiEntitiesAction = page.recognizePiiEntitiesResults[0];
@@ -90,6 +99,8 @@ export async function main() {
9099
console.error("\tError:", doc.error);
91100
}
92101
}
102+
console.log("Action statistics: ");
103+
console.log(JSON.stringify(piiEntitiesAction.results.statistics));
93104
}
94105
}
95106
}

sdk/textanalytics/ai-text-analytics/src/analyzeBatchActionsResult.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
TasksStateTasksEntityRecognitionPiiTasksItem,
1313
TasksStateTasksEntityRecognitionTasksItem,
1414
TasksStateTasksKeyPhraseExtractionTasksItem,
15-
TextDocumentBatchStatistics,
1615
TextDocumentInput
1716
} from "./generated/models";
1817
import {
@@ -184,12 +183,12 @@ export type PagedAsyncIterableAnalyzeBatchActionsResult = PagedAsyncIterableIter
184183
*/
185184
export interface PagedAnalyzeBatchActionsResult
186185
extends PagedAsyncIterableAnalyzeBatchActionsResult {
187-
/**
188-
* Statistics about the input document batch and how it was processed
189-
* by the service. This property will have a value when includeStatistics is set to true
190-
* in the client call.
191-
*/
192-
statistics?: TextDocumentBatchStatistics;
186+
// /**
187+
// * Statistics about the input document batch and how it was processed
188+
// * by the service. This property will have a value when includeStatistics is set to true
189+
// * in the client call.
190+
// */
191+
// statistics?: TextDocumentBatchStatistics;
193192
}
194193

195194
/**

sdk/textanalytics/ai-text-analytics/src/lro/analyze/operation.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { OperationOptions } from "@azure/core-client";
66
import { AbortSignalLike } from "@azure/abort-controller";
77

88
import {
9+
AnalyzeJobState,
910
GeneratedClientAnalyzeResponse as BeginAnalyzeResponse,
1011
GeneratedClientAnalyzeStatusOptionalParams as AnalyzeBatchActionsOperationStatusOptions,
1112
JobManifestTasks as GeneratedActions,
@@ -101,6 +102,21 @@ export interface AnalyzeBatchActionsOperationState
101102
extends AnalysisPollOperationState<PagedAnalyzeBatchActionsResult>,
102103
AnalyzeBatchActionsOperationMetadata {}
103104

105+
/**
106+
* @internal
107+
*/
108+
function getMetaInfoFromResponse(response: AnalyzeJobState): AnalyzeBatchActionsOperationMetadata {
109+
return {
110+
createdOn: response.createdDateTime,
111+
lastModifiedOn: response.lastUpdateDateTime,
112+
expiresOn: response.expirationDateTime,
113+
status: response.status,
114+
actionsSucceededCount: response.tasks.completed,
115+
actionsFailedCount: response.tasks.failed,
116+
actionsInProgressCount: response.tasks.inProgress
117+
};
118+
}
119+
104120
/**
105121
* Class that represents a poller that waits for results of the analyze
106122
* operation.
@@ -218,19 +234,11 @@ export class BeginAnalyzeBatchActionsPollerOperation extends AnalysisPollOperati
218234
return {
219235
done: true,
220236
statistics: response.statistics,
221-
operationMetdata: {
222-
createdOn: response.createdDateTime,
223-
lastModifiedOn: response.lastUpdateDateTime,
224-
expiresOn: response.expirationDateTime,
225-
status: response.status,
226-
actionsSucceededCount: response.tasks.completed,
227-
actionsFailedCount: response.tasks.failed,
228-
actionsInProgressCount: response.tasks.inProgress
229-
}
237+
operationMetdata: getMetaInfoFromResponse(response)
230238
};
231239
}
232240
}
233-
return { done: false };
241+
return { done: false, operationMetdata: getMetaInfoFromResponse(response) };
234242
} catch (e) {
235243
span.setStatus({
236244
code: CanonicalCode.UNKNOWN,
@@ -311,18 +319,25 @@ export class BeginAnalyzeBatchActionsPollerOperation extends AnalysisPollOperati
311319
state.actionsInProgressCount = operationStatus.operationMetdata?.actionsInProgressCount;
312320

313321
if (!state.isCompleted && operationStatus.done) {
314-
if (typeof options.fireProgress === "function") {
315-
options.fireProgress(state);
316-
}
317322
const pagedIterator = this.listAnalyzeBatchActionsResults(state.operationId!, {
318323
abortSignal: this.options.abortSignal,
319-
tracingOptions: this.options.tracingOptions
320-
});
321-
state.result = Object.assign(pagedIterator, {
322-
statistics: operationStatus.statistics
324+
tracingOptions: this.options.tracingOptions,
325+
includeStatistics: this.options.includeStatistics,
326+
onResponse: this.options.onResponse,
327+
serializerOptions: this.options.serializerOptions
323328
});
329+
// Attach stats if the service starts to return them
330+
// https://github.com/Azure/azure-sdk-for-js/issues/14139
331+
// state.result = Object.assign(pagedIterator, {
332+
// statistics: operationStatus.statistics
333+
// });
334+
state.result = pagedIterator;
324335
state.isCompleted = true;
325336
}
337+
338+
if (typeof options.fireProgress === "function") {
339+
options.fireProgress(state);
340+
}
326341
return this;
327342
}
328343

sdk/textanalytics/ai-text-analytics/src/textAnalyticsClient.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { textAnalyticsAzureKeyCredentialPolicy } from "./azureKeyCredentialPolic
4848
import {
4949
AddParamsToTask,
5050
addStrEncodingParam,
51+
compose,
5152
handleInvalidDocumentBatch,
5253
setModelVersionParam,
5354
setStrEncodingParam,
@@ -904,7 +905,9 @@ export class TextAnalyticsClient {
904905
analysisOptions: {
905906
requestOptions: realOptions.requestOptions,
906907
tracingOptions: realOptions.tracingOptions,
907-
abortSignal: realOptions.abortSignal
908+
abortSignal: realOptions.abortSignal,
909+
onResponse: realOptions.onResponse,
910+
serializerOptions: realOptions.serializerOptions
908911
},
909912
updateIntervalInMs: realOptions.updateIntervalInMs,
910913
resumeFrom: realOptions.resumeFrom,
@@ -974,7 +977,9 @@ export class TextAnalyticsClient {
974977
analysisOptions: {
975978
requestOptions: realOptions.requestOptions,
976979
tracingOptions: realOptions.tracingOptions,
977-
abortSignal: realOptions.abortSignal
980+
abortSignal: realOptions.abortSignal,
981+
onResponse: realOptions.onResponse,
982+
serializerOptions: realOptions.serializerOptions
978983
},
979984
includeStatistics: realOptions.includeStatistics,
980985
updateIntervalInMs: realOptions.updateIntervalInMs,
@@ -986,13 +991,6 @@ export class TextAnalyticsClient {
986991
}
987992
}
988993

989-
/**
990-
* @internal
991-
*/
992-
function compose<T1, T2, T3>(fn1: (x: T1) => T2, fn2: (y: T2) => T3): (x: T1) => T3 {
993-
return (value: T1) => fn2(fn1(value));
994-
}
995-
996994
/**
997995
* @internal
998996
*/
@@ -1068,7 +1066,9 @@ function makeAnalyzeSentimentOptionsModel(
10681066
modelVersion: params.modelVersion,
10691067
requestOptions: params.requestOptions,
10701068
stringIndexType: params.stringIndexType,
1071-
tracingOptions: params.tracingOptions
1069+
tracingOptions: params.tracingOptions,
1070+
onResponse: params.onResponse,
1071+
serializerOptions: params.serializerOptions
10721072
};
10731073
}
10741074

@@ -1088,6 +1088,8 @@ function makePiiEntitiesOptionsModel(
10881088
requestOptions: params.requestOptions,
10891089
stringIndexType: params.stringIndexType,
10901090
tracingOptions: params.tracingOptions,
1091-
piiCategories: params.categoriesFilter
1091+
piiCategories: params.categoriesFilter,
1092+
onResponse: params.onResponse,
1093+
serializerOptions: params.serializerOptions
10921094
};
10931095
}

0 commit comments

Comments
 (0)