feat: Update notification trigger patterns and add alert trigger

This commit is contained in:
Marvin Zhang
2024-08-03 19:43:11 +08:00
parent dd6921d32b
commit a7da3a0442
5 changed files with 55 additions and 10 deletions

View File

@@ -326,6 +326,11 @@ func InitRoutes(app *gin.Engine) (err error) {
Path: "/:id", Path: "/:id",
HandlerFunc: GetSetting, HandlerFunc: GetSetting,
}, },
{
Method: http.MethodPost,
Path: "/:id",
HandlerFunc: PostSetting,
},
{ {
Method: http.MethodPut, Method: http.MethodPut,
Path: "/:id", Path: "/:id",

View File

@@ -1,11 +1,12 @@
package controllers package controllers
import ( import (
"github.com/crawlab-team/crawlab/core/models/models" "errors"
models2 "github.com/crawlab-team/crawlab/core/models/models/v2" "github.com/crawlab-team/crawlab/core/models/models/v2"
"github.com/crawlab-team/crawlab/core/models/service" "github.com/crawlab-team/crawlab/core/models/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
) )
func GetSetting(c *gin.Context) { func GetSetting(c *gin.Context) {
@@ -13,11 +14,45 @@ func GetSetting(c *gin.Context) {
key := c.Param("id") key := c.Param("id")
// setting // setting
s, err := service.NewModelServiceV2[models2.SettingV2]().GetOne(bson.M{"key": key}, nil) s, err := service.NewModelServiceV2[models.SettingV2]().GetOne(bson.M{"key": key}, nil)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
HandleSuccess(c)
return
}
HandleErrorInternalServerError(c, err)
return
}
HandleSuccessWithData(c, s)
}
func PostSetting(c *gin.Context) {
// key
key := c.Param("id")
// settings
var s models.SettingV2
if err := c.ShouldBindJSON(&s); err != nil {
HandleErrorInternalServerError(c, err)
return
}
if s.Key == "" {
s.Key = key
}
u := GetUserFromContextV2(c)
s.SetCreated(u.Id)
s.SetUpdated(u.Id)
id, err := service.NewModelServiceV2[models.SettingV2]().InsertOne(s)
if err != nil { if err != nil {
HandleErrorInternalServerError(c, err) HandleErrorInternalServerError(c, err)
return return
} }
s.Id = id
HandleSuccessWithData(c, s) HandleSuccessWithData(c, s)
} }
@@ -27,13 +62,13 @@ func PutSetting(c *gin.Context) {
key := c.Param("id") key := c.Param("id")
// settings // settings
var s models.Setting var s models.SettingV2
if err := c.ShouldBindJSON(&s); err != nil { if err := c.ShouldBindJSON(&s); err != nil {
HandleErrorInternalServerError(c, err) HandleErrorInternalServerError(c, err)
return return
} }
modelSvc := service.NewModelServiceV2[models2.SettingV2]() modelSvc := service.NewModelServiceV2[models.SettingV2]()
// setting // setting
_s, err := modelSvc.GetOne(bson.M{"key": key}, nil) _s, err := modelSvc.GetOne(bson.M{"key": key}, nil)

View File

@@ -68,7 +68,7 @@ func CreateIndexesV2() {
// settings // settings
mongo.GetMongoCol(service.GetCollectionNameByInstance(models2.SettingV2{})).MustCreateIndexes([]mongo2.IndexModel{ mongo.GetMongoCol(service.GetCollectionNameByInstance(models2.SettingV2{})).MustCreateIndexes([]mongo2.IndexModel{
{Keys: bson.M{"key": 1}}, {Keys: bson.D{{"key", 1}}, Options: options.Index().SetUnique(true)},
}) })
// tokens // tokens

View File

@@ -25,7 +25,7 @@ func (svc *Service) Init() (err error) {
func (svc *Service) initData() (err error) { func (svc *Service) initData() (err error) {
total, err := svc.col.Count(bson.M{ total, err := svc.col.Count(bson.M{
"key": "site_title", "key": "customize",
}) })
if err != nil { if err != nil {
return err return err
@@ -38,10 +38,13 @@ func (svc *Service) initData() (err error) {
settings := []models.Setting{ settings := []models.Setting{
{ {
Id: primitive.NewObjectID(), Id: primitive.NewObjectID(),
Key: "site_title", Key: "customize",
Value: bson.M{ Value: bson.M{
"customize_site_title": false, "show_custom_title": false,
"site_title": "", "custom_title": "",
"show_custom_logo": false,
"custom_logo": "",
"hide_platform_version": false,
}, },
}, },
} }

View File

@@ -844,6 +844,7 @@ github.com/golang/glog v1.2.1/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwm
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo= github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
@@ -945,6 +946,7 @@ github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LF
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc= github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo= github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7 h1:K//n/AqR5HjG3qxbrBCL4vJPW0MVFSs9CPK1OOJdRME= github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7 h1:K//n/AqR5HjG3qxbrBCL4vJPW0MVFSs9CPK1OOJdRME=