---
title: Status Update
author: Garrick Aden-Buie
format: pdf
---

```{r setup}
library(tidyverse)
library(fs)
```

```{r load-data}
tables <- dir_ls(here::here("data"))
names(tables) <- path_file(tables)

idx_report_list <- which(names(tables) == "report_list")

tbls_arrow <- tables[-idx_report_list] |>
  map(arrow::open_dataset, partitioning = "sboe_id")

tbls_arrow$report_list <- arrow::open_dataset(tables["report_list"])

con <- DBI::dbConnect(duckdb::duckdb())
tbls_db <- imap(tbls_arrow, \(t,n) duckdb::duckdb_register_arrow(con, n, t))
```

```{r}
tbl(con, "cover") |>
  filter(
    date_from >= "2016-01-01",
    date_from < date_to
  ) |>
  mutate(
    report = case_when(
      date_from > (date_to + months(3)) ~ paste0(year(date_from), "-SA-", ifelse(month(date_from) < 6, "1", "2")),
      TRUE ~ paste0(year(date_from), "-Q", quarter(date_from))
    )
  ) |>
  count(committee_type, report) |>
  arrange(report) |>
  ggplot() +
  aes(x = report, y = n) +
  geom_col() +
  facet_wrap(vars(committee_type), scales = "free_y")
```


```{r}
all_data_by_sboe_id <- function(sboe_id) {
  names(tables) |>
  set_names() |>
  map(function(name) {
    tbl(con, name) |>
      filter(sboe_id == !!sboe_id) |>
      collect()
  })
}


john_bell <- "STA-8S285O-C-001"
dan_forest <- "STA-M4HR0Y-C-001"
mitch_setzer <- "STA-C1877N-C-001"

jb_data <- john_bell |> all_data_by_sboe_id()
jb_data |> writexl::write_xlsx(here::here("reports/2023-09-28_example-john-bell.xlsx"))

df_data <- dan_forest |> all_data_by_sboe_id()
df_data |> writexl::write_xlsx(here::here("reports/2023-09-28_example-dan-forest.xlsx"))

ms_data <- mitch_setzer |> all_data_by_sboe_id()
ms_data |> writexl::write_xlsx(here::here("reports/2023-09-28_example-mitch-setzer.xlsx"))
```

```{r}
