Files
uscogdata/R/manifest.R
T
jared c682e6547d feat: cog_spending + cog_revenue + cog_explain
Three core query verbs over the spending_annotated / revenue_annotated
DuckDB views. Each verb accepts vector govid, vector years, optional
category filter, per_capita flag, and adjust_to_year for CPI-U
real-dollar conversion (bundled index).

Amounts are returned in full USD (SUM(amt) * 1000) so callers can
freely rescale to millions/billions. The $1,000s -> $USD conversion
is recorded in provenance$transformations$units_conversion.

Every result carries an attr(., "provenance") list matching
inst/schemas/provenance-v1.json. cog_explain() prints the structured
form via cli or returns the raw list for MCP/JSON consumers.

Also: .fetch_or_cache_manifest() now handles local fixture paths so
tests can point USCOGDATA_FIXTURE_URL at the pipeline publish_cache/
without a working HTTP server.

Tests: 80 pass / 0 fail. devtools::check() 0E/0W/2N (both notes
pre-existing / environmental).
2026-04-24 09:49:09 -04:00

57 lines
1.8 KiB
R

# R/manifest.R
#' Fetch manifest.json from URL (or read from a local fixture path),
#' cache locally, validate TTL.
#' @noRd
.fetch_or_cache_manifest <- function(url, cache_dir) {
# Local fixture path: read manifest directly; skip cache/TTL plumbing so
# tests pick up regenerated manifests immediately.
if (.is_local_path(url)) {
local_manifest <- file.path(url, "manifest.json")
if (!file.exists(local_manifest)) {
cli::cli_abort("Local fixture has no manifest.json at {local_manifest}")
}
return(jsonlite::fromJSON(local_manifest, simplifyVector = FALSE))
}
cache_path <- file.path(cache_dir, "manifest.json")
ttl <- as.integer(.cfg("manifest_ttl_secs"))
needs_fetch <- !file.exists(cache_path) ||
difftime(Sys.time(), file.info(cache_path)$mtime, units = "secs") > ttl
if (needs_fetch) {
resp <- httr2::request(paste0(url, "manifest.json")) |>
httr2::req_error(is_error = function(r) httr2::resp_status(r) >= 400) |>
httr2::req_perform()
writeLines(httr2::resp_body_string(resp), cache_path)
}
jsonlite::fromJSON(cache_path, simplifyVector = FALSE)
}
#' @noRd
.is_local_path <- function(url) {
!grepl("^[a-zA-Z][a-zA-Z0-9+.-]*://", url)
}
#' @noRd
.validate_schema <- function(manifest, expected_version) {
if (manifest$schema_version != expected_version) {
cli::cli_abort(c(
"Corpus schema version mismatch.",
x = "Package expects schema_version = {expected_version}; corpus has {manifest$schema_version}.",
i = "Update uscogdata (install.packages or pak::pkg_install) or re-publish corpus."
))
}
}
#' @noRd
.validate_scope <- function(manifest) {
included <- manifest$scope$gov_types_included
.uscogdata_env$scope_included <- included
invisible(NULL)
}
`%||%` <- function(a, b) if (is.null(a) || (length(a) == 1 && is.na(a))) b else a