mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-18 13:38:53 -04:00
@@ -425,6 +425,33 @@ def dao_fetch_todays_stats_for_service(service_id):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def dao_fetch_stats_for_service_from_day(service_id, day):
|
||||||
|
# today = datetime.now(timezone.utc).date()
|
||||||
|
# 2024-05-20
|
||||||
|
# day_date = datetime.strptime(day, '%Y-%m-%d').date()
|
||||||
|
start_date = get_midnight_in_utc(day)
|
||||||
|
end_date = get_midnight_in_utc(day + timedelta(days=1))
|
||||||
|
print(start_date)
|
||||||
|
return (
|
||||||
|
db.session.query(
|
||||||
|
NotificationHistory.notification_type,
|
||||||
|
NotificationHistory.status,
|
||||||
|
func.count(NotificationHistory.id).label("count"),
|
||||||
|
)
|
||||||
|
.filter(
|
||||||
|
NotificationHistory.service_id == service_id,
|
||||||
|
NotificationHistory.key_type != KeyType.TEST,
|
||||||
|
NotificationHistory.created_at >= start_date,
|
||||||
|
NotificationHistory.created_at <= end_date,
|
||||||
|
)
|
||||||
|
.group_by(
|
||||||
|
NotificationHistory.notification_type,
|
||||||
|
NotificationHistory.status,
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def dao_fetch_todays_stats_for_all_services(
|
def dao_fetch_todays_stats_for_all_services(
|
||||||
include_from_test_key=True, only_active=True
|
include_from_test_key=True, only_active=True
|
||||||
):
|
):
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import itertools
|
import itertools
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from flask import Blueprint, current_app, jsonify, request
|
from flask import Blueprint, current_app, jsonify, request
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
@@ -63,6 +63,7 @@ from app.dao.services_dao import (
|
|||||||
dao_fetch_all_services_by_user,
|
dao_fetch_all_services_by_user,
|
||||||
dao_fetch_live_services_data,
|
dao_fetch_live_services_data,
|
||||||
dao_fetch_service_by_id,
|
dao_fetch_service_by_id,
|
||||||
|
dao_fetch_stats_for_service_from_day,
|
||||||
dao_fetch_todays_stats_for_all_services,
|
dao_fetch_todays_stats_for_all_services,
|
||||||
dao_fetch_todays_stats_for_service,
|
dao_fetch_todays_stats_for_service,
|
||||||
dao_remove_user_from_service,
|
dao_remove_user_from_service,
|
||||||
@@ -210,6 +211,36 @@ def get_service_notification_statistics(service_id):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@service_blueprint.route("/<uuid:service_id>/statistics/<string:start>/<int:days>")
|
||||||
|
def get_service_notification_statistics_by_day(service_id, start, days):
|
||||||
|
return jsonify(
|
||||||
|
data=get_service_statistics_for_specific_days(service_id, start, int(days))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_service_statistics_for_specific_days(service_id, start, days=1):
|
||||||
|
start_date = datetime.strptime(start, "%Y-%m-%d").date()
|
||||||
|
|
||||||
|
if days == 1:
|
||||||
|
stats = {}
|
||||||
|
stats[start] = {
|
||||||
|
"value": statistics.format_statistics(
|
||||||
|
dao_fetch_stats_for_service_from_day(service_id, start_date)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
stats = {}
|
||||||
|
for d in range(days):
|
||||||
|
new_date = start_date + timedelta(days=d)
|
||||||
|
key = new_date.strftime("%Y-%m-%d")
|
||||||
|
value = statistics.format_statistics(
|
||||||
|
dao_fetch_stats_for_service_from_day(service_id, new_date)
|
||||||
|
)
|
||||||
|
stats[key] = {"value": value}
|
||||||
|
|
||||||
|
return stats
|
||||||
|
|
||||||
|
|
||||||
@service_blueprint.route("", methods=["POST"])
|
@service_blueprint.route("", methods=["POST"])
|
||||||
def create_service():
|
def create_service():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|||||||
Reference in New Issue
Block a user