Files
ForgeBucket/internal/api/handlers/insights.go
T

50 lines
1.1 KiB
Go

package handlers
import (
"net/http"
"xorm.io/xorm"
gitdomain "github.com/forgeo/forgebucket/internal/domain/git"
)
type InsightsHandler struct {
db *xorm.Engine
}
func NewInsightsHandler(db *xorm.Engine) *InsightsHandler {
return &InsightsHandler{db: db}
}
type insightsResponse struct {
Languages []gitdomain.LangStat `json:"languages"`
Contributors []gitdomain.Contributor `json:"contributors"`
TotalCommits int `json:"totalCommits"`
}
// Get returns language breakdown, top contributors, and total commit count
// for a repository. All data is read directly from git — no DB writes.
func (h *InsightsHandler) Get(w http.ResponseWriter, r *http.Request) {
repo, ok := resolveRepo(h.db, w, r)
if !ok {
return
}
langs, _ := gitdomain.LanguageStats(repo.DiskPath, repo.DefaultBranch)
contribs, _ := gitdomain.Contributors(repo.DiskPath, 8)
count, _ := gitdomain.CommitCount(repo.DiskPath)
if langs == nil {
langs = []gitdomain.LangStat{}
}
if contribs == nil {
contribs = []gitdomain.Contributor{}
}
jsonOK(w, insightsResponse{
Languages: langs,
Contributors: contribs,
TotalCommits: count,
})
}