Skip to content

Commit

Permalink
feat(template): 模板测试
Browse files Browse the repository at this point in the history
  • Loading branch information
aide-cloud committed Apr 26, 2024
1 parent be1f03a commit 21ecbff
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
56 changes: 56 additions & 0 deletions pkg/strategy/alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ package strategy

import (
"testing"
"time"

"github.com/aide-family/moon/pkg/util/hash"
)

type AlertBo struct {
Status string `json:"status"`
Labels *Labels `json:"labels"`
Annotations *Annotations `json:"annotations"`
StartsAt string `json:"startsAt"`
EndsAt string `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
Fingerprint string `json:"fingerprint"`
Value float64 `json:"value"`
}

func TestNewAlarm(t *testing.T) {
// 定义一个 map[string]string 类型的数据结构
data := map[string]any{
Expand All @@ -28,3 +42,45 @@ Label {{ labels.String }}`

t.Log(Formatter(str, data))
}

func TestFormatter(t *testing.T) {
labels := Labels{
"__name__": "test",
"__alert_id__": "1",
"__group_name__": "test",
"__group_id__": "1",
"__level_id__": "1",
"instance": "test-instance",
"alertname": "test-alert",
}
annotations := Annotations{
"summary": "test summary",
"description": "test description",
}
data := &AlertBo{
Status: "firing",
Labels: &labels,
Annotations: &annotations,
StartsAt: time.Now().Format(time.RFC3339),
EndsAt: time.Now().Add(time.Hour).Format(time.RFC3339),
GeneratorURL: "https://prometheus.aide-cloud.cn/#/home",
Fingerprint: hash.MD5("test"),
Value: 100.0011,
}

templateStr := `
告警状态: {{ .Status }}
告警标签: {{ .Labels }}
机器实例: {{ .Labels.instance }}
告警规则名称: {{ .Labels.alertname }}
告警内容: {{ .Annotations }}
告警描述: {{ .Annotations.summary }}
告警详情: {{ .Annotations.description }}
告警时间: {{ .StartsAt }}
恢复时间: {{ .EndsAt }}
链接地址: {{ .GeneratorURL }}
告警指纹: {{ .Fingerprint }}
当前值: {{ .Value }}
`
t.Log(Formatter(templateStr, data))
}
24 changes: 18 additions & 6 deletions web/src/pages/home/monitor/strategy-group/strategy/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -981,12 +981,24 @@ export const tourSteps = (refs: {
const TemplateTooltip = () => {
return (
<div>
<p>
告警内容,用于在告警信息中展示, 支持模板语法, 例如:
<br />
{'{{ $labels.instance }}'}
</p>
<hr />
告警内容,用于在告警信息中展示, 支持模板语法, 例如:
<br/>
<hr/>
<div style={{fontSize: '12px'}}>
{`告警状态: {{ .Status }}`}<br/>
{`告警标签: {{ .Labels }}`}<br/>
{`机器实例: {{ .Labels.instance }}`}<br/>
{`规则名称: {{ .Labels.alertname }}`}<br/>
{`告警内容: {{ .Annotations }}`}<br/>
{`告警描述: {{ .Annotations.summary }}`}<br/>
{`告警详情: {{ .Annotations.description }}`}<br/>
{`告警时间: {{ .StartsAt }}`}<br/>
{`恢复时间: {{ .EndsAt }}`}<br/>
{`链接地址: {{ .GeneratorURL }}`}<br/>
{`告警指纹: {{ .Fingerprint }}`}<br/>
{`当前值: {{ .Value }}`}<br/>
</div>
<hr/>
<Button
type="link"
href="https://open.dingtalk.com/document/orgapp/custom-bot-send-message-type#"
Expand Down

0 comments on commit 21ecbff

Please sign in to comment.