首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中的Azure成本管理数据

R中的Azure成本管理数据
EN

Stack Overflow用户
提问于 2020-04-20 19:29:24
回答 1查看 261关注 0票数 1

我有兴趣获得Azure成本管理数据(特别是摊销成本)。乍一看,AzureR包集似乎是一个很好的起点。

是否可以以某种方式使用AzureAuth来授权我,然后尝试使用REST?到目前为止,由于不了解参数,我甚至还没有正确地使用AzureAuth。

你们中有谁在R中研究这些数据吗?你是怎么弄到的?在Azure Portal中保存CSV文件是一种糟糕的体验,因为设置正确的过滤器需要很长时间,而且Microsoft不时地更改导出模式。例如,今天很少有列消失(i.a )。(资源类型)按标记分组时。

如能提供任何帮助,将不胜感激:)

编辑:由于洪Ooi (https://github.com/Azure/AzureR/issues/6)的帮助,我现在设法用AzureRMR连接到成本管理API:

代码语言:javascript
复制
    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:响应示例:

代码语言:javascript
复制
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")
    )
  ))

我想要做的是从响应中获取数据帧。我找到了可行的解决方案,但看起来很糟糕:

代码语言:javascript
复制
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:如果有人需要的话,我找到了一种要求特定列的方法。该机构是:

代码语言:javascript
复制
{
   "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"
   }
}

有效的参数(列名)是:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-22 16:30:40

AzureRMR自动将列表转换为json。通常这是最方便的选择,但您可以通过设置encode="raw"发送原始json文本。

代码语言:javascript
复制
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"。这将返回一个httr响应对象;有关更多细节,请参见?httr::response。注意,如果您这样做,您将不得不自己处理错误。

代码语言:javascript
复制
sub1$do_operation("providers/Microsoft.CostManagement/query", api_version = "2019-11-01",
    http_status_handler = "pass",
    ...)

我不熟悉成本管理API,所以我无法帮助您获得更多的列。如果你在这里得不到回复,我建议你联系技术支持部门。

若要将结果转换为数据框架,请执行以下操作:

代码语言:javascript
复制
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)
}))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61330504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档