R 中的 Azure 成本管理数据
Azure Cost Management data in R
我有兴趣获取 Azure 成本管理数据(尤其是摊余成本)。乍一看,AzureR 包集似乎是一个不错的起点。
是否可以以某种方式使用 AzureAuth 授权我,然后尝试使用 REST API?至于现在,我什至无法正确使用AzureAuth,因为我不了解这些参数。
你们中有人在 R 中处理这些数据吗?你如何获得它?在 Azure 门户中保存 CSV 文件是一种糟糕的体验,因为设置正确的过滤器需要花费大量时间,而且 Microsoft 会不时更改导出架构。例如,今天按标签分组时,几列消失了(i.a。资源类型)。
任何帮助将不胜感激:)
编辑:
感谢 Hong Ooi (https://github.com/Azure/AzureR/issues/6) 的帮助,我现在设法使用 AzureRMR 连接到成本管理 API:
library(AzureRMR)
az <- create_azure_login()
sub1 <- az$get_subscription(my_sub_guid)
d1 <- sub1$do_operation("providers/Microsoft.CostManagement/query", api_version = "2019-11-01", http_verb = "POST", encode = "json",
body = list(
timeframe = "Custom",
timePeriod = list(
from = "2020-04-01",
to = "2020-04-01"
),
type = "AmortizedCost",
dataset = list(
granularity = "daily"
)
))
我还不知道的是:
- 如何将正文作为 JSON 而不是列表列表发送?
- 如何接收响应 JSON?
- 如何在响应中接收更多列?我特别需要资源名称和资源类型。
编辑 2:
响应示例:
response_example <-
list(properties = list(
nextLink = NULL,
columns = list(
list(name = "UsageDate",
type = "Number"),
list(name = "Currency",
type = "String")
),
rows = list(
list(as.integer(20200401),
"EUR"),
list(as.integer(20200402),
"EUR")
)
))
我想做的是从响应中取出数据框。我找到了可行的解决方案,但它看起来很糟糕:
d1_ <- do.call(what = "rbind", args = lapply(d1$properties$rows, as_tibble, .name_repair = "unique"))
colnames(d1_) <- do.call(what = "rbind", args = lapply(d1$properties$columns, as_tibble, .name_repair = "unique")) %>%
select(name) %>% pull()
编辑 3:
如果有人需要,我找到了一种方法来询问特定的专栏。正文是:
{
"type":"AmortizedCost",
"dataSet":{
"granularity":"Daily",
"aggregation":{
"PreTaxCost":{
"name":"PreTaxCost",
"function":"Sum"
}
},
"sorting":[
{
"direction":"ascending",
"name":"UsageDate"
}
],
"grouping":[
{
"type":"TagKey",
"name":"myTag"
},
{
"type":"Dimension",
"name":"ResourceType"
},
{
"type":"Dimension",
"name":"ResourceGroupName"
}
]
},
"timeframe":"Custom",
"timePeriod":{
"from":"2020-04-01T00:00:00+00:00",
"to":"2020-04-30T23:59:59+00:00"
}
}
有效参数(列名)为:
AccountName, BillingAccountId, BillingAccountName, BillingMonth, BillingPeriod, BillingProfileId, BillingProfileName, ChargeType, ConsumedService, CostAllocationRuleId, CostAllocationRuleName, CustomerName, CustomerTenantDomainName, CustomerTenantId, DepartmentName, EnrollmentAccountName, Frequency, InvoiceId, InvoiceNumber, InvoiceSection, InvoiceSectionId, InvoiceSectionName, MarkupRuleId, MarkupRuleName, Meter, MeterCategory, MeterId, MeterSubcategory, PartNumber, PartnerEarnedCreditApplied, PartnerName, PricingModel, Product, ProductOrderId, ProductOrderName, PublisherType, ResellerMPNId, ReservationId, ReservationName, ResourceGroup, ResourceGroupName, ResourceGuid, ResourceId, ResourceLocation, ResourceType, ServiceFamily, ServiceName, ServiceTier, SubscriptionId, SubscriptionName, UnitOfMeasure
此外,这是我处理响应的方式:
Turning Azure Cost Management API's response into data frame
AzureRMR 自动将列表列表转换为 json。通常这是最方便的选择,但您可以通过设置 encode="raw"
:
发送原始 json 文本
sub1$do_operation("providers/Microsoft.CostManagement/query", api_version = "2019-11-01",
http_verb = "POST",
encode = "raw",
body = '{"timeframe":"MonthToDate","type":"actualcost","dataset":{"granularity":"daily"}}')
如果您想要原始输出而不是解析后的输出,请设置 http_status_handler="pass"
。这 returns 一个 httr 响应对象;有关详细信息,请参阅 ?httr::response
。请注意,如果您这样做,您将不得不自己处理错误。
sub1$do_operation("providers/Microsoft.CostManagement/query", api_version = "2019-11-01",
http_status_handler = "pass",
...)
我不熟悉成本管理API,所以我无法帮助您获得更多专栏。如果您在这里没有得到回应,我建议您联系技术支持。
将结果转为数据框:
res <- sub1$do_operation(...)
rows <- do.call(rbind, lapply(res$properties$rows, function(r)
{
names(r) <- sapply(res$properties$columns, `[[`, 1)
data.frame(r, stringsAsFactors=FALSE)
}))
我有兴趣获取 Azure 成本管理数据(尤其是摊余成本)。乍一看,AzureR 包集似乎是一个不错的起点。
是否可以以某种方式使用 AzureAuth 授权我,然后尝试使用 REST API?至于现在,我什至无法正确使用AzureAuth,因为我不了解这些参数。
你们中有人在 R 中处理这些数据吗?你如何获得它?在 Azure 门户中保存 CSV 文件是一种糟糕的体验,因为设置正确的过滤器需要花费大量时间,而且 Microsoft 会不时更改导出架构。例如,今天按标签分组时,几列消失了(i.a。资源类型)。
任何帮助将不胜感激:)
编辑: 感谢 Hong Ooi (https://github.com/Azure/AzureR/issues/6) 的帮助,我现在设法使用 AzureRMR 连接到成本管理 API:
library(AzureRMR)
az <- create_azure_login()
sub1 <- az$get_subscription(my_sub_guid)
d1 <- sub1$do_operation("providers/Microsoft.CostManagement/query", api_version = "2019-11-01", http_verb = "POST", encode = "json",
body = list(
timeframe = "Custom",
timePeriod = list(
from = "2020-04-01",
to = "2020-04-01"
),
type = "AmortizedCost",
dataset = list(
granularity = "daily"
)
))
我还不知道的是:
- 如何将正文作为 JSON 而不是列表列表发送?
- 如何接收响应 JSON?
- 如何在响应中接收更多列?我特别需要资源名称和资源类型。
编辑 2: 响应示例:
response_example <-
list(properties = list(
nextLink = NULL,
columns = list(
list(name = "UsageDate",
type = "Number"),
list(name = "Currency",
type = "String")
),
rows = list(
list(as.integer(20200401),
"EUR"),
list(as.integer(20200402),
"EUR")
)
))
我想做的是从响应中取出数据框。我找到了可行的解决方案,但它看起来很糟糕:
d1_ <- do.call(what = "rbind", args = lapply(d1$properties$rows, as_tibble, .name_repair = "unique"))
colnames(d1_) <- do.call(what = "rbind", args = lapply(d1$properties$columns, as_tibble, .name_repair = "unique")) %>%
select(name) %>% pull()
编辑 3: 如果有人需要,我找到了一种方法来询问特定的专栏。正文是:
{
"type":"AmortizedCost",
"dataSet":{
"granularity":"Daily",
"aggregation":{
"PreTaxCost":{
"name":"PreTaxCost",
"function":"Sum"
}
},
"sorting":[
{
"direction":"ascending",
"name":"UsageDate"
}
],
"grouping":[
{
"type":"TagKey",
"name":"myTag"
},
{
"type":"Dimension",
"name":"ResourceType"
},
{
"type":"Dimension",
"name":"ResourceGroupName"
}
]
},
"timeframe":"Custom",
"timePeriod":{
"from":"2020-04-01T00:00:00+00:00",
"to":"2020-04-30T23:59:59+00:00"
}
}
有效参数(列名)为:
AccountName, BillingAccountId, BillingAccountName, BillingMonth, BillingPeriod, BillingProfileId, BillingProfileName, ChargeType, ConsumedService, CostAllocationRuleId, CostAllocationRuleName, CustomerName, CustomerTenantDomainName, CustomerTenantId, DepartmentName, EnrollmentAccountName, Frequency, InvoiceId, InvoiceNumber, InvoiceSection, InvoiceSectionId, InvoiceSectionName, MarkupRuleId, MarkupRuleName, Meter, MeterCategory, MeterId, MeterSubcategory, PartNumber, PartnerEarnedCreditApplied, PartnerName, PricingModel, Product, ProductOrderId, ProductOrderName, PublisherType, ResellerMPNId, ReservationId, ReservationName, ResourceGroup, ResourceGroupName, ResourceGuid, ResourceId, ResourceLocation, ResourceType, ServiceFamily, ServiceName, ServiceTier, SubscriptionId, SubscriptionName, UnitOfMeasure
此外,这是我处理响应的方式: Turning Azure Cost Management API's response into data frame
AzureRMR 自动将列表列表转换为 json。通常这是最方便的选择,但您可以通过设置 encode="raw"
:
sub1$do_operation("providers/Microsoft.CostManagement/query", api_version = "2019-11-01",
http_verb = "POST",
encode = "raw",
body = '{"timeframe":"MonthToDate","type":"actualcost","dataset":{"granularity":"daily"}}')
如果您想要原始输出而不是解析后的输出,请设置 http_status_handler="pass"
。这 returns 一个 httr 响应对象;有关详细信息,请参阅 ?httr::response
。请注意,如果您这样做,您将不得不自己处理错误。
sub1$do_operation("providers/Microsoft.CostManagement/query", api_version = "2019-11-01",
http_status_handler = "pass",
...)
我不熟悉成本管理API,所以我无法帮助您获得更多专栏。如果您在这里没有得到回应,我建议您联系技术支持。
将结果转为数据框:
res <- sub1$do_operation(...)
rows <- do.call(rbind, lapply(res$properties$rows, function(r)
{
names(r) <- sapply(res$properties$columns, `[[`, 1)
data.frame(r, stringsAsFactors=FALSE)
}))