-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implement context based progress tracker (#741)
- Loading branch information
Showing
41 changed files
with
1,038 additions
and
1,442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Copyright 2023 gorse Project Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package progress | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"sync" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"modernc.org/mathutil" | ||
) | ||
|
||
type spanKeyType string | ||
|
||
var spanKeyName = spanKeyType(uuid.New().String()) | ||
|
||
type Status string | ||
|
||
const ( | ||
StatusPending Status = "Pending" | ||
StatusComplete Status = "Complete" | ||
StatusRunning Status = "Running" | ||
StatusSuspended Status = "Suspended" | ||
StatusFailed Status = "Failed" | ||
) | ||
|
||
type Tracer struct { | ||
name string | ||
spans sync.Map | ||
} | ||
|
||
func NewTracer(name string) *Tracer { | ||
return &Tracer{name: name} | ||
} | ||
|
||
// Start creates a root span. | ||
func (t *Tracer) Start(ctx context.Context, name string, total int) (context.Context, *Span) { | ||
span := &Span{ | ||
name: name, | ||
status: StatusRunning, | ||
total: total, | ||
start: time.Now(), | ||
} | ||
t.spans.Store(name, span) | ||
return context.WithValue(ctx, spanKeyName, span), span | ||
} | ||
|
||
func (t *Tracer) List() []Progress { | ||
var progress []Progress | ||
t.spans.Range(func(key, value interface{}) bool { | ||
span := value.(*Span) | ||
progress = append(progress, span.Progress()) | ||
return true | ||
}) | ||
// sort by start time | ||
sort.Slice(progress, func(i, j int) bool { | ||
return progress[i].StartTime.Before(progress[j].StartTime) | ||
}) | ||
return progress | ||
} | ||
|
||
type Span struct { | ||
name string | ||
status Status | ||
total int | ||
count int | ||
err string | ||
start time.Time | ||
finish time.Time | ||
children sync.Map | ||
} | ||
|
||
func (s *Span) Add(n int) { | ||
s.count = mathutil.Min(s.count+n, s.total) | ||
} | ||
|
||
func (s *Span) End() { | ||
s.status = StatusComplete | ||
s.count = s.total | ||
s.finish = time.Now() | ||
} | ||
|
||
func (s *Span) Error(err error) { | ||
s.err = err.Error() | ||
} | ||
|
||
func (s *Span) Count() int { | ||
return s.count | ||
} | ||
|
||
func (s *Span) Progress() Progress { | ||
// find running children | ||
var children []Progress | ||
s.children.Range(func(key, value interface{}) bool { | ||
child := value.(*Span) | ||
progress := child.Progress() | ||
if progress.Status == StatusRunning { | ||
children = append(children, progress) | ||
} | ||
return true | ||
}) | ||
// leaf node | ||
if len(children) == 0 { | ||
return Progress{ | ||
Name: s.name, | ||
Status: s.status, | ||
Error: s.err, | ||
Count: s.count, | ||
Total: s.total, | ||
StartTime: s.start, | ||
FinishTime: s.finish, | ||
} | ||
} | ||
// non-leaf node | ||
childTotal := children[0].Total | ||
parentTotal := s.total * childTotal | ||
parentCount := s.count * childTotal | ||
for _, child := range children { | ||
parentCount += childTotal * child.Count / child.Total | ||
} | ||
return Progress{ | ||
Name: s.name, | ||
Status: s.status, | ||
Error: s.err, | ||
Count: parentCount, | ||
Total: parentTotal, | ||
StartTime: s.start, | ||
FinishTime: s.finish, | ||
} | ||
} | ||
|
||
func Start(ctx context.Context, name string, total int) (context.Context, *Span) { | ||
childSpan := &Span{ | ||
name: name, | ||
status: StatusRunning, | ||
total: total, | ||
count: 0, | ||
start: time.Now(), | ||
} | ||
if ctx == nil { | ||
return nil, childSpan | ||
} | ||
span, ok := (ctx).Value(spanKeyName).(*Span) | ||
if !ok { | ||
return nil, childSpan | ||
} | ||
span.children.Store(name, childSpan) | ||
return context.WithValue(ctx, spanKeyName, childSpan), childSpan | ||
} | ||
|
||
type Progress struct { | ||
Tracer string | ||
Name string | ||
Status Status | ||
Error string | ||
Count int | ||
Total int | ||
StartTime time.Time | ||
FinishTime time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2023 gorse Project Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package progress | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type ProgressTestSuite struct { | ||
suite.Suite | ||
tracer Tracer | ||
} | ||
|
||
func (suite *ProgressTestSuite) SetupTest() { | ||
suite.tracer = Tracer{} | ||
} | ||
|
||
func TestProgressTestSuite(t *testing.T) { | ||
suite.Run(t, new(ProgressTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.