Skip to content

Commit

Permalink
fix(stats/historical): avoid runtime SIGSEGV (#1169)
Browse files Browse the repository at this point in the history
* fix(stats/historical): avoid runtime SIGSEGV

* refactor(stats/historical): optionally set fields
  • Loading branch information
Integralist authored Mar 27, 2024
1 parent 6fc2fc1 commit a20e03c
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions pkg/commands/stats/historical.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ const statusSuccess = "success"
type HistoricalCommand struct {
argparser.Base

Input fastly.GetStatsInput
by string
formatFlag string
from string
region string
serviceName argparser.OptionalServiceNameID
to string
}

// NewHistoricalCommand is the "stats historical" subcommand.
Expand All @@ -41,10 +44,10 @@ func NewHistoricalCommand(parent argparser.Registerer, g *global.Data) *Historic
Dst: &c.serviceName.Value,
})

c.CmdClause.Flag("from", "From time, accepted formats at https://fastly.dev/reference/api/metrics-stats/historical-stats").StringVar(c.Input.From)
c.CmdClause.Flag("to", "To time").StringVar(c.Input.To)
c.CmdClause.Flag("by", "Aggregation period (minute/hour/day)").EnumVar(c.Input.By, "minute", "hour", "day")
c.CmdClause.Flag("region", "Filter by region ('stats regions' to list)").StringVar(c.Input.Region)
c.CmdClause.Flag("from", "From time, accepted formats at https://fastly.dev/reference/api/metrics-stats/historical-stats").StringVar(&c.from)
c.CmdClause.Flag("to", "To time").StringVar(&c.to)
c.CmdClause.Flag("by", "Aggregation period (minute/hour/day)").EnumVar(&c.by, "minute", "hour", "day")
c.CmdClause.Flag("region", "Filter by region ('stats regions' to list)").StringVar(&c.region)

c.CmdClause.Flag("format", "Output format (json)").EnumVar(&c.formatFlag, "json")

Expand All @@ -61,10 +64,24 @@ func (c *HistoricalCommand) Exec(_ io.Reader, out io.Writer) error {
argparser.DisplayServiceID(serviceID, flag, source, out)
}

c.Input.Service = fastly.ToPointer(serviceID)
input := fastly.GetStatsInput{
Service: fastly.ToPointer(serviceID),
}
if c.by != "" {
input.By = &c.by
}
if c.from != "" {
input.From = &c.from
}
if c.region != "" {
input.Region = &c.region
}
if c.to != "" {
input.To = &c.to
}

var envelope statsResponse
err = c.Globals.APIClient.GetStatsJSON(&c.Input, &envelope)
err = c.Globals.APIClient.GetStatsJSON(&input, &envelope)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Service ID": serviceID,
Expand Down

0 comments on commit a20e03c

Please sign in to comment.