diff --git a/docker-compose.yml b/docker-compose.yml index fcf286e..61cce1a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,6 +18,8 @@ services: - DB_NAME=${DB_NAME} - DB_USER=${DB_USER} - DB_PASSWORD=${DB_PASSWORD} + - NEW_RELIC_INSERT_KEY=${NEW_RELIC_INSERT_KEY} + - NR_ENV=${NR_ENV} wallamanta_db: image: mysql:8 diff --git a/wallamanta/constants.py b/wallamanta/constants.py index f833856..a3ddd13 100644 --- a/wallamanta/constants.py +++ b/wallamanta/constants.py @@ -13,6 +13,10 @@ DB_NAME = os.getenv("DB_NAME") LATITUDE = os.getenv("LATITUDE") LONGITUDE = os.getenv("LONGITUDE") SLEEP_TIME = int(os.getenv("SLEEP_TIME")) +NEW_RELIC_INSERT_KEY = os.getenv("NEW_RELIC_INSERT_KEY") +NR_ENV = os.getenv("NR_ENV") + + CATEGORIES = { 100: ["🚗 Coches 🚗"], diff --git a/wallamanta/helpers.py b/wallamanta/helpers.py index 9111cbc..213e81d 100644 --- a/wallamanta/helpers.py +++ b/wallamanta/helpers.py @@ -5,6 +5,7 @@ import logging import constants import pytz import walladb +from newrelic_telemetry_sdk import Event, EventClient from PIL import Image, ImageDraw, ImageFont from datetime import datetime, timedelta, timezone, date @@ -138,6 +139,7 @@ async def send_article(article, product): markup = InlineKeyboardMarkup(keyboard) response = await application.bot.send_photo(chat_id=product['telegram_user_id'], photo=open(f"/app/data/images/products/{article['id']}_composed.png", 'rb'), caption=text, parse_mode=ParseMode.MARKDOWN_V2, reply_markup=markup) #logging.info(requests.post(url, files=files).content) + send_to_nr(article, product) logging.info(response) def get_category_name(category): @@ -271,4 +273,24 @@ def get_thread(product_name): for product_thread in SEARCH_THREADS_LIST: if product_name == product_thread[0]: return product_thread[1] - return None \ No newline at end of file + return None + +def send_to_nr(article, product): + event = Event( + "ProductFound", { + "article_name": article['title'], + "article_price": article['price'], + "article_web_slug": article['web_slug'], + "product_name": product['product_name'], + "telegram_user_id": product['telegram_user_id'], + "telegram_user_name": product['telegram_user_name'], + "environment": constants.NR_ENV + } + ) + event_client = EventClient(insert_key=constants.NEW_RELIC_INSERT_KEY, host="insights-collector.eu01.nr-data.net") + try: + response = event_client.send(event) + except Exception as e: + logging.error(f"Error sending to NR: {e}") + response.raise_for_status() + diff --git a/wallamanta/requirements.txt b/wallamanta/requirements.txt index 3e51ca2..0552074 100644 --- a/wallamanta/requirements.txt +++ b/wallamanta/requirements.txt @@ -3,4 +3,5 @@ python-telegram-bot[job-queue]==20.1 requests==2.28.1 prettytable==3.6.0 Pillow==9.4.0 -mysql-connector-python==8.0.32 \ No newline at end of file +mysql-connector-python==8.0.32 +newrelic-telemetry-sdk==0.4.3 \ No newline at end of file diff --git a/wallamanta/worker.py b/wallamanta/worker.py index ebacdf7..dd48fcf 100644 --- a/wallamanta/worker.py +++ b/wallamanta/worker.py @@ -16,7 +16,7 @@ logger = logging.getLogger(__name__) class Worker: _stop = False - + def is_valid_request(self, product): is_valid = False if walladb.get_product(product):