models

package
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2017 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package models implements metric, index, rule, project, user modeling.

Index

Constants

View Source
const (
	RuleLevelLow = iota
	RuleLevelMiddle
	RuleLevelHigh
)

Rule Levels

View Source
const (
	// Max value of the project name length.
	MaxProjectNameLen = 64
	// Max value of the user name length.
	MaxUserNameLen = 32
	// Max value of the rule pattern length.
	MaxRulePatternLen = 256
	// Max value of the metric name length.
	MaxMetricNameLen = 256
	// Min value of the metric stamp.
	MinMetricStamp uint32 = 1450322633
)

Limitations

Variables

View Source
var (
	ErrProjectNameEmpty         = errors.New("project name is empty")
	ErrProjectNameTooLong       = errors.New("project name is too long")
	ErrProjectSilentTimeStart   = errors.New("project silent time start is invalid")
	ErrProjectSilentTimeEnd     = errors.New("project silent time end is invalid")
	ErrProjectSilentTimeRange   = errors.New("project silent time start should be smaller than end")
	ErrUserNameEmpty            = errors.New("user name is empty")
	ErrUserNameTooLong          = errors.New("user name is too long")
	ErrUserEmailEmpty           = errors.New("user email is empty")
	ErrUserEmailFormat          = errors.New("user email format is invalid")
	ErrUserPhoneLen             = errors.New("user phone length should be 10 or 11")
	ErrUserPhoneFormat          = errors.New("user phone should contains 10 or 11 numbers")
	ErrRulePatternEmpty         = errors.New("rule pattern is empty")
	ErrRulePatternTooLong       = errors.New("rule pattern is too long")
	ErrRulePatternContainsSpace = errors.New("rule pattern contains spaces")
	ErrRulePatternFormat        = errors.New("rule pattern format is invalid")
	ErrRuleLevel                = errors.New("rule level is invalid")
	ErrWebHookURLEmpty          = errors.New("webhook url is empty")
	ErrWebHookURLFormat         = errors.New("webhook url format is invalid")
	ErrMetricNameEmpty          = errors.New("metric name is empty")
	ErrMetricNameTooLong        = errors.New("metric name is too long")
	ErrMetricStampTooSmall      = errors.New("metric stamp is too small")
)

Errors

Functions

func ValidateMetricName added in v0.1.2

func ValidateMetricName(name string) error

ValidateMetricName validates metric name.

func ValidateMetricStamp added in v0.1.2

func ValidateMetricStamp(stamp uint32) error

ValidateMetricStamp validates metric stamp.

func ValidateProjectName added in v0.0.7

func ValidateProjectName(name string) error

ValidateProjectName validates project name

func ValidateProjectSilentRange added in v0.1.0

func ValidateProjectSilentRange(start, end int) error

ValidateProjectSilentRange validates project silent time start and end.

func ValidateRuleLevel added in v0.1.5

func ValidateRuleLevel(level int) error

ValidateRuleLevel validates rule level.

func ValidateRulePattern added in v0.0.7

func ValidateRulePattern(pattern string) error

ValidateRulePattern validates rule pattern.

func ValidateTeamName added in v0.2.5

func ValidateTeamName(name string) error

ValidateTeamName validates project name

func ValidateUserEmail added in v0.0.7

func ValidateUserEmail(email string) error

ValidateUserEmail validates user email.

func ValidateUserName added in v0.0.7

func ValidateUserName(name string) error

ValidateUserName validates user name.

func ValidateUserPhone added in v0.0.7

func ValidateUserPhone(phone string) error

ValidateUserPhone validates user phone.

func ValidateWebHookURL added in v0.2.5

func ValidateWebHookURL(url string) error

ValidateWebHookURL validates webhook url.

Types

type BulkMetric added in v0.2.5

type BulkMetric struct {
	Err   error
	Ms    []*Metric
	Start uint32
	Stop  uint32
}

BulkMetric is a data container for time series data points among a few days.

type ByStamp added in v0.2.5

type ByStamp []BulkMetric

ByStamp sort BulkMetric by start stamp

func (ByStamp) Len added in v0.2.5

func (s ByStamp) Len() int

func (ByStamp) Less added in v0.2.5

func (s ByStamp) Less(i, j int) bool

func (ByStamp) Swap added in v0.2.5

func (s ByStamp) Swap(i, j int)

type Event added in v0.1.8

