33use std:: collections:: HashMap ;
44
55use crate :: planner_identity:: constants:: MISSING_MESSAGE_ITEM_LIMIT ;
6- use crate :: planner_identity:: models:: { GraphKey , MissingUpstream } ;
6+ use crate :: planner_identity:: models:: { GraphKey , MissingUpstream , MissingUpstreamFixes } ;
77
8- /// Missing lineage upstreams by name and, when only an audit or hook read requires one, that read's origin.
8+ /// Missing lineage upstreams with how to fix them and, when only an audit or hook read requires one, that read's origin.
99pub fn missing_upstream_message (
1010 missing : & [ MissingUpstream ] ,
1111 edge_origins : & HashMap < ( GraphKey , GraphKey ) , String > ,
12+ fixes : & MissingUpstreamFixes ,
1213) -> String {
13- let mut lineage_missing: Vec < & MissingUpstream > = Vec :: new ( ) ;
14+ let mut lineage_missing: Vec < & str > = Vec :: new ( ) ;
15+ let mut kinds: std:: collections:: BTreeSet < & str > = std:: collections:: BTreeSet :: new ( ) ;
1416 let mut read_names: Vec < & str > = Vec :: new ( ) ;
1517 for entry in missing {
1618 let origins: Vec < & str > = entry
@@ -23,7 +25,8 @@ pub fn missing_upstream_message(
2325 } )
2426 . collect ( ) ;
2527 if origins. len ( ) < entry. required_by . len ( ) {
26- lineage_missing. push ( entry) ;
28+ lineage_missing. push ( entry. key . name . as_str ( ) ) ;
29+ kinds. insert ( entry. key . kind . as_str ( ) ) ;
2730 continue ;
2831 }
2932 for origin in origins {
@@ -32,39 +35,119 @@ pub fn missing_upstream_message(
3235 }
3336 }
3437 }
35- let mut parts: Vec < String > = Vec :: new ( ) ;
36- if !lineage_missing. is_empty ( ) {
37- let names: Vec < & str > = lineage_missing
38+ let reads: Option < String > = audit_reads_text ( & read_names) ;
39+ if lineage_missing. is_empty ( ) {
40+ return format ! ( "cannot build selected scope: {}" , reads. unwrap_or_default( ) ) ;
41+ }
42+ let mut text: String = lineage_text ( & lineage_missing, & kinds, fixes) ;
43+ if let Some ( reads) = reads {
44+ text. push_str ( & format ! ( "\n \n Also, {reads}" ) ) ;
45+ }
46+ text
47+ }
48+
49+ /// The missing lineage upstreams, a few by name, then how to read, clone or build them.
50+ fn lineage_text (
51+ names : & [ & str ] ,
52+ kinds : & std:: collections:: BTreeSet < & str > ,
53+ fixes : & MissingUpstreamFixes ,
54+ ) -> String {
55+ let one: bool = names. len ( ) == 1 ;
56+ let noun: String = match kinds. iter ( ) . collect :: < Vec < _ > > ( ) . as_slice ( ) {
57+ [ kind] if one => ( * kind) . to_string ( ) ,
58+ [ kind] => format ! ( "{kind}s" ) ,
59+ _ => "models and seeds" . to_owned ( ) ,
60+ } ;
61+ let place: String = fixes. target . as_ref ( ) . map_or_else (
62+ || "the warehouse" . to_owned ( ) ,
63+ |target| format ! ( "target '{target}'" ) ,
64+ ) ;
65+ let mut lines: Vec < String > = vec ! [ format!(
66+ "{} upstream {noun} this build reads {} exist in {place}" ,
67+ names. len( ) ,
68+ if one { "doesn't" } else { "don't" }
69+ ) ] ;
70+ lines. extend (
71+ names
3872 . iter ( )
3973 . take ( MISSING_MESSAGE_ITEM_LIMIT )
40- . map ( |entry| entry . key . name . as_str ( ) )
41- . collect ( ) ;
42- parts . push ( format ! (
43- "{} missing upstream dependencies ({})" ,
44- lineage_missing . len ( ) ,
45- names. join ( ", " )
74+ . map ( |name| format ! ( " {name}" ) ) ,
75+ ) ;
76+ if names . len ( ) > MISSING_MESSAGE_ITEM_LIMIT {
77+ lines . push ( format ! (
78+ " ... {} more (sqb plan --json lists all)" ,
79+ names. len ( ) - MISSING_MESSAGE_ITEM_LIMIT
4680 ) ) ;
4781 }
48- if let [ name] = read_names. as_slice ( ) {
49- parts. push ( format ! (
50- "{name}, which is not selected and does not exist in the warehouse; select it or build it first"
51- ) ) ;
52- } else if !read_names. is_empty ( ) {
53- let shown: String = read_names
54- . iter ( )
55- . take ( MISSING_MESSAGE_ITEM_LIMIT )
56- . copied ( )
57- . collect :: < Vec < & str > > ( )
58- . join ( "; " ) ;
59- let more: & str = if read_names. len ( ) > MISSING_MESSAGE_ITEM_LIMIT {
60- "; ..."
82+ lines. push ( String :: new ( ) ) ;
83+ let ( exists, them) : ( & str , & str ) = if one {
84+ ( "it exists" , "it" )
85+ } else {
86+ ( "they exist" , "them" )
87+ } ;
88+ let read_label: String = format ! ( "read {them} from it for this run:" ) ;
89+ let copy_label: String = format ! ( "copy {them} into this target:" ) ;
90+ let build_label: String = format ! (
91+ "{} {them} too:" ,
92+ if fixes. origins. is_empty( ) {
93+ "Build"
6194 } else {
62- ""
63- } ;
64- parts. push ( format ! (
65- "{} audit reads are not selected and do not exist in the warehouse ({shown}{more}); select them or build them first" ,
66- read_names. len( )
95+ "Or build"
96+ }
97+ ) ;
98+ let width: usize = if fixes. origins . is_empty ( ) {
99+ build_label. len ( ) + 2
100+ } else {
101+ ( read_label. len ( ) + 4 ) . max ( copy_label. len ( ) + 4 )
102+ }
103+ . max ( build_label. len ( ) + 2 ) ;
104+ let to: String = fixes
105+ . clone_to
106+ . as_ref ( )
107+ . map ( |target| format ! ( " --to {target}" ) )
108+ . unwrap_or_default ( ) ;
109+ for origin in & fixes. origins {
110+ lines. push ( format ! ( " If {exists} in {origin}:" ) ) ;
111+ lines. push ( format ! (
112+ " {read_label:<0$} --defer-to {origin}" ,
113+ width - 4
67114 ) ) ;
115+ lines. push ( format ! (
116+ " {copy_label:<0$} sqb clone --from {origin}{to} -s {1}" ,
117+ width - 4 ,
118+ names. join( " " )
119+ ) ) ;
120+ }
121+ lines. push ( format ! (
122+ " {build_label:<0$} add + before your selection" ,
123+ width - 2
124+ ) ) ;
125+ lines. join ( "\n " )
126+ }
127+
128+ /// The audit and hook reads that need a missing upstream, a few by name.
129+ fn audit_reads_text ( read_names : & [ & str ] ) -> Option < String > {
130+ match read_names {
131+ [ ] => None ,
132+ [ name] => Some ( format ! (
133+ "{name}, which is not selected and does not exist in the warehouse; select it or build it first"
134+ ) ) ,
135+ _ => {
136+ let shown: String = read_names
137+ . iter ( )
138+ . take ( MISSING_MESSAGE_ITEM_LIMIT )
139+ . copied ( )
140+ . collect :: < Vec < & str > > ( )
141+ . join ( "; " ) ;
142+ let more: & str = if read_names. len ( ) > MISSING_MESSAGE_ITEM_LIMIT {
143+ "; ..."
144+ } else {
145+ ""
146+ } ;
147+ Some ( format ! (
148+ "{} audit reads are not selected and do not exist in the warehouse ({shown}{more}); select them or build them first" ,
149+ read_names. len( )
150+ ) )
151+ }
68152 }
69- format ! ( "cannot build selected scope: {}" , parts. join( "; " ) )
70153}
0 commit comments