-
-
Notifications
You must be signed in to change notification settings - Fork 740
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start collecting prometheus metrics for onboarding events (#8012)
We start collecting prometheus metrics for onboarding events. Co-authored-by: @kwasniew
- Loading branch information
Showing
9 changed files
with
254 additions
and
0 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,14 @@ | ||
import type { IOnboardingReadModel } from '../../types'; | ||
import type { InstanceOnboarding } from './onboarding-read-model-type'; | ||
|
||
export class FakeOnboardingReadModel implements IOnboardingReadModel { | ||
getInstanceOnboardingMetrics(): Promise<InstanceOnboarding> { | ||
return Promise.resolve({ | ||
firstLogin: null, | ||
secondLogin: null, | ||
firstFeatureFlag: null, | ||
firstPreLive: null, | ||
firstLive: null, | ||
}); | ||
} | ||
} |
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 @@ | ||
/** | ||
* All the values are in minutes | ||
*/ | ||
export type InstanceOnboarding = { | ||
firstLogin: number | null; | ||
secondLogin: number | null; | ||
firstFeatureFlag: number | null; | ||
firstPreLive: number | null; | ||
firstLive: number | null; | ||
}; | ||
|
||
export interface IOnboardingReadModel { | ||
getInstanceOnboardingMetrics(): Promise<InstanceOnboarding>; | ||
} |
106 changes: 106 additions & 0 deletions
106
src/lib/features/onboarding/onboarding-read-model.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,106 @@ | ||
import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init'; | ||
import getLogger from '../../../test/fixtures/no-logger'; | ||
import type { | ||
IFeatureLifecycleStore, | ||
IFeatureToggleStore, | ||
IUserStore, | ||
} from '../../types'; | ||
import { OnboardingReadModel } from './onboarding-read-model'; | ||
import type { IOnboardingReadModel } from './onboarding-read-model-type'; | ||
import { minutesToMilliseconds } from 'date-fns'; | ||
|
||
let db: ITestDb; | ||
let onboardingReadModel: IOnboardingReadModel; | ||
let userStore: IUserStore; | ||
let lifecycleStore: IFeatureLifecycleStore; | ||
let featureToggleStore: IFeatureToggleStore; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('onboarding_read_model', getLogger, { | ||
experimental: { flags: { onboardingMetrics: true } }, | ||
}); | ||
onboardingReadModel = new OnboardingReadModel(db.rawDatabase); | ||
userStore = db.stores.userStore; | ||
lifecycleStore = db.stores.featureLifecycleStore; | ||
featureToggleStore = db.stores.featureToggleStore; | ||
}); | ||
|
||
afterAll(async () => { | ||
if (db) { | ||
await db.destroy(); | ||
} | ||
}); | ||
|
||
beforeEach(async () => { | ||
await userStore.deleteAll(); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
test('can get onboarding durations', async () => { | ||
const initialResult = | ||
await onboardingReadModel.getInstanceOnboardingMetrics(); | ||
expect(initialResult).toMatchObject({ | ||
firstLogin: null, | ||
secondLogin: null, | ||
firstFeatureFlag: null, | ||
firstPreLive: null, | ||
firstLive: null, | ||
}); | ||
|
||
const firstUser = await userStore.insert({}); | ||
await userStore.successfullyLogin(firstUser); | ||
|
||
const firstLoginResult = | ||
await onboardingReadModel.getInstanceOnboardingMetrics(); | ||
expect(firstLoginResult).toMatchObject({ | ||
firstLogin: 0, | ||
secondLogin: null, | ||
}); | ||
jest.useFakeTimers(); | ||
jest.advanceTimersByTime(minutesToMilliseconds(10)); | ||
|
||
const secondUser = await userStore.insert({}); | ||
await userStore.successfullyLogin(secondUser); | ||
|
||
jest.advanceTimersByTime(minutesToMilliseconds(10)); | ||
|
||
await featureToggleStore.create('default', { | ||
name: 'test', | ||
createdByUserId: secondUser.id, | ||
}); | ||
|
||
await lifecycleStore.insert([ | ||
{ | ||
feature: 'test', | ||
stage: 'initial', | ||
}, | ||
]); | ||
|
||
jest.advanceTimersByTime(minutesToMilliseconds(10)); | ||
|
||
await lifecycleStore.insert([ | ||
{ | ||
feature: 'test', | ||
stage: 'pre-live', | ||
}, | ||
]); | ||
|
||
jest.advanceTimersByTime(minutesToMilliseconds(10)); | ||
|
||
await lifecycleStore.insert([ | ||
{ | ||
feature: 'test', | ||
stage: 'live', | ||
}, | ||
]); | ||
|
||
const secondLoginResult = | ||
await onboardingReadModel.getInstanceOnboardingMetrics(); | ||
expect(secondLoginResult).toMatchObject({ | ||
firstLogin: 0, | ||
secondLogin: 10, | ||
firstFeatureFlag: 20, | ||
firstPreLive: 30, | ||
firstLive: 40, | ||
}); | ||
}); |
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,92 @@ | ||
import type { Db } from '../../db/db'; | ||
import type { | ||
IOnboardingReadModel, | ||
InstanceOnboarding, | ||
} from './onboarding-read-model-type'; | ||
import { millisecondsToMinutes } from 'date-fns'; | ||
|
||
interface IOnboardingUser { | ||
first_login: string; | ||
} | ||
const parseStringToNumber = (value: string): number | null => { | ||
return Number.isNaN(Number(value)) ? null : Number(value); | ||
}; | ||
|
||
const calculateTimeDifferenceInMinutes = (date1?: Date, date2?: Date) => { | ||
if (date1 && date2) { | ||
const diffInMilliseconds = date2.getTime() - date1.getTime(); | ||
return millisecondsToMinutes(diffInMilliseconds); | ||
} | ||
return null; | ||
}; | ||
|
||
export class OnboardingReadModel implements IOnboardingReadModel { | ||
private db: Db; | ||
|
||
constructor(db: Db) { | ||
this.db = db; | ||
} | ||
|
||
async getInstanceOnboardingMetrics(): Promise<InstanceOnboarding> { | ||
const firstUserCreatedResult = await this.db('users') | ||
.select('created_at') | ||
.orderBy('created_at') | ||
.first(); | ||
const firstLoginResult = await this.db('users') | ||
.select('first_seen_at') | ||
.orderBy('first_seen_at') | ||
.limit(2); | ||
|
||
const firstInitialResult = await this.db('feature_lifecycles') | ||
.select('created_at') | ||
.where('stage', 'initial') | ||
.orderBy('created_at') | ||
.first(); | ||
const firstPreLiveResult = await this.db('feature_lifecycles') | ||
.select('created_at') | ||
.where('stage', 'pre-live') | ||
.orderBy('created_at') | ||
.first(); | ||
const firstLiveResult = await this.db('feature_lifecycles') | ||
.select('created_at') | ||
.where('stage', 'live') | ||
.orderBy('created_at') | ||
.first(); | ||
|
||
const createdAt = firstUserCreatedResult?.created_at; | ||
const firstLogin = firstLoginResult[0]?.first_seen_at; | ||
const secondLogin = firstLoginResult[1]?.first_seen_at; | ||
const firstInitial = firstInitialResult?.created_at; | ||
const firstPreLive = firstPreLiveResult?.created_at; | ||
const firstLive = firstLiveResult?.created_at; | ||
|
||
const firstLoginDiff = calculateTimeDifferenceInMinutes( | ||
createdAt, | ||
firstLogin, | ||
); | ||
const secondLoginDiff = calculateTimeDifferenceInMinutes( | ||
createdAt, | ||
secondLogin, | ||
); | ||
const firstFlagDiff = calculateTimeDifferenceInMinutes( | ||
createdAt, | ||
firstInitial, | ||
); | ||
const firstPreLiveDiff = calculateTimeDifferenceInMinutes( | ||
createdAt, | ||
firstPreLive, | ||
); | ||
const firstLiveDiff = calculateTimeDifferenceInMinutes( | ||
createdAt, | ||
firstLive, | ||
); | ||
|
||
return { | ||
firstLogin: firstLoginDiff, | ||
secondLogin: secondLoginDiff, | ||
firstFeatureFlag: firstFlagDiff, | ||
firstPreLive: firstPreLiveDiff, | ||
firstLive: firstLiveDiff, | ||
}; | ||
} | ||
} |
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