teqo-backup/src/lib/bahiaElectionAggregates.ts
Francisco Ignácio Fontoura Solla 717d584f6d E8 follow-up: touch-friendly metric tooltips, 2014/2018 majoritarian seed, E18 docs plan
- MunicipalityHoverTooltip/MunicipalityGoalAccountCard: hover/focus tooltips on
  the 4 "Conta da cadeira" diagnostic numbers, hardened per /impeccable
  critique (touch dead-end fix via controlled open + pointerup toggle +
  outside-tap dismiss, tooltip no longer covers "Cobertura da meta", clearer
  two-tier copy, distinguishable button trigger, thicker 0% progress bar).
- seed-tse-results.mjs/electionResultsBuild/electionResultsParse: seed also
  imports presidente/governador for 2014/2018 (previously deputado federal
  only), with vote-em-transito duplicate merging so the unique constraints
  hold; artifact/formulas stay 2022-only by design.
- Registered E18 (documentacao-conceitos-campanha) roadmap item/plan for a
  future /campanha/conceitos glossary page.
2026-07-24 22:55:51 -03:00

81 lines
3.3 KiB
TypeScript

import artifactJson from '@/lib/electionAggregates/bahia-federal-baseline.json'
/**
* Committed per-municipality election baseline (immutable TSE 2014/2018/2022):
* Solla nominal votes + federal T1 valid votes, pre-cut by the same
* `municipalityCatalog` geography the runtime uses (Salvador split by zone,
* every other municipality whole — Camaçari included).
*
* v2 (E8 "conta da cadeira"): adds the curated campo-parties federal nominal
* votes, federal T1 turnout/blank/null tally, and 2022 majoritarian
* (presidente/governador #13) votes+tally used for "teto do campo",
* "share intracampo" and roll-off. 2022-only for the majoritarian slice by
* design (E8's formulas anchor the field ceiling and roll-off to the single
* most recent majoritarian race) — the DB has majoritária seeded for
* 2014/2018 too (`pnpm db:seed:tse`, since 2026-07-24), but it isn't cut into
* this artifact; a future consumer that needs it extends `majoritarian2022`
* into a `majoritarianByYear` map.
*
* Generated by `pnpm build:election-aggregates` from the locally seeded TSE
* data — never computed at build or request time (see the script header for
* the rationale). A snapshot test pins artifact keys ↔ catalog slugs.
*/
/** Turnout/valid/blank/null tally cell (`municipalityElectoralBaseline.ts`'s `OfficeTallyCell` is this same type). */
export type FederalBaselineTallyCell = {
comparecimento: number
votosValidos: number
votosBranco: number
votosNulo: number
}
/** One 2022 majoritarian office (presidente/governador #13): votes + tally. */
type MajoritarianOfficeCell = FederalBaselineTallyCell & {
votes: number
}
export type MunicipalityFederalBaseline = {
/** TSE year (as string) → Solla nominal votes inside the municipality geography. */
votesByYear: Record<string, number>
/** TSE year (as string) → federal T1 valid votes inside the municipality geography. */
validVotesByYear: Record<string, number>
/** TSE year (as string) → curated campo-parties (`campoParties.ts`) federal T1 nominal votes. */
campoFederalVotesByYear: Record<string, number>
/** TSE year (as string) → federal T1 turnout/valid/blank/null tally. */
federalTallyByYear: Record<string, FederalBaselineTallyCell>
/** 2022-only majoritarian tickets (absent for older artifacts / unknown slugs). */
majoritarian2022?: {
president: MajoritarianOfficeCell
governor: MajoritarianOfficeCell
}
}
type FederalBaselineArtifact = {
provenance: string
candidateNumber: number
candidateName: string
years: number[]
municipalities: Record<string, MunicipalityFederalBaseline>
}
const artifact = artifactJson as FederalBaselineArtifact
export const FEDERAL_BASELINE_CANDIDATE_NUMBER = artifact.candidateNumber
export const federalBaselineMunicipalitySlugs = (): string[] =>
Object.keys(artifact.municipalities)
const EMPTY_BASELINE: MunicipalityFederalBaseline = {
votesByYear: {},
validVotesByYear: {},
campoFederalVotesByYear: {},
federalTallyByYear: {},
}
/**
* Baseline for one catalog municipality slug. Unknown slugs return an empty
* baseline (renders as 0 votes) — the artifact↔catalog snapshot test is the
* guard against silent drift.
*/
export const getMunicipalityFederalBaseline = (slug: string): MunicipalityFederalBaseline =>
artifact.municipalities[slug] ?? EMPTY_BASELINE