mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Use better date function to get the first of each month.
Build the json object to return for the new endpoint.
This commit is contained in:
@@ -32,14 +32,16 @@ register_errors(billing_blueprint)
|
||||
|
||||
|
||||
@billing_blueprint.route('/ft-monthly-usage')
|
||||
def get_yearly_usage_by_monthy_from_ft_billing(service_id):
|
||||
def get_yearly_usage_by_monthly_from_ft_billing(service_id):
|
||||
try:
|
||||
year = int(request.args.get('year'))
|
||||
results = fetch_monthly_billing_for_year(service_id=service_id, year=year)
|
||||
serialize_ft_billing(results)
|
||||
except TypeError:
|
||||
return jsonify(result='error', message='No valid year provided'), 400
|
||||
|
||||
results = fetch_monthly_billing_for_year(service_id=service_id, year=year)
|
||||
data = serialize_ft_billing(results)
|
||||
return jsonify(monthly_usage=data)
|
||||
|
||||
|
||||
@billing_blueprint.route('/monthly-usage')
|
||||
def get_yearly_usage_by_month(service_id):
|
||||
@@ -207,16 +209,15 @@ def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, fin
|
||||
|
||||
def serialize_ft_billing(data):
|
||||
results = []
|
||||
|
||||
for d in data:
|
||||
j = {
|
||||
"Month": d.month,
|
||||
"service_id": d.service_id,
|
||||
"month": (datetime.strftime(d.month, "%B")),
|
||||
"service_id": str(d.service_id),
|
||||
"notifications_type": d.notification_type,
|
||||
"notifications_sent": d.notifications_sent,
|
||||
"billable_units": d.billable_units,
|
||||
"rate": d.rate,
|
||||
"rate_multiplier": d.rate_multiplier,
|
||||
"notifications_sent": int(d.notifications_sent),
|
||||
"billable_units": int(d.billable_units),
|
||||
"rate": float(d.rate),
|
||||
"rate_multiplier": int(d.rate_multiplier),
|
||||
"international": d.international,
|
||||
}
|
||||
results.append(j)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from datetime import datetime, timedelta, time
|
||||
|
||||
from flask import current_app
|
||||
from sqlalchemy import func, case, desc, extract
|
||||
from sqlalchemy import func, case, desc
|
||||
|
||||
from app import db
|
||||
from app.dao.date_util import get_financial_year
|
||||
@@ -33,7 +33,7 @@ def fetch_monthly_billing_for_year(service_id, year):
|
||||
update_fact_billing(data=d, process_day=day)
|
||||
|
||||
yearly_data = db.session.query(
|
||||
extract('month', FactBilling.bst_date).label("Month"),
|
||||
func.date_trunc('month', FactBilling.bst_date).label("month"),
|
||||
func.sum(FactBilling.notifications_sent).label("notifications_sent"),
|
||||
func.sum(FactBilling.billable_units).label("billable_units"),
|
||||
FactBilling.service_id,
|
||||
@@ -46,7 +46,7 @@ def fetch_monthly_billing_for_year(service_id, year):
|
||||
FactBilling.bst_date >= year_start_date,
|
||||
FactBilling.bst_date <= year_end_date
|
||||
).group_by(
|
||||
'Month',
|
||||
'month',
|
||||
FactBilling.service_id,
|
||||
FactBilling.rate,
|
||||
FactBilling.rate_multiplier,
|
||||
@@ -54,7 +54,7 @@ def fetch_monthly_billing_for_year(service_id, year):
|
||||
FactBilling.notification_type
|
||||
).order_by(
|
||||
FactBilling.service_id,
|
||||
'Month',
|
||||
'month',
|
||||
FactBilling.notification_type
|
||||
).all()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user