type Event struct {
	ID     string  `json:"id"`
	Index  *Index  `json:"index"`
	Metric *Metric `json:"metric"`
	Rule   *Rule   `json:"rule"`
}

Event is the alert event.

func NewEvent added in v0.1.8

func NewEvent(m *Metric, idx *Index, rule *Rule) *Event

NewEvent returns a new event from metric and index.

func (*Event) TranslateRuleComment added in v0.1.8

func (ev *Event) TranslateRuleComment() string

TranslateRuleComment translates rule comment variables with metric name and rule pattern.

m := &Metric{Name: "timer.count_ps.foo"}
r := &Rule{Pattern: "timer.count_ps.*", Comment: "$1 timing"}
ev := &Event{Metric:m, Rule:r}
ev.TranslateRuleComment()  // => "foo timing"

type EventWrapper added in v0.2.2

type EventWrapper struct {
	*Event
	Team                  *Team    `json:"team"`
	Project               *Project `json:"project"`
	User                  *User    `json:"user,omitempty"`
	RuleTranslatedComment string   `json:"ruleTranslatedComment"`
}

EventWrapper is a wrapper of Event for tmp usage.

func NewWrapperOfEvent added in v0.2.2

func NewWrapperOfEvent(ev *Event) *EventWrapper

NewWrapperOfEvent creates an event wrapper from given event.

type Index

type Index struct {

	// Metric name
	Name string `json:"name"`
	// Latest stamp for the metric.
	Stamp uint32 `json:"stamp"`
	// Latest trending score for the metric.
	Score float64 `json:"score"`
	// Latest average for the metric.
	Average float64 `json:"average"`
	// Link between index and metric.
	Link uint32 `json:"link"`
	// Matched rules.
	MatchedRules []*Rule `json:"matchedRules,omitempty"`
	// contains filtered or unexported fields
}

Index is for metric indexing, it records metric name for faster metric indexing. And metric latest informations like timstamp, score, average are also indexed.

func (*Index) Copy

func (idx *Index) Copy() *Index

Copy the index.

func (*Index) CopyTo

func (idx *Index) CopyTo(i *Index)

CopyTo copy the index to another.

func (*Index) Equal

func (idx *Index) Equal(i *Index) bool

Equal tests the equality.

func (*Index) Lock

func (c *Index) Lock()

Lock locks instance for writing.

func (*Index) RLock

func (c *Index) RLock()

RLock locks instance for reading.

func (*Index) RUnlock

func (c *Index) RUnlock()

RUnlock unlocks instance reading.

func (*Index) Share

func (c *Index) Share()

Share the instance between goroutines.

func (*Index) Unlock

func (c *Index) Unlock()

Unlock unlocks instance writing.

func (*Index) WriteMetric

func (idx *Index) WriteMetric(m *Metric)

WriteMetric writes metric to index.

type Metric

type Metric struct {
	// Name
	Name string `json:"name"`
	// Metric unix time stamp
	Stamp uint32 `json:"stamp"`
	// Metric value
	Value float64 `json:"value"`
	// Anomaly score
	Score float64 `json:"score"`
	// Average old
	Average float64 `json:"average"`
	// Link between index and metric.
	Link uint32 `json:"link"`
}

Metric is a data container for time series datapoint.

func (*Metric) LinkTo added in v0.1.9

func (m *Metric) LinkTo(idx *Index)

LinkTo links the metric to an index.

func (*Metric) Validate added in v0.2.2

func (m *Metric) Validate() (err error)

Validate the metric.

type Project

type Project struct {
	// ID in db.
	ID int `json:"id"`
	// Name
	Name string `sql:"not null;unique" json:"name"`
	// Project may have many rules, they shouldn't be shared.
	Rules []*Rule `json:"-"`
	// Project may have many users, they shouldn't be shared.
	Users []*User `gorm:"many2many:project_users" json:"-"`
	// Project may have many webhooks, they shouldn't be shared.
	WebHooks []*WebHook `gorm:"many2many:project_webhooks" json:"-"`
	// Silent time range in one day.
	EnableSilent    bool `json:"enableSilent"`
	SilentTimeStart int  `json:"silentTimeStart"`
	SilentTimeEnd   int  `json:"silentTimeEnd"`
	// Team belongs to
	TeamID int `sql:"index;not null;DEFAULT:1" json:"teamID"`
}

Project is a rules group.

type Rule

