This commit is contained in:
suyl
2021-07-20 17:21:38 +08:00
parent 0fd6aa4207
commit d750bfe18d
10 changed files with 374 additions and 16 deletions

View File

@@ -5,10 +5,12 @@ import (
"encoding/json"
"fmt"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-print/globals"
"git.rosy.net.cn/jx-print/model"
"github.com/dchest/captcha"
"github.com/gin-gonic/gin"
"math"
"math/rand"
"net/http"
"strings"
@@ -33,6 +35,12 @@ func init() {
var (
letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterBytesNum = "0123456789"
flowUnitMap = map[string]string{
"KB": "KB",
"MB": "MB",
"GB": "GB",
}
)
func RandStringBytes(n int) string {
@@ -192,3 +200,55 @@ func Format4Output(obj interface{}, isSingleLine bool) (retVal string) {
}
return retVal
}
func GetNextTimeFromList(now time.Time, timeList []string) (snapshotAt time.Time) {
dateStr := utils.Time2DateStr(now)
timeListLen := len(timeList)
selectTime := utils.Str2Time(utils.Time2DateStr(now.Add(24*time.Hour)) + " " + timeList[0])
for k := range timeList {
v := timeList[timeListLen-k-1]
tmpTime := utils.Str2Time(dateStr + " " + v)
if tmpTime.Sub(now) < 0 {
break
}
selectTime = tmpTime
}
return selectTime
}
func SplitFlowAndUnit(flowStr string) (flow float64, unit string) {
for _, v := range flowUnitMap {
if strings.Contains(flowStr, v) {
return utils.Str2Float64WithDefault(flowStr[:len(flowStr)-2], 0), flowStr[len(flowStr)-2:]
}
}
return flow, unit
}
func Flow2KB(flow float64, unit string) (flowKB float64) {
if unit == "KB" {
return flow
} else if unit == "MB" {
return flow * 1024
} else if unit == "GB" {
return flow * 1024 * 1024
}
return flowKB
}
func FlowKB2Other(flowKB float64) (flow float64, unit string) {
if flowKB < 1024 {
return flowKB, "KB"
} else {
flowMB := math.Round(flowKB / float64(1024))
if flowMB < 1024 {
return flowMB, "MB"
} else {
flowGB := math.Round(flowMB / float64(1024))
if flowGB < 1024 {
return flowGB, "GB"
}
}
}
return flow, unit
}