From 0ba5ef0789fdb2c706e4167506b4b46f3198ba3c Mon Sep 17 00:00:00 2001 From: Administrator Date: Sun, 12 Nov 2023 17:59:22 +0300 Subject: [PATCH] add stats --- .deploy/deploy-dev.yaml | 24 ++++++++++++++++++++++++ .deploy/deploy-prod.yaml | 24 ++++++++++++++++++++++++ api.py | 16 ++++++++++++++++ main.py | 8 +++++++- 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 api.py diff --git a/.deploy/deploy-dev.yaml b/.deploy/deploy-dev.yaml index 8481d66..95d8263 100644 --- a/.deploy/deploy-dev.yaml +++ b/.deploy/deploy-dev.yaml @@ -5,6 +5,7 @@ services: bot: image: mathwave/sprint-repo:pizda-bot + command: bot environment: TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV MONGO_HOST: "mongo.develop.sprinthub.ru" @@ -18,3 +19,26 @@ services: update_config: parallelism: 1 order: start-first + + pizda-bot-nginx: + image: mathwave/sprint-repo:pizda-bot + command: api + environment: + TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV + MONGO_HOST: "mongo.develop.sprinthub.ru" + MONGO_PASSWORD: $MONGO_PASSWORD_DEV + PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN + STAGE: "development" + networks: + - common-infra-nginx + deploy: + mode: replicated + restart_policy: + condition: any + update_config: + parallelism: 1 + order: start-first + +networks: + common-infra-nginx: + external: true \ No newline at end of file diff --git a/.deploy/deploy-prod.yaml b/.deploy/deploy-prod.yaml index 29ec39c..f6ee1f2 100644 --- a/.deploy/deploy-prod.yaml +++ b/.deploy/deploy-prod.yaml @@ -5,6 +5,7 @@ services: bot: image: mathwave/sprint-repo:pizda-bot + command: bot environment: TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD MONGO_HOST: "mongo.sprinthub.ru" @@ -18,3 +19,26 @@ services: update_config: parallelism: 1 order: start-first + + pizda-bot-nginx: + image: mathwave/sprint-repo:pizda-bot + command: api + environment: + TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD + MONGO_HOST: "mongo.sprinthub.ru" + MONGO_PASSWORD: $MONGO_PASSWORD_PROD + PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN + STAGE: "production" + networks: + - common-infra-nginx + deploy: + mode: replicated + restart_policy: + condition: any + update_config: + parallelism: 1 + order: start-first + +networks: + common-infra-nginx: + external: true \ No newline at end of file diff --git a/api.py b/api.py new file mode 100644 index 0000000..7ea7e1a --- /dev/null +++ b/api.py @@ -0,0 +1,16 @@ +from flask import Flask + +from mongo import mongo + +app = Flask(__name__) + + +@app.route('/stats/json', methods=['GET']) +def stats_json(): + replies = 0 + for doc in mongo.counter_collection.find({}): + replies += doc['count'] + return { + "Всего чатов": mongo.chats_collection.count_documents({"chat_id": {"$lt": 0}}), + "Отвечено": replies + } diff --git a/main.py b/main.py index b877cea..cf3393c 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import os +import sys from random import randrange, choice import telebot @@ -178,4 +179,9 @@ def do_action(message: Message): mongo.inc(message.from_user.username, message.chat.id) -bot.polling() +arg = sys.argv[-1] +if arg == 'bot': + bot.polling() +else: + from api import app + app.run(host="0.0.0.0", port=1238)