Skip to content

Commit

Permalink
Merge pull request #86 from tkuchiki/add-output-json
Browse files Browse the repository at this point in the history
Add `--format json`
  • Loading branch information
tkuchiki committed Oct 2, 2023
2 parents 4e95db4 + d6d13fa commit 971c46c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/alp/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions convert/json.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 23 additions & 2 deletions counter/printer.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -55,6 +56,8 @@ func (p *Printer) Print(groups *groups) {
p.printCSV(groups)
case "html":
p.printHTML(groups)
case "json":
p.printJSON(groups)
}
}

Expand Down Expand Up @@ -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))
}
18 changes: 18 additions & 0 deletions log_reader/printer.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -153,6 +155,8 @@ func (p *Printer) Print(logs []*AccessLog) {
p.printCSV(logs)
case "html":
p.printHTML(logs)
case "json":
p.printJSON(logs)
}
}

Expand Down Expand Up @@ -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))
}
31 changes: 31 additions & 0 deletions stats/printer.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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))
}

0 comments on commit 971c46c

Please sign in to comment.