Source document

scripts/loaders/miami/ada_adm.ts

Served verbatim from the project repository. Internal working document conventions apply: documents may reference file paths, branch names, and findings-ledger anchors from the repo.

// Load FLDOE ADA/ADM by school → attendance_rate (% present × 100), with
// total days as the denominator.  Run: npx tsx scripts/loaders/miami/ada_adm.ts

import fs from "node:fs";
import { requireSource, startDataLoad, finishDataLoad, padSchoolNumber, parseCell, schoolUniverse, upsertFacts, sheetRows, runMain, SOURCES_ROOT, type Fact } from "./_lib";

const SCRIPT_PATH = "scripts/loaders/miami/ada_adm.ts";
const norm = (s: unknown) => String(s ?? "").toLowerCase().replace(/[^a-z0-9%]+/g, " ").trim();

async function main() {
  const dirs = fs.readdirSync(SOURCES_ROOT).filter((d) => /^fldoe_ada_adm_school_\d{4}-\d{2}$/.test(d)).sort();
  const universe = await schoolUniverse();
  for (const sourceId of dirs) {
    const year = sourceId.slice(-7);
    const { filePath, prov } = requireSource(sourceId);
    const { rows } = sheetRows(filePath, "School");
    const h = rows.findIndex((r) => norm(r?.[0]) === "district");
    const header = rows[h].map(norm);
    const iSch = header.indexOf("school"), iPct = header.indexOf("% present"), iTot = header.indexOf("total days");
    const facts: Fact[] = [];
    let dade = 0;
    for (const r of rows.slice(h + 1)) {
      if (String(r[0] ?? "").trim().replace(/\.0$/, "") !== "13") continue;
      dade++;
      const num = padSchoolNumber(r[iSch]);
      if (!num || !universe.has(num)) continue;
      const p = parseCell(r[iPct]), t = parseCell(r[iTot]);
      if (p.value !== null) facts.push({ schoolNumber: num, year, metricKey: "attendance_rate", value: p.value * 100, denominator: t.value });
    }
    const load = await startDataLoad({ sourceId, sourceUrl: prov.url, sha256: prov.sha256, bytes: prov.bytes, yearCovered: year, scriptPath: SCRIPT_PATH });
    const res = await upsertFacts(facts, load.id);
    await finishDataLoad(load.id, { inserted: res.written, updated: 0, suppressed: 0 }, `Dade rows ${dade}`);
    console.log(`[ada_adm ${year}] ${res.written} facts`);
  }
}

runMain(main);