Skip to content

Commit

Permalink
Add filter arg to aws_billing()
Browse files Browse the repository at this point in the history
  • Loading branch information
ateucher committed Sep 20, 2024
1 parent 3692a93 commit 2039fb2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
41 changes: 33 additions & 8 deletions R/billing.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @importFrom dplyr rename rename_with left_join coalesce
#' @param date_start,date_end Start and end date to get billing data for.
#' Date format expected: `yyyy-MM-dd`. required
#' @param filter (list) filters costs by different dimensions. optional.
#' @autoglobal
#' @references <https://www.paws-r-sdk.com/docs/costexplorer/>
#' @family billing
Expand All @@ -24,6 +25,15 @@
#' beyond 14 months". See
#' <https://docs.aws.amazon.com/cost-management/latest/userguide/ce-advanced-cost-analysis.html> #nolint
#' for help
#' @section Filtering:
#' You can optionally pass a list to the `filter` argument to filter
#' AWS costs by different dimensions (see possible dimensions: https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetDimensionValues.html)

Check warning on line 30 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=30,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 159 characters.
#'

Check warning on line 31 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=31,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' This is supplied as a list of Dimensions, with key-value pairs for each
#' Dimension. For example, to only get "Usage" costs as opposed to credits,

Check warning on line 33 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=33,col=76,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' tax refunds etc., use the `RECORD_TYPE` dimension Key and set the Value to

Check warning on line 34 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=34,col=78,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' "Usage" (see examples)

Check warning on line 35 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=35,col=26,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#'

Check warning on line 36 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=36,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' @return tibble with columns:
#' - id: "blended", "unblended"
#' - date: date, in format `yyyy-MM-dd`
Expand Down Expand Up @@ -55,14 +65,26 @@
#' group_by(service) %>%
#' summarise(sum_cost = sum(cost)) %>%
#' filter(service == "Amazon Relational Database Service")
aws_billing <- function(date_start, date_end = as.character(Sys.Date())) {
#'

Check warning on line 68 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=68,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' # Filter to return only "Usage" costs:
#'

Check warning on line 70 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=70,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' aws_billing(
#' date_start = start_date,

Check warning on line 72 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=72,col=30,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' filter = list(
#' Dimensions = list(
#' Key = "RECORD_TYPE",
#' Values = "Usage"
#' ))
#' )
#'

Check warning on line 79 in R/billing.R

View workflow job for this annotation

GitHub Actions / lint

file=R/billing.R,line=79,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
aws_billing <- function(date_start, date_end = as.character(Sys.Date()), filter = NULL) {
bind_rows(
unblended = rename(
billing_unblended(date_start, date_end),
billing_unblended(date_start, date_end, filter = filter),
cost = UnblendedCost
),
blended = rename(
billing_blended(date_start, date_end),
billing_blended(date_start, date_end, filter = filter),
cost = BlendedCost
),
.id = "id"
Expand All @@ -74,17 +96,19 @@ aws_billing <- function(date_start, date_end = as.character(Sys.Date())) {
#' @keywords internal
#' @noRd
billing_factory <- function(type) {
function(date_start, date_end) {
function(date_start, date_end, filter = NULL) {
groupby <- list(
list(Type = "DIMENSION", Key = "SERVICE"),
list(Type = "DIMENSION", Key = "LINKED_ACCOUNT")
)
raw_billing_data <- aws_billing_raw(date_start,
raw_billing_data <- aws_billing_raw(
date_start,
metrics = type,
granularity = "daily", group_by = groupby,
date_end = date_end
granularity = "daily",
group_by = groupby,
date_end = date_end,
filter = filter
)

raw_billing_data$ResultsByTime %>%
map(function(x) {
tibble(
Expand Down Expand Up @@ -146,6 +170,7 @@ aws_billing_raw <- function(
con_ce()$get_cost_and_usage(
TimePeriod = list(Start = date_start, End = date_end),
Granularity = toupper(granularity),
Filter = filter,
Metrics = metrics,
GroupBy = group_by
)
Expand Down
26 changes: 25 additions & 1 deletion man/aws_billing.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2039fb2

Please sign in to comment.