type Rule struct {

	// ID in db.
	ID int `gorm:"primary_key" json:"id"`
	// Project belongs to
	ProjectID int `sql:"index;not null" json:"projectID"`
	// Pattern is a wildcard string
	Pattern string `sql:"size:400;not null" json:"pattern"`
	// Trend
	TrendUp   bool `json:"trendUp"`
	TrendDown bool `json:"trendDown"`
	// Optional thresholds data, only used if the rule condition is about
	// threshold. Although we really don't need any thresholds for trending
	// analyzation and alertings, but we still provide a way to alert by
	// thresholds.
	ThresholdMax float64 `json:"thresholdMax"`
	ThresholdMin float64 `json:"thresholdMin"`
	// Number of metrics matched.
	NumMetrics int `sql:"-" json:"numMetrics"`
	// Comment
	Comment string `sql:"type:varchar(256)" json:"comment"`
	// Level
	Level int `json:"level"`
	// Disabled
	Disabled bool `sql:"default:false" json:"disabled"`
	// DisabledFor
	DisabledFor int `json:"disabledFor"`
	// DisabledAt
	DisabledAt time.Time `json:"disabledAt"`
	// TrackIdle
	TrackIdle bool `json:"trackIdle"`
	// FillZero
	NeverFillZero bool `sql:"default:false" json:"neverFillZero"`
	// contains filtered or unexported fields
}

Rule is a type to describe alerter rule.

func (*Rule) Copy

func (rule *Rule) Copy() *Rule

Copy the rule.

func (*Rule) CopyTo

func (rule *Rule) CopyTo(r *Rule)

CopyTo copy the rule to another.

func (*Rule) Equal

func (rule *Rule) Equal(r *Rule) bool

Equal tests rule equality

func (*Rule) IsTrendRelated added in v0.2.2

func (rule *Rule) IsTrendRelated() bool

IsTrendRelated returns true if any trend options is set.

func (*Rule) Lock

func (c *Rule) Lock()

Lock locks instance for writing.

func (*Rule) RLock

func (c *Rule) RLock()

RLock locks instance for reading.

func (*Rule) RUnlock

func (c *Rule) RUnlock()

RUnlock unlocks instance reading.

func (*Rule) SetNumMetrics added in v0.0.7

func (rule *Rule) SetNumMetrics(n int)

SetNumMetrics sets the rule's number of metrics matched.

func (*Rule) Share

func (c *Rule) Share()

Share the instance between goroutines.

func (*Rule) Test

func (rule *Rule) Test(m *Metric, idx *Index, cfg *config.Config) bool

Test if a metric hits this rule.

  1. For trend related conditions, index.Score will be used.
  2. For value related conditions, metric.Value will be used.

func (*Rule) Unlock

func (c *Rule) Unlock()

Unlock unlocks instance writing.

type Team added in v0.2.5

type Team struct {
	// ID in db.
	ID int `json:"id"`
	// Name
	Name string `sql:"not null;unique" json:"name"`
	// Project may have many rules, they shouldn't be shared.
	Project []*Project `json:"-"`
}

Team is a projects group.

type User

type User struct {
	// ID in db.
	ID int `gorm:"primary_key" json:"id"`
	// Name
	Name string `sql:"index;not null;unique" json:"name"`
	// Email
	Email       string `json:"email"`
	EnableEmail bool   `json:"enableEmail"`
	// Phone
	Phone       string `json:"phone"`
	EnablePhone bool   `json:"enablePhone"`
	// Universal
	Universal bool `sql:"index" json:"universal"`
	// Users can subscribe many projects.
	Projects []*Project `gorm:"many2many:project_users" json:"-"`
	// Rule level to receive
	RuleLevel int `json:"ruleLevel"`
}

User is the alerter message receiver.

type WebHook added in v0.2.5

type WebHook struct {
	// ID in db.
	ID int `gorm:"primary_key" json:"id"`
	// Name
	Name string `sql:"index;not null;unique" json:"name"`
	//Type
	Type string `json:"type"`
	// Email
	URL string `json:"url"`
	// Universal
	Universal bool `sql:"index;DEFAULT:false" json:"universal"`
	// Rule level to receive
	RuleLevel int `sql:"not null;DEFAULT:0" json:"ruleLevel"`
	// WebHooks can been subscribed by many projects.
	Projects []*Project `gorm:"many2many:project_webhooks" json:"-"`
}

WebHook is the alerter message receiver.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL