Merge pull request #2050 from GSA/2968-per-service-quota-display

Get service usage
This commit is contained in:
Beverly Nguyen
2025-10-22 12:15:18 -07:00
committed by GitHub
2 changed files with 16 additions and 4 deletions

View File

@@ -7,7 +7,10 @@ from sqlalchemy.sql.expression import case, literal
from app import db
from app.dao.date_util import get_calendar_year_dates, get_calendar_year_for_datetime
from app.dao.organization_dao import dao_get_organization_live_services
from app.dao.organization_dao import (
dao_get_organization_live_services,
dao_get_organization_services,
)
from app.enums import KeyType, NotificationStatus, NotificationType
from app.models import (
AnnualBilling,
@@ -613,6 +616,7 @@ def fetch_sms_billing_for_organization(organization_id, financial_year):
func.coalesce(chargeable_sms, 0).label("chargeable_billable_sms"),
func.coalesce(sms_cost, 0).label("sms_cost"),
Service.active,
Service.restricted,
)
.select_from(Service)
.outerjoin(
@@ -695,10 +699,14 @@ def query_organization_sms_usage_for_year(organization_id, year):
)
def fetch_usage_year_for_organization(organization_id, year):
def fetch_usage_year_for_organization(organization_id, year, include_all_services=False):
year_start, year_end = get_calendar_year_dates(year)
today = utc_now().date()
services = dao_get_organization_live_services(organization_id)
if include_all_services:
services = dao_get_organization_services(organization_id)
else:
services = dao_get_organization_live_services(organization_id)
# if year end date is less than today, we are calculating for data in the past and have no need for deltas.
if year_end >= today:
@@ -719,6 +727,7 @@ def fetch_usage_year_for_organization(organization_id, year):
"sms_cost": 0.0,
"emails_sent": 0,
"active": service.active,
"restricted": service.restricted,
}
sms_usages = fetch_sms_billing_for_organization(organization_id, year)
email_usages = fetch_email_usage_for_organization(
@@ -735,6 +744,7 @@ def fetch_usage_year_for_organization(organization_id, year):
"sms_cost": float(usage.sms_cost),
"emails_sent": 0,
"active": usage.active,
"restricted": usage.restricted,
}
for email_usage in email_usages:
service_with_usage[str(email_usage.service_id)][

View File

@@ -151,7 +151,9 @@ def get_organization_services_usage(organization_id):
year = int(request.args.get("year", "none"))
except ValueError:
return jsonify(result="error", message="No valid year provided"), 400
services = fetch_usage_year_for_organization(organization_id, year)
include_all = request.args.get("include_all_services", "false").lower() == "true"
services = fetch_usage_year_for_organization(organization_id, year, include_all_services=include_all)
list_services = services.values()
sorted_services = sorted(
list_services, key=lambda s: (-s["active"], s["service_name"].lower())