mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-12 20:29:04 +08:00
chore: update rule create response (#4090)
This commit is contained in:
parent
8eb2b9e3d0
commit
5e0b6366cc
@ -1249,13 +1249,13 @@ func (aH *APIHandler) createRule(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = aH.ruleManager.CreateRule(r.Context(), string(body))
|
rule, err := aH.ruleManager.CreateRule(r.Context(), string(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RespondError(w, &model.ApiError{Typ: model.ErrorBadData, Err: err}, nil)
|
RespondError(w, &model.ApiError{Typ: model.ErrorBadData, Err: err}, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
aH.Respond(w, "rule successfully added")
|
aH.Respond(w, rule)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
// Data store to capture user alert rule settings
|
// Data store to capture user alert rule settings
|
||||||
type RuleDB interface {
|
type RuleDB interface {
|
||||||
// CreateRuleTx stores rule in the db and returns tx and group name (on success)
|
// CreateRuleTx stores rule in the db and returns tx and group name (on success)
|
||||||
CreateRuleTx(ctx context.Context, rule string) (string, Tx, error)
|
CreateRuleTx(ctx context.Context, rule string) (int64, Tx, error)
|
||||||
|
|
||||||
// EditRuleTx updates the given rule in the db and returns tx and group name (on success)
|
// EditRuleTx updates the given rule in the db and returns tx and group name (on success)
|
||||||
EditRuleTx(ctx context.Context, rule string, id string) (string, Tx, error)
|
EditRuleTx(ctx context.Context, rule string, id string) (string, Tx, error)
|
||||||
@ -57,9 +57,7 @@ func newRuleDB(db *sqlx.DB) RuleDB {
|
|||||||
|
|
||||||
// CreateRuleTx stores a given rule in db and returns task name,
|
// CreateRuleTx stores a given rule in db and returns task name,
|
||||||
// sql tx and error (if any)
|
// sql tx and error (if any)
|
||||||
func (r *ruleDB) CreateRuleTx(ctx context.Context, rule string) (string, Tx, error) {
|
func (r *ruleDB) CreateRuleTx(ctx context.Context, rule string) (int64, Tx, error) {
|
||||||
|
|
||||||
var groupName string
|
|
||||||
var lastInsertId int64
|
var lastInsertId int64
|
||||||
|
|
||||||
var userEmail string
|
var userEmail string
|
||||||
@ -70,14 +68,14 @@ func (r *ruleDB) CreateRuleTx(ctx context.Context, rule string) (string, Tx, err
|
|||||||
updatedAt := time.Now()
|
updatedAt := time.Now()
|
||||||
tx, err := r.Begin()
|
tx, err := r.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return groupName, nil, err
|
return lastInsertId, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt, err := tx.Prepare(`INSERT into rules (created_at, created_by, updated_at, updated_by, data) VALUES($1,$2,$3,$4,$5);`)
|
stmt, err := tx.Prepare(`INSERT into rules (created_at, created_by, updated_at, updated_by, data) VALUES($1,$2,$3,$4,$5);`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zap.S().Errorf("Error in preparing statement for INSERT to rules\n", err)
|
zap.S().Errorf("Error in preparing statement for INSERT to rules\n", err)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return groupName, nil, err
|
return lastInsertId, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
@ -86,15 +84,17 @@ func (r *ruleDB) CreateRuleTx(ctx context.Context, rule string) (string, Tx, err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
zap.S().Errorf("Error in Executing prepared statement for INSERT to rules\n", err)
|
zap.S().Errorf("Error in Executing prepared statement for INSERT to rules\n", err)
|
||||||
tx.Rollback() // return an error too, we may want to wrap them
|
tx.Rollback() // return an error too, we may want to wrap them
|
||||||
return groupName, nil, err
|
return lastInsertId, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lastInsertId, _ = result.LastInsertId()
|
lastInsertId, err = result.LastInsertId()
|
||||||
|
if err != nil {
|
||||||
groupName = prepareTaskName(lastInsertId)
|
zap.S().Errorf("Error in getting last insert id for INSERT to rules\n", err)
|
||||||
|
tx.Rollback() // return an error too, we may want to wrap them
|
||||||
return groupName, tx, nil
|
return lastInsertId, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastInsertId, tx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EditRuleTx stores a given rule string in database and returns
|
// EditRuleTx stores a given rule string in database and returns
|
||||||
|
@ -366,34 +366,35 @@ func (m *Manager) deleteTask(taskName string) {
|
|||||||
|
|
||||||
// CreateRule stores rule def into db and also
|
// CreateRule stores rule def into db and also
|
||||||
// starts an executor for the rule
|
// starts an executor for the rule
|
||||||
func (m *Manager) CreateRule(ctx context.Context, ruleStr string) error {
|
func (m *Manager) CreateRule(ctx context.Context, ruleStr string) (*GettableRule, error) {
|
||||||
parsedRule, errs := ParsePostableRule([]byte(ruleStr))
|
parsedRule, errs := ParsePostableRule([]byte(ruleStr))
|
||||||
|
|
||||||
// check if the rule uses any feature that is not enabled
|
// check if the rule uses any feature that is not enabled
|
||||||
err := m.checkFeatureUsage(parsedRule)
|
err := m.checkFeatureUsage(parsedRule)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
zap.S().Errorf("failed to parse rules:", errs)
|
zap.S().Errorf("failed to parse rules:", errs)
|
||||||
// just one rule is being parsed so expect just one error
|
// just one rule is being parsed so expect just one error
|
||||||
return errs[0]
|
return nil, errs[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
taskName, tx, err := m.ruleDB.CreateRuleTx(ctx, ruleStr)
|
lastInsertId, tx, err := m.ruleDB.CreateRuleTx(ctx, ruleStr)
|
||||||
|
taskName := prepareTaskName(lastInsertId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !m.opts.DisableRules {
|
if !m.opts.DisableRules {
|
||||||
if err := m.addTask(parsedRule, taskName); err != nil {
|
if err := m.addTask(parsedRule, taskName); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = tx.Commit()
|
err = tx.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// update feature usage
|
// update feature usage
|
||||||
@ -401,7 +402,11 @@ func (m *Manager) CreateRule(ctx context.Context, ruleStr string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
zap.S().Errorf("error updating feature usage: %v", err)
|
zap.S().Errorf("error updating feature usage: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
gettableRule := &GettableRule{
|
||||||
|
Id: fmt.Sprintf("%d", lastInsertId),
|
||||||
|
PostableRule: *parsedRule,
|
||||||
|
}
|
||||||
|
return gettableRule, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) updateFeatureUsage(parsedRule *PostableRule, usage int64) error {
|
func (m *Manager) updateFeatureUsage(parsedRule *PostableRule, usage int64) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user