Notify status task

This commit is contained in:
Luciano Gervasoni
2025-08-14 15:06:53 +02:00
parent ffb0f85475
commit 4ccff2bc02
6 changed files with 93 additions and 150 deletions

View File

@@ -1,16 +1,7 @@
import os
from django.http import JsonResponse, HttpResponse
from django.db import connection
import requests
import os
from django.utils import timezone
from django.utils.timezone import now, timedelta
from .models import Urls, Source, Search, UrlContent, UrlsSourceSearch, UrlsDuplicate
from django.db.models import Q, Count
from .src.logger import get_logger
logger = get_logger()
####################################################################################################
"""
@@ -82,71 +73,3 @@ def log_db(request):
return HttpResponse( "\n".join([str(e) for e in r]) )
####################################################################################################
def notify_status(request):
last_hours = 24
start_date = timezone.now() - timedelta(hours=last_hours)
# Count the number of URLs grouped by status within the date range
urls_data_status = Urls.objects.filter(ts_fetch__gte=start_date) \
.values('status') \
.annotate(count=Count('id')) \
.order_by('status')
# Count the number of URLs grouped by source
urls_data_source = UrlsSourceSearch.objects \
.filter(id_url__ts_fetch__gte=start_date) \
.values('id_source__source') \
.annotate(count=Count('id_url')) \
.order_by('id_source__source')
# Count the number of URLs grouped by search
urls_data_search = UrlsSourceSearch.objects \
.filter(id_url__ts_fetch__gte=start_date) \
.values('id_search__search') \
.annotate(count=Count('id_url')) \
.order_by('id_search__search')
bot_token = os.environ.get("TELEGRAM_BOT_TOKEN", "")
chat_id = os.environ.get("TELEGRAM_CHAT_ID", "")
message = "During the last {} hours:\n".format(last_hours)
message += "\nURLs per status:\n"
for o in urls_data_status:
message += " {}: {}\n".format(o.get("status"), o.get("count"))
message += "\nURLs per source:\n"
for o in urls_data_source:
message += " {}: {}\n".format(o.get("id_source__source"), o.get("count"))
message += "\nURLs per search:\n"
for o in urls_data_search:
message += " {}: {}\n".format(o.get("id_search__search"), o.get("count"))
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
params = {
"chat_id": chat_id,
"text": message
}
logger.info("NOTIFY STATUS: {}".format(str(url)))
logger.info("NOTIFY STATUS: {}".format(str(params)))
# POST
# response = requests.post(url, params={"chat_id": chat_id, "text": "Hola!"})
response = requests.post(url, params=params)
# print(response.json()) # Check the response
"""
import json
from django.forms.models import model_to_dict
# readable_ = [model_to_dict(obj) for obj in urls_data_status]
response = requests.post(url, params={"chat_id": chat_id, "text": str(readable)})
response = requests.post(url, params={"chat_id": chat_id, "text": str(readable_)})
"""
return HttpResponse( "\n".join([str(e) for e in message]) )