Skip to content

Commit

Permalink
事件清理 bugfix,增加事件删除,如果在事件组由 collectiong -> pendding 时,事件组中包含的事件数为 0,则该…
Browse files Browse the repository at this point in the history
…事件组将被标记为 已取消
  • Loading branch information
mylxsw committed Nov 23, 2020
1 parent 216f33b commit 76a01a9
Show file tree
Hide file tree
Showing 8 changed files with 41,136 additions and 41,074 deletions.
16 changes: 16 additions & 0 deletions api/controller/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (m *EventController) Register(router *web.Router) {
router.Group("/events", func(router *web.Router) {
router.Get("/", m.Events).Name("events:all")
router.Get("/{id}/", m.Event).Name("events:one")
router.Delete("/{id}/", m.DeleteEvent).Name("events:delete")

router.Post("/{id}/matched-rules/", m.TestMatchedRules).Name("events:matched-rules")
router.Post("/{id}/reproduce/", m.ReproduceEvent).Name("events:reproduce-event")

Expand Down Expand Up @@ -398,3 +400,17 @@ func (m *EventController) AddEventRelationNote(ctx web.Context, evtRelationNoteR
"id": id,
})
}

// DeleteEvent 删除事件
func (m *EventController) DeleteEvent(ctx web.Context, evtRepo repository.EventRepo) web.Response {
eventID, err := primitive.ObjectIDFromHex(ctx.PathVar("id"))
if err != nil {
return ctx.JSONError("invalid event id", http.StatusUnprocessableEntity)
}

if err := evtRepo.DeleteID(eventID); err != nil {
return ctx.JSONError(err.Error(), http.StatusInternalServerError)
}

return ctx.JSON(web.M{})
}
1 change: 1 addition & 0 deletions api/controller/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (g GroupController) CutGroupEvents(webCtx web.Context, evtGrpRepo repositor
GroupID: grp.ID,
KeepCount: keepCount,
DeleteCount: deletedCount,
CreatedAt: time.Now(),
})
}

Expand Down
10 changes: 10 additions & 0 deletions api/controller/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,16 @@ type RuleSearch struct {
// Rules return all rules
func (r RuleController) Rules(ctx web.Context, ruleRepo repository.RuleRepo, userRepo repository.UserRepo) (*RulesResp, error) {
filter := bson.M{}
idStr := ctx.Input("id")
if idStr != "" {
id, err := primitive.ObjectIDFromHex(idStr)
if err != nil {
return nil, web.WrapJSONError(err, http.StatusUnprocessableEntity)
}

filter["_id"] = id
}

name := ctx.Input("name")
if name != "" {
filter["name"] = bson.M{"$regex": name}
Expand Down
82,139 changes: 41,075 additions & 41,064 deletions api/static.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dashboard/src/components/EventCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</b-btn-group>

<b-button class="ml-2" :to="{path:'/rules/add', query: {test_event_id: event.id}}" target="_blank" size="sm" variant="dark">创建规则</b-button>
<b-button class="ml-2" size="sm" variant="danger" v-if="deleteEvent" @click="deleteEvent(event_index, event.id)">删除</b-button>

<b-btn-group class="ml-2" v-if="event.relation_ids != null && event.relation_ids.length > 0">
<b-button size="sm" variant="primary" @click="eventNote(event.id)" v-if="eventNote">备注</b-button>
Expand Down Expand Up @@ -83,6 +84,7 @@
testMatchedRules: Function,
reproduceEvent: Function,
eventNote: Function,
deleteEvent: Function,
onlyShow: Boolean,
title: String,
fold: Boolean,
Expand Down
17 changes: 16 additions & 1 deletion dashboard/src/views/Events.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

<EventCard v-for="(message, index) in events" :key="index" class="mb-3"
:event="message"
:event_index="index" :reproduce-event="reproduceEvent" :eventNote="openEventNoteDialog"
:event_index="index" :reproduce-event="reproduceEvent" :eventNote="openEventNoteDialog" :deleteEvent="deleteEvent"
:test-matched-rules="testMatchedRules"></EventCard>
<b-card v-if="events.length === 0">
<b-card-body>There are no records to show</b-card-body>
Expand Down Expand Up @@ -172,6 +172,21 @@
});
});
},
deleteEvent(index, id) {
let self = this;
this.$bvModal.msgBoxConfirm('确定执行该操作 ?').then((value) => {
if (value !== true) {
return;
}
axios.delete('/api/events/' + id + '/', {}).then(() => {
self.events.splice(index, 1);
this.ToastSuccess('事件删除成功');
}).catch(error => {
this.ErrorBox(error);
});
});
},
testMatchedRules(id) {
axios.post('/api/events/' + id + '/matched-rules/', {}).then(resp => {
this.matched_rules = resp.data;
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/views/Groups.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</template>
<template v-slot:cell(rule_name)="row">
<span v-b-tooltip.hover :title="row.item.rule.rule">{{ row.item.rule.name }}</span>
<b-link :to="'/rules/' + row.item.rule.id + '/edit'" target="_blank" class="ml-2">
<b-link :to="'/rules/?id=' + row.item.rule.id" target="_blank" class="ml-2">
<font-awesome-icon icon="external-link-alt"></font-awesome-icon>
</b-link>
<p>
Expand Down
23 changes: 15 additions & 8 deletions internal/job/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,27 @@ func initializeMatchers(ruleRepo repository.RuleRepo) ([]*matcher.EventMatcher,
return matchers, nil
}

func (a *AggregationJob) pendingEventGroup(groupRepo repository.EventGroupRepo, msgRepo repository.EventRepo, em event.Manager) error {
func (a *AggregationJob) pendingEventGroup(groupRepo repository.EventGroupRepo, evtRepo repository.EventRepo, em event.Manager) error {
return groupRepo.Traverse(bson.M{"status": repository.EventGroupStatusCollecting}, func(grp repository.EventGroup) error {
if !grp.Ready() {
return nil
}

msgCount, err := msgRepo.Count(bson.M{"group_ids": grp.ID})
evtCount, err := evtRepo.Count(bson.M{"group_ids": grp.ID})
if err != nil {
log.WithFields(log.Fields{
"grp": grp,
"err": err,
}).Errorf("query message count failed: %v", err)
}

grp.MessageCount = msgCount
grp.Status = repository.EventGroupStatusPending
if evtCount == 0 {
grp.Status = repository.EventGroupStatusCanceled
} else {
grp.Status = repository.EventGroupStatusPending
}

grp.MessageCount = evtCount

if log.DebugEnabled() {
log.WithFields(log.Fields{
Expand All @@ -197,10 +202,12 @@ func (a *AggregationJob) pendingEventGroup(groupRepo repository.EventGroupRepo,

err = groupRepo.UpdateID(grp.ID, grp)

em.Publish(pubsub.MessageGroupPendingEvent{
Group: grp,
CreatedAt: time.Now(),
})
if evtCount > 0 {
em.Publish(pubsub.MessageGroupPendingEvent{
Group: grp,
CreatedAt: time.Now(),
})
}

return err
})
Expand Down

0 comments on commit 76a01a9

Please sign in to comment.