-
-
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 all 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
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,14 @@ | ||
import type { | ||
InstanceEvent, | ||
IOnboardingStore, | ||
ProjectEvent, | ||
} from './onboarding-store-type'; | ||
|
||
export class FakeOnboardingStore implements IOnboardingStore { | ||
insertProjectEvent(event: ProjectEvent): Promise<void> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
insertInstanceEvent(event: InstanceEvent): Promise<void> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/lib/features/onboarding/onboarding-service.e2e.test.ts
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,87 @@ | ||
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'; | ||
import { OnboardingService } from './onboarding-service'; | ||
import { createTestConfig } from '../../../test/config/test-config'; | ||
|
||
let db: ITestDb; | ||
let stores: IUnleashStores; | ||
let onboardingService: OnboardingService; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('onboarding_store', getLogger); | ||
const config = createTestConfig({ | ||
experimental: { flags: { onboardingMetrics: true } }, | ||
}); | ||
stores = db.stores; | ||
const { userStore, onboardingStore, projectReadModel } = stores; | ||
onboardingService = new OnboardingService( | ||
{ onboardingStore, userStore, projectReadModel }, | ||
config, | ||
); | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.destroy(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
test('Storing onboarding events', async () => { | ||
jest.useFakeTimers(); | ||
jest.setSystemTime(new Date()); | ||
const { userStore, featureToggleStore, projectStore, projectReadModel } = | ||
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 onboardingService.insert({ type: 'first-user-login' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingService.insert({ type: 'second-user-login' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingService.insert({ type: 'flag-created', flag: 'test' }); | ||
await onboardingService.insert({ type: 'flag-created', flag: 'test' }); | ||
await onboardingService.insert({ type: 'flag-created', flag: 'invalid' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingService.insert({ type: 'pre-live', flag: 'test' }); | ||
await onboardingService.insert({ type: 'pre-live', flag: 'test' }); | ||
await onboardingService.insert({ type: 'pre-live', flag: 'invalid' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingService.insert({ type: 'live', flag: 'test' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingService.insert({ type: 'live', flag: 'test' }); | ||
jest.advanceTimersByTime(minutesToMilliseconds(1)); | ||
await onboardingService.insert({ type: 'live', flag: 'invalid' }); | ||
|
||
const { rows: instanceEvents } = await db.rawDatabase.raw( | ||
'SELECT * FROM onboarding_events_instance', | ||
); | ||
expect(instanceEvents).toMatchObject([ | ||
{ event: 'first-user-login', time_to_event: 60 }, | ||
{ event: 'second-user-login', time_to_event: 120 }, | ||
{ event: 'first-flag', time_to_event: 180 }, | ||
{ event: 'first-pre-live', time_to_event: 240 }, | ||
{ event: 'first-live', time_to_event: 300 }, | ||
]); | ||
|
||
const { rows: projectEvents } = await db.rawDatabase.raw( | ||
'SELECT * FROM onboarding_events_project', | ||
); | ||
expect(projectEvents).toMatchObject([ | ||
{ event: 'first-flag', time_to_event: 180, project: 'test_project' }, | ||
{ | ||
event: 'first-pre-live', | ||
time_to_event: 240, | ||
project: 'test_project', | ||
}, | ||
{ event: 'first-live', 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,135 @@ | ||
import type { | ||
IFlagResolver, | ||
IProjectReadModel, | ||
IUnleashConfig, | ||
IUserStore, | ||
} 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 { | ||
InstanceEvent, | ||
IOnboardingStore, | ||
ProjectEvent, | ||
} from './onboarding-store-type'; | ||
import { millisecondsToSeconds } from 'date-fns'; | ||
|
||
export class OnboardingService { | ||
private flagResolver: IFlagResolver; | ||
|
||
private eventBus: EventEmitter; | ||
|
||
private logger: Logger; | ||
|
||
private onboardingStore: IOnboardingStore; | ||
|
||
private projectReadModel: IProjectReadModel; | ||
|
||
private userStore: IUserStore; | ||
|
||
constructor( | ||
{ | ||
onboardingStore, | ||
projectReadModel, | ||
userStore, | ||
}: { | ||
onboardingStore: IOnboardingStore; | ||
projectReadModel: IProjectReadModel; | ||
userStore: IUserStore; | ||
}, | ||
{ | ||
flagResolver, | ||
eventBus, | ||
getLogger, | ||
}: Pick<IUnleashConfig, 'flagResolver' | 'eventBus' | 'getLogger'>, | ||
) { | ||
this.onboardingStore = onboardingStore; | ||
this.projectReadModel = projectReadModel; | ||
this.userStore = userStore; | ||
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.insert({ type: 'first-user-login' }); | ||
} | ||
if (event.loginOrder === 1) { | ||
await this.insert({ | ||
type: 'second-user-login', | ||
}); | ||
} | ||
}); | ||
this.eventBus.on(STAGE_ENTERED, async (stage: NewStage) => { | ||
if (!this.flagResolver.isEnabled('onboardingMetrics')) return; | ||
|
||
if (stage.stage === 'initial') { | ||
await this.insert({ | ||
type: 'flag-created', | ||
flag: stage.feature, | ||
}); | ||
} else if (stage.stage === 'pre-live') { | ||
await this.insert({ | ||
type: 'pre-live', | ||
flag: stage.feature, | ||
}); | ||
} else if (stage.stage === 'live') { | ||
await this.insert({ | ||
type: 'live', | ||
flag: stage.feature, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
async insert( | ||
event: | ||
| { flag: string; type: ProjectEvent['type'] } | ||
| { type: 'first-user-login' | 'second-user-login' }, | ||
): Promise<void> { | ||
await this.insertInstanceEvent(event); | ||
if ('flag' in event) { | ||
await this.insertProjectEvent(event); | ||
} | ||
} | ||
|
||
private async insertInstanceEvent(event: { | ||
flag?: string; | ||
type: InstanceEvent['type']; | ||
}): Promise<void> { | ||
const firstInstanceUserDate = await this.userStore.getFirstUserDate(); | ||
if (!firstInstanceUserDate) return; | ||
|
||
const timeToEvent = millisecondsToSeconds( | ||
new Date().getTime() - firstInstanceUserDate.getTime(), | ||
); | ||
await this.onboardingStore.insertInstanceEvent({ | ||
type: event.type, | ||
timeToEvent, | ||
}); | ||
} | ||
|
||
private async insertProjectEvent(event: { | ||
flag: string; | ||
type: ProjectEvent['type']; | ||
}): Promise<void> { | ||
const project = await this.projectReadModel.getFeatureProject( | ||
event.flag, | ||
); | ||
if (!project) return; | ||
|
||
const timeToEvent = millisecondsToSeconds( | ||
new Date().getTime() - project.createdAt.getTime(), | ||
); | ||
await this.onboardingStore.insertProjectEvent({ | ||
type: event.type, | ||
timeToEvent, | ||
project: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export type ProjectEvent = | ||
| { type: 'flag-created'; project: string; timeToEvent: number } | ||
| { type: 'pre-live'; project: string; timeToEvent: number } | ||
| { type: 'live'; project: string; timeToEvent: number }; | ||
export type InstanceEvent = | ||
| { type: 'flag-created'; timeToEvent: number } | ||
| { type: 'pre-live'; timeToEvent: number } | ||
| { type: 'live'; timeToEvent: number } | ||
| { type: 'first-user-login'; timeToEvent: number } | ||
| { type: 'second-user-login'; timeToEvent: number }; | ||
|
||
export interface IOnboardingStore { | ||
insertProjectEvent(event: ProjectEvent): Promise<void>; | ||
|
||
insertInstanceEvent(event: InstanceEvent): 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 { Db } from '../../db/db'; | ||
import type { | ||
InstanceEvent, | ||
IOnboardingStore, | ||
ProjectEvent, | ||
} from './onboarding-store-type'; | ||
|
||
export type DBProjectEvent = { | ||
event: 'first-flag' | 'first-pre-live' | 'first-live'; | ||
time_to_event: number; | ||
project: string; | ||
}; | ||
|
||
export type DBInstanceEvent = | ||
| { | ||
event: 'first-flag' | 'first-pre-live' | 'first-live'; | ||
time_to_event: number; | ||
project?: string; | ||
} | ||
| { | ||
event: 'first-user-login' | 'second-user-login'; | ||
time_to_event: number; | ||
}; | ||
|
||
const projectEventLookup: Record< | ||
ProjectEvent['type'], | ||
DBProjectEvent['event'] | ||
> = { | ||
'flag-created': 'first-flag', | ||
'pre-live': 'first-pre-live', | ||
live: 'first-live', | ||
}; | ||
|
||
const instanceEventLookup: Record< | ||
InstanceEvent['type'], | ||
DBInstanceEvent['event'] | ||
> = { | ||
'flag-created': 'first-flag', | ||
'pre-live': 'first-pre-live', | ||
live: 'first-live', | ||
'first-user-login': 'first-user-login', | ||
'second-user-login': 'second-user-login', | ||
}; | ||
|
||
export class OnboardingStore implements IOnboardingStore { | ||
private db: Db; | ||
|
||
constructor(db: Db) { | ||
this.db = db; | ||
} | ||
|
||
async insertInstanceEvent(event: InstanceEvent): Promise<void> { | ||
await this.db('onboarding_events_instance') | ||
.insert({ | ||
event: instanceEventLookup[event.type], | ||
time_to_event: event.timeToEvent, | ||
}) | ||
.onConflict() | ||
.ignore(); | ||
} | ||
|
||
async insertProjectEvent(event: ProjectEvent): Promise<void> { | ||
await this.db<DBProjectEvent>('onboarding_events_project') | ||
.insert({ | ||
event: projectEventLookup[event.type], | ||
time_to_event: event.timeToEvent, | ||
project: event.project, | ||
}) | ||
.onConflict() | ||
.ignore(); | ||
} | ||
} |
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.
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