From 2778ddea1f33a875655a67585a782ef9c8e85433 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Wed, 6 Apr 2022 20:13:39 +0300 Subject: [PATCH] Update analytics --- src/analytics/users_counter.ts | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/analytics/users_counter.ts b/src/analytics/users_counter.ts index f99cc7b..a71767e 100644 --- a/src/analytics/users_counter.ts +++ b/src/analytics/users_counter.ts @@ -1,5 +1,4 @@ import { createClient, RedisClientType } from 'redis'; -import moment from 'moment'; import env from '@/config'; import BotsManager from '@/bots/manager'; @@ -9,6 +8,7 @@ import Sentry from '@/sentry'; enum RedisKeys { UsersActivity = "users_activity", + UsersActivity24 = "users_activity_24", RequestsCount = "requests_count", } @@ -42,30 +42,35 @@ export default class UsersCounter { return Promise.all(promises); } - static async _getUsersByBot(bot: string): Promise { + static async _getUsersByBot(bot: string, lastDay: boolean): Promise { const client = await this._getClient(); - return (await client.hKeys(`${RedisKeys.UsersActivity}_${bot}`)).map((userId) => parseInt(userId)); + const prefix = lastDay ? RedisKeys.UsersActivity24 : RedisKeys.UsersActivity; + const template = `${prefix}_${bot}_`; + + return (await client.keys(template + '*')).map( + (key) => parseInt(key.replace(template, "")) + ); } - static async _getAllUsersCount(botsUsernames: string[]): Promise { + static async _getAllUsersCount(botsUsernames: string[], lastDay: boolean): Promise { const users = new Set(); await Promise.all( botsUsernames.map(async (bot) => { - (await this._getUsersByBot(bot)).forEach((user) => users.add(user)); + (await this._getUsersByBot(bot, lastDay)).forEach((user) => users.add(user)); }) ); return users.size; } - static async _getUsersByBots(botsUsernames: string[]): Promise<{[bot: string]: number}> { + static async _getUsersByBots(botsUsernames: string[], lastDay: boolean): Promise<{[bot: string]: number}> { const result: {[bot: string]: number} = {}; await Promise.all( botsUsernames.map(async (bot) => { - result[bot] = (await this._getUsersByBot(bot)).length; + result[bot] = (await this._getUsersByBot(bot, lastDay)).length; }) ); @@ -104,7 +109,9 @@ export default class UsersCounter { static async take(userId: number, bot: string) { const client = await this._getClient(); - await client.hSet(`${RedisKeys.UsersActivity}_${bot}`, userId, moment().format()); + await client.set(`${RedisKeys.UsersActivity}_${bot}_${userId}`, 1); + await client.set(`${RedisKeys.UsersActivity24}_${bot}_${userId}`, 1, {EX: 24 * 60 * 60}); + await this._incrementRequests(bot); } @@ -113,18 +120,24 @@ export default class UsersCounter { const lines = []; - lines.push(`all_users_count ${await this._getAllUsersCount(botUsernames)}`); + lines.push(`all_users_count ${await this._getAllUsersCount(botUsernames, false)}`); + lines.push(`all_users_count_24h ${await this._getAllUsersCount(botUsernames, true)}`); const requestsByBotCount = await this._getRequestsByBotCount(botUsernames); Object.keys(requestsByBotCount).forEach((bot: string) => { lines.push(`requests_count{bot="${bot}"} ${requestsByBotCount[bot]}`); }); - const usersByBots = await this._getUsersByBots(botUsernames); + const usersByBots = await this._getUsersByBots(botUsernames, false); Object.keys(usersByBots).forEach((bot: string) => { lines.push(`users_count{bot="${bot}"} ${usersByBots[bot]}`) }); + const usersByBots24h = await this._getUsersByBots(botUsernames, true); + Object.keys(usersByBots24h).forEach((bot: string) => { + lines.push(`users_count_24h{bot="${bot}"} ${usersByBots24h[bot]}`) + }); + return lines.join("\n"); } }