123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- package util
- import (
- commonConfig "cicv-data-closedloop/kinglong/common/cfg"
- "cicv-data-closedloop/kinglong/common/ent"
- "cicv-data-closedloop/kinglong/common/global"
- "cicv-data-closedloop/kinglong/common/log"
- "fmt"
- "os"
- "os/exec"
- "path/filepath"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- func AppendIfNotExists(slice []string, element string) []string {
- for _, item := range slice {
- if item == element {
- return slice
- }
- }
- return append(slice, element)
- }
- func AddTimeWindowToTimeWindowProducerQueue(window ent.TimeWindow) {
- global.TimeWindowProducerQueueMutex.RLock()
- {
- global.TimeWindowProducerQueue = append(global.TimeWindowProducerQueue, window)
- }
- global.TimeWindowProducerQueueMutex.RUnlock()
- }
- func AddTimeWindowToTimeWindowConsumerQueue(window ent.TimeWindow) {
- global.TimeWindowConsumerQueueMutex.RLock()
- {
- global.TimeWindowConsumerQueue = append(global.TimeWindowConsumerQueue, window)
- }
- global.TimeWindowConsumerQueueMutex.RUnlock()
- }
- func RemoveHeadOfdTimeWindowProducerQueue() {
- global.TimeWindowProducerQueueMutex.RLock()
- {
- global.TimeWindowProducerQueue = global.TimeWindowProducerQueue[1:]
- }
- global.TimeWindowProducerQueueMutex.RUnlock()
- }
- func RemoveHeaOfdTimeWindowConsumerQueue() {
- global.TimeWindowConsumerQueueMutex.RLock()
- {
- global.TimeWindowConsumerQueue = global.TimeWindowConsumerQueue[1:]
- }
- global.TimeWindowConsumerQueueMutex.RUnlock()
- }
- func GetBagTime(bagName string) string {
- s1 := strings.Split(bagName, "_")[0]
- s1Split := strings.Split(s1, "/")
- s2 := s1Split[len(s1Split)-1]
- return s2
- }
- func GetNowTimeCustom() string {
- currentTime := time.Now()
- formattedTime := currentTime.Format("2006-01-02-15-04-05")
- return formattedTime
- }
- func TimeCustomChange(originalTimeStr string, number int) string {
- var newTimeStr string
-
- layout := "2006-01-02-15-04-05"
- originalTime, err := time.Parse(layout, originalTimeStr)
- if err != nil {
- log.GlobalLogger.Info("无法解析时间字符串:", err)
- return newTimeStr
- }
-
- newTime := originalTime.Add(time.Duration(number) * time.Second)
-
- return newTime.Format(layout)
- }
- func CalculateDifferenceOfTimeCustom(timeCustom1 string, timeCustom2 string) int {
- timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
- timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
- return timeInt2 - timeInt1 + 1
- }
- func TimeCustom1GreaterTimeCustom2(timeCustom1 string, timeCustom2 string) bool {
- timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
- timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
- return timeInt1 > timeInt2
- }
- func TimeCustom1GreaterEqualThanTimeCustom2(timeCustom1 string, timeCustom2 string) bool {
- timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
- timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
- return timeInt1 >= timeInt2
- }
- func TimeCustom1LessEqualThanTimeCustom2(timeCustom1 string, timeCustom2 string) bool {
- timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
- timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
- return timeInt1 <= timeInt2
- }
- func GetDiskUsagePercent() float64 {
-
- cmd := exec.Command("df", "--total")
- output, err := cmd.Output()
- if err != nil {
- log.GlobalLogger.Info("执行命令失败:", err)
- return 0.0
- }
-
- lines := strings.Split(string(output), "\n")
- for _, line := range lines[1:] {
- fields := strings.Fields(line)
- if len(fields) >= 6 && fields[0] == "total" {
-
- total, _ := strconv.ParseFloat(strings.TrimSuffix(fields[1], "G"), 64)
- used, _ := strconv.ParseFloat(strings.TrimSuffix(fields[2], "G"), 64)
- usedPercent := (used / total) * 100
-
- return usedPercent
- }
- }
- return 0.0
- }
- func ListAbsolutePathAndSort(dir string) []string {
- var result []string
- if !strings.HasSuffix(dir, "/") {
- dir = dir + "/"
- }
- files, err := os.ReadDir(dir)
- if err != nil {
- log.GlobalLogger.Info("获取文件列表失败:", err)
- return result
- }
- for _, file := range files {
- result = append(result, dir+file.Name())
- }
-
- sort.Slice(result, func(i, j int) bool {
- return filepath.Base(result[i]) < filepath.Base(result[j])
- })
- return result
- }
- func GetCopyDir(faultTime string) string {
- return commonConfig.CloudConfig.BagCopyDir + faultTime + "/"
- }
- func MergeSlice(slice1 []string, slice2 []string) []string {
-
- for _, element := range slice2 {
- found := false
- for _, item := range slice1 {
- if element == item {
- found = true
- break
- }
- }
- if !found {
- slice1 = append(slice1, element)
- }
- }
- return slice1
- }
- func DeleteFile(path string) {
-
- if _, err := os.Stat(path); err == nil {
-
- err := os.Remove(path)
- if err != nil {
- fmt.Printf("删除文件时发生错误:%s\n", err)
- return
- }
- }
- }
- func GetLastTimeWindow() *ent.TimeWindow {
- var lastTimeWindow *ent.TimeWindow
- if len(global.TimeWindowProducerQueue) > 0 {
- lastTimeWindow = &global.TimeWindowProducerQueue[len(global.TimeWindowProducerQueue)-1]
- }
- return lastTimeWindow
- }
- func RefreshTcpSendTime() {
- global.TcpSendTimeMutex.Lock()
- global.TcpSendTime = time.Now()
- global.TcpSendTimeMutex.Unlock()
- }
- func SupplyCopyBags(currentTimeWindow ent.TimeWindow) {
-
- copyBags := ListAbsolutePathWithSuffixAndSort(GetCopyDir(currentTimeWindow.FaultTime), ".bag")
- copyBagsLength := len(copyBags)
- if copyBagsLength < currentTimeWindow.Length {
- time.Sleep(time.Duration(copyBagsLength) * time.Second)
- dataBags := ListAbsolutePathWithSuffixAndSort(commonConfig.CloudConfig.BagDataDir, ".bag")
- gap := currentTimeWindow.Length - copyBagsLength
- log.GlobalLogger.Info("故障 ", currentTimeWindow.FaultTime, "需要补充 ", gap, " 个 bag 包")
- for _, bag := range dataBags {
- bagTime := GetBagTime(bag)
- if TimeCustom1GreaterEqualThanTimeCustom2(bagTime, currentTimeWindow.FaultTime) {
- MoveFromDataToCopy(currentTimeWindow.FaultTime, bag)
- gap = gap - 1
- if gap == 0 {
- break
- }
- }
- }
- }
- }
|