|
|
# -*- coding: utf-8 -*-
|
|
|
import os
|
|
|
from collections import defaultdict
|
|
|
import pandas as pd
|
|
|
import numpy as np
|
|
|
import openpyxl
|
|
|
import time
|
|
|
from pandas import Index
|
|
|
from calendar import monthrange
|
|
|
import datetime
|
|
|
import calendar
|
|
|
import logging
|
|
|
import platform
|
|
|
|
|
|
plat = platform.system().lower()
|
|
|
# -*- 跑2023-01月脚本 02-06需求 -*-
|
|
|
'''
|
|
|
戴姆勒脚本变更代码
|
|
|
各word子账号合并到一个主账号,只要总数
|
|
|
'''
|
|
|
|
|
|
############初始化目录地址
|
|
|
merge_file_dir = '/server/billing/daimler/merge_data/'
|
|
|
alter_dbr_dir = '/server/billing/daimler/alter_data/'
|
|
|
############初始化变量参数
|
|
|
# csv表头字段
|
|
|
dbr_index = Index(['InvoiceID',
|
|
|
'PayerAccountId',
|
|
|
'LinkedAccountId',
|
|
|
'RecordType',
|
|
|
'RecordId',
|
|
|
'ProductName',
|
|
|
'RateId',
|
|
|
'SubscriptionId',
|
|
|
'PricingPlanId',
|
|
|
'UsageType',
|
|
|
'Operation',
|
|
|
'AvailabilityZone',
|
|
|
'ReservedInstance',
|
|
|
'ItemDescription',
|
|
|
'UsageStartDate',
|
|
|
'UsageEndDate',
|
|
|
'UsageQuantity',
|
|
|
'BlendedRate',
|
|
|
'BlendedCost',
|
|
|
'UnBlendedRate',
|
|
|
'UnBlendedCost',
|
|
|
'ResourceId'], dtype='object')
|
|
|
|
|
|
class CustomCsvByHour(object):
|
|
|
|
|
|
def __init__(self, pathlist, *args, **kwargs):
|
|
|
super().__init__(*args, **kwargs)
|
|
|
self.costs_maps = defaultdict()
|
|
|
self.path = [pathlist]
|
|
|
self.finally_dict = defaultdict(float)
|
|
|
self.sourcename = None
|
|
|
self.es_iid = None
|
|
|
self.es_total = 0
|
|
|
self.edp_iid = ''
|
|
|
self.edp_total = 0
|
|
|
self.exclude_id = list()
|
|
|
self.unblendedcost = 0
|
|
|
self.payer = None
|
|
|
self.accountdt = pd.DataFrame()
|
|
|
self.statementdt = pd.DataFrame()
|
|
|
self.name = None
|
|
|
self.filename = None
|
|
|
self.partname = None
|
|
|
self.filepath = None
|
|
|
self.word_dict = defaultdict()
|
|
|
self.init_edp_dict = defaultdict()
|
|
|
self.es_data = None
|
|
|
|
|
|
def create_new_csv(self):
|
|
|
self.aws_feb={}
|
|
|
for path in self.path:
|
|
|
payer = path.split('-')[0]
|
|
|
print(path.split('-')[0])
|
|
|
self.name = path
|
|
|
self.edp_iid = '1356844351'
|
|
|
if path.split('-')[0] == '010265887827':
|
|
|
self.edp_iid = '1356844499'
|
|
|
elif path.split('-')[0] == '565095721352':
|
|
|
self.edp_iid = '1356843675'
|
|
|
elif path.split('-')[0] == '565359735310':
|
|
|
self.edp_iid = '1356844263'
|
|
|
elif path.split('-')[0] == '563347375094':
|
|
|
self.edp_iid = '1356844635'
|
|
|
elif path.split('-')[0] == '563646727715':
|
|
|
self.edp_iid = '1356844351'
|
|
|
elif path.split('-')[0] == '011562250191':
|
|
|
self.edp_iid = '1356843707'
|
|
|
elif path.split('-')[0] == '011276597621':
|
|
|
self.edp_iid = '1366731839'
|
|
|
self.daimler_read_aws_feb_sinnetcostallocation(path.split('-')[0])
|
|
|
insert_data = defaultdict(list)
|
|
|
leng = 2
|
|
|
startdate = '2024/01/1 0:00:00'
|
|
|
enddate = '2024/01/31 23:59:59'
|
|
|
#lineItem
|
|
|
for k,v in self.aws_feb.items():
|
|
|
if k == 'total':
|
|
|
continue
|
|
|
insert_data['InvoiceID'].extend([self.edp_iid for _ in range(leng)])
|
|
|
insert_data['PayerAccountId'].extend([payer for _ in range(leng)])
|
|
|
insert_data['LinkedAccountId'].extend([k for _ in range(leng)])
|
|
|
insert_data['RecordType'].extend(['LineItem' for _ in range(leng)])
|
|
|
insert_data['ProductName'].extend(['Sinnet/L2C Support BJS', '']) # 改这俩
|
|
|
insert_data['UsageStartDate'].extend([startdate for _ in range(leng)])
|
|
|
insert_data['UsageEndDate'].extend([enddate for _ in range(leng)])
|
|
|
insert_data['ItemDescription'].extend(['Sinnet/L2C Support BJS', '税金 VAT 类型'])
|
|
|
insert_data['BlendedCost'].extend(['{:.2f}'.format(float(v.get('unBlendedCost',0))), '{:.2f}'.format(float(v.get('unBlendedCost',0)) * 0.06)])
|
|
|
insert_data['UnBlendedCost'].extend(['{:.2f}'.format(float(v.get('unBlendedCost',0))), '{:.2f}'.format(float(v.get('unBlendedCost',0)) * 0.06)])
|
|
|
#AccountTotal
|
|
|
for k,v in self.aws_feb.items():
|
|
|
leng=1
|
|
|
if k == 'total':
|
|
|
continue
|
|
|
insert_data['InvoiceID'].extend(['' for _ in range(leng)])
|
|
|
insert_data['PayerAccountId'].extend([payer for _ in range(leng)])
|
|
|
insert_data['LinkedAccountId'].extend([k for _ in range(leng)])
|
|
|
insert_data['RecordType'].extend(['AccountTotal' for _ in range(leng)])
|
|
|
insert_data['ProductName'].extend(['']) # 改这俩
|
|
|
insert_data['UsageStartDate'].extend(['' for _ in range(leng)])
|
|
|
insert_data['UsageEndDate'].extend(['' for _ in range(leng)])
|
|
|
insert_data['ItemDescription'].extend(['关联账户# 019723979408 总额'])
|
|
|
unBlendedCost = float(v.get('unBlendedCost', 0)) * 1.06;
|
|
|
insert_data['BlendedCost'].extend(['{:.2f}'.format(unBlendedCost)])
|
|
|
insert_data['UnBlendedCost'].extend(['{:.2f}'.format(unBlendedCost)])
|
|
|
leng = 1
|
|
|
insert_data['InvoiceID'].extend(['' for _ in range(leng)])
|
|
|
insert_data['PayerAccountId'].extend([payer for _ in range(leng)])
|
|
|
insert_data['LinkedAccountId'].extend([''])
|
|
|
insert_data['RecordType'].extend(['StatementTotal' for _ in range(leng)])
|
|
|
insert_data['ProductName'].extend(['']) # 改这俩
|
|
|
insert_data['UsageStartDate'].extend(['' for _ in range(leng)])
|
|
|
insert_data['UsageEndDate'].extend(['' for _ in range(leng)])
|
|
|
insert_data['ItemDescription'].extend(['2024-01-01 00:00:00 - 2024-01-31 23:59:59 期间内的对账单总额是'])
|
|
|
if self.aws_feb.get('total', '')=='':
|
|
|
continue
|
|
|
totalAmount=float(self.aws_feb.get('total',''))
|
|
|
insert_data['BlendedCost'].extend(['{:.2f}'.format(float(totalAmount) * 1.06)])
|
|
|
insert_data['UnBlendedCost'].extend(['{:.2f}'.format(float(totalAmount) * 1.06)])
|
|
|
|
|
|
# self.invoiceId='1355493111'Sinnet Support-011562250191-2023-08.csv
|
|
|
self.partname = 'Sinnet Support-'+path.split('-')[0]+'-2024-01.csv'
|
|
|
self.sourcename = path
|
|
|
self.init_part_file()
|
|
|
fill_data = insert_data # 获取导出LineItem的数据
|
|
|
self.save_data_to_dbr(fill_data) # 写入修改部分_文件
|
|
|
|
|
|
# partname保存 修改部分_文件,self.filename 改文件
|
|
|
def save_data_to_dbr(self, insert_data):
|
|
|
partname = os.path.join(alter_dbr_dir,self.partname)
|
|
|
with open(partname, 'a', encoding='utf-8') as pw:
|
|
|
dbr_dict = {v: int(k) for k, v in enumerate(list(dbr_index))}
|
|
|
for i in range(len(list(insert_data.values())[0])):
|
|
|
empty_data = ["" for _ in range(len(dbr_index))]
|
|
|
for k, v in insert_data.items():
|
|
|
empty_data[dbr_dict[k]] = v[i]
|
|
|
# 修改部分写入
|
|
|
pw.write(','.join('"{}"'.format(x) for x in empty_data) + '\n')
|
|
|
|
|
|
|
|
|
# 初始化:修改部分_ 文件标题行
|
|
|
def init_part_file(self):
|
|
|
partname = os.path.join(alter_dbr_dir,self.partname)
|
|
|
with open(partname, 'w', encoding='gb2312') as f:
|
|
|
f.write(','.join('"{}"'.format(x) for x in list(dbr_index)) + '\n')
|
|
|
|
|
|
def daimler_read_aws_feb_sinnetcostallocation(self, payerId):
|
|
|
self.aws_feb = {}
|
|
|
read_file = 'D:\\workspace\\zhangdanjiaoben\\billing-python\\daimler\\config\\aws_feb.csv'
|
|
|
total = 0
|
|
|
with open(read_file, 'r', encoding='gb2312') as r: # open(alterpath, 'w', encoding='utf-8') as w:
|
|
|
num = 0
|
|
|
while True:
|
|
|
line = r.readline()
|
|
|
if not line:
|
|
|
break
|
|
|
num += 1
|
|
|
data = line.split(',')
|
|
|
payer = data[0].replace(' ', '')
|
|
|
linkedAccountId = data[1].replace(' ', '')
|
|
|
if len(linkedAccountId) == 10:
|
|
|
linkedAccountId = "00" + linkedAccountId
|
|
|
elif len(linkedAccountId) == 9:
|
|
|
linkedAccountId = "000" + linkedAccountId
|
|
|
elif len(linkedAccountId) == 8:
|
|
|
linkedAccountId = "0000" + linkedAccountId
|
|
|
elif len(linkedAccountId) == 7:
|
|
|
linkedAccountId = "00000" + linkedAccountId
|
|
|
elif len(linkedAccountId) == 6:
|
|
|
linkedAccountId = "000000" + linkedAccountId
|
|
|
elif len(linkedAccountId) == 11:
|
|
|
linkedAccountId = "0" + linkedAccountId
|
|
|
unBlendedCost = data[2].replace('\n', '')
|
|
|
if not payer in payerId:
|
|
|
continue
|
|
|
if unBlendedCost=='' or unBlendedCost == None:
|
|
|
continue
|
|
|
cost={}
|
|
|
cost['unBlendedCost'] = unBlendedCost
|
|
|
self.aws_feb[linkedAccountId] = cost
|
|
|
print(self.aws_feb[linkedAccountId])
|
|
|
total += float(unBlendedCost)
|
|
|
self.aws_feb["total"] = total
|
|
|
print(self.aws_feb)
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
logging.basicConfig(filename='logger.log', level=logging.INFO)
|
|
|
if (plat == 'windows'): # 1==1 本地运行是目录
|
|
|
alter_dbr_dir = 'D:\\workspace\\zhangdanjiaoben\\billing-python\\daimler\\alter_data'
|
|
|
merge_file_dir = 'D:\\workspace\\hangdanjiaoben\\billing-python\\daimler\\merge_data'
|
|
|
dbr_typle = (
|
|
|
# ('563347375094-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-2024-01.csv',
|
|
|
# '563347375094-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-Ningxia-2024-01.csv',),
|
|
|
# ('563646727715-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-2024-01.csv',
|
|
|
# '563646727715-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-Ningxia-2024-01.csv',),
|
|
|
('565095721352-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-2024-01.csv',
|
|
|
'565095721352-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-Ningxia-2024-01.csv',),
|
|
|
# ('565359735310-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-2024-01.csv',
|
|
|
# '565359735310-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-Ningxia-2024-01.csv',),
|
|
|
# ('010265887827-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-2024-01.csv',
|
|
|
# '010265887827-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-Ningxia-2024-01.csv',),
|
|
|
# ('011562250191-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-2024-01.csv',
|
|
|
# '011562250191-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-Ningxia-2024-01.csv'),
|
|
|
# ('011276597621-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-2024-01.csv',
|
|
|
# '011276597621-aws-billing-detailed-line-items-with-resources-and-tags-ACTS-Ningxia-2024-01.csv'),
|
|
|
)
|
|
|
# 3.核心计算
|
|
|
for i in dbr_typle:
|
|
|
CustomCsvByHour(i[0]).create_new_csv() |