From 015f92a06bcb1db46b2e6ca356581e4f53fa915e Mon Sep 17 00:00:00 2001 From: Luciano Gervasoni Date: Thu, 14 Aug 2025 14:04:33 +0200 Subject: [PATCH] View fix --- app_urls/fetcher/views.py | 45 ++---------------------------- app_urls/fetcher/views_base.py | 50 +++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/app_urls/fetcher/views.py b/app_urls/fetcher/views.py index 8cce588..afd803e 100644 --- a/app_urls/fetcher/views.py +++ b/app_urls/fetcher/views.py @@ -1,4 +1,4 @@ -from .views_base import link_list, logs, log_db #, trigger_task, +from .views_base import link_list, logs, log_db, notify_status #, trigger_task, from django.core.paginator import Paginator from django.shortcuts import render, get_object_or_404 @@ -9,8 +9,7 @@ from django.utils.timezone import now, timedelta from .models import Urls, Source, Search, UrlContent, UrlsSourceSearch, UrlsDuplicate from .src.llm import OllamaClient import json -import requests -import os + #################################################################################################### @@ -145,46 +144,6 @@ def urls_per_search(request): return JsonResponse(data) -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.getenv("TELEGRAM_BOT_TOKEN", "") - chat_id = os.getenv("TELEGRAM_CHAT_ID", "") - - message = "During the last {} hours:\n{}\n{}\n{}".format(last_hours, urls_data_status, urls_data_source, urls_data_search) - - url = f"https://api.telegram.org/bot{bot_token}/sendMessage" - params = { - "chat_id": chat_id, - "text": message - } - - # POST - response = requests.post(url, params=params) - # print(response.json()) # Check the response - #################################################################################################### def filtered_urls(request): diff --git a/app_urls/fetcher/views_base.py b/app_urls/fetcher/views_base.py index cb5a5ad..8311768 100644 --- a/app_urls/fetcher/views_base.py +++ b/app_urls/fetcher/views_base.py @@ -1,6 +1,12 @@ 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 #################################################################################################### """ @@ -71,4 +77,46 @@ def log_db(request): """).fetchall() return HttpResponse( "\n".join([str(e) for e in r]) ) -#################################################################################################### \ No newline at end of file +#################################################################################################### + +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.getenv("TELEGRAM_BOT_TOKEN", "") + chat_id = os.getenv("TELEGRAM_CHAT_ID", "") + + message = "During the last {} hours:\n{}\n{}\n{}".format(last_hours, urls_data_status, urls_data_source, urls_data_search) + + url = f"https://api.telegram.org/bot{bot_token}/sendMessage" + params = { + "chat_id": chat_id, + "text": message + } + + # POST + response = requests.post(url, params=params) + # print(response.json()) # Check the response + + return HttpResponse( "\n".join([str(e) for e in message]) ) \ No newline at end of file