Skip to content

Commit

Permalink
bugfix and ui imporve
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Jul 1, 2020
1 parent f7fd58a commit 1b8716b
Show file tree
Hide file tree
Showing 12 changed files with 12,891 additions and 28 deletions.
18 changes: 17 additions & 1 deletion api/controller/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (r RuleController) Register(router *web.Router) {
router.Delete("/{id}/", r.Delete).Name("rules:delete")
})

router.Group("/rules-meta/", func(router *web.Router) {
router.Get("/tags/", r.Tags).Name("rules:meta:tags")
})

router.Group("/rules-test/", func(router *web.Router) {
router.Post("/rule-message/", r.TestMessageMatch).Name("rules:test:rule-message")
router.Post("/rule-check/{type}/", r.Check).Name("rules:test:check")
Expand Down Expand Up @@ -133,7 +137,7 @@ func (r RuleController) Check(ctx web.Context) web.Response {
}

if err != nil {
return ctx.JSON(web.M{"error":err.Error()})
return ctx.JSON(web.M{"error": err.Error()})
}

return ctx.JSON(web.M{
Expand Down Expand Up @@ -382,3 +386,15 @@ func (r RuleController) TestMessageMatch(ctx web.Context) web.Response {
"matched": rs,
})
}

// Tags return all tags existed
func (r RuleController) Tags(ctx web.Context, repo repository.RuleRepo) web.Response {
tags, err := repo.Tags()
if err != nil {
return ctx.JSONError(fmt.Sprintf("query failed: %v", err), http.StatusInternalServerError)
}

return ctx.JSON(web.M{
"tags": tags,
})
}
12,723 changes: 12,723 additions & 0 deletions dashboard/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@fortawesome/free-solid-svg-icons": "^5.11.2",
"@fortawesome/vue-fontawesome": "^0.1.8",
"bootstrap-vue": "^2.15.0",
"core-js": "^2.6.5",
"core-js": "^2.6.11",
"markdown-it": "^10.0.0",
"moment": "^2.24.0",
"vue": "^2.6.10",
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/views/DingdingRobotEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
name: '',
description: '',
token: '',
secret: [],
secret: '',
},
};
},
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/views/Queue.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
{key: 'id', label: '时间/ID'},
{key: 'name', label: '类型'},
{key: 'requeue_times', label: '重试次数'},
{key: 'next_execute_at', label: '最早执行时间'},
{key: 'next_execute_at', label: '期望执行时间'},
{key: 'updated_at', label: '最后更新'},
{key: 'status', label: '状态'},
{key: 'operations', label: '操作'}
Expand Down
37 changes: 24 additions & 13 deletions dashboard/src/views/RuleEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
<b-btn-group class="mb-2 float-right">
<b-btn variant="primary" class="float-right" @click="checkRule(form.template)">检查</b-btn>
</b-btn-group>
<b-form-textarea id="rule" rows="5" v-model="form.rule" class="adanos-code-textarea text-monospace"
placeholder="输入规则,必须返回布尔值"/>
<codemirror v-model="form.rule" class="mt-3 adanos-code-textarea" :options="{smartIndent:true, lineNumbers: true, placeholder: '输入规则,必须返回布尔值', lineWrapping: true}"></codemirror>
<small class="form-text text-muted">
语法参考 <a href="https://github.com/antonmedv/expr/blob/master/docs/Language-Definition.md"
target="_blank">https://github.com/antonmedv/expr/blob/master/docs/Language-Definition.md</a>
Expand All @@ -62,8 +61,7 @@
<b-btn-group class="mb-2 float-right">
<b-btn variant="primary" class="float-right" @click="checkTemplate(form.template)">检查</b-btn>
</b-btn-group>
<b-form-textarea id="template" rows="5" v-model="form.template" class="adanos-code-textarea text-monospace"
placeholder="输入模板"/>
<codemirror v-model="form.template" class="mt-3 adanos-code-textarea" :options="{mode: 'markdown', smartIndent:true, lineNumbers: true, placeholder: '输入模板', lineWrapping: true}"></codemirror>
<small class="form-text text-muted">
语法参考 <a href="https://golang.org/pkg/text/template/" target="_blank">https://golang.org/pkg/text/template/</a>
</small>
Expand All @@ -90,8 +88,9 @@
<b-btn variant="primary" class="float-right" @click="checkTriggerRule(trigger)">检查
</b-btn>
</b-btn-group>
<b-form-textarea id="'trigger_pre_condition_' + i" v-model="trigger.pre_condition"
class="adanos-code-textarea text-monospace" placeholder="默认为 true (全部匹配)"/>
</b-form-group>
<b-form-group label-cols="2">
<codemirror v-model="trigger.pre_condition" class="mt-3 adanos-code-textarea" :options="{smartIndent:true, lineNumbers: true, placeholder: '默认为 true (全部匹配)', lineWrapping: true}"></codemirror>
<small class="form-text text-muted">
语法参考 <a
href="https://github.com/antonmedv/expr/blob/master/docs/Language-Definition.md"
Expand All @@ -117,8 +116,9 @@
<b-btn-group class="mb-2 float-right">
<b-btn variant="primary" class="float-right" @click="checkTemplate(trigger.meta_arr.template)">检查</b-btn>
</b-btn-group>
<b-form-textarea :id="'trigger_meta_template_' + i" rows="5" class="adanos-code-textarea text-monospace"
v-model="trigger.meta_arr.template" placeholder="默认使用分组展示模板"/>
</b-form-group>
<b-form-group>
<codemirror v-model="trigger.meta_arr.template" class="mt-3 adanos-code-textarea" :options="{mode: 'markdown', smartIndent:true, lineNumbers: true, placeholder: '默认使用分组展示模板', lineWrapping: true}"></codemirror>
<small class="form-text text-muted">
语法参考 <a href="https://golang.org/pkg/text/template/" target="_blank">https://golang.org/pkg/text/template/</a>
</small>
Expand Down Expand Up @@ -281,9 +281,15 @@
import TemplateHelp from "../components/TemplateHelp";
import TriggerHelp from "../components/TriggerHelp";
import {codemirror} from 'vue-codemirror-lite';
import 'codemirror/addon/display/placeholder.js';
require('codemirror/mode/go/go');
require('codemirror/mode/markdown/markdown');
export default {
name: 'RuleEdit',
components: {TriggerHelp, TemplateHelp, MatchRuleHelp},
components: {TriggerHelp, TemplateHelp, MatchRuleHelp, codemirror},
data() {
return {
form: {
Expand Down Expand Up @@ -519,8 +525,12 @@
},
},
mounted() {
if (this.$route.params.id !== undefined) {
axios.get('/api/rules/' + this.$route.params.id + '/').then(response => {
if (this.$route.params.id !== undefined || this.$route.query.copy_from !== undefined) {
let ruleId = this.$route.params.id;
if (this.$route.query.copy_from !== undefined) {
ruleId = this.$route.query.copy_from;
}
axios.get('/api/rules/' + ruleId + '/').then(response => {
this.form.name = response.data.name;
this.form.description = response.data.description;
this.form.interval = response.data.interval / 60;
Expand Down Expand Up @@ -604,7 +614,8 @@
font-size: 90%;
font-style: italic;
}
.adanos-code-textarea .text-monospace {
font-size: 85%;
.adanos-code-textarea {
font-size: 14px;
}
.CodeMirror pre.CodeMirror-placeholder { color: #999; }
</style>
14 changes: 13 additions & 1 deletion dashboard/src/views/Rules.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
</b-form>
<b-button to="/rules/add" variant="primary" class="float-right">新增规则</b-button>
</b-card-text>
<b-card-text>
<b-badge :variant="$route.query.tag === undefined ? 'primary':''" class="mr-1" :to="'/rules'">全部</b-badge>
<b-badge :variant="$route.query.tag === tag.name ? 'primary': ''" v-for="(tag, index) in tags" :key="index" class="mr-1" :to="'/rules?tag=' + tag.name">{{ tag.name }}({{ tag.count }})</b-badge>
</b-card-text>
</b-card>
<b-table :items="rules" :fields="fields" :busy="isBusy" show-empty>
<template v-slot:cell(name)="row">
<span v-b-tooltip.hover :title="row.item.id">{{ row.item.name }}</span>
<p>
<b-badge variant="primary" v-for="(tag, index) in row.item.tags" :key="index" class="mr-1" :to="'/rules?tag=' + tag">{{ tag }}</b-badge>
<b-badge :variant="$route.query.tag === tag ? 'primary': 'info'" v-for="(tag, index) in row.item.tags" :key="index" class="mr-1" :to="'/rules?tag=' + tag">{{ tag }}</b-badge>
</p>
</template>
<template v-slot:cell(rule)="row">
Expand Down Expand Up @@ -47,6 +51,7 @@
</template>
<template v-slot:cell(operations)="row">
<b-button-group>
<b-button size="sm" variant="warning" :to="{path:'/rules/add', query: {copy_from: row.item.id}}" target="_blank">复制</b-button>
<b-button size="sm" variant="info" :to="{path:'/rules/' + row.item.id + '/edit'}">编辑</b-button>
<b-button size="sm" variant="danger" @click="delete_rule(row.index, row.item.id)">删除</b-button>
</b-button-group>
Expand Down Expand Up @@ -86,6 +91,7 @@
],
cur: parseInt(this.$route.query.next !== undefined ? this.$route.query.next : 0),
next: -1,
tags: [],
};
},
watch: {
Expand Down Expand Up @@ -149,6 +155,12 @@
}).catch(error => {
this.ToastError(error);
});
axios.get('/api/rules-meta/tags/').then(response => {
this.tags = response.data.tags;
}).catch(error => {
this.ToastError(error);
});
}
},
mounted() {
Expand Down
19 changes: 12 additions & 7 deletions dashboard/src/views/TemplateEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<b-form @submit="onSubmit">
<b-card-group class="mb-3">
<b-card header="基本">
<b-form-group label-cols="2" id="template_type" label="类型" label-for="template_type_input">
<b-form-select id="template_type_input" v-model="form.type"
:options="type_options"></b-form-select>
</b-form-group>
<b-form-group label-cols="2" id="templatename" label="模板名称*" label-for="templatename_input">
<b-form-input id="templatename_input" type="text" v-model="form.name" required
placeholder="输入模板名称"></b-form-input>
Expand All @@ -16,13 +20,7 @@

<b-form-group label-cols="2" id="template_content" label="内容"
label-for="template_content_input">
<b-form-textarea id="template_content_input" rows="5" v-model="form.content"
placeholder="输入模板内容"></b-form-textarea>
</b-form-group>

<b-form-group label-cols="2" id="template_type" label="类型" label-for="template_type_input">
<b-form-select id="template_type_input" v-model="form.type"
:options="type_options"></b-form-select>
<codemirror v-model="form.content" class="mt-3 adanos-code-textarea" :options="{mode: (form.type === 'template' || form.type === 'template_dingding') ? 'markdown':'go',smartIndent:true, lineNumbers: true, placeholder: '输入模板内容', lineWrapping: true}"></codemirror>
</b-form-group>
</b-card>
</b-card-group>
Expand All @@ -37,8 +35,15 @@
<script>
import axios from 'axios'
import {codemirror} from 'vue-codemirror-lite';
import 'codemirror/addon/display/placeholder.js';
require('codemirror/mode/go/go');
require('codemirror/mode/markdown/markdown');
export default {
name: 'TemplateEdit',
components: {codemirror},
data() {
return {
form: {
Expand Down
7 changes: 4 additions & 3 deletions dashboard/src/views/Templates.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
</template>
<template v-slot:cell(operations)="row">
<b-button-group>
<b-button size="sm" @click="row.toggleDetails">
{{ row.detailsShowing ? '隐藏' : '显示' }}详情
</b-button>

<b-button v-if="!row.item.predefined" size="sm" variant="info" :to="{path:'/templates/' + row.item.id + '/edit'}">编辑</b-button>
<b-button v-if="!row.item.predefined" size="sm" variant="danger" @click="delete_template(row.index, row.item.id)">删除</b-button>
<b-button v-if="row.item.predefined" size="sm" disabled>预置</b-button>
<b-button size="sm" @click="row.toggleDetails" class="mr-2">
{{ row.detailsShowing ? '隐藏' : '显示' }}详情
</b-button>
</b-button-group>
</template>
<template v-slot:row-details="row">
Expand Down
26 changes: 26 additions & 0 deletions internal/repository/impl/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ func NewRuleRepo(db *mongo.Database) repository.RuleRepo {
return &RuleRepo{col: db.Collection("rule")}
}

func (r RuleRepo) Tags() ([]repository.Tag, error) {
aggregate, err := r.col.Aggregate(context.TODO(), mongo.Pipeline{
bson.D{{"$unwind", "$tags"}},
bson.D{{"$group", bson.M{
"_id": "$tags",
"count": bson.M{"$sum": 1},
}}},
bson.D{{"$sort", bson.M{"_id": -1}}},
})
if err != nil {
return nil, err
}

tags := make([]repository.Tag, 0)
for aggregate.Next(context.TODO()) {
var tag repository.Tag
if err := aggregate.Decode(&tag); err != nil {
return nil, err
}

tags = append(tags, tag)
}

return tags, nil
}

func (r RuleRepo) Add(rule repository.Rule) (id primitive.ObjectID, err error) {
rule.CreatedAt = time.Now()
rule.UpdatedAt = rule.CreatedAt
Expand Down
63 changes: 63 additions & 0 deletions internal/repository/impl/rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package impl_test

import (
"testing"

"github.com/mylxsw/adanos-alert/internal/repository"
"github.com/mylxsw/adanos-alert/internal/repository/impl"
"github.com/stretchr/testify/suite"
"go.mongodb.org/mongo-driver/bson"
)

type RuleTestSuite struct {
suite.Suite
ruleRepo repository.RuleRepo
}

func (r *RuleTestSuite) TearDownTest() {
r.NoError(r.ruleRepo.Delete(bson.M{}))
}

func (r *RuleTestSuite) SetupTest() {
db, err := Database()
r.NoError(err)

r.ruleRepo = impl.NewRuleRepo(db)
}

func (r *RuleTestSuite) TestTags() {
_, _ = r.ruleRepo.Add(repository.Rule{
Name: "test1",
Tags: []string{"mysql", "oracle", "nginx"},
})
_, _ = r.ruleRepo.Add(repository.Rule{
Name: "test2",
Tags: []string{"mysql"},
})
_, _ = r.ruleRepo.Add(repository.Rule{
Name: "test3",
Tags: nil,
})
_, _ = r.ruleRepo.Add(repository.Rule{
Name: "test4",
Tags: []string{"基础设施"},
})
_, _ = r.ruleRepo.Add(repository.Rule{
Name: "test5",
Tags: []string{"数据"},
})

tags, err := r.ruleRepo.Tags()
r.NoError(err)
r.ElementsMatch(tags, []repository.Tag{
{Name: "oracle", Count: 1},
{Name: "mysql", Count: 2},
{Name: "nginx", Count: 1},
{Name: "数据", Count: 1},
{Name: "基础设施", Count: 1},
})
}

func TestRuleRepo(t *testing.T) {
suite.Run(t, new(RuleTestSuite))
}
6 changes: 6 additions & 0 deletions internal/repository/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const (
RuleStatusDisabled RuleStatus = "disabled"
)

type Tag struct {
Name string `bson:"_id" json:"name"`
Count int64 `bson:"count" json:"count"`
}

// Rule is a rule definition
type Rule struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
Expand Down Expand Up @@ -56,4 +61,5 @@ type RuleRepo interface {
Count(filter bson.M) (int64, error)
Delete(filter bson.M) error
DeleteID(id primitive.ObjectID) error
Tags() ([]Tag, error)
}

0 comments on commit 1b8716b

Please sign in to comment.