-
Notifications
You must be signed in to change notification settings - Fork 413
feat: Add execution statistics storage and dashboard graph #1896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
23b4f69
feat: Add execution statistics storage and dashboard graph
vcastellm e2eae9c
Merge remote-tracking branch 'origin/main' into feature/execution-sta…
vcastellm cb39f1f
Apply suggestion from @coderabbitai[bot]
vcastellm 80911c7
Apply suggestion from @coderabbitai[bot]
vcastellm 1d0ac3d
feat: Add /stats endpoint to OpenAPI specification (#1900)
Copilot c9152be
fix: Remove extraneous closing brace in store.go causing syntax error…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,23 @@ | ||
| package dkron | ||
|
|
||
| import "time" | ||
|
|
||
| // ExecutionStat represents aggregated execution statistics for a specific time period | ||
| type ExecutionStat struct { | ||
| // Date is the date for which this stat is recorded (truncated to day) | ||
| Date time.Time `json:"date"` | ||
| // SuccessCount is the number of successful executions on this date | ||
| SuccessCount int `json:"success_count"` | ||
| // FailedCount is the number of failed executions on this date | ||
| FailedCount int `json:"failed_count"` | ||
| } | ||
|
|
||
| // ExecutionStats is a collection of execution statistics | ||
| type ExecutionStats struct { | ||
| Stats []ExecutionStat `json:"stats"` | ||
| } | ||
|
|
||
| // TotalExecutions returns the total number of executions | ||
| func (es *ExecutionStat) TotalExecutions() int { | ||
| return es.SuccessCount + es.FailedCount | ||
| } |
This file contains hidden or 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,172 @@ | ||
| package dkron | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/otel/trace/noop" | ||
| ) | ||
|
|
||
| func TestExecutionStats(t *testing.T) { | ||
| logger := logrus.NewEntry(logrus.New()) | ||
| tracer := noop.NewTracerProvider().Tracer("test") | ||
|
|
||
| store, err := NewStore(logger, tracer) | ||
| require.NoError(t, err) | ||
| defer store.Shutdown() | ||
|
|
||
| ctx := context.Background() | ||
| now := time.Now() | ||
|
|
||
|
Comment on lines
+22
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stabilize time-dependent tests. 🧪 Deterministic time setup- now := time.Now()
+ now := time.Date(2025, 1, 2, 12, 0, 0, 0, time.UTC)
...
- now := time.Now()
+ now := time.Date(2025, 1, 2, 12, 0, 0, 0, time.UTC)Also applies to: 128-129 🤖 Prompt for AI Agents |
||
| t.Run("IncrementExecutionStat creates new stat entry", func(t *testing.T) { | ||
| err := store.IncrementExecutionStat(ctx, now, true) | ||
| require.NoError(t, err) | ||
|
|
||
| stats, err := store.GetExecutionStats(ctx, 1) | ||
| require.NoError(t, err) | ||
| require.Len(t, stats.Stats, 1) | ||
|
|
||
| assert.Equal(t, 1, stats.Stats[0].SuccessCount) | ||
| assert.Equal(t, 0, stats.Stats[0].FailedCount) | ||
| }) | ||
|
|
||
| t.Run("IncrementExecutionStat increments existing stat", func(t *testing.T) { | ||
| // Add another success | ||
| err := store.IncrementExecutionStat(ctx, now, true) | ||
| require.NoError(t, err) | ||
|
|
||
| // Add a failure | ||
| err = store.IncrementExecutionStat(ctx, now, false) | ||
| require.NoError(t, err) | ||
|
|
||
| stats, err := store.GetExecutionStats(ctx, 1) | ||
| require.NoError(t, err) | ||
| require.Len(t, stats.Stats, 1) | ||
|
|
||
| assert.Equal(t, 2, stats.Stats[0].SuccessCount) | ||
| assert.Equal(t, 1, stats.Stats[0].FailedCount) | ||
| }) | ||
|
|
||
| t.Run("GetExecutionStats returns empty stats for missing days", func(t *testing.T) { | ||
| // Create a new store to have clean data | ||
| store2, err := NewStore(logger, tracer) | ||
| require.NoError(t, err) | ||
| defer store2.Shutdown() | ||
|
|
||
| stats, err := store2.GetExecutionStats(ctx, 7) | ||
| require.NoError(t, err) | ||
| require.Len(t, stats.Stats, 7) | ||
|
|
||
| // All should be zero | ||
| for _, stat := range stats.Stats { | ||
| assert.Equal(t, 0, stat.SuccessCount) | ||
| assert.Equal(t, 0, stat.FailedCount) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("GetExecutionStats returns stats in chronological order", func(t *testing.T) { | ||
| store3, err := NewStore(logger, tracer) | ||
| require.NoError(t, err) | ||
| defer store3.Shutdown() | ||
|
|
||
| // Add stats for yesterday | ||
| yesterday := now.AddDate(0, 0, -1) | ||
| err = store3.IncrementExecutionStat(ctx, yesterday, true) | ||
| require.NoError(t, err) | ||
|
|
||
| // Add stats for today | ||
| err = store3.IncrementExecutionStat(ctx, now, false) | ||
| require.NoError(t, err) | ||
|
|
||
| stats, err := store3.GetExecutionStats(ctx, 2) | ||
| require.NoError(t, err) | ||
| require.Len(t, stats.Stats, 2) | ||
|
|
||
| // First stat should be yesterday | ||
| assert.Equal(t, 1, stats.Stats[0].SuccessCount) | ||
| assert.Equal(t, 0, stats.Stats[0].FailedCount) | ||
|
|
||
| // Second stat should be today | ||
| assert.Equal(t, 0, stats.Stats[1].SuccessCount) | ||
| assert.Equal(t, 1, stats.Stats[1].FailedCount) | ||
| }) | ||
|
|
||
| t.Run("TotalExecutions returns sum of success and failed", func(t *testing.T) { | ||
| stat := ExecutionStat{ | ||
| SuccessCount: 5, | ||
| FailedCount: 3, | ||
| } | ||
| assert.Equal(t, 8, stat.TotalExecutions()) | ||
| }) | ||
| } | ||
|
|
||
| func TestSetExecutionDoneUpdatesStats(t *testing.T) { | ||
| logger := logrus.NewEntry(logrus.New()) | ||
| tracer := noop.NewTracerProvider().Tracer("test") | ||
|
|
||
| store, err := NewStore(logger, tracer) | ||
| require.NoError(t, err) | ||
| defer store.Shutdown() | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| // Create a test job | ||
| testJob := &Job{ | ||
| Name: "stats-test-job", | ||
| Schedule: "@manually", | ||
| Executor: "shell", | ||
| ExecutorConfig: map[string]string{"command": "/bin/true"}, | ||
| } | ||
|
|
||
| err = store.SetJob(ctx, testJob, true) | ||
| require.NoError(t, err) | ||
|
|
||
| now := time.Now() | ||
|
|
||
| // Create a successful execution | ||
| exec1 := &Execution{ | ||
| JobName: testJob.Name, | ||
| Group: now.UnixNano(), | ||
| StartedAt: now, | ||
| FinishedAt: now.Add(time.Second), | ||
| NodeName: "test-node", | ||
| Success: true, | ||
| Output: "success output", | ||
| } | ||
|
|
||
| // SetExecutionDone should update the stats | ||
| _, err = store.SetExecutionDone(ctx, exec1) | ||
| require.NoError(t, err) | ||
|
|
||
| // Check stats were updated | ||
| stats, err := store.GetExecutionStats(ctx, 1) | ||
| require.NoError(t, err) | ||
| require.Len(t, stats.Stats, 1) | ||
| assert.Equal(t, 1, stats.Stats[0].SuccessCount) | ||
| assert.Equal(t, 0, stats.Stats[0].FailedCount) | ||
|
|
||
| // Create a failed execution | ||
| exec2 := &Execution{ | ||
| JobName: testJob.Name, | ||
| Group: now.UnixNano() + 1, | ||
| StartedAt: now, | ||
| FinishedAt: now.Add(time.Second), | ||
| NodeName: "test-node", | ||
| Success: false, | ||
| Output: "failed output", | ||
| } | ||
|
|
||
| _, err = store.SetExecutionDone(ctx, exec2) | ||
| require.NoError(t, err) | ||
|
|
||
| // Check stats were updated | ||
| stats, err = store.GetExecutionStats(ctx, 1) | ||
| require.NoError(t, err) | ||
| require.Len(t, stats.Stats, 1) | ||
| assert.Equal(t, 1, stats.Stats[0].SuccessCount) | ||
| assert.Equal(t, 1, stats.Stats[0].FailedCount) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate and cap the
daysquery parameter.Line 554 accepts any value; negative or very large inputs can lead to unexpected ranges or excessive work. Consider enforcing a positive range and a reasonable maximum (e.g., 365).
🔧 Suggested bounds handling
🤖 Prompt for AI Agents