diff --git a/cmd/alp/cmd/flags.go b/cmd/alp/cmd/flags.go index 9cab9a8..cce8e3f 100644 --- a/cmd/alp/cmd/flags.go +++ b/cmd/alp/cmd/flags.go @@ -97,7 +97,7 @@ func (f *flags) defineLoad(cmd *cobra.Command) { } func (f *flags) defineFormat(cmd *cobra.Command) { - cmd.PersistentFlags().StringP(flagFormat, "", options.DefaultFormatOption, "The output format (table, markdown, tsv, csv and html)") + cmd.PersistentFlags().StringP(flagFormat, "", options.DefaultFormatOption, "The output format (table, markdown, tsv, csv, html, and json)") } func (f *flags) defineSort(cmd *cobra.Command) { diff --git a/convert/json.go b/convert/json.go new file mode 100644 index 0000000..f60b252 --- /dev/null +++ b/convert/json.go @@ -0,0 +1,30 @@ +package convert + +import "encoding/json" + +func ToJSONValues(data [][]string) [][]interface{} { + i := [][]interface{}{} + for _, values := range data { + l := []interface{}{} + for _, val := range values { + n := json.Number(val) + + i64, err := n.Int64() + if err == nil { + l = append(l, i64) + continue + } + + f64, err := n.Float64() + if err == nil { + l = append(l, f64) + continue + } + + l = append(l, val) + } + i = append(i, l) + } + + return i +} diff --git a/counter/printer.go b/counter/printer.go index ca6fdca..a55afa2 100644 --- a/counter/printer.go +++ b/counter/printer.go @@ -1,13 +1,14 @@ package counter import ( + "encoding/json" "fmt" "io" "strings" - "github.com/tkuchiki/alp/html" - "github.com/olekukonko/tablewriter" + "github.com/tkuchiki/alp/convert" + "github.com/tkuchiki/alp/html" ) const ( @@ -55,6 +56,8 @@ func (p *Printer) Print(groups *groups) { p.printCSV(groups) case "html": p.printHTML(groups) + case "json": + p.printJSON(groups) } } @@ -146,3 +149,21 @@ func (p *Printer) printHTML(groups *groups) { content, _ := html.RenderTableWithGridJS("alp", headers, data, p.printOptions.paginationLimit) fmt.Println(content) } + +func (p *Printer) printJSON(groups *groups) { + var headers []string + headers = append(headers, defaultSumHeader) + headers = append(headers, groups.keys...) + + var data [][]string + data = append(data, headers) + + for _, group := range groups.groups { + data = append(data, p.generateLine(groups.keys, group)) + } + + i := convert.ToJSONValues(data) + b, _ := json.Marshal(i) + + fmt.Println(string(b)) +} diff --git a/log_reader/printer.go b/log_reader/printer.go index 6774313..c95b993 100644 --- a/log_reader/printer.go +++ b/log_reader/printer.go @@ -1,11 +1,13 @@ package log_reader import ( + "encoding/json" "fmt" "io" "strings" "github.com/olekukonko/tablewriter" + "github.com/tkuchiki/alp/convert" "github.com/tkuchiki/alp/html" ) @@ -153,6 +155,8 @@ func (p *Printer) Print(logs []*AccessLog) { p.printCSV(logs) case "html": p.printHTML(logs) + case "json": + p.printJSON(logs) } } @@ -221,3 +225,17 @@ func (p *Printer) printHTML(logs []*AccessLog) { content, _ := html.RenderTableWithGridJS("alp", p.headers, data, p.printOptions.paginationLimit) fmt.Println(content) } + +func (p *Printer) printJSON(logs []*AccessLog) { + var data [][]string + data = append(data, headerKeys) + + for _, l := range logs { + data = append(data, p.GenerateLine(l, true)) + } + + i := convert.ToJSONValues(data) + b, _ := json.Marshal(i) + + fmt.Println(string(b)) +} diff --git a/stats/printer.go b/stats/printer.go index d26ad3e..4742e0c 100644 --- a/stats/printer.go +++ b/stats/printer.go @@ -1,11 +1,13 @@ package stats import ( + "encoding/json" "fmt" "io" "strings" "github.com/olekukonko/tablewriter" + "github.com/tkuchiki/alp/convert" "github.com/tkuchiki/alp/helpers" "github.com/tkuchiki/alp/html" ) @@ -389,6 +391,8 @@ func (p *Printer) Print(hs, hsTo *HTTPStats) { p.printCSV(hs, hsTo) case "html": p.printHTML(hs, hsTo) + case "json": + p.printJSON(hs, hsTo) } } @@ -551,3 +555,30 @@ func (p *Printer) printHTML(hsFrom, hsTo *HTTPStats) { content, _ := html.RenderTableWithGridJS("alp", p.headers, data, p.printOptions.paginationLimit) fmt.Println(content) } + +func (p *Printer) printJSON(hsFrom, hsTo *HTTPStats) { + var data [][]string + + data = append(data, p.keywords) + + if hsTo == nil { + for _, s := range hsFrom.stats { + data = append(data, p.GenerateLine(s, true)) + } + } else { + for _, to := range hsTo.stats { + from := findHTTPStatFrom(hsFrom, to) + + if from == nil { + data = append(data, p.GenerateLine(to, false)) + } else { + data = append(data, p.GenerateLineWithDiff(from, to, false)) + } + } + } + + i := convert.ToJSONValues(data) + b, _ := json.Marshal(i) + + fmt.Println(string(b)) +}