-
-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
380 additions
and
1 deletion.
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.