From 3cc665f48fd233d09f778d7e887488dde6f03ef6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 7 May 2020 10:39:09 +0200 Subject: Add last login date to users --- server/models/account/user.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'server/models') diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 260c1b28e..fbd3080c6 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -353,6 +353,11 @@ export class UserModel extends Model { @Column pluginAuth: string + @AllowNull(true) + @Default(null) + @Column + lastLoginDate: Date + @CreatedAt createdAt: Date @@ -691,10 +696,28 @@ export class UserModel extends Model { } static async getStats () { + function getActiveUsers (days: number) { + const query = { + where: { + [Op.and]: [ + literal(`"lastLoginDate" > NOW() - INTERVAL '${days}d'`) + ] + } + } + + return UserModel.count(query) + } + const totalUsers = await UserModel.count() + const totalDailyActiveUsers = await getActiveUsers(1) + const totalWeeklyActiveUsers = await getActiveUsers(7) + const totalMonthlyActiveUsers = await getActiveUsers(30) return { - totalUsers + totalUsers, + totalDailyActiveUsers, + totalWeeklyActiveUsers, + totalMonthlyActiveUsers } } @@ -808,7 +831,9 @@ export class UserModel extends Model { createdAt: this.createdAt, - pluginAuth: this.pluginAuth + pluginAuth: this.pluginAuth, + + lastLoginDate: this.lastLoginDate } if (parameters.withAdminFlags) { -- cgit v1.2.3