UNPKG

ams-ssk

Version:

NestJS AMS Library for file management

85 lines 3.3 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var botLoadBalancer_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.botLoadBalancer = void 0; const common_1 = require("@nestjs/common"); const node_telegram_bot_api_1 = __importDefault(require("node-telegram-bot-api")); let botLoadBalancer = botLoadBalancer_1 = class botLoadBalancer { constructor() { this.bots = []; this.logger = new common_1.Logger(botLoadBalancer_1.name); } getBotOperationCount(bot) { const botInfo = this.bots.find((b) => b.bot === bot); return botInfo?.operationCount || 0; } getBotMaxOperations(bot) { const botInfo = this.bots.find((b) => b.bot === bot); return botInfo?.config.maxConcurrentOperations || 0; } getBotUtilizationPercentage(bot) { const maxOps = this.getBotMaxOperations(bot); if (!maxOps) return 0; return (this.getBotOperationCount(bot) / maxOps) * 100; } addBot(config) { const bot = new node_telegram_bot_api_1.default(config.token, { polling: true }); this.bots.push({ bot, operationCount: 0, config, }); this.logger.log(`Added new bot to the pool. Total bots: ${this.bots.length}`); } getNextBot() { if (!this.bots.length) { throw new Error('No bots available in the pool'); } const selectedBot = this.bots.reduce((prev, curr) => { return prev.operationCount <= curr.operationCount ? prev : curr; }); if (selectedBot.operationCount >= selectedBot.config.maxConcurrentOperations) { } selectedBot.operationCount++; return selectedBot.bot; } releaseBot(bot) { const botInfo = this.bots.find((b) => b.bot === bot); if (botInfo) { botInfo.operationCount = Math.max(0, botInfo.operationCount - 1); } } getAllBots() { return this.bots.map((b) => b.bot); } getBotToken(bot) { const botInfo = this.bots.find((b) => b.bot === bot); return botInfo?.config.token; } getBotByToken(token) { for (const botInfo of this.bots) { if (botInfo.config.token === token) { return botInfo.bot; } } return undefined; } getBots() { return this.bots; } }; exports.botLoadBalancer = botLoadBalancer; exports.botLoadBalancer = botLoadBalancer = botLoadBalancer_1 = __decorate([ (0, common_1.Injectable)() ], botLoadBalancer); //# sourceMappingURL=bot.load-balancer.js.map