Skip to content

Commit c738d70

Browse files
Alec CunninghamAlec Cunningham
Alec Cunningham
authored and
Alec Cunningham
committed
feat: added extended statistics
1 parent b5a5350 commit c738d70

File tree

9 files changed

+68
-28
lines changed

9 files changed

+68
-28
lines changed

cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func main() {
4141
wp.Start()
4242

4343
// Start the API server
44-
go api.StartServer(cfg, database, githubClient)
44+
go api.StartServer(cfg, database, githubClient, wp)
4545

4646
// Wait for interrupt signal to gracefully shut down the worker pool
4747
quit := make(chan os.Signal, 1)

docs/DESIGN.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ Recommended Approach
7373
### Database Schema Design
7474

7575
- Tables:
76-
- users: Stores user information.
77-
- repositories: Tracks repositories being monitored.
78-
- workflows: Contains workflow definitions.
79-
- workflow_runs: Records individual workflow executions.
80-
- statistics: Stores precomputed stats for quick access.
76+
- `users`: Stores user information.
77+
- `repositories`: Tracks repositories being monitored.
78+
- `workflows`: Contains workflow definitions.
79+
- `workflow_runs`: Records individual workflow executions.
80+
- `statistics`: Stores precomputed stats for quick access.
8181

8282
- Relationships:
8383
- Establish foreign keys between tables for referential integrity.

pkg/api/handlers.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// pkg/api/handlers.go
2-
31
package api
42

53
import (
@@ -80,37 +78,55 @@ func GetWorkflowStats(c *gin.Context) {
8078
return
8179
}
8280

83-
// Calculate statistics
81+
// Initialize counters
8482
totalRuns := len(runs)
8583
successCount := 0
8684
failureCount := 0
85+
cancelledCount := 0
86+
timedOutCount := 0
87+
actionRequiredCount := 0
8788

8889
for _, run := range runs {
8990
switch run.Conclusion {
9091
case "success":
9192
successCount++
9293
case "failure":
9394
failureCount++
95+
case "cancelled":
96+
cancelledCount++
97+
case "timed_out":
98+
timedOutCount++
99+
case "action_required":
100+
actionRequiredCount++
94101
}
95102
}
96103

97104
// Calculate percentages
98-
var successRate, failureRate float64
105+
var successRate, failureRate, cancelledRate, timedOutRate, actionRequiredRate float64
99106
if totalRuns > 0 {
100107
successRate = float64(successCount) / float64(totalRuns) * 100
101108
failureRate = float64(failureCount) / float64(totalRuns) * 100
109+
cancelledRate = float64(cancelledCount) / float64(totalRuns) * 100
110+
timedOutRate = float64(timedOutCount) / float64(totalRuns) * 100
111+
actionRequiredRate = float64(actionRequiredCount) / float64(totalRuns) * 100
102112
}
103113

104114
// Respond with statistics
105115
c.JSON(http.StatusOK, gin.H{
106-
"workflow_id": workflowID,
107-
"workflow_name": workflow.Name,
108-
"total_runs": totalRuns,
109-
"success_count": successCount,
110-
"failure_count": failureCount,
111-
"success_rate": successRate,
112-
"failure_rate": failureRate,
113-
"start_time": startTime.Format(time.RFC3339),
114-
"end_time": endTime.Format(time.RFC3339),
116+
"workflow_id": workflowID, //The unique identifier of the workflow (e.g., 123).
117+
"workflow_name": workflow.Name, //The name of the workflow (e.g., "CI Build and Test").
118+
"total_runs": totalRuns, //The total number of workflow runs within the specified time range (e.g., 200).
119+
"success_count": successCount, //The number of runs that concluded with a success status (e.g., 150).
120+
"failure_count": failureCount, //The number of runs that concluded with a failure status (e.g., 30).
121+
"cancelled_count": cancelledCount, //The number of runs that were cancelled (e.g., 10).
122+
"timed_out_count": timedOutCount, //The number of runs that timed out (e.g., 5).
123+
"action_required_count": actionRequiredCount, //The number of runs that required action (e.g., 5).
124+
"success_rate": successRate, //The percentage of runs that were successful (success_count / total_runs * 100, e.g., 75.0).
125+
"failure_rate": failureRate, //The percentage of runs that failed (failure_count / total_runs * 100, e.g., 15.0).
126+
"cancelled_rate": cancelledRate, //The percentage of runs that were cancelled (cancelled_count / total_runs * 100, e.g., 5.0).
127+
"timed_out_rate": timedOutRate, //The percentage of runs that timed out (timed_out_count / total_runs * 100, e.g., 2.5).
128+
"action_required_rate": actionRequiredRate, //The percentage of runs that required action (action_required_count / total_runs * 100, e.g., 2.5).
129+
"start_time": startTime.Format(time.RFC3339), //The start of the time range for the statistics (e.g., "2023-09-01T00:00:00Z").
130+
"end_time": endTime.Format(time.RFC3339), //The end of the time range for the statistics (e.g., "2023-09-30T23:59:59Z").
115131
})
116132
}

pkg/api/middleware.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
package api
22

3-
// Middleware functions for request logging, authentication checks, etc.
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"gorm.io/gorm"
6+
)
7+
8+
func DBMiddleware(db *gorm.DB) gin.HandlerFunc {
9+
return func(c *gin.Context) {
10+
c.Set("db", db)
11+
c.Next()
12+
}
13+
}

pkg/api/router.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212
func StartServer(cfg *config.Config, db *db.Database, githubClient *github.Client, workerPool *worker.WorkerPool) {
1313
r := gin.Default()
1414

15+
// Apply the database middleware
16+
r.Use(DBMiddleware(db.Conn))
17+
1518
// Public routes
1619
r.GET("/login", auth.GitHubLogin)
1720
r.GET("/callback", auth.GitHubCallback)

pkg/auth/oauth.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010

1111
"github.com/gin-gonic/gin"
12-
"github.com/moosh3/github-actions-aggregator/pkg/db"
1312
"github.com/moosh3/github-actions-aggregator/pkg/db/models"
1413
"golang.org/x/oauth2"
1514
"golang.org/x/oauth2/github"
@@ -54,12 +53,6 @@ func GitHubCallback(c *gin.Context) {
5453
return
5554
}
5655

57-
// Save or update user in database
58-
err = db.SaveUser(user)
59-
if err != nil {
60-
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Failed to save user"})
61-
return
62-
}
6356
// Set user session (implement setUserSession)
6457
setUserSession(c, user)
6558

pkg/worker/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (wp *WorkerPool) worker(id int) {
6969
for {
7070
select {
7171
case job := <-wp.JobQueue:
72-
72+
log.Printf("Worker %d received job: %v", id, job)
7373
// ...
7474
}
7575
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GET /workflows/123/stats?start_time=2023-09-01T00:00:00Z&end_time=2023-09-30T23:59:59Z
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"workflow_id": 123,
3+
"workflow_name": "CI Build and Test",
4+
"total_runs": 200,
5+
"success_count": 150,
6+
"failure_count": 30,
7+
"cancelled_count": 10,
8+
"timed_out_count": 5,
9+
"action_required_count": 5,
10+
"success_rate": 75.0,
11+
"failure_rate": 15.0,
12+
"cancelled_rate": 5.0,
13+
"timed_out_rate": 2.5,
14+
"action_required_rate": 2.5,
15+
"start_time": "2023-09-01T00:00:00Z",
16+
"end_time": "2023-09-30T23:59:59Z"
17+
}

0 commit comments

Comments
 (0)