Skip to content

Commit

Permalink
feat: updated node monitoring tab
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jul 4, 2024
1 parent 40f37e8 commit f4025a9
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions core/utils/time.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package utils

import (
"errors"
"github.com/apex/log"
"github.com/crawlab-team/crawlab/trace"
"regexp"
"strconv"
"time"
)

Expand All @@ -16,3 +21,53 @@ func GetLocalTimeString(t time.Time) string {
t = GetLocalTime(t)
return GetTimeString(t)
}

func GetTimeUnitParts(timeUnit string) (num int, unit string, err error) {
re := regexp.MustCompile(`^(\d+)([a-zA-Z])$`)
groups := re.FindStringSubmatch(timeUnit)
if len(groups) < 3 {
err = errors.New("failed to parse duration text")
log.Errorf("failed to parse duration text: %v", err)
trace.PrintError(err)
return 0, "", err
}
num, err = strconv.Atoi(groups[1])
if err != nil {
log.Errorf("failed to convert string to int: %v", err)
trace.PrintError(err)
return 0, "", err
}
unit = groups[2]
return num, unit, nil
}

func GetTimeDuration(num string, unit string) (d time.Duration, err error) {
numInt, err := strconv.Atoi(num)
if err != nil {
log.Errorf("failed to convert string to int: %v", err)
trace.PrintError(err)
return d, err
}
switch unit {
case "s":
d = time.Duration(numInt) * time.Second
case "m":
d = time.Duration(numInt) * time.Minute
case "h":
d = time.Duration(numInt) * time.Hour
case "d":
d = time.Duration(numInt) * 24 * time.Hour
case "w":
d = time.Duration(numInt) * 7 * 24 * time.Hour
case "M":
d = time.Duration(numInt) * 30 * 24 * time.Hour
case "y":
d = time.Duration(numInt) * 365 * 24 * time.Hour
default:
err = errors.New("invalid time unit")
log.Errorf("invalid time unit: %v", unit)
trace.PrintError(err)
return d, err
}
return d, nil
}

0 comments on commit f4025a9

Please sign in to comment.