60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package ipdailystatmod
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
"videoweb/pub/mongos"
|
|
|
|
"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
|
|
}
|