Skip to content

Commit

Permalink
增加事件清理功能
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Nov 22, 2020
1 parent 1b0b04a commit 216f33b
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 190 deletions.
45 changes: 45 additions & 0 deletions api/controller/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (

"github.com/mylxsw/adanos-alert/internal/repository"
"github.com/mylxsw/adanos-alert/internal/template"
"github.com/mylxsw/adanos-alert/pubsub"
"github.com/mylxsw/adanos-alert/service"
"github.com/mylxsw/container"
"github.com/mylxsw/glacier/event"
"github.com/mylxsw/glacier/web"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand All @@ -26,6 +29,7 @@ func (g GroupController) Register(router *web.Router) {
router.Group("/groups/", func(router *web.Router) {
router.Get("/", g.Groups).Name("groups:all")
router.Get("/{id}/", g.Group).Name("groups:one")
router.Delete("/{id}/reduce/", g.CutGroupEvents).Name("groups:reduce")
})

router.Group("/recoverable-groups/", func(router *web.Router) {
Expand Down Expand Up @@ -151,6 +155,47 @@ func (g GroupController) Group(
}, nil
}

// CutGroupEvents 缩减事件组中包含的事件,对已经完成聚合的事件组有效,
// 该操作不会影响事件组上对事件总数的计数
func (g GroupController) CutGroupEvents(webCtx web.Context, evtGrpRepo repository.EventGroupRepo, evtGroupSvc service.EventGroupService, em event.Manager) web.Response {
groupID, err := primitive.ObjectIDFromHex(webCtx.PathVar("id"))
if err != nil {
return webCtx.JSONError(err.Error(), http.StatusUnprocessableEntity)
}

grp, err := evtGrpRepo.Get(groupID)
if err != nil {
return webCtx.JSONError(err.Error(), http.StatusInternalServerError)
}

if grp.Status == repository.EventGroupStatusCollecting || grp.Status == repository.EventGroupStatusPending {
return webCtx.JSONError("当前事件组暂时不支持该操作", http.StatusUnprocessableEntity)
}

keepCount := webCtx.Int64Input("keep", 20)
if keepCount < 0 || keepCount > 1000 {
return webCtx.JSONError("keep: 保留事件数必须在 0 - 1000 之间", http.StatusUnprocessableEntity)
}

ctx, cancel := context.WithTimeout(webCtx.Context(), 10*time.Second)
defer cancel()

deletedCount, err := evtGroupSvc.CutGroup(ctx, groupID, keepCount)
if err != nil {
return webCtx.JSONError(err.Error(), http.StatusInternalServerError)
}

if deletedCount > 0 {
em.Publish(pubsub.EventGroupReduceEvent{
GroupID: grp.ID,
KeepCount: keepCount,
DeleteCount: deletedCount,
})
}

return webCtx.JSON(web.M{"deleted_count": deletedCount})
}

// RecoverableGroups 当前待恢复的报警组
func (g GroupController) RecoverableGroups(recoveryRepo repository.RecoveryRepo) ([]repository.Recovery, error) {
return recoveryRepo.RecoverableEvents(context.TODO(), time.Now().AddDate(1, 0, 0))
Expand Down
172 changes: 66 additions & 106 deletions dashboard/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"mutationobserver-shim": "^0.3.3",
"node-sass": "^4.12.0",
"node-sass": "^4.14.1",
"popper.js": "^1.15.0",
"portal-vue": "^2.1.4",
"sass-loader": "^7.1.0",
Expand Down
16 changes: 16 additions & 0 deletions dashboard/src/views/Groups.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
<b-dropdown-item :href="$store.getters.serverUrl + '/ui/groups/' + row.item.id + '.html'" target="_blank">事件组</b-dropdown-item>
<b-dropdown-item :href="$store.getters.serverUrl + '/ui/reports/' + row.item.id + '.html'" target="_blank">报告</b-dropdown-item>
</b-dropdown>
<b-button size="sm" variant="danger"
@click="clearGroup(row.item.id)"
v-if="row.item.status != 'collecting' && row.item.status != 'pending'">清理</b-button>
</b-button-group>
</template>
</b-table>
Expand Down Expand Up @@ -111,6 +114,19 @@
return this.userRefs[u] !== undefined ? this.userRefs[u] : '-';
}).join(', ')
},
clearGroup(id) {
this.$bvModal.msgBoxConfirm('确定执行该操作 ?').then((value) => {
if (value !== true) {
return;
}
axios.delete('/api/groups/' + id + '/reduce/').then((resp) => {
this.SuccessBox('操作成功,删除事件数为 ' + resp.data.deleted_count);
}).catch(error => {
this.ErrorBox(error);
});
});
},
formatAction(action) {
let actions = {
'dingding': '钉钉通知',
Expand Down
Loading

0 comments on commit 216f33b

Please sign in to comment.