The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/util/strings.go

168 lines
3.5 KiB

package util
import (
"encoding/json"
"fmt"
"math"
"regexp"
"strings"
"time"
"unicode"
)
var stringListItemMatcher = regexp.MustCompile(`"[^"]+"|[^,\t\n\v\f\r ]+`)
// StringsFallback2 returns the first of two not empty strings.
func StringsFallback2(val1 string, val2 string) string {
return stringsFallback(val1, val2)
}
// StringsFallback3 returns the first of three not empty strings.
func StringsFallback3(val1 string, val2 string, val3 string) string {
return stringsFallback(val1, val2, val3)
}
func stringsFallback(vals ...string) string {
for _, v := range vals {
if v != "" {
return v
}
}
return ""
}
// SplitString splits a string and returns a list of strings. It supports JSON list syntax and strings separated by commas or spaces.
// It supports quoted strings with spaces, e.g. "foo bar", "baz".
func SplitString(str string) []string {
if len(str) == 0 {
return []string{}
}
// JSON list syntax support
if strings.Index(strings.TrimSpace(str), "[") == 0 {
var res []string
err := json.Unmarshal([]byte(str), &res)
if err != nil {
return []string{}
}
return res
}
var result []string
matches := stringListItemMatcher.FindAllString(str, -1)
for _, match := range matches {
result = append(result, strings.Trim(match, "\""))
}
return result
}
// GetAgeString returns a string representing certain time from years to minutes.
func GetAgeString(t time.Time) string {
if t.IsZero() {
return "?"
}
sinceNow := time.Since(t)
minutes := sinceNow.Minutes()
years := int(math.Floor(minutes / 525600))
months := int(math.Floor(minutes / 43800))
days := int(math.Floor(minutes / 1440))
hours := int(math.Floor(minutes / 60))
var amount string
if years > 0 {
if years == 1 {
amount = "year"
} else {
amount = "years"
}
return fmt.Sprintf("%d %s", years, amount)
}
if months > 0 {
if months == 1 {
amount = "month"
} else {
amount = "months"
}
return fmt.Sprintf("%d %s", months, amount)
}
if days > 0 {
if days == 1 {
amount = "day"
} else {
amount = "days"
}
return fmt.Sprintf("%d %s", days, amount)
}
if hours > 0 {
if hours == 1 {
amount = "hour"
} else {
amount = "hours"
}
return fmt.Sprintf("%d %s", hours, amount)
}
if int(minutes) > 0 {
if int(minutes) == 1 {
amount = "minute"
} else {
amount = "minutes"
}
return fmt.Sprintf("%d %s", int(minutes), amount)
}
return "< 1 minute"
}
func RemainingDaysUntil(expiration time.Time) string {
currentTime := time.Now()
durationUntil := expiration.Sub(currentTime)
daysUntil := int(durationUntil.Hours() / 24)
if daysUntil == 0 {
return "Today"
} else if daysUntil == 1 {
return "Tomorrow"
} else {
return fmt.Sprintf("%d days", daysUntil)
}
}
// ToCamelCase changes kebab case, snake case or mixed strings to camel case. See unit test for examples.
func ToCamelCase(str string) string {
var finalParts []string
parts := strings.Split(str, "_")
for _, part := range parts {
finalParts = append(finalParts, strings.Split(part, "-")...)
}
for index, part := range finalParts[1:] {
finalParts[index+1] = strings.Title(part)
}
return strings.Join(finalParts, "")
}
func Capitalize(s string) string {
if len(s) == 0 {
return s
}
r := []rune(s)
r[0] = unicode.ToUpper(r[0])
return string(r)
}
func ByteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}