admin: Add stats about submissions rate
This commit is contained in:
parent
116c061715
commit
329bd246c7
4 changed files with 144 additions and 1 deletions
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
|
@ -10,6 +11,7 @@ import (
|
|||
"time"
|
||||
|
||||
"srs.epita.fr/fic-server/admin/pki"
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
|
@ -31,6 +33,8 @@ func declareHealthRoutes(router *gin.RouterGroup) {
|
|||
})
|
||||
})
|
||||
router.GET("/health.json", GetHealth)
|
||||
router.GET("/submissions-stats.json", GetSubmissionsStats)
|
||||
router.GET("/validations-stats.json", GetValidationsStats)
|
||||
}
|
||||
|
||||
type healthFileReport struct {
|
||||
|
|
@ -73,3 +77,67 @@ func GetHealth(c *gin.Context) {
|
|||
|
||||
c.JSON(http.StatusOK, getHealth(TimestampCheck))
|
||||
}
|
||||
|
||||
type SubmissionsStats struct {
|
||||
NbSubmissionLastMinute uint `json:"nbsubminute"`
|
||||
NbSubmissionLast5Minute uint `json:"nbsub5minute"`
|
||||
NbSubmissionLastQuarter uint `json:"nbsubquarter"`
|
||||
NbSubmissionLastHour uint `json:"nbsubhour"`
|
||||
NbSubmissionLastDay uint `json:"nbsubday"`
|
||||
}
|
||||
|
||||
func calcSubmissionsStats(tries []time.Time) (stats SubmissionsStats) {
|
||||
lastMinute := time.Now().Add(-1 * time.Minute)
|
||||
last5Minute := time.Now().Add(-5 * time.Minute)
|
||||
lastQuarter := time.Now().Add(-15 * time.Minute)
|
||||
lastHour := time.Now().Add(-1 * time.Hour)
|
||||
lastDay := time.Now().Add(-24 * time.Hour)
|
||||
|
||||
for _, t := range tries {
|
||||
if lastMinute.Before(t) {
|
||||
stats.NbSubmissionLastMinute += 1
|
||||
stats.NbSubmissionLast5Minute += 1
|
||||
stats.NbSubmissionLastQuarter += 1
|
||||
stats.NbSubmissionLastHour += 1
|
||||
stats.NbSubmissionLastDay += 1
|
||||
} else if last5Minute.Before(t) {
|
||||
stats.NbSubmissionLast5Minute += 1
|
||||
stats.NbSubmissionLastQuarter += 1
|
||||
stats.NbSubmissionLastHour += 1
|
||||
stats.NbSubmissionLastDay += 1
|
||||
} else if lastQuarter.Before(t) {
|
||||
stats.NbSubmissionLastQuarter += 1
|
||||
stats.NbSubmissionLastHour += 1
|
||||
stats.NbSubmissionLastDay += 1
|
||||
} else if lastHour.Before(t) {
|
||||
stats.NbSubmissionLastHour += 1
|
||||
stats.NbSubmissionLastDay += 1
|
||||
} else if lastDay.Before(t) {
|
||||
stats.NbSubmissionLastDay += 1
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetSubmissionsStats(c *gin.Context) {
|
||||
tries, err := fic.GetTries(nil, nil)
|
||||
if err != nil {
|
||||
log.Println("Unable to GetTries:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieves tries."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, calcSubmissionsStats(tries))
|
||||
}
|
||||
|
||||
func GetValidationsStats(c *gin.Context) {
|
||||
tries, err := fic.GetValidations(nil, nil)
|
||||
if err != nil {
|
||||
log.Println("Unable to GetTries:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieves tries."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, calcSubmissionsStats(tries))
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue