Skip to content

Commit 711e510

Browse files
Alec CunninghamAlec Cunningham
authored andcommitted
remove mooshe3 ref
1 parent 3fe5b2c commit 711e510

File tree

5 files changed

+112
-14
lines changed

5 files changed

+112
-14
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/mooshe3/github-actions-aggregator
1+
module github.com/moosh3/github-actions-aggregator
22

33
go 1.23.2
44

pkg/api/handlers.go

Lines changed: 106 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,116 @@
1+
// pkg/api/handlers.go
2+
13
package api
24

35
import (
46
"net/http"
7+
"strconv"
8+
"time"
59

610
"github.com/gin-gonic/gin"
11+
"github.com/moosh3/github-actions-aggregator/pkg/db/models"
12+
"gorm.io/gorm"
713
)
814

9-
func GetStats(c *gin.Context) {
10-
// Placeholder for actual logic
11-
stats := map[string]interface{}{
12-
"total_runs": 100,
13-
"success_rate": 95.0,
15+
func GetWorkflowStats(c *gin.Context) {
16+
workflowIDParam := c.Param("id")
17+
startTimeParam := c.Query("start_time")
18+
endTimeParam := c.Query("end_time")
19+
20+
// Convert workflowID to integer
21+
workflowID, err := strconv.ParseInt(workflowIDParam, 10, 64)
22+
if err != nil {
23+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid workflow ID"})
24+
return
25+
}
26+
27+
// Parse start and end times
28+
var startTime, endTime time.Time
29+
if startTimeParam != "" {
30+
startTime, err = time.Parse(time.RFC3339, startTimeParam)
31+
if err != nil {
32+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time format"})
33+
return
34+
}
35+
} else {
36+
startTime = time.Now().AddDate(0, 0, -30)
37+
}
38+
39+
if endTimeParam != "" {
40+
endTime, err = time.Parse(time.RFC3339, endTimeParam)
41+
if err != nil {
42+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end_time format"})
43+
return
44+
}
45+
} else {
46+
endTime = time.Now()
47+
}
48+
49+
if !startTime.Before(endTime) {
50+
c.JSON(http.StatusBadRequest, gin.H{"error": "start_time must be before end_time"})
51+
return
52+
}
53+
54+
// Access the database
55+
db, ok := c.MustGet("db").(*gorm.DB)
56+
if !ok {
57+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not found"})
58+
return
1459
}
15-
c.JSON(http.StatusOK, stats)
60+
61+
// Check if the workflow exists
62+
var workflow models.Workflow
63+
err = db.First(&workflow, "id = ?", workflowID).Error
64+
if err != nil {
65+
if err == gorm.ErrRecordNotFound {
66+
c.JSON(http.StatusNotFound, gin.H{"error": "Workflow not found"})
67+
} else {
68+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve workflow"})
69+
}
70+
return
71+
}
72+
73+
// Query workflow runs
74+
var runs []models.WorkflowRun
75+
err = db.Where("workflow_id = ?", workflowID).
76+
Where("created_at BETWEEN ? AND ?", startTime, endTime).
77+
Find(&runs).Error
78+
if err != nil {
79+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve workflow runs"})
80+
return
81+
}
82+
83+
// Calculate statistics
84+
totalRuns := len(runs)
85+
successCount := 0
86+
failureCount := 0
87+
88+
for _, run := range runs {
89+
switch run.Conclusion {
90+
case "success":
91+
successCount++
92+
case "failure":
93+
failureCount++
94+
}
95+
}
96+
97+
// Calculate percentages
98+
var successRate, failureRate float64
99+
if totalRuns > 0 {
100+
successRate = float64(successCount) / float64(totalRuns) * 100
101+
failureRate = float64(failureCount) / float64(totalRuns) * 100
102+
}
103+
104+
// Respond with statistics
105+
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),
115+
})
16116
}

pkg/api/router.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ func StartServer(cfg *config.Config, db *db.Database, githubClient *github.Clien
2222
// Protected routes
2323
protected := r.Group("/", auth.AuthMiddleware())
2424
{
25-
protected.GET("/dashboard", dashboardHandler)
26-
protected.GET("/stats", statsHandler)
27-
// Add other protected routes
25+
protected.GET("/workflows/:id/stats", GetWorkflowStats)
2826
}
2927

3028
r.Run(":" + cfg.ServerPort)

pkg/db/db.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55

66
"github.com/google/go-github/v50/github"
7-
"github.com/mooshe3/github-actions-aggregator/pkg/config"
8-
"github.com/mooshe3/github-actions-aggregator/pkg/db/models"
7+
"github.com/moosh3/github-actions-aggregator/pkg/config"
8+
"github.com/moosh3/github-actions-aggregator/pkg/db/models"
99
"gorm.io/driver/postgres"
1010
"gorm.io/gorm"
1111
"gorm.io/gorm/clause"

pkg/github/webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
"github.com/gin-gonic/gin"
1111
"github.com/google/go-github/v50/github"
12+
"github.com/moosh3/github-actions-aggregator/pkg/db"
1213
"github.com/moosh3/github-actions-aggregator/pkg/worker"
13-
"github.com/mooshe3/github-actions-aggregator/pkg/db"
1414
)
1515

1616
type WebhookHandler struct {
@@ -20,7 +20,7 @@ type WebhookHandler struct {
2020
webhookSecret []byte
2121
}
2222

23-
func NewWebhookHandler(db *db.Database, client *Client, secret string, wp *worker.WorkerPool) *WebhookHandler {
23+
func NewWebhookHandler(db *db.Database, client *Client, wp *worker.WorkerPool, secret string) *WebhookHandler {
2424
return &WebhookHandler{
2525
db: db,
2626
client: client,

0 commit comments

Comments
 (0)