python - 为什么我的 Microsoft Azure 使用详情 API propterties.cost 总和会波动?
问题描述
我编写了一个 python 脚本,它返回我所有订阅的使用详细信息并将它们保存在数据框中。它稍后会汇总 properties.cost 并返回当前月份的总数。我的目标是知道这个月我已经花了多少钱。但是随着我提出的每一个新请求,我的总成本都会上升或下降。
输出示例:
{'request1': {'costTotal': 96.99047619273139,
'time': '17-03-2020T13:39:19:000Z'},
'request2': {'costTotal': 105.28680062444838,
'time': '17-03-2020T13:41:19:000Z'},
'request3': {'costTotal': 103.50071786335579,
'time': '17-03-2020T13:43:19:000Z'},
'request4': {'costTotal': 103.04300834107798,
'time': '17-03-2020T13:45:19:000Z'}}
我使用的 API:
https://management.azure.com/{scope}/providers/Microsoft.Consumption/usageDetails?api-version=2019-10-01
我的 download.py 脚本的一部分,它连接数据帧并将其保存为 pkl:
ENV = sys.argv[1]
ORG_FILES = None
RESULTING_FILES = ["billing_file.pkl"]
def main():
execution = {"file": RESULTING_FILES, "orgfile": ORG_FILES}
def generate_billinginfo(subscription, cred):
scope = "subscriptions/" + subscription
url = (
"https://management.azure.com/"
+ scope
+ "/providers/Microsoft.Consumption/usageDetails?api-version=2019-10-01"
)
headers = {
"Authorization": "Bearer " + cred.token["access_token"],
"Content-Type": "application/json",
}
connection_attempts = 0
while True:
httpresponse = requests.get(url=url, headers=headers, verify=False)
connection_attempts = connection_attempts + 1
if httpresponse.status_code != 200:
logger.error(
"Problem accessing server status code: %s",
httpresponse.status_code,
)
if connection_attempts >= 3:
raise ValueError(
"Can not access server. Status code: %s",
httpresponse.status_code,
)
else:
time.sleep(3 * connection_attempts)
else:
break
# Extracting tags & properties
Tags = []
Properties = []
for resource in httpresponse.json()["value"]:
Tags.append(resource["tags"])
Properties.append(resource["properties"])
dfgeneral = pd.DataFrame(httpresponse.json()["value"])
dfprop = pd.DataFrame(Properties)
# Rejoining dataframes
billingdf = dfprop.join(dfgeneral[["tags"]])
return billingdf
cred = ServicePrincipalCredentials(
client_id=config["client_id"],
secret=config["password"],
tenant=config["tenant_id"],
)
subscriptions = [
"0f5dd24a-f6e0-473f-8643-75dc3481bdc1",
....
]
for subscription in subscriptions:
try:
billinginfo
billinginfoexists = True
except:
billinginfoexists = False
if billinginfoexists:
billinginfo = pd.concat(
[billinginfo, generate_billinginfo(subscription, cred)]
)
else:
billinginfo = generate_billinginfo(subscription, cred)
billinginfo.to_pickle(RESULTING_FILES[0])
# Resulting file
with open(Path("./execution.json"), "w") as outfile:
json.dump(execution, outfile, indent=4)
任何想法可能是什么问题?
解决方案
推荐阅读
- html - Bootstrap align-items 无法按预期工作
- npm - Ember:运行 ember serve 时出错
- dart - 从坐标中获取阿拉伯语地址行
- jenkins - Jenkins崩溃或关闭后如何自动重启?
- node.js - Visual Studio 代码调试器未附加由 express 生成器生成的示例 express 项目
- jquery - 如何获取数据表中自动填充单元格的类名?
- javascript - 将选择范围扩展到 Unicode 文本上的字边界
- arrays - Json 提取与 Json 提取标量
- ios - 在核心数据swift 4中将一个实体与另一个实体一起保存
- vue.js - tns 预览版是否支持 Vuex?