-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20084 from storybookjs/shilman/ugrade-success-tel…
…emetry Telemetry: Add precedingUpgrade data to dev/build events
- Loading branch information
Showing
6 changed files
with
215 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,146 @@ | ||
import { getPrecedingUpgrade } from './event-cache'; | ||
|
||
expect.addSnapshotSerializer({ | ||
print: (val: any) => JSON.stringify(val, null, 2), | ||
test: (val) => typeof val !== 'string', | ||
}); | ||
|
||
describe('event-cache', () => { | ||
const init = { body: { eventType: 'init', eventId: 'init' }, timestamp: 1 }; | ||
const upgrade = { body: { eventType: 'upgrade', eventId: 'upgrade' }, timestamp: 2 }; | ||
const dev = { body: { eventType: 'dev', eventId: 'dev' }, timestamp: 3 }; | ||
|
||
describe('data handling', () => { | ||
it('errors', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { | ||
init: { timestamp: 1, body: { ...init.body, error: {} } }, | ||
}); | ||
expect(preceding).toMatchInlineSnapshot(` | ||
{ | ||
"timestamp": 1, | ||
"eventType": "init", | ||
"eventId": "init" | ||
} | ||
`); | ||
}); | ||
|
||
it('session IDs', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { | ||
init: { timestamp: 1, body: { ...init.body, sessionId: 100 } }, | ||
}); | ||
expect(preceding).toMatchInlineSnapshot(` | ||
{ | ||
"timestamp": 1, | ||
"eventType": "init", | ||
"eventId": "init", | ||
"sessionId": 100 | ||
} | ||
`); | ||
}); | ||
|
||
it('extra fields', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { | ||
init: { timestamp: 1, body: { ...init.body, foobar: 'baz' } }, | ||
}); | ||
expect(preceding).toMatchInlineSnapshot(` | ||
{ | ||
"timestamp": 1, | ||
"eventType": "init", | ||
"eventId": "init" | ||
} | ||
`); | ||
}); | ||
}); | ||
|
||
describe('no intervening dev events', () => { | ||
it('no upgrade events', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', {}); | ||
expect(preceding).toBeUndefined(); | ||
}); | ||
|
||
it('init', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { init }); | ||
expect(preceding).toMatchInlineSnapshot(` | ||
{ | ||
"timestamp": 1, | ||
"eventType": "init", | ||
"eventId": "init" | ||
} | ||
`); | ||
}); | ||
|
||
it('upgrade', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { upgrade }); | ||
expect(preceding).toMatchInlineSnapshot(` | ||
{ | ||
"timestamp": 2, | ||
"eventType": "upgrade", | ||
"eventId": "upgrade" | ||
} | ||
`); | ||
}); | ||
|
||
it('both init and upgrade', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { init, upgrade }); | ||
expect(preceding).toMatchInlineSnapshot(` | ||
{ | ||
"timestamp": 2, | ||
"eventType": "upgrade", | ||
"eventId": "upgrade" | ||
} | ||
`); | ||
}); | ||
}); | ||
|
||
describe('intervening dev events', () => { | ||
it('no upgrade events', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { dev }); | ||
expect(preceding).toBeUndefined(); | ||
}); | ||
|
||
it('init', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { init, dev }); | ||
expect(preceding).toBeUndefined(); | ||
}); | ||
|
||
it('upgrade', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { upgrade, dev }); | ||
expect(preceding).toBeUndefined(); | ||
}); | ||
|
||
it('init followed by upgrade', async () => { | ||
const preceding = await getPrecedingUpgrade('dev', { init, upgrade, dev }); | ||
expect(preceding).toBeUndefined(); | ||
}); | ||
|
||
it('both init and upgrade with intervening dev', async () => { | ||
const secondUpgrade = { | ||
body: { eventType: 'upgrade', eventId: 'secondUpgrade' }, | ||
timestamp: 4, | ||
}; | ||
const preceding = await getPrecedingUpgrade('dev', { init, dev, upgrade: secondUpgrade }); | ||
expect(preceding).toMatchInlineSnapshot(` | ||
{ | ||
"timestamp": 4, | ||
"eventType": "upgrade", | ||
"eventId": "secondUpgrade" | ||
} | ||
`); | ||
}); | ||
|
||
it('both init and upgrade with non-intervening dev', async () => { | ||
const earlyDev = { | ||
body: { eventType: 'dev', eventId: 'earlyDev' }, | ||
timestamp: -1, | ||
}; | ||
const preceding = await getPrecedingUpgrade('dev', { dev: earlyDev, init, upgrade }); | ||
expect(preceding).toMatchInlineSnapshot(` | ||
{ | ||
"timestamp": 2, | ||
"eventType": "upgrade", | ||
"eventId": "upgrade" | ||
} | ||
`); | ||
}); | ||
}); | ||
}); |
This file contains 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,39 @@ | ||
import { cache } from '@storybook/core-common'; | ||
import type { EventType } from './types'; | ||
|
||
export const set = async (eventType: EventType, body: any) => { | ||
const lastEvents = (await cache.get('lastEvents')) || {}; | ||
lastEvents[eventType] = { body, timestamp: Date.now() }; | ||
await cache.set('lastEvents', lastEvents); | ||
}; | ||
|
||
export const get = async (eventType: EventType) => { | ||
const lastEvents = await cache.get('lastEvents'); | ||
return lastEvents?.[eventType]; | ||
}; | ||
|
||
const upgradeFields = (event: any) => { | ||
const { body, timestamp } = event; | ||
return { | ||
timestamp, | ||
eventType: body?.eventType, | ||
eventId: body?.eventId, | ||
sessionId: body?.sessionId, | ||
}; | ||
}; | ||
|
||
export const getPrecedingUpgrade = async (eventType: EventType, events: any = undefined) => { | ||
const lastEvents = events || (await cache.get('lastEvents')); | ||
const init = lastEvents?.init; | ||
let precedingUpgrade = init; | ||
const upgrade = lastEvents?.upgrade; | ||
if (upgrade && (!precedingUpgrade || upgrade.timestamp > precedingUpgrade?.timestamp)) { | ||
precedingUpgrade = upgrade; | ||
} | ||
if (!precedingUpgrade) return undefined; | ||
|
||
const lastEventOfType = lastEvents?.[eventType]; | ||
return !lastEventOfType?.timestamp || precedingUpgrade.timestamp > lastEventOfType.timestamp | ||
? upgradeFields(precedingUpgrade) | ||
: undefined; | ||
}; |
This file contains 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
This file contains 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