// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * @file The align phase's accumulators as a record a worker writes and a coordinator merges. * * A serial build fills one set of accumulators by visiting every source's rows in adapter order. A parallel build * fills one set per align range. The coordinator merges those sets in adapter order, then range order, and visits the same rows * in the same order, so every sum is equal and every map lists its keys in the order the serial build first saw * them. The build manifest serializes those maps, and a key order that followed worker scheduling would change * its bytes. * * Maps are stored as entry arrays rather than objects, because an object lists integer-like keys first. */ import type { LicenseRefusalKind } from "@mailwoman/license/policy" import type { AlignPhaseResult, AlignTally } from "#build/align" import type { ComponentFindingCounts, ComponentRule } from "#build/component-check" import type { SplitName } from "#utils/split" /** * The align accumulators of one source, one align range, or one whole build. */ export interface AlignTotals extends AlignTally { counts: Record licenseCounts: Map refusedByKind: Map refusedBySource: Map trainRowsLeftOutBySource: Map rowsBySource: Map ineligibleSources: Map /** * Every license value the policy refused, with its refusal kind. */ refusedValues: Map componentFindingsBySource: ComponentFindingCounts uncheckedComponentRules: Set regionValues: Map } /** * {@linkcode AlignTotals} as JSON. */ export interface AlignTotalsRecord { aligned: number quarantined: number excluded_by_license: number admitted_unresolved_license_rows: number excluded_by_eligibility: number counts: Record license_counts: [string, number][] refused_by_kind: [LicenseRefusalKind, number][] refused_by_source: [string, number][] /** * Absent from a record written before `trainShare` existed. */ train_rows_left_out_by_source?: [string, number][] rows_by_source: [string, { rows: number; license: string }][] ineligible_sources: [string, readonly string[]][] refused_license_values: [string, LicenseRefusalKind][] component_findings_by_source: [string, [ComponentRule, number][]][] unchecked_component_rules: ComponentRule[] /** * The rows that reached the component check with a region, per `regionValueKey`. * * Absent from a record written before the region data left the source derivation. */ region_values?: [string, number][] } /** * The totals of a finished align phase. */ export function alignTotalsOf(result: AlignPhaseResult): AlignTotals { return { aligned: result.aligned, quarantined: result.quarantined, excludedByLicense: result.excludedByLicense, admittedUnresolved: result.admittedUnresolved, excludedByEligibility: result.excludedByEligibility, counts: { ...result.counts }, licenseCounts: result.licenseCounts, refusedByKind: result.refusedByKind, refusedBySource: result.refusedBySource, trainRowsLeftOutBySource: result.trainRowsLeftOutBySource, rowsBySource: result.rowsBySource, ineligibleSources: result.ineligibleSources, refusedValues: result.licenses.refusedValues(), componentFindingsBySource: result.componentFindingsBySource, uncheckedComponentRules: result.uncheckedComponentRules, regionValues: result.regionValues, } } export function serializeAlignTotals(totals: AlignTotals): AlignTotalsRecord { return { aligned: totals.aligned, quarantined: totals.quarantined, excluded_by_license: totals.excludedByLicense, admitted_unresolved_license_rows: totals.admittedUnresolved, excluded_by_eligibility: totals.excludedByEligibility, counts: { ...totals.counts }, license_counts: [...totals.licenseCounts], refused_by_kind: [...totals.refusedByKind], refused_by_source: [...totals.refusedBySource], train_rows_left_out_by_source: [...totals.trainRowsLeftOutBySource], rows_by_source: [...totals.rowsBySource], ineligible_sources: [...totals.ineligibleSources], refused_license_values: [...totals.refusedValues], component_findings_by_source: [...totals.componentFindingsBySource].map(([source, rules]) => [source, [...rules]]), unchecked_component_rules: [...totals.uncheckedComponentRules], region_values: [...totals.regionValues], } } function addCounts(into: Map, entries: Iterable<[K, number]>): void { for (const [key, count] of entries) { into.set(key, (into.get(key) ?? 0) + count) } } function keepFirst(into: Map, entries: Iterable<[K, V]>): void { for (const [key, value] of entries) { if (!into.has(key)) { into.set(key, value) } } } /** * Merge records in the order their rows would have been aligned by one serial pass. * * Sums add. * `rowsBySource` keeps the license of the last record that saw the source, because the * serial align phase records each contributing row's license over the one before. * * The ineligibility reasons and refused values keep their first sighting, as the serial phase's caches do. */ export function mergeAlignTotals(records: readonly AlignTotalsRecord[]): AlignTotals { const totals: AlignTotals = { aligned: 0, quarantined: 0, excludedByLicense: 0, admittedUnresolved: 0, excludedByEligibility: 0, counts: { train: 0, val: 0, test: 0 }, licenseCounts: new Map(), refusedByKind: new Map(), refusedBySource: new Map(), trainRowsLeftOutBySource: new Map(), rowsBySource: new Map(), ineligibleSources: new Map(), refusedValues: new Map(), componentFindingsBySource: new Map(), uncheckedComponentRules: new Set(), regionValues: new Map(), } for (const record of records) { totals.aligned += record.aligned totals.quarantined += record.quarantined totals.excludedByLicense += record.excluded_by_license totals.admittedUnresolved += record.admitted_unresolved_license_rows totals.excludedByEligibility += record.excluded_by_eligibility for (const split of ["train", "val", "test"] as const) { totals.counts[split] += record.counts[split] } addCounts(totals.licenseCounts, record.license_counts) addCounts(totals.refusedByKind, record.refused_by_kind) addCounts(totals.refusedBySource, record.refused_by_source) addCounts(totals.trainRowsLeftOutBySource, record.train_rows_left_out_by_source ?? []) addCounts(totals.regionValues, record.region_values ?? []) for (const [source, contributed] of record.rows_by_source) { const before = totals.rowsBySource.get(source) totals.rowsBySource.set(source, { rows: (before?.rows ?? 0) + contributed.rows, license: contributed.license }) } keepFirst(totals.ineligibleSources, record.ineligible_sources) keepFirst(totals.refusedValues, record.refused_license_values) for (const [source, rules] of record.component_findings_by_source) { const into = totals.componentFindingsBySource.get(source) ?? new Map() totals.componentFindingsBySource.set(source, into) addCounts(into, rules) } // A rule one record couldn't check leaves the merged counts short, so it stays unchecked for the whole. for (const rule of record.unchecked_component_rules) { totals.uncheckedComponentRules.add(rule) } } return totals }