@@ -17,7 +17,6 @@ limitations under the License.
1717package summarize
1818
1919import (
20- "encoding/json"
2120 "errors"
2221 "fmt"
2322 "io"
@@ -31,6 +30,7 @@ import (
3130 "golang.org/x/term"
3231
3332 "github.com/vitessio/vt/go/data"
33+ "github.com/vitessio/vt/go/web"
3434)
3535
3636type (
@@ -40,12 +40,22 @@ type (
4040 }
4141)
4242
43+ type SummaryOutput struct {
44+ Summary
45+ DateOfAnalysis string
46+ }
47+
4348type summaryWorker = func (s * Summary ) error
4449
45- func Run (files []string , hotMetric string , showGraph bool , outputFormat string , port * int64 ) {
50+ func Run (files []string , hotMetric string , showGraph bool , outputFormat string , launchWebServer bool ) {
4651 var traces []traceSummary
4752 var workers []summaryWorker
4853
54+ if launchWebServer && outputFormat != "html" {
55+ fmt .Println ("cannot use --web flag without --format=html" )
56+ os .Exit (1 )
57+ }
58+
4959 for _ , file := range files {
5060 typ , err := data .GetFileType (file )
5161 exitIfError (err )
@@ -77,7 +87,7 @@ func Run(files []string, hotMetric string, showGraph bool, outputFormat string,
7787
7888 traceCount := len (traces )
7989 if traceCount <= 0 {
80- s , err := printSummary (hotMetric , workers , outputFormat , port )
90+ s , err := printSummary (hotMetric , workers , outputFormat , launchWebServer )
8191 exitIfError (err )
8292 if showGraph {
8393 err := renderQueryGraph (s )
@@ -106,7 +116,7 @@ func exitIfError(err error) {
106116 os .Exit (1 )
107117}
108118
109- func printSummary (hotMetric string , workers []summaryWorker , outputFormat string , port * int64 ) (* Summary , error ) {
119+ func printSummary (hotMetric string , workers []summaryWorker , outputFormat string , launchWebServer bool ) (* Summary , error ) {
110120 s , err := NewSummary (hotMetric )
111121 if err != nil {
112122 return nil , err
@@ -118,25 +128,27 @@ func printSummary(hotMetric string, workers []summaryWorker, outputFormat string
118128 }
119129 }
120130 outputFormat = strings .ToLower (outputFormat )
121- if * port == 0 && outputFormat == "html" {
122- fmt .Println ("port is required when output format is html" )
123- os .Exit (1 )
124- }
125131 switch outputFormat {
126132 case "html" :
127- summaryJSON , err := json .Marshal (* s )
128- if err != nil {
129- fmt .Println ("Error marshalling summary:" , err )
130- return nil , err
133+ summarizeOutput := SummaryOutput {
134+ Summary : * s ,
135+ DateOfAnalysis : time .Now ().Format (time .DateTime ),
131136 }
132137
133- tmpFile , err := writeToTempFile (summaryJSON )
134- if err != nil {
135- return s , err
136- }
137- err = launchInBrowser (tmpFile )
138- if err != nil {
139- return s , err
138+ if launchWebServer {
139+ err = launchInBrowser (summarizeOutput )
140+ if err != nil {
141+ return s , err
142+ }
143+ } else {
144+ html , err := web .RenderFile ("summarize.html" , "layout_standalone.html" , summarizeOutput )
145+ if err != nil {
146+ return nil , err
147+ }
148+ _ , err = io .Copy (os .Stdout , html )
149+ if err != nil {
150+ return nil , err
151+ }
140152 }
141153 case "markdown" :
142154 // Print the response
@@ -150,16 +162,30 @@ func printSummary(hotMetric string, workers []summaryWorker, outputFormat string
150162 return s , nil
151163}
152164
153- func launchInBrowser (tmpFile * os.File ) error {
165+ func launchInBrowser (summarizeOutput SummaryOutput ) error {
166+ html , err := web .RenderFile ("summarize.html" , "layout.html" , summarizeOutput )
167+ if err != nil {
168+ return err
169+ }
170+ tmpFile , err := writeToTempFile (html .Bytes ())
171+ if err != nil {
172+ return err
173+ }
174+
175+ ch := make (chan error )
176+ go func () {
177+ web .Run (8080 )
178+ ch <- nil
179+ }()
154180 port := int64 (8080 ) // FIXME: take this from flags
155181 url := fmt .Sprintf ("http://localhost:%d/summarize?file=" , port ) + tmpFile .Name ()
156- err : = exec .Command ("open" , url ).Start ()
182+ err = exec .Command ("open" , url ).Start ()
157183 if err != nil {
158184 fmt .Println ("Error launching browser:" , err )
159185 return err
160186 }
161187 fmt .Println ("URL launched in default browser:" , url )
162- return nil
188+ return <- ch
163189}
164190
165191func writeToTempFile (summaryJSON []byte ) (* os.File , error ) {
0 commit comments