This commit is contained in:
@@ -6,9 +6,11 @@ import (
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/backend/internal/grpcsrv/commander"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/backend/internal/models"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/backend/internal/repository"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/backend/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -17,10 +19,11 @@ type JobsHandlers struct {
|
||||
tracker *commander.ConnTracker
|
||||
svc *service.ScriptService
|
||||
whereami string
|
||||
jobRepo *repository.JobRepository
|
||||
}
|
||||
|
||||
func NewJobsHandlers(tracker *commander.ConnTracker, svc *service.ScriptService, whereami string) JobsHandlers {
|
||||
return JobsHandlers{tracker: tracker, svc: svc, whereami: whereami}
|
||||
func NewJobsHandlers(tracker *commander.ConnTracker, svc *service.ScriptService, whereami string, jobRepo *repository.JobRepository) JobsHandlers {
|
||||
return JobsHandlers{tracker: tracker, svc: svc, whereami: whereami, jobRepo: jobRepo}
|
||||
}
|
||||
|
||||
// AddJobIn is the request body for creating a job.
|
||||
@@ -197,3 +200,46 @@ type CheckCmdIn struct {
|
||||
type CheckCmdOut struct {
|
||||
Exists bool `json:"exists"`
|
||||
}
|
||||
|
||||
// JobMetricsOut is the response body for the job metrics endpoint.
|
||||
type JobMetricsOut struct {
|
||||
Total int `json:"total"`
|
||||
Success int `json:"success"`
|
||||
Failed int `json:"failed"`
|
||||
Pending int `json:"pending"`
|
||||
Period string `json:"period"`
|
||||
}
|
||||
|
||||
// GetJobMetrics returns job success metrics over a parameterized period.
|
||||
// @Summary Get job metrics
|
||||
// @Description Returns total, successful, failed, and pending job counts over the given period
|
||||
// @Tags jobs
|
||||
// @Produce json
|
||||
// @Param period query string false "Time period (e.g. 1h, 24h, 7d)" default(24h)
|
||||
// @Success 200 {object} JobMetricsOut
|
||||
// @Failure 400 {object} map[string]string
|
||||
// @Security Bearer
|
||||
// @Router /jobs/metrics [get]
|
||||
func (h *JobsHandlers) GetJobMetrics(c *gin.Context) {
|
||||
periodStr := c.DefaultQuery("period", "24h")
|
||||
period, err := time.ParseDuration(periodStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid period, use Go duration format (e.g. 1h, 24h, 7d)"})
|
||||
return
|
||||
}
|
||||
|
||||
since := time.Now().Add(-period)
|
||||
metrics, err := h.jobRepo.GetJobMetrics(c.Request.Context(), since)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, JobMetricsOut{
|
||||
Total: metrics.Total,
|
||||
Success: metrics.Success,
|
||||
Failed: metrics.Failed,
|
||||
Pending: metrics.Pending,
|
||||
Period: periodStr,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user