Skip to content

Commit

Permalink
finish all api
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Oct 30, 2019
1 parent 86c5bb8 commit 1236380
Show file tree
Hide file tree
Showing 45 changed files with 13,527 additions and 149 deletions.
8 changes: 4 additions & 4 deletions api/controller/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ type GroupsResp struct {
Next int64 `json:"next"`
}



// Groups list all message groups
// Arguments:
// - offset/limit
// - status
// - rule_id
func (g GroupController) Groups(ctx hades.Context, groupRepo repository.MessageGroupRepo) (*GroupsResp, error) {
offset := ctx.Int64Input("offset", 0)
limit := ctx.Int64Input("limit", 10)

offset, limit := offsetAndLimit(ctx)
filter := bson.M{}

status := ctx.Input("status")
Expand Down Expand Up @@ -87,7 +87,7 @@ func (g GroupController) Group(
}

filter := messagesFilter(ctx)
filter["group_ids"] = bson.M{"$in": groupID}
filter["group_ids"] = groupID

messages, next, err := messageRepo.Paginate(filter, offset, limit)
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions api/controller/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (m *MessageController) Register(router *hades.Router) {
router.Get("/", m.Messages).Name("messages:all")
router.Get("/{id}/", m.Message).Name("messages:one")

router.Post("/common/", m.AddCommonMessage).Name("messages:add:common")
router.Post("/", m.AddCommonMessage).Name("messages:add:common")
router.Post("/logstash/", m.AddLogstashMessage).Name("messages:add:logstash")
router.Post("/grafana/", m.AddGrafanaMessage).Name("messages:add:grafana")
router.Post("/prometheus/api/v1/alerts", m.AddPrometheusMessage).Name("messages:add:prometheus") // url 地址末尾不包含 "/"
Expand Down Expand Up @@ -81,14 +81,13 @@ func (m *MessageController) Count(ctx hades.Context, msgRepo repository.MessageR

// MessagesResp is a response object for Messages API
type MessagesResp struct {
Messages []repository.Message
Next int64
Messages []repository.Message `json:"messages"`
Next int64 `json:"next"`
}

// Messages return all messages
func (m *MessageController) Messages(ctx hades.Context, msgRepo repository.MessageRepo) (*MessagesResp, error) {
offset := ctx.Int64Input("offset", 0)
limit := ctx.Int64Input("limit", 10)
offset, limit := offsetAndLimit(ctx)

filter := messagesFilter(ctx)
groupIDHex := ctx.Input("group_id")
Expand All @@ -98,7 +97,7 @@ func (m *MessageController) Messages(ctx hades.Context, msgRepo repository.Messa
return nil, hades.WrapJSONError(fmt.Errorf("invalid group_id: %w", err), http.StatusUnprocessableEntity)
}

filter["group_ids"] = bson.M{"$in": groupID}
filter["group_ids"] = groupID
}

messages, next, err := msgRepo.Paginate(filter, offset, limit)
Expand Down Expand Up @@ -142,7 +141,7 @@ func (m *MessageController) saveMessage(messageRepo repository.MessageRepo, repo
}

return ctx.JSON(hades.M{
"id": id.String(),
"id": id.Hex(),
})
}

Expand All @@ -167,7 +166,7 @@ func (msg CommonMessage) ToRepo() repository.Message {
func (m *MessageController) AddCommonMessage(ctx hades.Context, messageRepo repository.MessageRepo) hades.Response {
var commonMessage CommonMessage
if err := ctx.Unmarshal(&commonMessage); err != nil {
return ctx.JSONError("invalid request", http.StatusUnprocessableEntity)
return ctx.JSONError(fmt.Sprintf("invalid request: %v", err), http.StatusUnprocessableEntity)
}

return m.saveMessage(messageRepo, commonMessage, ctx)
Expand Down
3 changes: 1 addition & 2 deletions api/controller/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ type QueueJobsResp struct {
}

func (q *QueueController) Jobs(ctx hades.Context, repo repository.QueueRepo) (*QueueJobsResp, error) {
offset := ctx.Int64Input("offset", 0)
limit := ctx.Int64Input("limit", 10)
offset, limit := offsetAndLimit(ctx)

filter := bson.M{}

Expand Down
16 changes: 9 additions & 7 deletions api/controller/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ func (r RuleForm) Validate() error {
}

for i, tr := range r.Triggers {
_, err := matcher.NewTriggerMatcher(repository.Trigger{
PreCondition: tr.PreCondition,
})
if err != nil {
return fmt.Errorf("trigger #%d is invalid: %w", i, err)
if tr.PreCondition != "" {
_, err := matcher.NewTriggerMatcher(repository.Trigger{
PreCondition: tr.PreCondition,
})
if err != nil {
return fmt.Errorf("trigger #%d is invalid: %w", i, err)
}
}

for j, u := range tr.UserRefs {
Expand All @@ -112,7 +114,7 @@ func (r RuleForm) Validate() error {
}

// Add create a new rule
func (r RuleController) Add(ctx hades.WebContext, repo repository.RuleRepo, manager action.Manager) (*repository.Rule, error) {
func (r RuleController) Add(ctx hades.Context, repo repository.RuleRepo, manager action.Manager) (*repository.Rule, error) {
var ruleForm RuleForm
if err := ctx.Unmarshal(&ruleForm); err != nil {
return nil, hades.WrapJSONError(err, http.StatusUnprocessableEntity)
Expand Down Expand Up @@ -162,7 +164,7 @@ func (r RuleController) Add(ctx hades.WebContext, repo repository.RuleRepo, mana
return &rule, nil
}

// Update replace one rule for specified id
// UpdateID replace one rule for specified id
func (r RuleController) Update(ctx hades.Context, ruleRepo repository.RuleRepo) (*repository.Rule, error) {
id, err := primitive.ObjectIDFromHex(ctx.PathVar("id"))
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions api/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ func (u UserController) User(ctx hades.Context, userRepo repository.UserRepo) (*
}

func (u UserController) Users(ctx hades.Context, userRepo repository.UserRepo) hades.Response {
offset := ctx.Int64Input("offset", 0)
limit := ctx.Int64Input("limit", 10)
offset, limit := offsetAndLimit(ctx)

filter := bson.M{}

Expand Down
14 changes: 14 additions & 0 deletions api/controller/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,17 @@ type WelcomeMessage struct {
func (w *WelcomeController) Home(ctx hades.Context, req hades.Request) WelcomeMessage {
return WelcomeMessage{Version: w.cc.MustGet("version").(string)}
}

func offsetAndLimit(ctx hades.Context) (offset int64, limit int64) {
offset = ctx.Int64Input("offset", 0)
if offset < 0 {
offset = 0
}

limit = ctx.Int64Input("limit", 10)
if limit < 0 || limit > 1000 {
limit = 10
}

return
}
Loading

0 comments on commit 1236380

Please sign in to comment.