Skip to content

Commit 01dc02c

Browse files
authored
search: add json-progress flag, to allow easy collection of metadata (#123)
* add json-progress feature to search, to allow easy collection of metadata * add additional fields to json output: timestamp, start, end
1 parent c7663fc commit 01dc02c

File tree

1 file changed

+76
-7
lines changed

1 file changed

+76
-7
lines changed

cmd/humioctl/search.go

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"io"
78
"math"
@@ -21,12 +22,13 @@ import (
2122

2223
func newSearchCmd() *cobra.Command {
2324
var (
24-
start string
25-
end string
26-
live bool
27-
fmtStr string
28-
noWrap bool
29-
noProgress bool
25+
start string
26+
end string
27+
live bool
28+
fmtStr string
29+
noWrap bool
30+
noProgress bool
31+
jsonProgress bool
3032
)
3133

3234
cmd := &cobra.Command{
@@ -40,6 +42,14 @@ func newSearchCmd() *cobra.Command {
4042

4143
ctx := contextCancelledOnInterrupt(context.Background())
4244

45+
// get the search start time, used for json output
46+
startMillis := time.Now().UnixMilli()
47+
48+
// set noProgress if getting json
49+
if jsonProgress {
50+
noProgress = true
51+
}
52+
4353
// run in lambda func to be able to defer and delete the query job
4454
err := func() error {
4555
id, err := client.QueryJobs().Create(repository, api.Query{
@@ -90,6 +100,10 @@ func newSearchCmd() *cobra.Command {
90100
if progress != nil {
91101
progress.Update(result)
92102
}
103+
if jsonProgress {
104+
jsonProgress, _ := printQueryResultProgressJson(result, args, startMillis)
105+
fmt.Printf("%s\n", jsonProgress)
106+
}
93107
result, err = poller.WaitAndPollContext(ctx)
94108
if err != nil {
95109
return err
@@ -101,7 +115,15 @@ func newSearchCmd() *cobra.Command {
101115
progress.Finish()
102116
}
103117

104-
printer.print(result)
118+
if jsonProgress {
119+
jsonProgress, _ := printQueryResultProgressJson(result, args, startMillis)
120+
fmt.Printf("%s\n", jsonProgress)
121+
}
122+
123+
// no output if using jsonProgress
124+
if !jsonProgress {
125+
printer.print(result)
126+
}
105127

106128
if live {
107129
for {
@@ -139,6 +161,7 @@ func newSearchCmd() *cobra.Command {
139161
"{@timestamp:-40} left aligns and right pads to 40 characters.")
140162
cmd.Flags().BoolVarP(&noWrap, "no-wrap", "n", false, "Do not autowrap long strings.")
141163
cmd.Flags().BoolVar(&noProgress, "no-progress", false, "Do not should progress information.")
164+
cmd.Flags().BoolVar(&jsonProgress, "json-progress", false, "Print progress in json format. This disables progress and output, useful for logging search metadata.")
142165

143166
return cmd
144167
}
@@ -214,6 +237,52 @@ func (b *queryResultProgressBar) Finish() {
214237
b.bar.Finish()
215238
}
216239

240+
type queryResultProgressJson struct {
241+
Timestamp int64 `json:"timestamp"`
242+
StartMillis int64 `json:"startMillis"`
243+
Repo string `json:"repo"`
244+
QueryString string `json:"queryString"`
245+
Start uint64 `json:"start"`
246+
End uint64 `json:"end"`
247+
TotalWork uint64 `json:"totalWork"`
248+
WorkDone uint64 `json:"workDone"`
249+
TimeMillis uint64 `json:"timeMillis"`
250+
EpsValue float64 `json:"epsValue"`
251+
BpsValue float64 `json:"bpsValue"`
252+
EventCount uint64 `json:"eventCount"`
253+
Done bool `json:"done"`
254+
}
255+
256+
func printQueryResultProgressJson(result api.QueryResult, args []string, startMillis int64) (string, error) {
257+
var epsValue, bpsValue float64
258+
259+
if result.Metadata.TimeMillis > 0 {
260+
epsValue = float64(result.Metadata.ProcessedEvents) / float64(result.Metadata.TimeMillis) * 1000
261+
bpsValue = float64(result.Metadata.ProcessedBytes) / float64(result.Metadata.TimeMillis) * 1000
262+
}
263+
264+
timestamp := time.Now().UnixMilli()
265+
266+
jsonResult := &queryResultProgressJson{
267+
Timestamp: timestamp,
268+
StartMillis: startMillis,
269+
Repo: args[0],
270+
QueryString: args[1],
271+
Start: result.Metadata.QueryStart,
272+
End: result.Metadata.QueryEnd,
273+
TotalWork: result.Metadata.TotalWork,
274+
WorkDone: result.Metadata.WorkDone,
275+
TimeMillis: result.Metadata.TimeMillis,
276+
EpsValue: epsValue,
277+
BpsValue: bpsValue,
278+
EventCount: result.Metadata.EventCount,
279+
Done: result.Done,
280+
}
281+
282+
data, err := json.Marshal(jsonResult)
283+
return string(data), err
284+
}
285+
217286
type queryJobPoller struct {
218287
queryJobs *api.QueryJobs
219288
repository string

0 commit comments

Comments
 (0)