Пример #1
0
// editRule edits a rule
func editRule(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// id
	id, err := strconv.Atoi(ps.ByName("id"))
	if err != nil {
		ResponseError(w, ErrProjectID)
		return
	}

	// Request
	req := &createRuleRequest{}
	if err := RequestBind(r, req); err != nil {
		ResponseError(w, ErrBadRequest)
		return
	}
	// Validate
	if err := models.ValidateRulePattern(req.Pattern); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateRuleLevel(req.Level); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if !req.TrendUp && !req.TrendDown && req.ThresholdMax == 0 && req.ThresholdMin == 0 {
		ResponseError(w, ErrRuleNoCondition)
		return
	}

	rule := &models.Rule{}
	if db.Admin.DB().Where("id = ?", id).First(&rule).Error != nil {
		ResponseError(w, ErrRuleNotFound)
		return
	}

	rule.Comment = req.Comment
	rule.Level = req.Level
	rule.Pattern = req.Pattern
	rule.TrendUp = req.TrendUp
	rule.TrendDown = req.TrendDown
	rule.ThresholdMax = req.ThresholdMax
	rule.ThresholdMin = req.ThresholdMin
	rule.Disabled = req.Disabled

	if db.Admin.DB().Save(rule).Error != nil {
		ResponseError(w, ErrRuleUpdateFailed)
		return
	}
	// Cache
	db.Admin.RulesCache.Delete(id)
	db.Admin.RulesCache.Put(rule)
	rule.SetNumMetrics(len(db.Index.Filter(rule.Pattern)))
	ResponseJSONOK(w, rule)
}
Пример #2
0
func getEventsByProjectID(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Options
	n, err := strconv.Atoi(r.URL.Query().Get("past"))
	if err != nil {
		n = 3600 * 24 // 1 day
	}
	past := uint32(n)
	if past > cfg.Expiration {
		ResponseError(w, ErrEventPast)
		return
	}
	level, err := strconv.Atoi(r.URL.Query().Get("level"))
	if err != nil {
		level = 0 // low
	}
	if err := models.ValidateRuleLevel(level); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	// Params
	id, err := strconv.Atoi(ps.ByName("id"))
	if err != nil {
		ResponseError(w, ErrProjectID)
		return
	}
	end := uint32(time.Now().Unix())
	start := end - past
	ews, err := db.Event.GetByProjectID(id, level, start, end)
	if err != nil {
		ResponseError(w, NewUnexceptedWebError(err))
		return
	}
	if ews == nil {
		ews = make([]eventdb.EventWrapper, 0) // Note: Avoid js null
	}
	// Reverse
	for i, j := 0, len(ews)-1; i < j; i, j = i+1, j-1 {
		ews[i], ews[j] = ews[j], ews[i]
	}
	ResponseJSONOK(w, ews)
}
Пример #3
0
// createRule creates a rule.
func createRule(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Params
	projectID, err := strconv.Atoi(ps.ByName("id"))
	if err != nil {
		ResponseError(w, ErrProjectID)
		return
	}
	// Request
	req := &createRuleRequest{
		Level:    models.RuleLevelLow,
		Disabled: false,
	}
	if err := RequestBind(r, req); err != nil {
		ResponseError(w, ErrBadRequest)
		return
	}
	// Validate
	if err := models.ValidateRulePattern(req.Pattern); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if projectID <= 0 {
		// ProjectID is invalid.
		ResponseError(w, ErrProjectID)
		return
	}
	if !req.TrendUp && !req.TrendDown && req.ThresholdMax == 0 && req.ThresholdMin == 0 {
		ResponseError(w, ErrRuleNoCondition)
		return
	}
	if err := models.ValidateRuleLevel(req.Level); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	// Find project.
	proj := &models.Project{}
	if err := db.Admin.DB().First(proj, projectID).Error; err != nil {
		switch err {
		case gorm.RecordNotFound:
			ResponseError(w, ErrProjectNotFound)
			return
		default:
			ResponseError(w, NewUnexceptedWebError(err))
			return
		}
	}
	// Create rule.
	rule := &models.Rule{
		ProjectID:    projectID,
		Pattern:      req.Pattern,
		TrendUp:      req.TrendUp,
		TrendDown:    req.TrendDown,
		ThresholdMax: req.ThresholdMax,
		ThresholdMin: req.ThresholdMin,
		Comment:      req.Comment,
		Level:        req.Level,
		Disabled:     req.Disabled,
	}
	if err := db.Admin.DB().Create(rule).Error; err != nil {
		// Write errors.
		sqliteErr, ok := err.(sqlite3.Error)
		if ok {
			switch sqliteErr.ExtendedCode {
			case sqlite3.ErrConstraintNotNull:
				ResponseError(w, ErrNotNull)
				return
			case sqlite3.ErrConstraintPrimaryKey:
				ResponseError(w, ErrPrimaryKey)
				return
			case sqlite3.ErrConstraintUnique:
				ResponseError(w, ErrDuplicateRulePattern)
				return
			}
		}
		// Unexcepted error.
		ResponseError(w, NewUnexceptedWebError(err))
		return
	}
	// Cache
	db.Admin.RulesCache.Put(rule)
	// Response
	rule.SetNumMetrics(len(db.Index.Filter(rule.Pattern)))
	ResponseJSONOK(w, rule)
}
Пример #4
0
// createUser creats a user.
func createUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Request
	req := &createUserRequest{
		EnableEmail: true,
		EnablePhone: true,
		Universal:   false,
		RuleLevel:   models.RuleLevelLow,
	}
	if err := RequestBind(r, req); err != nil {
		ResponseError(w, ErrBadRequest)
		return
	}
	// Validation
	if err := models.ValidateUserName(req.Name); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateUserEmail(req.Email); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateUserPhone(req.Phone); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateRuleLevel(req.RuleLevel); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	// Save
	user := &models.User{
		Name:        req.Name,
		Email:       req.Email,
		EnableEmail: req.EnableEmail,
		Phone:       req.Phone,
		EnablePhone: req.EnablePhone,
		Universal:   req.Universal,
		RuleLevel:   req.RuleLevel,
	}
	if err := db.Admin.DB().Create(user).Error; err != nil {
		// Write errors.
		sqliteErr, ok := err.(sqlite3.Error)
		if ok {
			switch sqliteErr.ExtendedCode {
			case sqlite3.ErrConstraintNotNull:
				ResponseError(w, ErrNotNull)
				return
			case sqlite3.ErrConstraintPrimaryKey:
				ResponseError(w, ErrPrimaryKey)
				return
			case sqlite3.ErrConstraintUnique:
				ResponseError(w, ErrDuplicateUserName)
				return
			}
		}
		// Unexcepted.
		ResponseError(w, NewUnexceptedWebError(err))
		return
	}
	ResponseJSONOK(w, user)
}
Пример #5
0
// updateUser updates a user.
func updateUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// Params
	id, err := strconv.Atoi(ps.ByName("id"))
	if err != nil {
		ResponseError(w, ErrUserID)
		return
	}
	// Request
	req := &updateUserRequest{}
	if err := RequestBind(r, req); err != nil {
		ResponseError(w, ErrBadRequest)
		return
	}
	// Validation
	if err := models.ValidateUserName(req.Name); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateUserEmail(req.Email); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateUserPhone(req.Phone); err != nil {
		ResponseError(w, NewValidationWebError(err))
		return
	}
	if err := models.ValidateRuleLevel(req.RuleLevel); err != nil {
		ResponseError(w, NewValidationWebError(err))
	}
	// Find
	user := &models.User{}
	if err := db.Admin.DB().First(user, id).Error; err != nil {
		switch err {
		case gorm.RecordNotFound:
			ResponseError(w, ErrUserNotFound)
			return
		default:
			ResponseError(w, NewUnexceptedWebError(err))
			return
		}
	}
	// Patch
	user.Name = req.Name
	user.Email = req.Email
	user.EnableEmail = req.EnableEmail
	user.Phone = req.Phone
	user.EnablePhone = req.EnablePhone
	user.Universal = req.Universal
	user.RuleLevel = req.RuleLevel
	if err := db.Admin.DB().Save(user).Error; err != nil {
		if err == gorm.RecordNotFound {
			// User not found.
			ResponseError(w, ErrUserNotFound)
			return
		}
		// Write errors.
		sqliteErr, ok := err.(sqlite3.Error)
		if ok {
			switch sqliteErr.ExtendedCode {
			case sqlite3.ErrConstraintNotNull:
				ResponseError(w, ErrNotNull)
				return
			case sqlite3.ErrConstraintUnique:
				ResponseError(w, ErrDuplicateUserName)
				return
			}
		}
		// Unexcepted error.
		ResponseError(w, NewUnexceptedWebError(err))
		return
	}
	ResponseJSONOK(w, user)
}