-
Notifications
You must be signed in to change notification settings - Fork 13
feat(telemetry): report which host event and which params are triggered (SCAL-333657) #634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ecb5f18
feat(telemetry): report which host event and which params are triggered
sastaachar 73a1139
docs(telemetry): note that ui-passthrough can fall back internally too
sastaachar 64dcca4
fix(telemetry): report a timed-out UI passthrough setter as timed out
sastaachar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| import { | ||
| init, AuthType, LiveboardEmbed, HostEvent, EmbedErrorCodes, RuntimeFilterOp, | ||
| } from '../index'; | ||
| import { getDocumentBody, getRootEl } from '../test/test-utils'; | ||
| import { ERROR_MESSAGE } from '../errors'; | ||
| import { UIPassthroughEvent } from './hostEventClient/contracts'; | ||
| import { logger } from '../utils/logger'; | ||
| import * as authInstance from '../auth'; | ||
| import * as mixpanelInstance from '../mixpanel-service'; | ||
| import { MIXPANEL_EVENT } from '../mixpanel-service'; | ||
| import * as processTriggerInstance from '../utils/processTrigger'; | ||
|
|
||
| /** | ||
| * Returns the properties of the single `visual-sdk-host-event` upload. | ||
| * @param mock The spy on `uploadMixpanelEvent` | ||
| */ | ||
| const getHostEventProps = (mock: jest.SpyInstance) => { | ||
| const calls = mock.mock.calls.filter( | ||
| ([eventId]) => eventId === MIXPANEL_EVENT.VISUAL_SDK_HOST_EVENT, | ||
| ); | ||
| expect(calls).toHaveLength(1); | ||
| return calls[0][1] as Record<string, any>; | ||
| }; | ||
|
|
||
| /** | ||
| * Renders a Liveboard embed, so that `trigger` runs its normal path. | ||
| */ | ||
| const renderLiveboard = async () => { | ||
| init({ | ||
| thoughtSpotHost: 'https://tshost', | ||
| authType: AuthType.None, | ||
| }); | ||
| const embed = new LiveboardEmbed(getRootEl(), { | ||
| frameParams: { width: '100%', height: '100%' }, | ||
| liveboardId: '4c8a1b2e-0000-0000-0000-000000000001', | ||
| }); | ||
| await embed.render(); | ||
| return embed; | ||
| }; | ||
|
|
||
| describe('Host event telemetry', () => { | ||
| let mockUploadMixpanelEvent: jest.SpyInstance; | ||
| let mockProcessTrigger: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| document.body.innerHTML = getDocumentBody(); | ||
| jest.spyOn(authInstance, 'postLoginService').mockImplementation( | ||
| () => Promise.resolve(true as any), | ||
| ); | ||
| mockUploadMixpanelEvent = jest.spyOn(mixpanelInstance, 'uploadMixpanelEvent'); | ||
| mockProcessTrigger = jest | ||
| .spyOn(processTriggerInstance, 'processTrigger') | ||
| .mockResolvedValue({ session: 'ok' }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test('reports the host event, its parameters and a successful outcome', async () => { | ||
| const embed = await renderLiveboard(); | ||
| mockUploadMixpanelEvent.mockClear(); | ||
|
|
||
| await embed.trigger(HostEvent.DownloadAsCsv, { vizId: 'd0a1' }); | ||
|
|
||
| // The per-event upload keeps its name so existing Mixpanel reports | ||
| // still work, and now carries the same properties. | ||
| expect(mockUploadMixpanelEvent).toHaveBeenCalledWith( | ||
| `${MIXPANEL_EVENT.VISUAL_SDK_TRIGGER}-${HostEvent.DownloadAsCsv}`, | ||
| expect.objectContaining({ | ||
| hostEvent: HostEvent.DownloadAsCsv, | ||
| paramKeys: ['vizId'], | ||
| }), | ||
| ); | ||
|
|
||
| expect(getHostEventProps(mockUploadMixpanelEvent)).toEqual( | ||
| expect.objectContaining({ | ||
| hostEvent: HostEvent.DownloadAsCsv, | ||
| embedComponentType: 'LiveboardEmbed', | ||
| contextType: 'none', | ||
| hasPayload: true, | ||
| paramCount: 1, | ||
| paramKeys: ['vizId'], | ||
| paramShape: ['vizId:string'], | ||
| status: 'success', | ||
| route: 'legacy', | ||
| durationMs: expect.any(Number), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| test('reports parameter names and enum members, never customer values', async () => { | ||
| const embed = await renderLiveboard(); | ||
| mockUploadMixpanelEvent.mockClear(); | ||
|
|
||
| await embed.trigger(HostEvent.UpdateRuntimeFilters, [ | ||
| { columnName: 'Region', operator: RuntimeFilterOp.EQ, values: ['west'] }, | ||
| ]); | ||
|
|
||
| const serialized = JSON.stringify(mockUploadMixpanelEvent.mock.calls); | ||
| ['Region', 'west'].forEach((value) => expect(serialized).not.toContain(value)); | ||
|
|
||
| const props = getHostEventProps(mockUploadMixpanelEvent); | ||
| expect(props.paramKeys).toEqual(['columnName', 'operator', 'values']); | ||
| expect(props.paramShape).toEqual( | ||
| expect.arrayContaining([ | ||
| 'payload[].columnName:string', | ||
| 'payload[].operator:EQ', | ||
| 'payload[].values:array(1)', | ||
| ]), | ||
| ); | ||
| }); | ||
|
|
||
| test('reports a trigger that the embedded app never answered', async () => { | ||
| // processTrigger resolves, rather than rejects, when it times out. | ||
| mockProcessTrigger.mockResolvedValue(new Error(ERROR_MESSAGE.TRIGGER_TIMED_OUT)); | ||
| const embed = await renderLiveboard(); | ||
| mockUploadMixpanelEvent.mockClear(); | ||
|
|
||
| await embed.trigger(HostEvent.DownloadAsCsv, { vizId: 'd0a1' }); | ||
|
|
||
| expect(getHostEventProps(mockUploadMixpanelEvent).status).toBe('timed-out'); | ||
| }); | ||
|
|
||
| test('reports a failed trigger without its error message', async () => { | ||
| mockProcessTrigger.mockRejectedValue(new Error('Answer 4c8a1b2e not found')); | ||
| const embed = await renderLiveboard(); | ||
| mockUploadMixpanelEvent.mockClear(); | ||
|
|
||
| await expect(embed.trigger(HostEvent.DownloadAsCsv, { vizId: 'd0a1' })).rejects.toThrow(); | ||
|
|
||
| const props = getHostEventProps(mockUploadMixpanelEvent); | ||
| expect(props.status).toBe('error'); | ||
| expect(JSON.stringify(props)).not.toContain('4c8a1b2e'); | ||
| }); | ||
|
|
||
| /** | ||
| * Makes the embedded app answer UI passthrough calls, advertising the given | ||
| * passthrough keys. Everything else resolves over the legacy channel. | ||
| * @param keys The passthrough keys the app claims to support | ||
| * @param passthroughResult What a passthrough call other than the key | ||
| * lookup resolves with | ||
| */ | ||
| const mockPassthroughApp = (keys: string[], passthroughResult: any = [{ value: { ok: true } }]) => { | ||
| mockProcessTrigger.mockImplementation( | ||
| (_iFrame: any, messageType: any, _host: any, data: any) => { | ||
| if (messageType !== HostEvent.UIPassthrough) { | ||
| return Promise.resolve({ session: 'ok' }); | ||
| } | ||
| if (data?.type === UIPassthroughEvent.GetAvailableUIPassthroughs) { | ||
| return Promise.resolve([{ value: { keys } }]); | ||
| } | ||
| return Promise.resolve(passthroughResult); | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| test('reports the ui-passthrough route for a getter the app supports', async () => { | ||
| mockPassthroughApp([UIPassthroughEvent.GetTabs]); | ||
| const embed = await renderLiveboard(); | ||
| mockUploadMixpanelEvent.mockClear(); | ||
|
|
||
| await embed.trigger(HostEvent.GetTabs, {}); | ||
|
|
||
| expect(getHostEventProps(mockUploadMixpanelEvent)).toEqual( | ||
| expect.objectContaining({ hostEvent: HostEvent.GetTabs, route: 'ui-passthrough' }), | ||
| ); | ||
| }); | ||
|
|
||
| test('reports the legacy route when the app lacks the passthrough key', async () => { | ||
| mockPassthroughApp(['someUnrelatedPassthrough']); | ||
| const embed = await renderLiveboard(); | ||
| mockUploadMixpanelEvent.mockClear(); | ||
|
|
||
| await embed.trigger(HostEvent.GetTabs, {}); | ||
|
|
||
| expect(getHostEventProps(mockUploadMixpanelEvent)).toEqual( | ||
| expect.objectContaining({ route: 'legacy' }), | ||
| ); | ||
| }); | ||
|
|
||
| test('reports the custom-handler route for a setter with custom logic', async () => { | ||
| mockPassthroughApp([UIPassthroughEvent.PinAnswerToLiveboard]); | ||
| const embed = await renderLiveboard(); | ||
| mockUploadMixpanelEvent.mockClear(); | ||
|
|
||
| await embed.trigger(HostEvent.Pin, { | ||
| newVizName: 'Quarterly revenue', | ||
| liveboardId: '4c8a1b2e-0000-0000-0000-000000000002', | ||
| }); | ||
|
|
||
| expect(getHostEventProps(mockUploadMixpanelEvent)).toEqual( | ||
| expect.objectContaining({ | ||
| route: 'custom-handler', | ||
| paramKeys: ['liveboardId', 'newVizName'], | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| test('reports a custom-handler trigger that the app never answered as timed out', async () => { | ||
| // A UI passthrough setter turns the resolved timeout | ||
| // Error into a thrown "no answer", which used to be | ||
| // reported as a plain error and hid the timeout for | ||
| // Pin, SaveAnswer, UpdateFilters and DrillDown. | ||
| mockPassthroughApp( | ||
| [UIPassthroughEvent.PinAnswerToLiveboard], | ||
| new Error(ERROR_MESSAGE.TRIGGER_TIMED_OUT), | ||
| ); | ||
| const embed = await renderLiveboard(); | ||
| mockUploadMixpanelEvent.mockClear(); | ||
|
|
||
| await expect( | ||
| embed.trigger(HostEvent.Pin, { | ||
| newVizName: 'Quarterly revenue', | ||
| liveboardId: '4c8a1b2e-0000-0000-0000-000000000002', | ||
| }), | ||
| ).rejects.toBeDefined(); | ||
|
|
||
| expect(getHostEventProps(mockUploadMixpanelEvent).status).toBe('timed-out'); | ||
| }); | ||
|
|
||
| test('reports a trigger called before render', async () => { | ||
| jest.spyOn(logger, 'error').mockImplementation(() => undefined); | ||
| init({ | ||
| thoughtSpotHost: 'https://tshost', | ||
| authType: AuthType.None, | ||
| }); | ||
| const embed = new LiveboardEmbed(getRootEl(), { | ||
| frameParams: { width: '100%', height: '100%' }, | ||
| liveboardId: '4c8a1b2e-0000-0000-0000-000000000001', | ||
| }); | ||
| mockUploadMixpanelEvent.mockClear(); | ||
|
|
||
| await embed.trigger(HostEvent.DownloadAsCsv, { vizId: 'd0a1' }); | ||
|
|
||
| expect(getHostEventProps(mockUploadMixpanelEvent)).toEqual( | ||
| expect.objectContaining({ | ||
| status: 'render-not-called', | ||
| errorCode: EmbedErrorCodes.RENDER_NOT_CALLED, | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing test coverage: every test here drives
HostEvent.DownloadAsCsv, which always resolves toroute: 'legacy'. Nothing in this file (orhostEventTelemetry.spec.ts) exercises the'custom-handler'or'ui-passthrough'values ofHostEventRoute, even thoughonRouteinhost-event-client.tsis new code introduced by this PR with three distinct branches. Worth a case that triggers e.g.HostEvent.Pinor a getter event and assertsrouteon the reported props.