Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
Signed-off-by: Yijie Qin <[email protected]>
  • Loading branch information
qinxx108 committed Jun 7, 2023
1 parent 10f5604 commit c87d345
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func (api *API) getAlertGroupInfoListHandler(params alertgroupinfolist_ops.GetAl
for _, alertGroup := range ags {

// Skip the aggregation group if the next token is set and hasn't arrived the nextToken item yet.
if params.NextToken != nil && *params.NextToken != "" && *params.NextToken >= alertGroup.ID {
if params.NextToken != nil && *params.NextToken >= alertGroup.ID {
continue
}

Expand Down Expand Up @@ -824,7 +824,7 @@ func validateMaxResult(maxItem *int64) error {
}

func validateNextToken(nextToken *string) error {
if nextToken != nil && *nextToken != "" {
if nextToken != nil {
match, _ := regexp.MatchString("^[a-fA-F0-9]{40}$", *nextToken)
if !match {
return fmt.Errorf("invalid nextToken: %s", *nextToken)
Expand Down
22 changes: 13 additions & 9 deletions api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func convertIntToPointerInt64(x int64) *int64 {
return &x
}

func convertStringToPointer(x string) *string {
return &x
}

func TestGetAlertGroupInfosHandler(t *testing.T) {
aginfos := dispatch.AlertGroupInfos{
&dispatch.AlertGroupInfo{
Expand Down Expand Up @@ -165,56 +169,56 @@ func TestGetAlertGroupInfosHandler(t *testing.T) {
}
for _, tc := range []struct {
maxResult *int64
nextToken string
nextToken *string
body string
expectedCode int
}{
// Invalid next token.
{
convertIntToPointerInt64(int64(1)),
"$$$",
convertStringToPointer("$$$"),
`failed to parse NextToken param: $$$`,
400,
},
// Invalid next token.
{
convertIntToPointerInt64(int64(1)),
"1234s",
convertStringToPointer("1234s"),
`failed to parse NextToken param: 1234s`,
400,
},
// Invalid MaxResults.
{
convertIntToPointerInt64(int64(-1)),
"",
convertStringToPointer("478b4114226224a35910d449fdba8186ebfb441f"),
`failed to parse MaxResults param: -1`,
400,
},
// One item to return, no next token.
{
convertIntToPointerInt64(int64(1)),
"",
nil,
`{"alertGroupInfoList":[{"labels":{"alertname":"TestingAlert","service":"api"},"receiver":{"name":"testing"}}],"nextToken":"478b4114226224a35910d449fdba8186ebfb441f"}`,
200,
},
// One item to return, has next token.
{
convertIntToPointerInt64(int64(1)),
"478b4114226224a35910d449fdba8186ebfb441f",
convertStringToPointer("478b4114226224a35910d449fdba8186ebfb441f"),
`{"alertGroupInfoList":[{"labels":{"alertname":"HighErrorRate","cluster":"bb","service":"api"},"receiver":{"name":"prod"}}],"nextToken":"7f4084a078a3fe29d6de82fad15af8f1411e803f"}`,
200,
},
// Five item to return, has next token.
{
convertIntToPointerInt64(int64(5)),
"7f4084a078a3fe29d6de82fad15af8f1411e803f",
convertStringToPointer("7f4084a078a3fe29d6de82fad15af8f1411e803f"),
`{"alertGroupInfoList":[{"labels":{"alertname":"OtherAlert"},"receiver":{"name":"prod"}},{"labels":{"alertname":"HighErrorRate","cluster":"aa","service":"api"},"receiver":{"name":"prod"}}]}`,
200,
},
// Return all results.
{
nil,
"",
nil,
`{"alertGroupInfoList":[{"labels":{"alertname":"TestingAlert","service":"api"},"receiver":{"name":"testing"}},{"labels":{"alertname":"HighErrorRate","cluster":"bb","service":"api"},"receiver":{"name":"prod"}},{"labels":{"alertname":"OtherAlert"},"receiver":{"name":"prod"}},{"labels":{"alertname":"HighErrorRate","cluster":"aa","service":"api"},"receiver":{"name":"prod"}}]}`,
200,
},
Expand All @@ -233,7 +237,7 @@ func TestGetAlertGroupInfosHandler(t *testing.T) {
p := runtime.TextProducer()
responder := api.getAlertGroupInfoListHandler(alertgroupinfolist_ops.GetAlertGroupInfoListParams{
MaxResults: tc.maxResult,
NextToken: &tc.nextToken,
NextToken: tc.nextToken,
HTTPRequest: r,
})
responder.WriteResponse(w, p)
Expand Down
8 changes: 4 additions & 4 deletions dispatch/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,21 @@ func (r *Route) Key() string {
func (r *Route) ID() string {
b := strings.Builder{}

var position *int
position := -1
if r.parent != nil {
// Find the position in the same level leaf.
for i, cr := range r.parent.Routes {
if cr == r {
position = &i
position = i
break
}
}
}
b.WriteString(r.Key())

if position != nil {
if position > -1 {
b.WriteRune('/')
b.WriteString(fmt.Sprint(*position))
b.WriteString(fmt.Sprint(position))
}
return b.String()
}
Expand Down

0 comments on commit c87d345

Please sign in to comment.