-
-
Notifications
You must be signed in to change notification settings - Fork 734
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
feat: onboarding store #8027
Merged
Merged
feat: onboarding store #8027
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2213b01
feat: onboarding store
kwasniew bdad753
feat: onboarding store
kwasniew 4e55255
feat: onboarding store
kwasniew a6831ce
feat: onboarding store
kwasniew 2af57f0
feat: onboarding store
kwasniew f7005f5
feat: onboarding store
kwasniew b320116
feat: onboarding store
kwasniew 5a2f441
feat: onboarding store
kwasniew 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 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,10 @@ | ||
import type { | ||
IOnboardingStore, | ||
OnboardingEvent, | ||
} from './onboarding-store-type'; | ||
|
||
export class FakeOnboardingStore implements IOnboardingStore { | ||
async insert(event: OnboardingEvent): Promise<void> { | ||
return; | ||
} | ||
} |
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,63 @@ | ||
import type { IFlagResolver, IUnleashConfig } from '../../types'; | ||
import type EventEmitter from 'events'; | ||
import type { Logger } from '../../logger'; | ||
import { STAGE_ENTERED, USER_LOGIN } from '../../metric-events'; | ||
import type { NewStage } from '../feature-lifecycle/feature-lifecycle-store-type'; | ||
import type { IOnboardingStore } from './onboarding-store-type'; | ||
|
||
export class OnboardingService { | ||
private flagResolver: IFlagResolver; | ||
|
||
private eventBus: EventEmitter; | ||
|
||
private logger: Logger; | ||
|
||
private onboardingStore: IOnboardingStore; | ||
|
||
constructor( | ||
{ onboardingStore }: { onboardingStore: IOnboardingStore }, | ||
{ | ||
flagResolver, | ||
eventBus, | ||
getLogger, | ||
}: Pick<IUnleashConfig, 'flagResolver' | 'eventBus' | 'getLogger'>, | ||
) { | ||
this.onboardingStore = onboardingStore; | ||
this.flagResolver = flagResolver; | ||
this.eventBus = eventBus; | ||
this.logger = getLogger('onboarding/onboarding-service.ts'); | ||
} | ||
|
||
listen() { | ||
this.eventBus.on(USER_LOGIN, async (event: { loginOrder: number }) => { | ||
if (!this.flagResolver.isEnabled('onboardingMetrics')) return; | ||
|
||
if (event.loginOrder === 0) { | ||
await this.onboardingStore.insert({ type: 'firstUserLogin' }); | ||
} | ||
if (event.loginOrder === 1) { | ||
await this.onboardingStore.insert({ type: 'secondUserLogin' }); | ||
} | ||
}); | ||
this.eventBus.on(STAGE_ENTERED, async (stage: NewStage) => { | ||
if (!this.flagResolver.isEnabled('onboardingMetrics')) return; | ||
|
||
if (stage.stage === 'initial') { | ||
await this.onboardingStore.insert({ | ||
type: 'flagCreated', | ||
flag: stage.feature, | ||
}); | ||
} else if (stage.stage === 'pre-live') { | ||
await this.onboardingStore.insert({ | ||
type: 'preLive', | ||
flag: stage.feature, | ||
}); | ||
} else if (stage.stage === 'live') { | ||
await this.onboardingStore.insert({ | ||
type: 'live', | ||
flag: stage.feature, | ||
}); | ||
} | ||
}); | ||
} | ||
} |
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,12 @@ | ||
export type SharedEvent = | ||
| { type: 'flagCreated'; flag: string } | ||
| { type: 'preLive'; flag: string } | ||
| { type: 'live'; flag: string }; | ||
export type InstanceEvent = | ||
| { type: 'firstUserLogin' } | ||
| { type: 'secondUserLogin' }; | ||
export type OnboardingEvent = SharedEvent | InstanceEvent; | ||
|
||
export interface IOnboardingStore { | ||
insert(event: OnboardingEvent): Promise<void>; | ||
} |
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,72 @@ | ||
import type { IUnleashStores } from '../../../lib/types'; | ||
import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init'; | ||
import getLogger from '../../../test/fixtures/no-logger'; | ||
import { minutesToMilliseconds } from 'date-fns'; | ||
|
||
let db: ITestDb; | ||
let stores: IUnleashStores; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('onboarding_store', getLogger); | ||
stores = db.stores; | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.destroy(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
test('Storing onboarding events', async () => { | ||
jest.useFakeTimers(); | ||
jest.setSystemTime(new Date()); | ||
const { userStore, onboardingStore, featureToggleStore, projectStore } = | ||
stores; | ||
const user = await userStore.insert({}); | ||
await projectStore.create({ id: 'test_project', name: 'irrelevant' }); | ||
await featureToggleStore.create('test_project', { | ||
name: 'test', | ||
createdByUserId: user.id, | ||
}); | ||
|
||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingStore.insert({ type: 'firstUserLogin' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingStore.insert({ type: 'secondUserLogin' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingStore.insert({ type: 'flagCreated', flag: 'test' }); | ||
await onboardingStore.insert({ type: 'flagCreated', flag: 'test' }); | ||
await onboardingStore.insert({ type: 'flagCreated', flag: 'invalid' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingStore.insert({ type: 'preLive', flag: 'test' }); | ||
await onboardingStore.insert({ type: 'preLive', flag: 'test' }); | ||
await onboardingStore.insert({ type: 'preLive', flag: 'invalid' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingStore.insert({ type: 'live', flag: 'test' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingStore.insert({ type: 'live', flag: 'test' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingStore.insert({ type: 'live', flag: 'invalid' }); | ||
|
||
const { rows: instanceEvents } = await db.rawDatabase.raw( | ||
'SELECT * FROM onboarding_events_instance', | ||
); | ||
expect(instanceEvents).toMatchObject([ | ||
{ event: 'firstUserLogin', time_to_event: 60 }, | ||
{ event: 'secondUserLogin', time_to_event: 120 }, | ||
{ event: 'firstFlag', time_to_event: 180 }, | ||
{ event: 'firstPreLive', time_to_event: 240 }, | ||
{ event: 'firstLive', time_to_event: 300 }, | ||
]); | ||
|
||
const { rows: projectEvents } = await db.rawDatabase.raw( | ||
'SELECT * FROM onboarding_events_project', | ||
); | ||
expect(projectEvents).toMatchObject([ | ||
{ event: 'firstFlag', time_to_event: 180, project: 'test_project' }, | ||
{ event: 'firstPreLive', time_to_event: 240, project: 'test_project' }, | ||
{ event: 'firstLive', time_to_event: 300, project: 'test_project' }, | ||
]); | ||
}); |
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,136 @@ | ||
import type { Db } from '../../db/db'; | ||
import type { | ||
IOnboardingStore, | ||
OnboardingEvent, | ||
SharedEvent, | ||
} from './onboarding-store-type'; | ||
import { millisecondsToSeconds } from 'date-fns'; | ||
|
||
type DBInstanceType = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sticking to dashed convention as in lifecycle events instead of camel casing |
||
event: | ||
| 'firstUserLogin' | ||
| 'secondUserLogin' | ||
| 'firstFlag' | ||
| 'firstPreLive' | ||
| 'firstLive'; | ||
time_to_event: number; | ||
}; | ||
|
||
type DBProjectType = { | ||
event: 'firstFlag' | 'firstPreLive' | 'firstLive'; | ||
time_to_event: number; | ||
project: string; | ||
}; | ||
|
||
const translateEvent = ( | ||
event: OnboardingEvent['type'], | ||
): DBInstanceType['event'] => { | ||
if (event === 'flagCreated') { | ||
return 'firstFlag'; | ||
} | ||
if (event === 'preLive') { | ||
return 'firstPreLive'; | ||
} | ||
if (event === 'live') { | ||
return 'firstLive'; | ||
} | ||
return event as DBInstanceType['event']; | ||
}; | ||
|
||
const calculateTimeDifferenceInSeconds = (date1?: Date, date2?: Date) => { | ||
if (date1 && date2) { | ||
const diffInMilliseconds = date2.getTime() - date1.getTime(); | ||
return millisecondsToSeconds(diffInMilliseconds); | ||
} | ||
return null; | ||
}; | ||
|
||
export class OnboardingStore implements IOnboardingStore { | ||
private db: Db; | ||
|
||
constructor(db: Db) { | ||
this.db = db; | ||
} | ||
|
||
async insert(event: OnboardingEvent): Promise<void> { | ||
await this.insertInstanceEvent(event); | ||
if ( | ||
event.type !== 'firstUserLogin' && | ||
event.type !== 'secondUserLogin' | ||
) { | ||
await this.insertProjectEvent(event); | ||
} | ||
} | ||
|
||
private async insertInstanceEvent(event: OnboardingEvent): Promise<void> { | ||
const dbEvent = translateEvent(event.type); | ||
const exists = await this.db<DBInstanceType>( | ||
'onboarding_events_instance', | ||
) | ||
.where({ event: dbEvent }) | ||
.first(); | ||
|
||
if (exists) return; | ||
|
||
const firstInstanceUser = await this.db('users') | ||
.select('created_at') | ||
.orderBy('created_at', 'asc') | ||
.first(); | ||
|
||
if (!firstInstanceUser) return; | ||
|
||
const timeToEvent = calculateTimeDifferenceInSeconds( | ||
firstInstanceUser.created_at, | ||
new Date(), | ||
); | ||
if (timeToEvent === null) return; | ||
|
||
await this.db('onboarding_events_instance').insert({ | ||
event: dbEvent, | ||
time_to_event: timeToEvent, | ||
}); | ||
} | ||
|
||
private async insertProjectEvent(event: SharedEvent): Promise<void> { | ||
const dbEvent = translateEvent(event.type) as DBProjectType['event']; | ||
|
||
const projectRow = await this.db<{ project: string; name: string }>( | ||
'features', | ||
) | ||
.select('project') | ||
.where({ name: event.flag }) | ||
.first(); | ||
|
||
if (!projectRow) return; | ||
|
||
const project = projectRow.project; | ||
|
||
const exists = await this.db('onboarding_events_project') | ||
.where({ event: dbEvent, project }) | ||
.first(); | ||
|
||
if (exists) return; | ||
|
||
const projectCreatedAt = await this.db<{ | ||
created_at: Date; | ||
id: string; | ||
}>('projects') | ||
.select('created_at') | ||
.where({ id: project }) | ||
.first(); | ||
|
||
if (!projectCreatedAt) return; | ||
|
||
const timeToEvent = calculateTimeDifferenceInSeconds( | ||
projectCreatedAt.created_at, | ||
new Date(), | ||
); | ||
if (timeToEvent === null) return; | ||
|
||
await this.db<DBProjectType>('onboarding_events_project').insert({ | ||
event: dbEvent, | ||
time_to_event: timeToEvent, | ||
project: project, | ||
}); | ||
} | ||
} |
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
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.
we do this translation since there's not 100% domain vocab match between lifecycle and onboarding. lifecycle cares about initial flag, while onboarding cares about the first flag created