mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-05 08:01:10 +00:00
38 lines
710 B
Go
38 lines
710 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yibaiba/hideck/internal/db"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func (s *Server) handleTrafficAnalysis(c *gin.Context) {
|
|
s.useTrafficAnalysis(c)
|
|
}
|
|
|
|
func (s *Server) useTrafficAnalysis(c businessContext) {
|
|
rng := c.Query("range")
|
|
if rng == "" {
|
|
rng = "day"
|
|
}
|
|
deviceID := strings.TrimSpace(c.Query("device_id"))
|
|
now := time.Now()
|
|
|
|
buckets, chartData, err := db.GetTrafficAnalysisWithChart(rng, deviceID, now)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ok",
|
|
"range": rng,
|
|
"buckets": buckets,
|
|
"chart": chartData,
|
|
})
|
|
}
|