feat: 从黄果 NET hgdjnet-server 9bfbafe 导入 HKDJ 官网后端
来源:hgdjnet-server 9bfbafe10d417deea959bd53117bc88a10e60a83 项目身份 HKDJ / JHA-216,独立官网仓库,Jenkins 守卫键 hkdjnet 测试目录 /server/hkdj_net,App/Web API 30331/30332 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package ipdailystatmod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"hkdj-web-server/pub/mongos"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var db *mongos.Collection
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
db = mongos.Shop.Coll(TN.IpDailyStat)
|
||||
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.IpDailyStat.AppId, Value: 1},
|
||||
{Key: FN.IpDailyStat.Date, Value: 1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
_, _ = db.CreateIndex(ctx, indexes)
|
||||
}
|
||||
|
||||
func UpsertCount(ctx context.Context, appId int64, date string, count int64) error {
|
||||
filter := bson.M{
|
||||
FN.IpDailyStat.AppId: appId,
|
||||
FN.IpDailyStat.Date: date,
|
||||
}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
FN.IpDailyStat.Count: count,
|
||||
FN.IpDailyStat.UpdatedAt: time.Now(),
|
||||
},
|
||||
}
|
||||
_, err := db.UpsertOne(ctx, filter, update)
|
||||
return err
|
||||
}
|
||||
|
||||
func FindCount(ctx context.Context, appId int64, date string) (int64, error) {
|
||||
filter := bson.M{
|
||||
FN.IpDailyStat.AppId: appId,
|
||||
FN.IpDailyStat.Date: date,
|
||||
}
|
||||
var stat IpDailyStat
|
||||
err := db.FindOneEx(ctx, &stat, filter)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return stat.Count, nil
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package ipdailystatmod
|
||||
|
||||
type tagIpDailyStat struct {
|
||||
AppId string
|
||||
Date string
|
||||
Count string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
IpDailyStat tagIpDailyStat
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
IpDailyStat string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.IpDailyStat.AppId = "appId"
|
||||
FN.IpDailyStat.Date = "date"
|
||||
FN.IpDailyStat.Count = "count"
|
||||
FN.IpDailyStat.UpdatedAt = "updatedAt"
|
||||
|
||||
TN.IpDailyStat = "ip_daily_stat"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package ipdailystatmod
|
||||
|
||||
import "time"
|
||||
|
||||
// IpDailyStat 每日访客 IP 去重统计。
|
||||
// @table:ip_daily_stat
|
||||
type IpDailyStat struct {
|
||||
AppId int64 `bson:"appId"` // 站点 ID
|
||||
Date string `bson:"date"` // 日期,格式 "20060102"
|
||||
Count int64 `bson:"count"` // 当日去重 IP 数
|
||||
UpdatedAt time.Time `bson:"updatedAt"` // 最后同步时间
|
||||
}
|
||||
Reference in New Issue
Block a user