Skip to content

Commit

Permalink
feat: start populating user first seen column (#8010)
Browse files Browse the repository at this point in the history
Co-authored-by: kwasniew <[email protected]>
  • Loading branch information
sjaanus and kwasniew authored Aug 29, 2024
1 parent 8923e28 commit 5fe811c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/lib/__snapshots__/create-config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ exports[`should create default config 1`] = `
"migrationLock": true,
"navigationSidebar": true,
"newEventSearch": false,
"onboardingMetrics": false,
"originMiddleware": false,
"outdatedSdksBanner": false,
"personalAccessTokensKillSwitch": false,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const createStores = (
config.flagResolver,
),
settingStore: new SettingStore(db, getLogger),
userStore: new UserStore(db, getLogger),
userStore: new UserStore(db, getLogger, config.flagResolver),
accountStore: new AccountStore(db, getLogger),
projectStore: new ProjectStore(
db,
Expand Down
21 changes: 16 additions & 5 deletions src/lib/db/user-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
IUserUpdateFields,
} from '../types/stores/user-store';
import type { Db } from './db';
import type { IFlagResolver } from '../types';

const TABLE = 'users';
const PASSWORD_HASH_TABLE = 'used_passwords';
Expand All @@ -26,8 +27,6 @@ const USER_COLUMNS_PUBLIC = [
'scim_id',
];

const USED_PASSWORDS = ['user_id', 'password_hash', 'used_at'];

const USER_COLUMNS = [...USER_COLUMNS_PUBLIC, 'login_attempts', 'created_at'];

const emptify = (value) => {
Expand Down Expand Up @@ -69,9 +68,12 @@ class UserStore implements IUserStore {

private logger: Logger;

constructor(db: Db, getLogger: LogProvider) {
private flagResolver: IFlagResolver;

constructor(db: Db, getLogger: LogProvider, flagResolver: IFlagResolver) {
this.db = db;
this.logger = getLogger('user-store.ts');
this.flagResolver = flagResolver;
}

async getPasswordsPreviouslyUsed(userId: number): Promise<string[]> {
Expand Down Expand Up @@ -230,10 +232,19 @@ class UserStore implements IUserStore {
}

async successfullyLogin(user: User): Promise<void> {
return this.buildSelectUser(user).update({
const currentDate = new Date();
const updateQuery = this.buildSelectUser(user).update({
login_attempts: 0,
seen_at: new Date(),
seen_at: currentDate,
});
if (this.flagResolver.isEnabled('onboardingMetrics')) {
updateQuery.update({
first_seen_at: this.db.raw('COALESCE(first_seen_at, ?)', [
currentDate,
]),
});
}
return updateQuery;
}

async deleteAll(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const createInstanceStatsService = (db: Db, config: IUnleashConfig) => {
getLogger,
flagResolver,
);
const userStore = new UserStore(db, getLogger);
const userStore = new UserStore(db, getLogger, flagResolver);
const projectStore = new ProjectStore(
db,
eventBus,
Expand Down
7 changes: 6 additions & 1 deletion src/lib/types/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export type IFlagKey =
| 'archiveProjects'
| 'projectListImprovements'
| 'useProjectReadModel'
| 'addonUsageMetrics';
| 'addonUsageMetrics'
| 'onboardingMetrics';

export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>;

Expand Down Expand Up @@ -308,6 +309,10 @@ const flags: IFlags = {
process.env.UNLEASH_EXPERIMENTAL_ADDON_USAGE_METRICS,
false,
),
onboardingMetrics: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_ONBOARDING_METRICS,
false,
),
};

export const defaultExperimentalOptions: IExperimentalOptions = {
Expand Down
1 change: 1 addition & 0 deletions src/server-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ process.nextTick(async () => {
projectListImprovements: true,
useProjectReadModel: true,
addonUsageMetrics: true,
onboardingMetrics: true,
},
},
authentication: {
Expand Down

0 comments on commit 5fe811c

Please sign in to comment.