EasyDarwin/routers/stats.go

135 lines
4.1 KiB
Go
Raw Normal View History

2018-11-07 11:28:13 +00:00
package routers
import (
"fmt"
"strings"
2018-11-16 13:36:52 +00:00
"github.com/EasyDarwin/EasyDarwin/rtsp"
2018-11-07 11:28:13 +00:00
"github.com/gin-gonic/gin"
"github.com/penggy/EasyGoLib/utils"
)
/**
2018-12-03 05:26:42 +00:00
* @apiDefine stats
2018-11-07 11:28:13 +00:00
*/
/**
* @apiDefine playerInfo
* @apiSuccess (200) {String} rows.id
* @apiSuccess (200) {String} rows.path
* @apiSuccess (200) {String} rows.transType
* @apiSuccess (200) {Number} rows.inBytes
* @apiSuccess (200) {Number} rows.outBytes
* @apiSuccess (200) {String} rows.startAt
*/
/**
* @api {get} /api/v1/pushers
* @apiGroup stats
* @apiName Pushers
* @apiParam {Number} [start] ,
* @apiParam {Number} [limit]
* @apiParam {String} [sort]
* @apiParam {String=ascending,descending} [order]
* @apiParam {String} [q]
* @apiSuccess (200) {Number} total
* @apiSuccess (200) {Array} rows
* @apiSuccess (200) {String} rows.id
* @apiSuccess (200) {String} rows.path
* @apiSuccess (200) {String} rows.transType
* @apiSuccess (200) {Number} rows.inBytes
* @apiSuccess (200) {Number} rows.outBytes
* @apiSuccess (200) {String} rows.startAt
* @apiSuccess (200) {Number} rows.onlines 线
*/
func (h *APIHandler) Pushers(c *gin.Context) {
form := utils.NewPageForm()
if err := c.Bind(form); err != nil {
return
}
hostname := utils.GetRequestHostname(c.Request)
pushers := make([]interface{}, 0)
for _, pusher := range rtsp.Instance.GetPushers() {
port := pusher.Server().TCPPort
rtsp := fmt.Sprintf("rtsp://%s:%d%s", hostname, port, pusher.Path())
2018-11-07 11:28:13 +00:00
if port == 554 {
rtsp = fmt.Sprintf("rtsp://%s%s", hostname, pusher.Path())
2018-11-07 11:28:13 +00:00
}
if form.Q != "" && !strings.Contains(strings.ToLower(rtsp), strings.ToLower(form.Q)) {
continue
}
pushers = append(pushers, map[string]interface{}{
"id": pusher.ID(),
2018-11-24 14:07:02 +00:00
"url": rtsp,
"path": pusher.Path(),
"source": pusher.Source(),
2018-11-24 02:46:37 +00:00
"transType": pusher.TransType(),
"inBytes": pusher.InBytes(),
"outBytes": pusher.OutBytes(),
2018-11-24 02:46:37 +00:00
"startAt": utils.DateTime(pusher.StartAt()),
2018-11-07 11:28:13 +00:00
"onlines": len(pusher.GetPlayers()),
})
}
pr := utils.NewPageResult(pushers)
if form.Sort != "" {
pr.Sort(form.Sort, form.Order)
}
pr.Slice(form.Start, form.Limit)
c.IndentedJSON(200, pr)
}
/**
* @api {get} /api/v1/players
* @apiGroup stats
* @apiName Players
* @apiParam {Number} [start] ,
* @apiParam {Number} [limit]
* @apiParam {String} [sort]
* @apiParam {String=ascending,descending} [order]
* @apiParam {String} [q]
* @apiSuccess (200) {Number} total
* @apiSuccess (200) {Array} rows
* @apiSuccess (200) {String} rows.id
* @apiSuccess (200) {String} rows.path
* @apiSuccess (200) {String} rows.transType
* @apiSuccess (200) {Number} rows.inBytes
* @apiSuccess (200) {Number} rows.outBytes
* @apiSuccess (200) {String} rows.startAt
*/
func (h *APIHandler) Players(c *gin.Context) {
form := utils.NewPageForm()
if err := c.Bind(form); err != nil {
return
}
players := make([]*rtsp.Player, 0)
for _, pusher := range rtsp.Instance.GetPushers() {
for _, player := range pusher.GetPlayers() {
players = append(players, player)
}
}
hostname := utils.GetRequestHostname(c.Request)
_players := make([]interface{}, 0)
for i := 0; i < len(players); i++ {
player := players[i]
port := player.Server.TCPPort
rtsp := fmt.Sprintf("rtsp://%s:%d%s", hostname, port, player.Path)
if port == 554 {
rtsp = fmt.Sprintf("rtsp://%s%s", hostname, player.Path)
}
_players = append(_players, map[string]interface{}{
"id": player.ID,
"path": rtsp,
"transType": player.TransType.String(),
"inBytes": player.InBytes,
"outBytes": player.OutBytes,
"startAt": utils.DateTime(player.StartAt),
})
}
pr := utils.NewPageResult(_players)
if form.Sort != "" {
pr.Sort(form.Sort, form.Order)
}
pr.Slice(form.Start, form.Limit)
c.IndentedJSON(200, pr)
}