fix(anatomy): R5.4 review drawings bounded per annotation, report tables summed, docs counts
This commit is contained in:
1 parent
29eb499539
commit
411e1058c0
9 files changed
+271
-36
No files matched your search
@@ -97,7 +97,11 @@ fn record_row(record: &ChangeRecord) -> Result<Vec<Cell>, PipelineError> {
|
||||
])
|
||||
}
|
||||
|
||||
fn level_section(level: ChangeLevel, records: &[ChangeRecord]) -> Result<String, PipelineError> {
|
||||
fn level_section(
|
||||
level: ChangeLevel,
|
||||
records: &[ChangeRecord],
|
||||
spent: &mut usize,
|
||||
) -> Result<String, PipelineError> {
|
||||
let rows = records
|
||||
.iter()
|
||||
.filter(|record| record.change.level == level)
|
||||
@@ -113,9 +117,9 @@ fn level_section(level: ChangeLevel, records: &[ChangeRecord]) -> Result<String,
|
||||
"Références",
|
||||
"Confirmation requise",
|
||||
];
|
||||
// The rows are bounded through the records' weight; their alignment is checked here,
|
||||
// before the raw table is joined.
|
||||
assert_table_bound(&head, &rows)?;
|
||||
// The rows are bounded through the records' weight; the alignment of the three level
|
||||
// tables together is checked here, before each raw table is joined.
|
||||
assert_table_bound(&head, &rows, spent)?;
|
||||
let body = if rows.is_empty() {
|
||||
"Aucun changement de ce niveau.".to_owned()
|
||||
} else {
|
||||
@@ -192,8 +196,9 @@ pub fn render_changelog(
|
||||
comparison(before, after),
|
||||
migration(migration_stats),
|
||||
];
|
||||
let mut spent = 0;
|
||||
for level in LEVELS {
|
||||
sections.push(level_section(level, records)?);
|
||||
sections.push(level_section(level, records, &mut spent)?);
|
||||
}
|
||||
Ok(format!("{}\n", sections.join("\n\n")))
|
||||
}
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
use serde_json::{Map, Value, json};
|
||||
|
||||
use crate::json::to_js_compact;
|
||||
|
||||
use super::app_review::render_app_review_drawing;
|
||||
use super::apply::{apply_changeset, curated_issues};
|
||||
use super::changelog::render_changelog;
|
||||
@@ -199,23 +201,77 @@ fn view_ids(document: &Value) -> Vec<String> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The review drawings' bytes, projected before any is rendered: every drawing repeats the
|
||||
/// whole cleaned drawing, two per view (the equine draft: 2 views, about 0.55 MB). Views added
|
||||
/// by a changeset multiply it (2,000 views asked for 547 MB, security gate R5.4 round 8).
|
||||
/// The review drawings' bytes, projected as an upper bound before any is rendered (the equine
|
||||
/// draft projects 1.4 MB for 0.56 MB written): every drawing repeats the whole cleaned drawing,
|
||||
/// two per view, and each annotation of a view adds its markup. Views added by a changeset
|
||||
/// multiply the drawing (2,000 views asked for 547 MB, security gate R5.4 round 8); unnamed
|
||||
/// triggers repeat the opened view's names in theirs (a 485 KB changeset wrote a 131 MB
|
||||
/// drawing, round 10).
|
||||
pub const MAX_REVIEW_DRAWINGS_BYTES: usize = 64 * 1024 * 1024;
|
||||
|
||||
/// An upper bound of what one annotation writes in a drawing: a fixed part (tags, styles,
|
||||
/// numbers the renderer adds), then its own JSON text and the longest view name, each copied
|
||||
/// at most four times (data attributes and titles of the hit box and the visible circle) and
|
||||
/// escaped at most six times longer (`"` → `"`).
|
||||
fn annotation_bytes(annotation: &Value, view_names: usize) -> Result<usize, PipelineError> {
|
||||
let text = to_js_compact(annotation).map_err(|error| fail(format!("annotation: {error}")))?;
|
||||
Ok(1024 + 24 * (text.len() + view_names))
|
||||
}
|
||||
|
||||
/// The bound of the markup a document's annotations add to one view's drawing.
|
||||
fn layer_bytes(document: &Value, view_id: &str, view_names: usize) -> Result<usize, PipelineError> {
|
||||
let annotations = document.get("annotations").and_then(Value::as_array);
|
||||
let mut total = 0usize;
|
||||
for annotation in annotations.into_iter().flatten() {
|
||||
if annotation.get("view_id").and_then(Value::as_str) == Some(view_id) {
|
||||
total = total.saturating_add(annotation_bytes(annotation, view_names)?);
|
||||
}
|
||||
}
|
||||
Ok(total)
|
||||
}
|
||||
|
||||
/// The longest JSON text of a view name, in either document.
|
||||
fn longest_view_name(documents: [&Value; 2]) -> Result<usize, PipelineError> {
|
||||
let mut longest = 0;
|
||||
for document in documents {
|
||||
let views = document.get("views").and_then(Value::as_array);
|
||||
for name in views
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|view| view.get("name"))
|
||||
{
|
||||
let text = to_js_compact(name).map_err(|error| fail(format!("view: {error}")))?;
|
||||
longest = longest.max(text.len());
|
||||
}
|
||||
}
|
||||
Ok(longest)
|
||||
}
|
||||
|
||||
/// The projected bytes of the review drawings: per view, the overlay (both documents' markers)
|
||||
/// and the application's drawing (the curated layer), each over the cleaned drawing.
|
||||
pub fn review_drawings_bytes(draft: &DraftResult) -> Result<usize, PipelineError> {
|
||||
let names = longest_view_name([&draft.baseline, &draft.document])?;
|
||||
let mut total = 0usize;
|
||||
for view_id in view_ids(&draft.document) {
|
||||
let after = layer_bytes(&draft.document, &view_id, names)?;
|
||||
let before = layer_bytes(&draft.baseline, &view_id, names)?;
|
||||
let view = draft.cleaned_svg.len().saturating_mul(2) + before + after.saturating_mul(2);
|
||||
total = total.saturating_add(view);
|
||||
}
|
||||
Ok(total)
|
||||
}
|
||||
|
||||
/// One review overlay per view (`review/annotations-<view>.svg`), then the application's
|
||||
/// drawing of each view (`review/app-<view>.svg`).
|
||||
fn review_drawings(draft: &DraftResult) -> Result<Vec<(String, String)>, PipelineError> {
|
||||
let views = view_ids(&draft.document);
|
||||
let projected = draft.cleaned_svg.len().saturating_mul(2 * views.len());
|
||||
let projected = review_drawings_bytes(draft)?;
|
||||
if projected > MAX_REVIEW_DRAWINGS_BYTES {
|
||||
return Err(fail(format!(
|
||||
"{} views would write {projected} bytes of review drawings, past the \
|
||||
{MAX_REVIEW_DRAWINGS_BYTES} bound",
|
||||
views.len()
|
||||
"the review drawings would write up to {projected} bytes, past the \
|
||||
{MAX_REVIEW_DRAWINGS_BYTES} bound"
|
||||
)));
|
||||
}
|
||||
let views = view_ids(&draft.document);
|
||||
let drawing = read_cleaned_drawing(&draft.cleaned_svg)?;
|
||||
let typed: AnatomicalSheetDocument = serde_json::from_value(draft.document.clone())
|
||||
.map_err(|error| fail(format!("curated document: {error}")))?;
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
use serde::Serialize;
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use super::schema::{ChangeSummary, Op};
|
||||
use super::schema::{ChangeSummary, Op, Position};
|
||||
use super::targets::{Item, Path, Step, at_mut};
|
||||
use crate::curation::PipelineError;
|
||||
|
||||
@@ -187,15 +187,23 @@ fn parts_weight(parts: &RecordParts<'_>) -> usize {
|
||||
fn op_weight(op: &Op) -> usize {
|
||||
let carried = match op {
|
||||
Op::Set(op) => text_weight(&op.field) + value_weight(&op.value),
|
||||
Op::Add(op) => value_weight(&op.value),
|
||||
Op::Add(op) => value_weight(&op.value) + position_weight(op.position.as_ref()),
|
||||
Op::Rename(op) => text_weight(&op.id),
|
||||
Op::Move(op) => text_weight(&op.to),
|
||||
Op::Move(op) => text_weight(&op.to) + position_weight(op.position.as_ref()),
|
||||
Op::Cite(op) => op.reference_ids.iter().map(|id| text_weight(id)).sum(),
|
||||
Op::Remove(_) => 0,
|
||||
};
|
||||
64 + text_weight(op.target()) + carried
|
||||
}
|
||||
|
||||
/// The anchor an add or a move clones with its op.
|
||||
fn position_weight(position: Option<&Position>) -> usize {
|
||||
match position {
|
||||
Some(Position::After { after: id } | Position::Before { before: id }) => text_weight(id),
|
||||
Some(Position::Index(_)) | None => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// A cloned string: its header and allocation, then its bytes.
|
||||
fn text_weight(text: &str) -> usize {
|
||||
64 + text.len()
|
||||
|
||||
@@ -48,23 +48,29 @@ pub fn cell(value: &Cell) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// Refuses a table whose alignment would pass `MAX_TABLE_COLUMNS_TOTAL`, from its cells, before
|
||||
/// the raw table is joined: a cell's bytes bound its display width (an emoji, the one glyph
|
||||
/// wider than its bytes, is refused by the width table), so the projection never under-counts.
|
||||
pub fn assert_table_bound(head: &[&str], rows: &[Vec<Cell>]) -> Result<(), PipelineError> {
|
||||
/// Refuses tables whose alignment would pass `MAX_TABLE_COLUMNS_TOTAL` together (`spent` carries
|
||||
/// the projections of the report's earlier tables), from their cells, before the raw table is
|
||||
/// joined: a cell's bytes bound its display width (an emoji, the one glyph wider than its bytes,
|
||||
/// is refused by the width table), so the projection never under-counts.
|
||||
pub fn assert_table_bound(
|
||||
head: &[&str],
|
||||
rows: &[Vec<Cell>],
|
||||
spent: &mut usize,
|
||||
) -> Result<(), PipelineError> {
|
||||
let mut widths: Vec<usize> = head.iter().map(|text| text.len().max(3)).collect();
|
||||
for row in rows {
|
||||
for (width, value) in widths.iter_mut().zip(row) {
|
||||
let bytes = match value {
|
||||
Cell::Text(text) => text.len() + text.matches('|').count(),
|
||||
Cell::Number(_) => 24,
|
||||
Cell::Number(_) => 25,
|
||||
Cell::Null => 1,
|
||||
};
|
||||
*width = (*width).max(bytes);
|
||||
}
|
||||
}
|
||||
let row_width = widths.iter().sum::<usize>() + 3 * widths.len() + 1;
|
||||
if row_width.saturating_mul(rows.len() + 2) > MAX_TABLE_COLUMNS_TOTAL {
|
||||
*spent = spent.saturating_add(row_width.saturating_mul(rows.len() + 2));
|
||||
if *spent > MAX_TABLE_COLUMNS_TOTAL {
|
||||
return Err(PipelineError::Guard(format!(
|
||||
"an aligned table of {} rows × {row_width} columns exceeds the {MAX_TABLE_COLUMNS_TOTAL} bound",
|
||||
rows.len() + 2
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
//! punctuation of `PUNCTUATION`, an underscore between two ASCII letters or digits and not
|
||||
//! glued to a URL, a backslash before `"` or `|` (a JSON quote, the cell escape), `**word**`
|
||||
//! (one run of letters between spaces or line edges, the import report's form), no inline
|
||||
//! construct (`rewritten_construct`). Inside code spans (the generated ids and targets):
|
||||
//! anything but a carriage return or a tab, never empty, no outer space; an authored text holds
|
||||
//! construct (`rewritten_construct`). Inside code spans (the generated ids, targets, field
|
||||
//! names and file paths): only `[A-Za-z0-9_:/. -]`, never empty, no outer space; an authored text holds
|
||||
//! no backtick (`assert_authored`), and a plain underscore is never followed by another one on its
|
||||
//! line.
|
||||
|
||||
|
||||
@@ -511,9 +511,38 @@ fn review_drawings_past_their_bound_are_refused_before_rendering() {
|
||||
}
|
||||
let error = render_draft_artifacts(&draft, &result.anomalies).unwrap_err();
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("bytes of review drawings, past the"),
|
||||
error.to_string().contains("past the 67108864 bound"),
|
||||
"{error}"
|
||||
);
|
||||
}
|
||||
|
||||
/// An unnamed trigger writes the opened view's names in its markup: many of them opening a
|
||||
/// long-named view are refused before rendering too (security gate R5.4 round 10: a 485 KB
|
||||
/// changeset wrote a 131 MB drawing). The equine draft stays far below the bound.
|
||||
#[test]
|
||||
fn unnamed_triggers_repeating_a_long_view_name_are_refused_before_rendering() {
|
||||
use faunosteo_core::curation::draft::pipeline::{
|
||||
MAX_REVIEW_DRAWINGS_BYTES, review_drawings_bytes,
|
||||
};
|
||||
let (result, mut draft) = equine();
|
||||
let equine = review_drawings_bytes(&draft).unwrap();
|
||||
println!("equine review drawings projected at {equine} bytes");
|
||||
assert!(equine * 4 < MAX_REVIEW_DRAWINGS_BYTES, "{equine}");
|
||||
draft.document["views"][1]["name"]["fr"] = Value::from("t".repeat(32_768));
|
||||
let annotations = draft.document["annotations"].as_array_mut().unwrap();
|
||||
let trigger = annotations
|
||||
.iter()
|
||||
.find(|a| a["kind"] == "detail_trigger")
|
||||
.unwrap()
|
||||
.clone();
|
||||
for index in 0..1_000 {
|
||||
let mut copy = trigger.clone();
|
||||
copy["id"] = Value::from(format!("trigger-{index}"));
|
||||
annotations.push(copy);
|
||||
}
|
||||
let error = render_draft_artifacts(&draft, &result.anomalies).unwrap_err();
|
||||
assert!(
|
||||
error.to_string().contains("past the 67108864 bound"),
|
||||
"{error}"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user