diff --git a/src/models/statistics.ts b/src/models/statistics.ts index 0cd97fb1ec1fdd7b7806492b6712f91f85f260dd..4f027d7efc173ce039bee5fe6571d04c1849575d 100644 --- a/src/models/statistics.ts +++ b/src/models/statistics.ts @@ -1,4 +1,4 @@ -type LastDayStatistics = { +type LastStatistics = { channels_created: number; notifications_sent: number; users_active: number; @@ -17,7 +17,8 @@ export class Statistics { mutes: number; categories: number; tags: number; - last_day: LastDayStatistics = <LastDayStatistics>{}; + last_day: LastStatistics = <LastStatistics>{}; + last_month: LastStatistics = <LastStatistics>{}; top_channels_with_self_subscription: Record<string, number>; top_channels_with_notifications: Record<string, number>; diff --git a/src/services/impl/statistics/get-statistics.ts b/src/services/impl/statistics/get-statistics.ts index 1291a35e56200e8fca1e18abb0421e8ae54db711..4ea9b7e6f2fe161506f852e0b0d03ffff8bfb16c 100644 --- a/src/services/impl/statistics/get-statistics.ts +++ b/src/services/impl/statistics/get-statistics.ts @@ -81,6 +81,23 @@ export class GetStatistics implements Command { .where('"lastLogin" > NOW() - INTERVAL \'1 DAY\'') .getCount(); + // Monthly numbers + this.s.last_month.channels_created = await transactionManager + .getRepository(Channel) + .createQueryBuilder('channel') + .where('"creationDate" > NOW() - INTERVAL \'1 month\'') + .getCount(); + this.s.last_month.notifications_sent = await transactionManager + .getRepository(Notification) + .createQueryBuilder('notification') + .where('"sentAt" > NOW() - INTERVAL \'1 month\'') + .getCount(); + this.s.last_month.users_active = await transactionManager + .getRepository(User) + .createQueryBuilder('user') + .where('"lastLogin" > NOW() - INTERVAL \'1 month\'') + .getCount(); + await this.calculateDeviceStats(transactionManager); await this.calculateMuteStats(transactionManager); await this.calculatePreferenceStats(transactionManager); @@ -294,18 +311,6 @@ export class GetStatistics implements Command { this.s.avg_notifications_per_channel = notification_avg.average; - this.s.total_notifications_created_last_week = await transactionManager - .getRepository(Notification) - .createQueryBuilder('notification') - .where("notification.sentAt >= now() - interval '1 week'") - .getCount(); - - this.s.total_notifications_created_last_month = await transactionManager - .getRepository(Notification) - .createQueryBuilder('notification') - .where("notification.sentAt >= now() - interval '1 month'") - .getCount(); - const arrayNotification = await transactionManager .getRepository(Notification) .createQueryBuilder('notification') @@ -322,8 +327,6 @@ export class GetStatistics implements Command { this.s.total_end_notifications_weekly_across_devices = 0; for (const notification of arrayNotification) { - const anon_id = '***-' + count; // anonymize notification id - this.s.last_10_notifications_reach[anon_id] = {}; try { const oneAudit = (await AuditNotifications.getValuesAsJson(notification.notification_id)) as any; if ( @@ -338,7 +341,11 @@ export class GetStatistics implements Command { this.s.total_end_notifications_weekly += total_users_reach; if (count < 10) { - this.s.last_10_notifications_reach[anon_id]['target_users'] = total_users_reach; + const anon_id = '***-' + count; // anonymize notification id + this.s.last_10_notifications_reach[anon_id] = { + total_users_reach: total_users_reach, + total_device_reach: 0, + }; } for (const user of Object.keys(target_users)) { @@ -347,7 +354,8 @@ export class GetStatistics implements Command { this.s.total_end_notifications_weekly_across_devices += total_device_reach; if (count < 10) { - this.s.last_10_notifications_reach[anon_id]['devices'] += total_device_reach; + const anon_id = '***-' + count; // anonymize notification id + this.s.last_10_notifications_reach[anon_id]['total_device_reach'] += total_device_reach; } } count++;