def generate_daily_reports(date):
# Need to pass app context around because of how flask works
# can take a single argument date as follows
# flask generate_daily_reports --date 2017/01/31 will compute the billings for jan 2017, up to the 31st day of
# January
try:
timeend = datetime.strptime(date, '%Y/%m/%d').replace(tzinfo=pytz.UTC).replace(minute=0, second=0, hour=0, microsecond=0)
except:
timeend = datetime.utcnow().replace(tzinfo=pytz.UTC).replace(minute=0, second=0, hour=0, microsecond=0)
# HANDLE CLOSING OUT BILLINGS at end of month
if timeend.day == 1:
projects = get_projects_list()
for project in projects:
bill = Billing.query().filter(func.extract('month', Billing.end_date) == getLastMonth(timeend.month)) \
.filter(func.extract('year', Billing.end_date) == timeend.year).filter(Billing.closed_out is False) \
.filter(Billing.project == project).first()
if bill:
bill.update(end_date=timeend, closed_out=True)
monthstart = timeend.replace(day=1)
projects = get_projects_list()
seconds_into_month = (timeend-monthstart).total_seconds()
daysinmonth = calendar.monthrange(timeend.year, timeend.month)[1]
portion_of_month = Decimal(seconds_into_month)/Decimal(daysinmonth*3600*24)
for project in projects:
print(project)
file_size = get_previous_file_sizes(monthstart, project=project)
this_months_files = get_months_uploads(project, monthstart, timeend)
compute_cost_search = make_search_filter_query(monthstart,timeend,project)
compute_costs = get_compute_costs(compute_cost_search)
analysis_compute_json = create_analysis_costs_json(compute_cost_search['hits']['hits'], monthstart, timeend)
all_proj_files = get_previous_file_sizes(timeend, project)['hits']['hits']
analysis_storage_json = create_storage_costs_json(all_proj_files, monthstart, timeend, daysinmonth*3600*24)
storage_costs = get_storage_costs( file_size, portion_of_month,
this_months_files, timeend, daysinmonth*3600*24)
bill = Billing.query().filter(Billing.project == project).filter(func.extract('month', Billing.start_date) == monthstart.month) \
.filter(func.extract('year', Billing.start_date) == monthstart.year).first()
itemized_costs = {
"itemized_compute_costs": analysis_compute_json,
"itemized_storage_costs": analysis_storage_json
}
try:
if bill:
bill.update(compute_cost=compute_costs, storage_cost=storage_costs, end_date=timeend,
cost_by_analysis=itemized_costs)
else:
Billing.create(compute_cost=compute_costs, storage_cost=storage_costs, start_date=monthstart, \
end_date=timeend, project=project, closed_out=False,
cost_by_analysis=itemized_costs)
except:
print("IT'S GONE FAR SOUTH")
generate_billings.py 文件源码
python
阅读 20
收藏 0
点赞 0
评论 0
评论列表
文章目录