-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from p2p-org/subscribtions_improvement
validators_cache + middlewares
- Loading branch information
Showing
14 changed files
with
5,362 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __main__ import db,dp, bot | ||
from aiogram import BaseMiddleware | ||
from aiogram.types import Message,CallbackQuery | ||
from aiogram.dispatcher.event.bases import CancelHandler | ||
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional,Awaitable | ||
from aiogram.types.update import Update | ||
import asyncio | ||
|
||
@dp.update.outer_middleware() | ||
async def check_if_blocked(handler: Callable[[Update, Dict[str, Any]], Awaitable[Any]],event: Update,data: Dict[str, Any]) -> Any: | ||
if event.event_type == 'message': | ||
chat_id = event.message.chat.id | ||
elif event.event_type == 'callback_query': | ||
chat_id = event.callback_query.from_user.id | ||
|
||
if str(chat_id).startswith('-'): | ||
await event.message.answer("🧑🤝🧑 Group chats are not allowed.\nSorry and have a good day.") | ||
return | ||
|
||
account_status = db.get_records('account_status','id',chat_id) | ||
|
||
if account_status and account_status == 'off': | ||
if event.event_type == 'message': | ||
await event.message.answer("Your account has been disabled 🤷\nSorry and have a good day.") | ||
CancelHandler() | ||
elif event.event_type == 'callback_query': | ||
await event.callback_query.answer('Your account has been disabled.') | ||
CancelHandler() | ||
else: | ||
return await handler(event, data) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import logging | ||
import redis | ||
|
||
class CACHE(): | ||
def __init__(self, redis_host, redis_port): | ||
self.redis_host = redis_host | ||
self.redis_port = redis_port | ||
|
||
def connect(self): | ||
self.r = redis.Redis(host=self.redis_host, port=self.redis_port, password=self.redis_pass, db=0) | ||
|
||
def list(self,expr): | ||
self.connect() | ||
result = [] | ||
|
||
try: | ||
for key in self.r.scan_iter(expr + "*"): | ||
result.append(key.decode()) | ||
|
||
except redis.exceptions.ConnectionError: | ||
logging.error("Unable connect to redis.") | ||
return [] | ||
|
||
finally: self.r.close() | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ psycopg | |
aiogram | ||
aiohttp | ||
pyyaml | ||
redis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,12 +36,11 @@ services: | |
|
||
alertmanager: | ||
image: prom/alertmanager:latest | ||
restart: unless-stopped | ||
ports: | ||
- 9093:9093 | ||
- 9093:9093 | ||
volumes: | ||
- ./alertmanager:/config | ||
- alertmanager-data:/data | ||
- ./alertmanager:/config | ||
- alertmanager-data:/data | ||
command: --config.file=/config/alertmanager.yml | ||
networks: | ||
- monitoring | ||
|
@@ -50,11 +49,6 @@ services: | |
bot: | ||
build: | ||
context: ./bot | ||
environment: | ||
- "db_host=postgres" | ||
- "db_port=5432" | ||
- "prometheus_rules_url=http://prometheus:9090/api/v1/rules" | ||
- "prometheus_alert_groups=maas-rules" | ||
env_file: | ||
- ./bot.env | ||
ports: | ||
|
@@ -65,7 +59,6 @@ services: | |
- monitoring | ||
|
||
postgres: | ||
container_name: postgres | ||
image: postgres:13 | ||
environment: | ||
POSTGRES_USER: adm | ||
|
@@ -79,21 +72,36 @@ services: | |
networks: | ||
- postgres | ||
|
||
redis: | ||
image: redis:latest | ||
command: ["redis-server"] | ||
ports: | ||
- "6379:6379" | ||
networks: | ||
- bot | ||
- monitoring | ||
|
||
pgadmin: | ||
container_name: pgadmin | ||
image: dpage/pgadmin4 | ||
environment: | ||
PGADMIN_DEFAULT_EMAIL: [email protected] | ||
PGADMIN_DEFAULT_PASSWORD: adm | ||
PGADMIN_CONFIG_SERVER_MODE: 'False' | ||
volumes: | ||
- pgadmin:/var/lib/pgadmin | ||
|
||
ports: | ||
- "5050:80" | ||
networks: | ||
- postgres | ||
|
||
validators_cache: | ||
build: | ||
context: ./validators_cache | ||
networks: | ||
- redis | ||
- monitoring | ||
restart: unless-stopped | ||
|
||
volumes: | ||
postgres: {} | ||
prometheus_data: {} | ||
|
@@ -106,3 +114,4 @@ networks: | |
exporters: | ||
monitoring: | ||
postgres: | ||
redis: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM --platform=linux/amd64 ubuntu:22.04 | ||
|
||
WORKDIR / | ||
|
||
COPY requirements.txt requirements.txt | ||
COPY update_cache.py update_cache.py | ||
COPY validators.txt validators.txt | ||
|
||
RUN apt update && apt install -y gcc g++ libpq-dev git python3 python3-pip | ||
RUN pip3 install -r requirements.txt | ||
|
||
CMD ["python3", "update_cache.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests | ||
redis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import requests | ||
import redis | ||
import json | ||
import time | ||
import logging | ||
|
||
def write_to_cache(redis_host,redis_port,data): | ||
r = redis.Redis(host=redis_host, port=redis_port, db=0) | ||
|
||
for key in r.keys(): | ||
r.delete(key) | ||
|
||
for i in data: | ||
r.set(i,"") | ||
|
||
amount = len(r.keys()) | ||
|
||
return amount | ||
|
||
def get_from_file(path): | ||
with open(path) as file: | ||
lines = file.readlines() | ||
lines = [line.rstrip() for line in lines] | ||
|
||
return lines | ||
|
||
def get_from_prom(url): | ||
r = requests.get(url) | ||
|
||
return r.json()['data'] | ||
|
||
if __name__ == '__main__': | ||
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.DEBUG, datefmt='%Y-%m-%d %I:%M:%S') | ||
|
||
validators_from_prom = get_from_prom('http://prometheus:9090/api/v1/label/account/values') | ||
validators_from_file = get_from_file('./validators.txt') | ||
|
||
data = validators_from_prom | ||
|
||
for i in validators_from_file: | ||
if i not in data: | ||
data.append(i) | ||
|
||
r = write_to_cache('redis',6379,data) | ||
|
||
if isinstance(r, int): | ||
logging.info("Written validators to cache " + str(r)) | ||
|
||
time.sleep(300) |
Oops, something went wrong.