Skip to content

Commit

Permalink
Merge pull request #332 from xtso520ok/feature-timeformat
Browse files Browse the repository at this point in the history
Feature timeformat
  • Loading branch information
feiyu563 committed Aug 28, 2023
2 parents f6b1dfe + a946939 commit 6279ad6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
13 changes: 7 additions & 6 deletions controllers/prometheusalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,13 @@ func SetRecord(AlertValue interface{}) {
// 消息模版化
func TransformAlertMessage(p_json interface{}, tpltext string) (error error, msg string) {
funcMap := template.FuncMap{
"GetCSTtime": GetCSTtime,
"TimeFormat": TimeFormat,
"GetTime": GetTime,
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"title": strings.Title,
"GetTimeDuration": GetTimeDuration,
"GetCSTtime": GetCSTtime,
"TimeFormat": TimeFormat,
"GetTime": GetTime,
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"title": strings.Title,
// join is equal to strings.Join but inverts the argument order
// for easier pipelining in templates.
"join": func(sep string, s []string) string {
Expand Down
15 changes: 15 additions & 0 deletions controllers/prometheusalert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package controllers

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetTimeDuration(t *testing.T) {
source := "2023-08-04T02:51:54.972Z"
duration := GetTimeDuration(source)
if assert.NotEqual(t, "", duration) {
t.Log(duration)
}
}
36 changes: 31 additions & 5 deletions controllers/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package controllers

import (
"bufio"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"

"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
)

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

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

//转换UTC时区到CST
// 转换时间为持续时长
func GetTimeDuration(date string) string {
var tm = "N/A"
if date != "" {
T1 := date[0:10]
T2 := date[11:19]
T3 := T1 + " " + T2
tm2, _ := time.Parse("2006-01-02 15:04:05", T3)
sub := time.Now().UTC().Sub(tm2.UTC())

t := int64(sub.Seconds())
if t > 86400 {
tm = fmt.Sprintf("%dd%dh", t/86400, t%86400/3600)
} else if t > 3600 {
tm = fmt.Sprintf("%dh%dm", t/3600, t%3600/60)
} else if t > 60 {
tm = fmt.Sprintf("%dh%dm", t/60, t%60)
} else {
tm = fmt.Sprintf("%ds", t)
}
}
return tm
}

// 转换UTC时区到CST
func GetCSTtime(date string) string {
var tm string
tm = time.Now().Format("2006-01-02 15:04:05")
Expand Down Expand Up @@ -67,7 +93,7 @@ func TimeFormat(timestr, format string) string {
}
}

//获取用户号码
// 获取用户号码
func GetUserPhone(neednum int) string {
//判断是否存在user.csv文件
Num := beego.AppConfig.String("defaultphone")
Expand Down

1 comment on commit 6279ad6

@afghanistanyn
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

正缺这个函数来实现持续时间,666

Please sign in to comment.