Skip to content

Commit a946939

Browse files
committed
增加转换时间为持续时长用于消息模板
1 parent a53dc38 commit a946939

3 files changed

Lines changed: 53 additions & 11 deletions

File tree

controllers/prometheusalert.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,13 @@ func SetRecord(AlertValue interface{}) {
379379
// 消息模版化
380380
func TransformAlertMessage(p_json interface{}, tpltext string) (error error, msg string) {
381381
funcMap := template.FuncMap{
382-
"GetCSTtime": GetCSTtime,
383-
"TimeFormat": TimeFormat,
384-
"GetTime": GetTime,
385-
"toUpper": strings.ToUpper,
386-
"toLower": strings.ToLower,
387-
"title": strings.Title,
382+
"GetTimeDuration": GetTimeDuration,
383+
"GetCSTtime": GetCSTtime,
384+
"TimeFormat": TimeFormat,
385+
"GetTime": GetTime,
386+
"toUpper": strings.ToUpper,
387+
"toLower": strings.ToLower,
388+
"title": strings.Title,
388389
// join is equal to strings.Join but inverts the argument order
389390
// for easier pipelining in templates.
390391
"join": func(sep string, s []string) string {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package controllers
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGetTimeDuration(t *testing.T) {
10+
source := "2023-08-04T02:51:54.972Z"
11+
duration := GetTimeDuration(source)
12+
if assert.NotEqual(t, "", duration) {
13+
t.Log(duration)
14+
}
15+
}

controllers/public.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ package controllers
22

33
import (
44
"bufio"
5-
"github.com/astaxie/beego"
6-
"github.com/astaxie/beego/logs"
5+
"fmt"
76
"math/rand"
87
"os"
98
"strconv"
109
"strings"
1110
"time"
11+
12+
"github.com/astaxie/beego"
13+
"github.com/astaxie/beego/logs"
1214
)
1315

1416
func LogsSign() string {
1517
return strconv.FormatInt(time.Now().UnixNano(), 10)
1618
}
1719

18-
//转换时间戳到时间字符串
20+
// 转换时间戳到时间字符串
1921
func GetTime(timeStr interface{}, timeFormat ...string) string {
2022
var R_Time string
2123
//判断传入的timeStr是否为float64类型,如gerrit消息中时间戳就是float64
@@ -39,7 +41,31 @@ func GetTime(timeStr interface{}, timeFormat ...string) string {
3941
return R_Time
4042
}
4143

42-
//转换UTC时区到CST
44+
// 转换时间为持续时长
45+
func GetTimeDuration(date string) string {
46+
var tm = "N/A"
47+
if date != "" {
48+
T1 := date[0:10]
49+
T2 := date[11:19]
50+
T3 := T1 + " " + T2
51+
tm2, _ := time.Parse("2006-01-02 15:04:05", T3)
52+
sub := time.Now().UTC().Sub(tm2.UTC())
53+
54+
t := int64(sub.Seconds())
55+
if t > 86400 {
56+
tm = fmt.Sprintf("%dd%dh", t/86400, t%86400/3600)
57+
} else if t > 3600 {
58+
tm = fmt.Sprintf("%dh%dm", t/3600, t%3600/60)
59+
} else if t > 60 {
60+
tm = fmt.Sprintf("%dh%dm", t/60, t%60)
61+
} else {
62+
tm = fmt.Sprintf("%ds", t)
63+
}
64+
}
65+
return tm
66+
}
67+
68+
// 转换UTC时区到CST
4369
func GetCSTtime(date string) string {
4470
var tm string
4571
tm = time.Now().Format("2006-01-02 15:04:05")
@@ -67,7 +93,7 @@ func TimeFormat(timestr, format string) string {
6793
}
6894
}
6995

70-
//获取用户号码
96+
// 获取用户号码
7197
func GetUserPhone(neednum int) string {
7298
//判断是否存在user.csv文件
7399
Num := beego.AppConfig.String("defaultphone")

0 commit comments

Comments
 (0)