Skip to main content

mz_deploy/project/analysis/changeset/
logging.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Verbose logging helpers for the Datalog fixed-point computation.
11//!
12//! These functions emit structured progress information when the user
13//! enables verbose output, making it easy to trace rule firings and
14//! convergence behavior.
15
16use super::base_facts::BaseFacts;
17use super::datalog::DirtyState;
18use crate::project::ir::object_id::ObjectId;
19use crate::verbose;
20use owo_colors::{OwoColorize, Stream, Style};
21use std::collections::BTreeSet;
22
23/// Emits an initial summary of inputs before rule evaluation starts.
24pub(super) fn log_datalog_start(changed_stmts: &BTreeSet<ObjectId>, base_facts: &BaseFacts) {
25    let header_style = Style::new().cyan().bold();
26    verbose!(
27        "{} {}",
28        "▶".if_supports_color(Stream::Stderr, |t| t.cyan()),
29        "Starting fixed-point computation..."
30            .if_supports_color(Stream::Stderr, |t| header_style.style(t))
31    );
32    verbose!(
33        "  ├─ Initial changed statements: [{}]",
34        changed_stmts
35            .iter()
36            .map(|o| o
37                .to_string()
38                .if_supports_color(Stream::Stderr, |t| t.cyan())
39                .to_string())
40            .collect::<Vec<_>>()
41            .join(", ")
42    );
43    verbose!(
44        "  └─ Known sinks: [{}]",
45        base_facts
46            .is_sink
47            .iter()
48            .map(|o| o
49                .to_string()
50                .if_supports_color(Stream::Stderr, |t| t.yellow())
51                .to_string())
52            .collect::<Vec<_>>()
53            .join(", ")
54    );
55}
56
57/// Emits per-iteration progress for dirty set growth.
58pub(super) fn log_iteration(iteration: usize, state: &DirtyState) {
59    let header_style = Style::new().cyan().bold();
60    verbose!(
61        "\n{} {} (stmts={}, clusters={}, schemas={})",
62        "▶".if_supports_color(Stream::Stderr, |t| t.cyan()),
63        format!("Iteration {}", iteration)
64            .if_supports_color(Stream::Stderr, |t| header_style.style(t)),
65        state
66            .dirty_stmts
67            .len()
68            .to_string()
69            .if_supports_color(Stream::Stderr, |t| t.bold()),
70        state
71            .dirty_clusters
72            .len()
73            .to_string()
74            .if_supports_color(Stream::Stderr, |t| t.bold()),
75        state
76            .dirty_schemas
77            .len()
78            .to_string()
79            .if_supports_color(Stream::Stderr, |t| t.bold())
80    );
81}
82
83/// Emits final dirty object/cluster/schema sets after convergence.
84pub(super) fn log_final_results(state: &DirtyState) {
85    let header_style = Style::new().cyan().bold();
86    verbose!(
87        "{} {}",
88        "▶".if_supports_color(Stream::Stderr, |t| t.cyan()),
89        "Final Results".if_supports_color(Stream::Stderr, |t| header_style.style(t))
90    );
91    verbose!(
92        "  ├─ Dirty statements ({}): [{}]",
93        state
94            .dirty_stmts
95            .len()
96            .to_string()
97            .if_supports_color(Stream::Stderr, |t| t.bold()),
98        state
99            .dirty_stmts
100            .iter()
101            .map(|o| o
102                .to_string()
103                .if_supports_color(Stream::Stderr, |t| t.cyan())
104                .to_string())
105            .collect::<Vec<_>>()
106            .join(", ")
107    );
108    verbose!(
109        "  ├─ Dirty clusters ({}): [{}]",
110        state
111            .dirty_clusters
112            .len()
113            .to_string()
114            .if_supports_color(Stream::Stderr, |t| t.bold()),
115        state
116            .dirty_clusters
117            .iter()
118            .map(|c| c
119                .if_supports_color(Stream::Stderr, |t| t.magenta())
120                .to_string())
121            .collect::<Vec<_>>()
122            .join(", ")
123    );
124    verbose!(
125        "  └─ Dirty schemas ({}): [{}]",
126        state
127            .dirty_schemas
128            .len()
129            .to_string()
130            .if_supports_color(Stream::Stderr, |t| t.bold()),
131        state
132            .dirty_schemas
133            .iter()
134            .map(|sq| format!("{}.{}", sq.database, sq.schema)
135                .if_supports_color(Stream::Stderr, |t| t.blue())
136                .to_string())
137            .collect::<Vec<_>>()
138            .join(", ")
139    );
140}