11package web
22
33import (
4+ "cmp"
45 "context"
56 "embed"
67 "fmt"
@@ -9,6 +10,7 @@ import (
910 "net/http"
1011 "net/http/pprof"
1112 "os"
13+ "slices"
1214 "strings"
1315
1416 "github.com/arl/statsviz"
@@ -73,7 +75,7 @@ func (s *Server) SetBaseURL(baseURL string) {
7375 s .baseURL = baseURL
7476}
7577
76- func (s * Server ) Start (_ context.Context ) {
78+ func (s * Server ) Start (context.Context ) {
7779 mux := http .NewServeMux ()
7880 if err := statsviz .Register (mux ); err != nil {
7981 s .log .Message ("failed to register statsviz handler: %v" , err )
@@ -89,41 +91,46 @@ func (s *Server) Start(_ context.Context) {
8991 return
9092 }
9193
92- var enableStrict , enableAnnotationProcessing , enablePrint bool
94+ var (
95+ enableStrict , enableAnnotationProcessing , enablePrint bool // TODO(sr): expose
96+ hideIdentical bool
97+ tmpl string
98+ )
9399
94100 if err := r .ParseForm (); err == nil {
95101 enableStrict = r .Form .Get ("strict" ) == "on"
96102 enableAnnotationProcessing = r .Form .Get ("annotations" ) == "on"
97103 enablePrint = r .Form .Get ("print" ) == "on"
104+ hideIdentical = r .Form .Get ("hide_identical" ) == "on"
105+ tmpl = cmp .Or (r .Form .Get ("tmpl" ), mainTemplate )
98106 }
99107
100108 cs := explorer .CompilerStages (path , policy , enableStrict , enableAnnotationProcessing , enablePrint )
101- st := state {Code : policy , Result : make ([]stringResult , len (cs )+ 1 )}
109+ n := len (cs )
110+ st := state {Code : policy , Result : make ([]stringResult , n + 1 )}
102111
103112 for i := range cs {
104- st .Result [i ].Stage = cs [i ].Stage
113+ show := i == 0 || st .Result [i - 1 ].Output != cs [i ].Result .String ()
114+
115+ st .Result [i ] = stringResult {Stage : cs [i ].Stage , Show : show }
116+
105117 if cs [i ].Error != "" {
106118 st .Result [i ].Output = cs [i ].Error
107119 st .Result [i ].Class = "bad"
108120 } else {
109121 st .Result [i ].Output = cs [i ].Result .String ()
110122 }
111123
112- st .Result [i ].Show = i == 0 || st .Result [i - 1 ].Output != st .Result [i ].Output
113124 if st .Result [i ].Class == "" {
114- if st . Result [ i ]. Show {
125+ if show {
115126 st .Result [i ].Class = "ok"
116127 } else {
117128 st .Result [i ].Class = "plain"
118129 }
119130 }
120131 }
121132
122- n := len (cs )
123-
124- st .Result [n ].Stage = "Plan"
125- st .Result [n ].Show = true
126-
133+ st .Result [n ] = stringResult {Stage : "Plan" , Show : true }
127134 if p , err := explorer .Plan (r .Context (), path , policy , enablePrint ); err != nil {
128135 st .Result [n ].Class = "bad"
129136 st .Result [n ].Output = err .Error ()
@@ -132,7 +139,13 @@ func (s *Server) Start(_ context.Context) {
132139 st .Result [n ].Output = p
133140 }
134141
135- if err := tpl .ExecuteTemplate (w , mainTemplate , st ); err != nil {
142+ if hideIdentical {
143+ st .Result = slices .CompactFunc (st .Result , func (a , b stringResult ) bool {
144+ return a .Output == b .Output
145+ })
146+ }
147+
148+ if err := tpl .ExecuteTemplate (w , tmpl , st ); err != nil {
136149 http .Error (w , err .Error (), http .StatusInternalServerError )
137150 }
138151 })
0 commit comments