Skip to content

Commit

Permalink
Add the route ID to uuid (#3372)
Browse files Browse the repository at this point in the history
* Add the route ID to uuid

Signed-off-by: Yijie Qin <[email protected]>
---------

Signed-off-by: Yijie Qin <[email protected]>
  • Loading branch information
qinxx108 authored Sep 29, 2023
1 parent c6be0bc commit 57fdd13
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
23 changes: 23 additions & 0 deletions dispatch/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ func (r *Route) Key() string {
return b.String()
}

// ID returns a unique identifier for the route.
func (r *Route) ID() string {
b := strings.Builder{}

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
break
}
}
}
b.WriteString(r.Key())

if position > -1 {
b.WriteRune('/')
b.WriteString(fmt.Sprint(position))
}
return b.String()
}

// Walk traverses the route tree in depth-first order.
func (r *Route) Walk(visit func(*Route)) {
visit(r)
Expand Down
68 changes: 68 additions & 0 deletions dispatch/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,3 +853,71 @@ routes:
}
}
}

func TestRouteID(t *testing.T) {
in := `
receiver: 'notify-def'
routes:
- matchers: ['{owner="team-A"}', '{level!="critical"}']
receiver: 'notify-D'
group_by: [...]
continue: true
- matchers: ['{owner="team-A"}', '{level!="critical"}']
receiver: 'notify-A'
routes:
- matchers: ['{env="testing"}', '{baz!~".*quux"}']
receiver: 'notify-testing'
group_by: [...]
- match:
env: "production"
receiver: 'notify-productionA'
group_wait: 1m
continue: true
- matchers: [ env=~"produ.*", job=~".*"]
receiver: 'notify-productionB'
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
group_by: ['job']
- match_re:
owner: 'team-(B|C)'
group_by: ['foo', 'bar']
group_wait: 2m
receiver: 'notify-BC'
- matchers: [group_by="role"]
group_by: ['role']
routes:
- matchers: ['{env="testing"}']
receiver: 'notify-testing'
routes:
- matchers: [wait="long"]
group_wait: 2m
`

var ctree config.Route
if err := yaml.UnmarshalStrict([]byte(in), &ctree); err != nil {
t.Fatal(err)
}
tree := NewRoute(&ctree, nil)

expected := []string{
"{}",
"{}/{level!=\"critical\",owner=\"team-A\"}/0",
"{}/{level!=\"critical\",owner=\"team-A\"}/1",
"{}/{level!=\"critical\",owner=\"team-A\"}/{baz!~\".*quux\",env=\"testing\"}/0",
"{}/{level!=\"critical\",owner=\"team-A\"}/{env=\"production\"}/1",
"{}/{level!=\"critical\",owner=\"team-A\"}/{env=~\"produ.*\",job=~\".*\"}/2",
"{}/{owner=~\"^(?:team-(B|C))$\"}/2",
"{}/{group_by=\"role\"}/3",
"{}/{group_by=\"role\"}/{env=\"testing\"}/0",
"{}/{group_by=\"role\"}/{env=\"testing\"}/{wait=\"long\"}/0",
}

var got []string
tree.Walk(func(r *Route) {
got = append(got, r.ID())
})

require.ElementsMatch(t, got, expected)
}

0 comments on commit 57fdd13

Please sign in to comment.