-
Notifications
You must be signed in to change notification settings - Fork 70
/
command_query.go
79 lines (64 loc) · 2.14 KB
/
command_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package nrql
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/client"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
"github.com/newrelic/newrelic-client-go/v2/pkg/nrdb"
)
var (
historyLimit int
query string
)
var cmdQuery = &cobra.Command{
Use: "query",
Short: "Execute a NRQL query to New Relic",
Long: `Execute a NRQL query to New Relic
The query command requires the --query flag which represents a NRQL query string.
This command requires the --accountId <int> flag, which specifies the account to
issue the query against.
`,
Example: `newrelic nrql query --accountId 12345678 --query 'SELECT count(*) FROM Transaction TIMESERIES'`,
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
accountID := configAPI.RequireActiveProfileAccountID()
result, err := client.NRClient.Nrdb.QueryWithContext(utils.SignalCtx, accountID, nrdb.NRQL(query))
if err != nil {
log.Fatal(err)
}
utils.LogIfFatal(output.Print(result.Results))
},
}
var cmdHistory = &cobra.Command{
Use: "history",
Short: "Retrieve NRQL query history",
Long: `Retrieve NRQL query history
The history command will fetch a list of the most recent NRQL queries you executed.
`,
Example: `newrelic nrql query history`,
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
result, err := client.NRClient.Nrdb.QueryHistory()
if err != nil {
log.Fatal(err)
}
if result == nil {
log.Info("no history found. Try using the 'newrelc nrql query' command")
return
}
count := len(*result)
if count < historyLimit {
historyLimit = count
}
output.Text((*result)[0:historyLimit])
},
}
func init() {
Command.AddCommand(cmdQuery)
cmdQuery.Flags().StringVarP(&query, "query", "q", "", "the NRQL query you want to execute")
utils.LogIfError(cmdQuery.MarkFlagRequired("query"))
Command.AddCommand(cmdHistory)
cmdHistory.Flags().IntVarP(&historyLimit, "limit", "l", 10, "history items to return (default: 10, max: 100)")
}