Files
uscogdata/R/session.R
T
jared 377eed1240 feat: cog_gov_search + cog_mirror + scope-aware verb behavior
Three pieces:

1. cog_gov_search: name/state/type search over canonical_fips_xwalk
   for resolving human-readable place names into canonical_govids.
   Accepts USPS abbrev ('FL') or FIPS int (12) for state; integer
   0-3 or name ('state','county','city','township') for type. Types
   4/5 emit an explanatory cli message and return an empty tibble
   (v0.1 corpus excludes them). USPS<->FIPS table hardcoded with
   50 states + DC + territories; FIPS 66 = GU (not GA).

2. cog_mirror: downloads manifest-listed files to a local directory
   with SHA-256 idempotency (files with matching hash return status
   'cached'). Supports HTTP and local-path fixture URLs. Round-trip
   test: mirror + re-open against the mirror + query Broward 2020
   returns identical results.

3. Scope-aware verbs: .check_govids_in_scope() helper in session.R
   queries canonical_fips_xwalk for the requested govids, emits a
   cli_inform listing any missing ones, and records the found/missing
   sets under provenance$scope. Wired into cog_spending (and
   transitively into cog_revenue, cog_geographic_rollup,
   cog_peer_compare via their cog_spending calls).

Also: dropped dbplyr from Imports (unused).

Tests: +29 (22 search + 12 mirror - 5 refactored) / 159 total pass.
devtools::check() now clean: 0E / 0W / 0N.
2026-04-24 11:38:32 -04:00

72 lines
2.3 KiB
R

# R/session.R
#' Internal: open session, register views, cache manifest.
#' Not exported. Called lazily by verbs via .ensure_session().
#' @noRd
cog_open <- function(url = .resolve_url(),
cache_dir = .resolve_cache_dir()) {
if (!dir.exists(cache_dir)) dir.create(cache_dir, recursive = TRUE)
con <- DBI::dbConnect(duckdb::duckdb())
DBI::dbExecute(con, "INSTALL httpfs; LOAD httpfs;")
manifest <- .fetch_or_cache_manifest(url, cache_dir)
.validate_schema(manifest, expected_version = 3L)
.validate_scope(manifest)
.register_views(con, url, manifest)
.uscogdata_env$con <- con
.uscogdata_env$manifest <- manifest
.uscogdata_env$url <- url
.uscogdata_env$cache_dir <- cache_dir
invisible(con)
}
#' @noRd
.ensure_session <- function() {
if (is.null(.uscogdata_env$con) ||
!DBI::dbIsValid(.uscogdata_env$con)) {
cog_open()
}
.uscogdata_env$con
}
#' @noRd
#' Check which of the supplied govids exist in canonical_fips_xwalk.
#' Emits a cli message listing any missing ones alongside a pointer to the
#' v0.1 scope explanation; returns both sets so callers can attach them to
#' provenance.
.check_govids_in_scope <- function(govids) {
govids <- unique(as.character(govids))
if (length(govids) == 0L) return(list(found = character(0), missing = character(0)))
con <- .ensure_session()
sql <- sprintf(
"SELECT canonical_govid FROM canonical_fips_xwalk WHERE canonical_govid IN (%s)",
.sql_lit_chr(govids)
)
found <- DBI::dbGetQuery(con, sql)$canonical_govid
missing <- setdiff(govids, found)
if (length(missing) > 0L) {
n <- length(missing)
shown <- paste(utils::head(missing, 5L), collapse = ", ")
more <- if (n > 5L) sprintf(" (+%d more)", n - 5L) else ""
cli::cli_inform(c(
i = sprintf("%d govid%s not found in v0.1 corpus: %s%s",
n, if (n == 1L) "" else "s", shown, more),
i = "v0.1 covers gov_types 0-3 (state/county/city/township). Types 4/5 excluded; see vignette('coverage-scope')."
))
}
list(found = found, missing = missing)
}
#' @noRd
cog_close <- function() {
if (!is.null(.uscogdata_env$con) && DBI::dbIsValid(.uscogdata_env$con)) {
DBI::dbDisconnect(.uscogdata_env$con, shutdown = TRUE)
}
.uscogdata_env$con <- NULL
.uscogdata_env$manifest <- NULL
}