|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Print metrics documentation collected via `DocumentedMetrics`/`DocumentedExec`. |
| 19 | +//! Called from doc generation scripts to refresh `docs/source/user-guide/metrics.md`. |
| 20 | +
|
| 21 | +use std::collections::HashSet; |
| 22 | + |
| 23 | +use datafusion_doc::metric_doc_sections::{ |
| 24 | + ExecDoc, MetricDoc, MetricDocPosition, exec_docs, metric_docs, |
| 25 | +}; |
| 26 | +use datafusion_execution as _; // Link metrics defined in execution crate. |
| 27 | +use datafusion_physical_plan as _; // Link metrics and execs defined in physical plan. |
| 28 | + |
| 29 | +fn main() -> std::io::Result<()> { |
| 30 | + let mut content = String::new(); |
| 31 | + let mut metrics: Vec<&MetricDoc> = metric_docs().collect(); |
| 32 | + metrics.sort_by(|a, b| a.name.cmp(b.name)); |
| 33 | + |
| 34 | + let mut execs: Vec<&ExecDoc> = exec_docs().collect(); |
| 35 | + execs.sort_by(|a, b| a.name.cmp(b.name)); |
| 36 | + |
| 37 | + let common: Vec<&MetricDoc> = metrics |
| 38 | + .iter() |
| 39 | + .copied() |
| 40 | + .filter(|m| m.position == MetricDocPosition::Common) |
| 41 | + .collect(); |
| 42 | + |
| 43 | + // Collect names of common metric types for filtering embedded fields |
| 44 | + let common_metric_names: HashSet<&str> = common.iter().map(|m| m.name).collect(); |
| 45 | + |
| 46 | + if !common.is_empty() { |
| 47 | + content.push_str("## Common Metrics\n\n"); |
| 48 | + for metric in common { |
| 49 | + render_metric_doc(&mut content, metric, 3, &common_metric_names); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if !execs.is_empty() { |
| 54 | + content.push_str("## Operator-specific Metrics\n\n"); |
| 55 | + for exec in execs { |
| 56 | + render_exec_doc(&mut content, exec, &common_metric_names); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + println!("{content}"); |
| 61 | + Ok(()) |
| 62 | +} |
| 63 | + |
| 64 | +fn render_exec_doc( |
| 65 | + out: &mut String, |
| 66 | + exec: &ExecDoc, |
| 67 | + common_metric_names: &HashSet<&str>, |
| 68 | +) { |
| 69 | + out.push_str(&heading(3, exec.name)); |
| 70 | + out.push_str("\n\n"); |
| 71 | + |
| 72 | + if let Some(doc) = summarize(exec.doc) { |
| 73 | + if !doc.is_empty() { |
| 74 | + out.push_str(&sanitize(doc)); |
| 75 | + out.push_str("\n\n"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Filter to operator-specific metrics only (common metrics are documented separately) |
| 80 | + let mut metrics: Vec<&MetricDoc> = exec |
| 81 | + .metrics |
| 82 | + .iter() |
| 83 | + .copied() |
| 84 | + .filter(|metric| metric.position != MetricDocPosition::Common) |
| 85 | + .collect(); |
| 86 | + metrics.sort_by(|a, b| a.name.cmp(b.name)); |
| 87 | + |
| 88 | + if metrics.is_empty() { |
| 89 | + out.push_str("_No operator-specific metrics documented._\n\n"); |
| 90 | + } else { |
| 91 | + for metric in metrics { |
| 92 | + render_metric_doc(out, metric, 4, common_metric_names); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn render_metric_doc( |
| 98 | + out: &mut String, |
| 99 | + metric: &MetricDoc, |
| 100 | + heading_level: usize, |
| 101 | + common_metric_names: &HashSet<&str>, |
| 102 | +) { |
| 103 | + out.push_str(&heading(heading_level, metric.name)); |
| 104 | + out.push_str("\n\n"); |
| 105 | + |
| 106 | + if let Some(doc) = summarize(metric.doc) { |
| 107 | + if !doc.is_empty() { |
| 108 | + out.push_str(&sanitize(doc)); |
| 109 | + out.push_str("\n\n"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Filter out fields whose type is a common metric (documented separately) |
| 114 | + let fields: Vec<_> = metric |
| 115 | + .fields |
| 116 | + .iter() |
| 117 | + .filter(|field| !common_metric_names.contains(field.type_name)) |
| 118 | + .collect(); |
| 119 | + |
| 120 | + if fields.is_empty() { |
| 121 | + out.push_str("_No metrics documented._\n\n"); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + out.push_str("| Metric | Description |\n"); |
| 126 | + out.push_str("| --- | --- |\n"); |
| 127 | + for field in fields { |
| 128 | + out.push_str(&format!("| {} | {} |\n", field.name, sanitize(field.doc))); |
| 129 | + } |
| 130 | + out.push('\n'); |
| 131 | +} |
| 132 | + |
| 133 | +fn heading(level: usize, title: &str) -> String { |
| 134 | + format!("{} {}", "#".repeat(level), title) |
| 135 | +} |
| 136 | + |
| 137 | +fn summarize(doc: &str) -> Option<&str> { |
| 138 | + let trimmed = doc.trim(); |
| 139 | + if trimmed.is_empty() { |
| 140 | + return None; |
| 141 | + } |
| 142 | + trimmed.split("\n\n").next().map(str::trim) |
| 143 | +} |
| 144 | + |
| 145 | +fn sanitize(doc: &str) -> String { |
| 146 | + doc.split_whitespace().collect::<Vec<_>>().join(" ") |
| 147 | +} |
0 commit comments