Skip to content

Commit

Permalink
Add infrastructure to render nasa and noaa reports
Browse files Browse the repository at this point in the history
  • Loading branch information
ateucher committed Feb 10, 2025
1 parent 27ac1b6 commit 4aaa90c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
19 changes: 10 additions & 9 deletions .github/workflows/create-usage-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ jobs:
uses: quarto-dev/quarto-actions/setup@v2
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# To install LaTeX to build PDF book
tinytex: true

# From https://github.com/r-lib/actions/tree/v2-branch/setup-r
- name: Setup R
Expand All @@ -44,12 +41,16 @@ jobs:
run: |
ym=$(Rscript -e 'cat(format(lubridate::floor_date(Sys.Date(), unit = "months") - 1, "%Y-%m"))')
outdir="$REPORT_DIR/reports"
outname="aws-usage-report_$ym.pdf"
quarto render $REPORT_DIR/aws-usage-report.qmd -P "year_month:$ym" --output "$outname"
mkdir -p "$outdir" && \
mv "$outname" "$outdir" && \
printf "\n- [Usage report for $ym](reports/$outname)" >> "$REPORT_DIR/README.md"
for suffix in nasa noaa; do
target_filename="aws-usage-report-${suffix}_$ym.pdf"
quarto render "$REPORT_DIR/aws-usage-report-${suffix}.qmd" -P "year_month:$ym" --output "$target_filename"
mkdir -p "$outdir" && \
mv "$target_filename" "$outdir" && \
Rscript _hub-usage-reporting/update-report-page.R --args "$ym" "$target_filename"
done
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add "$outdir/$outname" "$REPORT_DIR/README.md" && git commit -m "Render report $ym" || echo "No changes to commit"
git add "$outdir/aws-usage-report-*.pdf" "usage-reporting.qmd" && \
git commit -m "Render reports $ym" || echo "No changes to commit"
git push origin || echo "No changes to commit"
22 changes: 11 additions & 11 deletions _hub-usage-reporting/aws-usage-report-nasa.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Sys.setenv(
"AWS_SECRET_ACCESS_KEY" = Sys.getenv("NASA_AWS_SECRET_ACCESS_KEY")
)
here::i_am("_hub-usage-reporting/aws-usage-report.qmd")
here::i_am("_hub-usage-reporting/aws-usage-report-nasa.qmd")
if (interactive()) {
params <- list(year_month = format(Sys.Date() %m-% months(1), "%Y-%m"))
Expand Down Expand Up @@ -458,16 +458,16 @@ ec2_cost_plot <-
ec2_cost_data,
aes(x = start_date, y = UnblendedCost, fill = instance_type)
) +
geom_col() +
scale_fill_discrete(
type = aws_ce_palette(n_distinct(ec2_cost_data$instance_type))
) +
labs(
title = "Daily EC2 cost by instance type",
x = "Date",
y = "Daily Cost ($)",
fill = "EC2 Instance Type"
)
geom_col() +
scale_fill_discrete(
type = aws_ce_palette(n_distinct(ec2_cost_data$instance_type))
) +
labs(
title = "Daily EC2 cost by instance type",
x = "Date",
y = "Daily Cost ($)",
fill = "EC2 Instance Type"
)
```

```{r patchwork-plot-ec2-usage-cost}
Expand Down
4 changes: 2 additions & 2 deletions _hub-usage-reporting/aws-usage-report-noaa.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ knitr::opts_chunk$set(
warning = FALSE
)
here::i_am("_hub-usage-reporting/aws-usage-report-NMFS.qmd")
here::i_am("_hub-usage-reporting/aws-usage-report-noaa.qmd")
Sys.setenv(
"GRAFANA_TOKEN" = Sys.getenv("NMFS_GRAFANA_TOKEN"),
Expand Down Expand Up @@ -250,7 +250,7 @@ size_by_date <- query_prometheus_range(
team_names <- c(
"longterm-access-2i2c",
"2024-mentors",
"mentors",
"2024-science-teams",
"2024-nmfs-champions-cohort",
"2024-coastwatch"
Expand Down
34 changes: 34 additions & 0 deletions _hub-usage-reporting/update-report-page.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
args <- setdiff(commandArgs(TRUE), "--args")

ym <- args[1]
target_filename <- args[2]

org <- sub(".+-((nasa)|(noaa))_.+\\.pdf", "\\1", target_filename)

new_report_line <- sprintf(
"- [Usage report for %s](_hub-usage-reporting/reports/%s)",
ym,
target_filename
)

lines <- readLines("usage-reporting.qmd")

if (!any(grepl(target_filename, lines, fixed = TRUE))) {
cat(
sprintf(
"Adding report %s to %s monthly reports",
target_filename,
toupper(org)
)
)
# split on html comment marking end of org reports
org_insert_line <- grep(sprintf("<!-- last-%s-report -->", org), lines)
org_before <- lines[seq_len(org_insert_line - 1)]
org_after <- lines[seq(org_insert_line, length(lines))]

# recombine with new report at the end of the list
outlines <- c(org_before, new_report_line, org_after)
writeLines(outlines, "usage-reporting.qmd", sep = "\n")
} else {
cat(sprintf("No new report to add for %s\n", org))
}
7 changes: 5 additions & 2 deletions usage-reporting.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: "Openscapes JupyterHub Usage Reports"
We generate monthly reports of usage and costs of AWS resources in the
NASA-Openscapes and NMFS-Openscapes JupyterHubs.

The report is generated automatically on the first day of every month using a
These reports are generated automatically on the first day of every month using a
GitHub Actions [workflow](https://github.com/Openscapes/openscapes.cloud/blob/main/.github/workflows/create-usage-report.yml), summarizing
usage for the preceding month. The report can also be created on demand by
triggering the workflow manually, by visiting the
Expand All @@ -22,9 +22,12 @@ Reports are stored in the [reports](_hub-usage-reporting/reports/) directory.
- [Usage report for 2024-10](_hub-usage-reporting/reports/aws-usage-report_2024-10.pdf)
- [Usage report for 2024-11](_hub-usage-reporting/reports/aws-usage-report_2024-11.pdf)
- [Usage report for 2024-12](_hub-usage-reporting/reports/aws-usage-report_2024-12.pdf)
- [Usage report for 2025-01](_hub-usage-reporting/reports/aws-usage-report_2025-01.pdf)
<!-- last-nasa-report -->

## Monthly NOAA reports

<!-- last-noaa-report -->

### AWS Cost Explorer

*This is a work in progress, currently with minimal steps and screenshots that we will augment.*
Expand Down

0 comments on commit 4aaa90c

Please sign in to comment.