Skip to content

Commit

Permalink
[Gitar] Updating TypeScript files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitar committed Oct 28, 2024
1 parent cb6b776 commit 5c7604d
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 45 deletions.
32 changes: 15 additions & 17 deletions src/lib/db/user-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,23 +240,21 @@ class UserStore implements IUserStore {

let firstLoginOrder = 0;

if (this.flagResolver.isEnabled('onboardingMetrics')) {
const existingUser =
await this.buildSelectUser(user).first('first_seen_at');

if (!existingUser.first_seen_at) {
const countEarlierUsers = await this.db(TABLE)
.whereNotNull('first_seen_at')
.andWhere('first_seen_at', '<', currentDate)
.count('*')
.then((res) => Number(res[0].count));

firstLoginOrder = countEarlierUsers;

await updateQuery.update({
first_seen_at: currentDate,
});
}
const existingUser =
await this.buildSelectUser(user).first('first_seen_at');

if (!existingUser.first_seen_at) {
const countEarlierUsers = await this.db(TABLE)
.whereNotNull('first_seen_at')
.andWhere('first_seen_at', '<', currentDate)
.count('*')
.then((res) => Number(res[0].count));

firstLoginOrder = countEarlierUsers;

await updateQuery.update({
first_seen_at: currentDate,
});
}

await updateQuery;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/features/onboarding/onboarding-read-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let app: IUnleashTest;

beforeAll(async () => {
db = await dbInit('onboarding_read_model', getLogger, {
experimental: { flags: { onboardingMetrics: true } },
experimental: { flags: {} },
});

app = await setupAppWithCustomConfig(
Expand Down
11 changes: 4 additions & 7 deletions src/lib/features/onboarding/onboarding-service.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let onboardingReadModel: IOnboardingReadModel;
beforeAll(async () => {
db = await dbInit('onboarding_store', getLogger);
const config = createTestConfig({
experimental: { flags: { onboardingMetrics: true } },
experimental: { flags: {} },
});
stores = db.stores;
eventBus = config.eventBus;
Expand All @@ -41,8 +41,7 @@ beforeEach(async () => {
test('Default project should take first user created instead of project created as start time', async () => {
jest.useFakeTimers();
jest.setSystemTime(new Date());
const { userStore, featureToggleStore, projectStore, projectReadModel } =
stores;
const { userStore, featureToggleStore, projectStore } = stores;

// default projects are created in advance and should be ignored
await projectStore.create({ id: 'default', name: 'irrelevant' });
Expand Down Expand Up @@ -106,8 +105,7 @@ test('Ignore system user in onboarding events', async () => {
test('Storing onboarding events', async () => {
jest.useFakeTimers();
jest.setSystemTime(new Date());
const { userStore, featureToggleStore, projectStore, projectReadModel } =
stores;
const { userStore, featureToggleStore, projectStore } = stores;
const user = await userStore.insert({});
await projectStore.create({ id: 'test_project', name: 'irrelevant' });
await featureToggleStore.create('test_project', {
Expand Down Expand Up @@ -172,8 +170,7 @@ const reachedOnboardingEvents = (count: number) => {
test('Reacting to events', async () => {
jest.useFakeTimers();
jest.setSystemTime(new Date());
const { userStore, featureToggleStore, projectStore, projectReadModel } =
stores;
const { userStore, featureToggleStore, projectStore } = stores;
const user = await userStore.insert({});
await projectStore.create({ id: 'test_project', name: 'irrelevant' });
await featureToggleStore.create('test_project', {
Expand Down
4 changes: 0 additions & 4 deletions src/lib/features/onboarding/onboarding-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ export class OnboardingService {

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' });
}
Expand All @@ -68,8 +66,6 @@ export class OnboardingService {
}
});
this.eventBus.on(STAGE_ENTERED, async (stage: NewStage) => {
if (!this.flagResolver.isEnabled('onboardingMetrics')) return;

if (stage.stage === 'initial') {
await this.insert({
type: 'flag-created',
Expand Down
12 changes: 3 additions & 9 deletions src/lib/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function registerPrometheusMetrics(
};

const { eventStore, environmentStore } = stores;
const { flagResolver, db } = config;
const { flagResolver } = config;
const dbMetrics = new DbMetricsMonitor(config);

const cachedEnvironments: () => Promise<IEnvironment[]> = memoizee(
Expand Down Expand Up @@ -530,10 +530,7 @@ export function registerPrometheusMetrics(
name: 'onboarding_duration',
labelNames: ['event'],
help: 'firstLogin, secondLogin, firstFeatureFlag, firstPreLive, firstLive from first user creation',
query: () =>
flagResolver.isEnabled('onboardingMetrics')
? stores.onboardingReadModel.getInstanceOnboardingMetrics()
: Promise.resolve({}),
query: () => stores.onboardingReadModel.getInstanceOnboardingMetrics(),
map: (result) =>
Object.keys(result)
.filter((key) => Number.isInteger(result[key]))
Expand All @@ -549,10 +546,7 @@ export function registerPrometheusMetrics(
name: 'project_onboarding_duration',
labelNames: ['event', 'project'],
help: 'firstFeatureFlag, firstPreLive, firstLive from project creation',
query: () =>
flagResolver.isEnabled('onboardingMetrics')
? stores.onboardingReadModel.getProjectsOnboardingMetrics()
: Promise.resolve([]),
query: () => stores.onboardingReadModel.getProjectsOnboardingMetrics(),
map: (projectsOnboardingMetrics) =>
projectsOnboardingMetrics.flatMap(
({ project, ...projectMetrics }) =>
Expand Down
5 changes: 0 additions & 5 deletions src/lib/types/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export type IFlagKey =
| 'enableLegacyVariants'
| 'extendedMetrics'
| 'removeUnsafeInlineStyleSrc'
| 'onboardingMetrics'
| 'onboardingUI'
| 'projectRoleAssignment'
| 'eventTimeline'
Expand Down Expand Up @@ -255,10 +254,6 @@ const flags: IFlags = {
process.env.UNLEASH_EXPERIMENTAL_REMOVE_UNSAFE_INLINE_STYLE_SRC,
false,
),
onboardingMetrics: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_ONBOARDING_METRICS,
false,
),
onboardingUI: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_ONBOARDING_UI,
false,
Expand Down
1 change: 0 additions & 1 deletion src/server-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ process.nextTick(async () => {
manyStrategiesPagination: true,
enableLegacyVariants: false,
extendedMetrics: true,
onboardingMetrics: true,
onboardingUI: true,
personalDashboardUI: true,
purchaseAdditionalEnvironments: true,
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/stores/user-store.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let db: ITestDb;

beforeAll(async () => {
db = await dbInit('user_store_serial', getLogger, {
experimental: { flags: { onboardingMetrics: true } },
experimental: { flags: {} },
});
stores = db.stores;
});
Expand Down

0 comments on commit 5c7604d

Please sign in to comment.