forked from all_user/stat_center
84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
package channelpackage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"math"
|
|
"math/rand"
|
|
"net/url"
|
|
"stat/common/stdLog"
|
|
"time"
|
|
)
|
|
|
|
func (c *ChannelPackage) IncDownCount(ctx context.Context) *stdLog.SrvErr {
|
|
update := bson.M{
|
|
"$inc": bson.M{
|
|
FN.ChannelPackage.DownCount: 1,
|
|
},
|
|
"$set": bson.M{
|
|
FN.ChannelPackage.DownLatest: time.Now(),
|
|
},
|
|
}
|
|
return UpdateOneChannelPackage(ctx, bson.M{FN.ChannelPackage.Id: c.Id}, update)
|
|
}
|
|
|
|
func (c *ChannelPackage) GetRandomLink(domains []string) string {
|
|
if len(domains) > 0 {
|
|
base := domains[rand.Intn(len(domains))]
|
|
link, _ := url.JoinPath(base, c.UrlPath)
|
|
return link
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (c *ChannelPackage) GetAllLink(domains []string) []string {
|
|
if len(domains) > 0 {
|
|
list := make([]string, 0)
|
|
for _, domain := range domains {
|
|
link, _ := url.JoinPath(domain, c.UrlPath)
|
|
list = append(list, link)
|
|
}
|
|
return list
|
|
}
|
|
return []string{}
|
|
}
|
|
|
|
func (c *ChannelPackage) CanMake(force bool) (bool, time.Duration, string) {
|
|
// 先判断锁定时间(强制生成不可破锁)
|
|
duration := ApkLockTimeDuration - time.Since(c.LockTime)
|
|
if duration > 0 {
|
|
return false, duration, fmt.Sprintf("记录锁定")
|
|
}
|
|
if force {
|
|
return true, 0, "强制生成"
|
|
}
|
|
if c.Status == ChanApkStatusSuccess {
|
|
// 已成功的记录
|
|
return false, duration, "已经生成成功过,无需重新生成"
|
|
} else if c.Status == ChanApkStatusMaking {
|
|
// 判断超时的记录
|
|
current := time.Now().Sub(c.GenTime)
|
|
diff := ApkNoResponseDuration - current
|
|
if diff <= 0 {
|
|
duration := time.Duration(math.Pow(3, float64(c.GenCount))) * time.Minute
|
|
if current > duration {
|
|
return true, -1, "可以生成"
|
|
} else {
|
|
return false, duration - current, "未达到可以再次生成的时间"
|
|
}
|
|
} else {
|
|
return false, diff, "正在生成中"
|
|
}
|
|
} else {
|
|
// 判断失败的记录
|
|
duration := time.Duration(math.Pow(3, float64(c.GenCount))) * time.Minute
|
|
current := time.Now().Sub(c.GenTime)
|
|
if current > duration {
|
|
return true, -1, "上次生成失败已超时,现在可以重新生成"
|
|
} else {
|
|
return false, duration - current, "上次生成失败,重新生成的时间未到"
|
|
}
|
|
}
|
|
}
|