mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Update analytics
This commit is contained in:
@@ -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<number[]> {
|
||||
static async _getUsersByBot(bot: string, lastDay: boolean): Promise<number[]> {
|
||||
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<number> {
|
||||
static async _getAllUsersCount(botsUsernames: string[], lastDay: boolean): Promise<number> {
|
||||
const users = new Set<number>();
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user