Skip to content

Commit

Permalink
port: [#4163] log multi-choice options in App Insights (#4164)
Browse files Browse the repository at this point in the history
* bug fix: log multi-choice options in App Insights

* unit test
  • Loading branch information
breakingram authored Mar 24, 2022
1 parent d079cc7 commit a700088
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ export class ChoiceInput extends InputDialog implements ChoiceInputConfiguration
outputFormat: EnumExpression<ChoiceOutputFormat>;
recognizerOptions?: ObjectExpression<FindChoicesOptions>;
style: EnumExpression<ListStyle>;
protected trackGeneratorResultEvent(dc: DialogContext, activityTemplate: TemplateInterface<Partial<Activity>, DialogStateManager>, msg: Partial<Activity>): void;
}

// @public (undocumented)
Expand Down Expand Up @@ -1275,6 +1276,7 @@ export abstract class InputDialog extends Dialog implements InputDialogConfigura
prompt: TemplateInterface<Partial<Activity>, DialogStateManager>;
property: StringExpression;
resumeDialog(dc: DialogContext, _reason: DialogReason, _result?: any): Promise<DialogTurnResult>;
protected trackGeneratorResultEvent(dc: DialogContext, activityTemplate: TemplateInterface<Partial<Activity>, DialogStateManager>, msg: Partial<Activity>): void;
// (undocumented)
static TURN_COUNT_PROPERTY: string;
unrecognizedPrompt: TemplateInterface<Partial<Activity>, DialogStateManager>;
Expand Down
25 changes: 25 additions & 0 deletions libraries/botbuilder-dialogs-adaptive/src/input/choiceInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import {
Converter,
ConverterFactory,
DialogContext,
DialogStateManager,
FindChoicesOptions,
ListStyle,
PromptCultureModels,
recognizeChoices,
TemplateInterface,
} from 'botbuilder-dialogs';
import { TelemetryLoggerConstants } from '../telemetryLoggerConstants';

export enum ChoiceOutputFormat {
value = 'value',
Expand Down Expand Up @@ -129,6 +132,28 @@ export class ChoiceInput extends InputDialog implements ChoiceInputConfiguration
}
}

/**
* {@inheritDoc InputDialog.trackGeneratorResultEvent}
*/
protected trackGeneratorResultEvent(
dc: DialogContext,
activityTemplate: TemplateInterface<Partial<Activity>, DialogStateManager>,
msg: Partial<Activity>
): void {
const options = dc.state.getValue(ChoiceInput.OPTIONS_PROPERTY);
const properties = {
template: activityTemplate,
result: msg,
choices: options?.choices ? options.choices : '',
context: TelemetryLoggerConstants.InputDialogResultEvent,
};

this.telemetryClient.trackEvent({
name: TelemetryLoggerConstants.GeneratorResultEvent,
properties: properties,
});
}

/**
* @protected
* Method which processes options.
Expand Down
21 changes: 18 additions & 3 deletions libraries/botbuilder-dialogs-adaptive/src/input/inputDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,31 @@ export abstract class InputDialog extends Dialog implements InputDialogConfigura
msg.inputHint = InputHints.ExpectingInput;
}

this.trackGeneratorResultEvent(dc, template, msg);

return msg;
}

/**
* @protected
* Track GeneratorResultEvent telemetry event with InputDialogResultEvent context.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param activityTemplate used to create the Activity.
* @param msg The Partial [Activity](xref:botframework-schema.Activity) which will be sent.
*/
protected trackGeneratorResultEvent(
dc: DialogContext,
activityTemplate: TemplateInterface<Partial<Activity>, DialogStateManager>,
msg: Partial<Activity>
): void {
this.telemetryClient.trackEvent({
name: TelemetryLoggerConstants.GeneratorResultEvent,
properties: {
template: template,
template: activityTemplate,
result: msg,
context: TelemetryLoggerConstants.InputDialogResultEvent,
},
});

return msg;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { ok, strictEqual } = require('assert');
const { createTelemetryClientAndStub } = require('./telemetryUtils');
const { ConversationState, MemoryStorage, TestAdapter } = require('botbuilder');
const { DialogManager, DialogSet, ChoicePrompt, ListStyle } = require('botbuilder-dialogs');
const { ChoiceInput, TelemetryLoggerConstants, ActivityTemplate, ChoiceSet } = require('../lib');
const { ObjectExpression, BoolExpression } = require('adaptive-expressions');

describe('ChoiceInput multi-choices properties', function () {
this.timeout(3000);
let telemetryName;
let telemetryProperties;
const captureTelemetryAction = (eventData) => {
telemetryName = eventData.name;
telemetryProperties = eventData.properties;
};
// Create telemetryClient and trackEventStub
const [telemetryClient, trackEventStub] = createTelemetryClientAndStub(captureTelemetryAction);

// Setup dialog manager
const storage = new MemoryStorage();
const conversationState = new ConversationState(storage);
const dialogState = conversationState.createProperty('dialog');
const dialogs = new DialogSet(dialogState);
const choicePrompt = new ChoicePrompt('prompt');
choicePrompt.style = ListStyle.none;
dialogs.add(choicePrompt);
const dm = new DialogManager();
dm.conversationState = conversationState;

// Setup inputDialog dialog
const dialog = new ChoiceInput();
dialog.prompt = new ActivityTemplate('testTemplate');
dialog.alwaysPrompt = true;

// set up prompt choices
const choiceSet = new ChoiceSet([{ value: 'test1' }, { value: 'test2' }, { value: 'test3' }]);
dialog.choices = new ObjectExpression(choiceSet);
dialog.alwaysPrompt = new BoolExpression(true);
dialog._telemetryClient = telemetryClient;
dm.rootDialog = dialog;

it('Log ChoiceInput choices properties', async function () {
// Send initial activity
const adapter = new TestAdapter(async (turnContext) => {
await dm.onTurn(turnContext);

// assert telemetry result
strictEqual(telemetryName, TelemetryLoggerConstants.GeneratorResultEvent);
strictEqual(telemetryProperties.choices, choiceSet);
ok(trackEventStub.calledOnce);
});
await adapter.send('test').sendConversationUpdate().startTest();
});
});

0 comments on commit a700088

Please sign in to comment.