39 lines
1.0 KiB
Go
39 lines
1.0 KiB
Go
package srv_ipstat
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/utils"
|
|
"videoweb/pub/models/ipdailystatmod"
|
|
"videoweb/pub/pubdef"
|
|
)
|
|
|
|
// IpStatResp 今日和昨日访问 IP 去重数。
|
|
type IpStatResp struct {
|
|
TodayIP int64 `json:"todayIP"`
|
|
YesterdayIP int64 `json:"yesterdayIP"`
|
|
}
|
|
|
|
// FetchIpStat 返回 app 端同步到 MongoDB 的每日访问 IP 去重数。
|
|
func FetchIpStat(ctx context.Context) *IpStatResp {
|
|
resp := &IpStatResp{}
|
|
now := time.Now().In(utils.LocBJ)
|
|
today := now.Format("20060102")
|
|
yesterday := now.AddDate(0, 0, -1).Format("20060102")
|
|
|
|
if cnt, err := ipdailystatmod.FindCount(ctx, pubdef.AppId, today); err == nil {
|
|
resp.TodayIP = cnt
|
|
} else {
|
|
log.Warn("FetchIpStat: FindCount today failed",
|
|
log.Any("appId", pubdef.AppId), log.E(err))
|
|
}
|
|
if cnt, err := ipdailystatmod.FindCount(ctx, pubdef.AppId, yesterday); err == nil {
|
|
resp.YesterdayIP = cnt
|
|
} else {
|
|
log.Warn("FetchIpStat: FindCount yesterday failed",
|
|
log.Any("appId", pubdef.AppId), log.E(err))
|
|
}
|
|
return resp
|
|
}
|