store

package
v0.0.0-...-fb7f86c Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const AlertDefinitionMaxTitleLength = 190

AlertDefinitionMaxTitleLength is the maximum length of the alert definition title

View Source
const AlertRuleMaxRuleGroupNameLength = 190

AlertRuleMaxRuleGroupNameLength is the maximum length of the alert rule group name

View Source
const AlertRuleMaxTitleLength = 190

AlertRuleMaxTitleLength is the maximum length of the alert rule title

Variables

View Source
var (
	ErrAlertRuleGroupNotFound = errors.New("rulegroup not found")
	ErrOptimisticLock         = errors.New("version conflict while updating a record in the database with optimistic locking")
)
View Source
var (
	// ErrNoAlertmanagerConfiguration is an error for when no alertmanager configuration is found.
	ErrNoAlertmanagerConfiguration = fmt.Errorf("could not find an Alertmanager configuration")
	// ErrVersionLockedObjectNotFound is returned when an object is not
	// found using the current hash.
	ErrVersionLockedObjectNotFound = fmt.Errorf("could not find object using provided id and hash")
	// ConfigRecordsLimit defines the limit of how many alertmanager configuration versions
	// should be stored in the database for each organization including the current one.
	// Has to be > 0
	ConfigRecordsLimit int = 100
)
View Source
var AlertRuleFieldsToIgnoreInDiff = [...]string{"ID", "Version", "Updated"}

AlertRuleFieldsToIgnoreInDiff contains fields that are ignored when calculating the RuleDelta.Diff.

View Source
var (
	// ErrNoAdminConfiguration is an error for when no admin configuration is found.
	ErrNoAdminConfiguration = fmt.Errorf("no admin configuration available")
)
View Source
var GenerateNewAlertRuleUID = func(sess *db.Session, orgID int64, ruleTitle string) (string, error) {
	for i := 0; i < 3; i++ {
		uid := util.GenerateShortUID()

		exists, err := sess.Where("org_id=? AND uid=?", orgID, uid).Get(&ngmodels.AlertRule{})
		if err != nil {
			return "", err
		}

		if !exists {
			return uid, nil
		}
	}

	return "", ngmodels.ErrAlertRuleFailedGenerateUniqueUID
}

GenerateNewAlertRuleUID generates a unique UID for a rule. This is set as a variable so that the tests can override it. The ruleTitle is only used by the mocked functions.

View Source
var TimeNow = time.Now

TimeNow makes it possible to test usage of time

Functions

This section is empty.

Types

type AdminConfigurationStore

type AdminConfigurationStore interface {
	GetAdminConfiguration(orgID int64) (*ngmodels.AdminConfiguration, error)
	GetAdminConfigurations() ([]*ngmodels.AdminConfiguration, error)
	DeleteAdminConfiguration(orgID int64) error
	UpdateAdminConfiguration(UpdateAdminConfigurationCmd) error
}

type AdminConfigurationStoreMock

type AdminConfigurationStoreMock struct {
	mock.Mock
}

AdminConfigurationStoreMock is an autogenerated mock type for the AdminConfigurationStore type

func (*AdminConfigurationStoreMock) DeleteAdminConfiguration

func (_m *AdminConfigurationStoreMock) DeleteAdminConfiguration(orgID int64) error

DeleteAdminConfiguration provides a mock function with given fields: orgID

func (*AdminConfigurationStoreMock) EXPECT

func (*AdminConfigurationStoreMock) GetAdminConfiguration

func (_m *AdminConfigurationStoreMock) GetAdminConfiguration(orgID int64) (*models.AdminConfiguration, error)

GetAdminConfiguration provides a mock function with given fields: orgID

func (*AdminConfigurationStoreMock) GetAdminConfigurations

func (_m *AdminConfigurationStoreMock) GetAdminConfigurations() ([]*models.AdminConfiguration, error)

GetAdminConfigurations provides a mock function with given fields:

func (*AdminConfigurationStoreMock) UpdateAdminConfiguration

func (_m *AdminConfigurationStoreMock) UpdateAdminConfiguration(_a0 UpdateAdminConfigurationCmd) error

UpdateAdminConfiguration provides a mock function with given fields: _a0

type AdminConfigurationStoreMock_DeleteAdminConfiguration_Call

type AdminConfigurationStoreMock_DeleteAdminConfiguration_Call struct {
	*mock.Call
}

AdminConfigurationStoreMock_DeleteAdminConfiguration_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DeleteAdminConfiguration'

func (*AdminConfigurationStoreMock_DeleteAdminConfiguration_Call) Return

func (*AdminConfigurationStoreMock_DeleteAdminConfiguration_Call) Run

type AdminConfigurationStoreMock_Expecter

type AdminConfigurationStoreMock_Expecter struct {
	// contains filtered or unexported fields
}

func (*AdminConfigurationStoreMock_Expecter) DeleteAdminConfiguration

DeleteAdminConfiguration is a helper method to define mock.On call

  • orgID int64

func (*AdminConfigurationStoreMock_Expecter) GetAdminConfiguration

GetAdminConfiguration is a helper method to define mock.On call

  • orgID int64

func (*AdminConfigurationStoreMock_Expecter) GetAdminConfigurations

GetAdminConfigurations is a helper method to define mock.On call

func (*AdminConfigurationStoreMock_Expecter) UpdateAdminConfiguration

UpdateAdminConfiguration is a helper method to define mock.On call

  • _a0 UpdateAdminConfigurationCmd

type AdminConfigurationStoreMock_GetAdminConfiguration_Call

type AdminConfigurationStoreMock_GetAdminConfiguration_Call struct {
	*mock.Call
}

AdminConfigurationStoreMock_GetAdminConfiguration_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetAdminConfiguration'

func (*AdminConfigurationStoreMock_GetAdminConfiguration_Call) Return

func (*AdminConfigurationStoreMock_GetAdminConfiguration_Call) Run

type AdminConfigurationStoreMock_GetAdminConfigurations_Call

type AdminConfigurationStoreMock_GetAdminConfigurations_Call struct {
	*mock.Call
}

AdminConfigurationStoreMock_GetAdminConfigurations_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetAdminConfigurations'

func (*AdminConfigurationStoreMock_GetAdminConfigurations_Call) Return

func (*AdminConfigurationStoreMock_GetAdminConfigurations_Call) Run

type AdminConfigurationStoreMock_UpdateAdminConfiguration_Call

type AdminConfigurationStoreMock_UpdateAdminConfiguration_Call struct {
	*mock.Call
}

AdminConfigurationStoreMock_UpdateAdminConfiguration_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdateAdminConfiguration'

func (*AdminConfigurationStoreMock_UpdateAdminConfiguration_Call) Return

func (*AdminConfigurationStoreMock_UpdateAdminConfiguration_Call) Run

type AlertingStore

type AlertingStore interface {
	GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error)
	GetAllLatestAlertmanagerConfiguration(ctx context.Context) ([]*models.AlertConfiguration, error)
	SaveAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
	SaveAlertmanagerConfigurationWithCallback(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd, callback SaveCallback) error
	UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
	MarkConfigurationAsApplied(ctx context.Context, cmd *models.MarkConfigurationAsAppliedCmd) error
	GetAppliedConfigurations(ctx context.Context, orgID int64, limit int) ([]*models.HistoricAlertConfiguration, error)
	GetHistoricalConfiguration(ctx context.Context, orgID int64, id int64) (*models.HistoricAlertConfiguration, error)
}

AlertingStore is the database interface used by the Alertmanager service.

type DBstore

type DBstore struct {
	Cfg              setting.UnifiedAlertingSettings
	FeatureToggles   featuremgmt.FeatureToggles
	SQLStore         db.DB
	Logger           log.Logger
	FolderService    folder.Service
	AccessControl    accesscontrol.AccessControl
	DashboardService dashboards.DashboardService
}

DBstore stores the alert definitions and instances in the database.

func ProvideDBStore

func ProvideDBStore(
	cfg *setting.Cfg, featureToggles featuremgmt.FeatureToggles, sqlstore db.DB, folderService folder.Service,
	access accesscontrol.AccessControl, dashboards dashboards.DashboardService) (*DBstore, error)

func (DBstore) Count

func (st DBstore) Count(ctx context.Context, orgID int64) (int64, error)

Count returns either the number of the alert rules under a specific org (if orgID is not zero) or the number of all the alert rules

func (DBstore) CountInFolder

func (st DBstore) CountInFolder(ctx context.Context, orgID int64, folderUID string, u *user.SignedInUser) (int64, error)

CountInFolder is a handler for retrieving the number of alert rules of specific organisation associated with a given namespace (parent folder).

func (DBstore) DeleteAdminConfiguration

func (st DBstore) DeleteAdminConfiguration(orgID int64) error

func (DBstore) DeleteAlertInstances

func (st DBstore) DeleteAlertInstances(ctx context.Context, keys ...models.AlertInstanceKey) error

DeleteAlertInstances deletes instances with the provided keys in a single transaction.

func (DBstore) DeleteAlertInstancesByRule

func (st DBstore) DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKey) error

func (DBstore) DeleteAlertRulesByUID

func (st DBstore) DeleteAlertRulesByUID(ctx context.Context, orgID int64, ruleUID ...string) error

DeleteAlertRulesByUID is a handler for deleting an alert rule.

func (DBstore) DeleteExpiredImages

func (st DBstore) DeleteExpiredImages(ctx context.Context) (int64, error)

func (DBstore) DeleteInFolder

func (st DBstore) DeleteInFolder(ctx context.Context, orgID int64, folderUID string, user *user.SignedInUser) error

DeleteInFolder deletes the rules contained in a given folder along with their associated data.

func (DBstore) DeleteProvenance

func (st DBstore) DeleteProvenance(ctx context.Context, o models.Provisionable, org int64) error

DeleteProvenance deletes the provenance record from the table

func (DBstore) FetchOrgIds

func (st DBstore) FetchOrgIds(ctx context.Context) ([]int64, error)

func (*DBstore) GetAdminConfiguration

func (st *DBstore) GetAdminConfiguration(orgID int64) (*ngmodels.AdminConfiguration, error)

func (DBstore) GetAdminConfigurations

func (st DBstore) GetAdminConfigurations() ([]*ngmodels.AdminConfiguration, error)

func (DBstore) GetAlertRuleByUID

func (st DBstore) GetAlertRuleByUID(ctx context.Context, query *ngmodels.GetAlertRuleByUIDQuery) (result *ngmodels.AlertRule, err error)

GetAlertRuleByUID is a handler for retrieving an alert rule from that database by its UID and organisation ID. It returns ngmodels.ErrAlertRuleNotFound if no alert rule is found for the provided ID.

func (DBstore) GetAlertRulesForScheduling

func (st DBstore) GetAlertRulesForScheduling(ctx context.Context, query *ngmodels.GetAlertRulesForSchedulingQuery) error

GetAlertRulesForScheduling returns a short version of all alert rules except those that belong to an excluded list of organizations

func (DBstore) GetAlertRulesGroupByRuleUID

func (st DBstore) GetAlertRulesGroupByRuleUID(ctx context.Context, query *ngmodels.GetAlertRulesGroupByRuleUIDQuery) (result []*ngmodels.AlertRule, err error)

GetAlertRulesGroupByRuleUID is a handler for retrieving a group of alert rules from that database by UID and organisation ID of one of rules that belong to that group.

func (DBstore) GetAlertRulesKeysForScheduling

func (st DBstore) GetAlertRulesKeysForScheduling(ctx context.Context) ([]ngmodels.AlertRuleKeyWithVersion, error)

func (*DBstore) GetAllLatestAlertmanagerConfiguration

func (st *DBstore) GetAllLatestAlertmanagerConfiguration(ctx context.Context) ([]*models.AlertConfiguration, error)

GetAllLatestAlertmanagerConfiguration returns the latest configuration of every organization

func (*DBstore) GetAppliedConfigurations

func (st *DBstore) GetAppliedConfigurations(ctx context.Context, orgID int64, limit int) ([]*models.HistoricAlertConfiguration, error)

GetAppliedConfigurations returns all configurations that have been marked as applied, ordered newest -> oldest by id.

func (*DBstore) GetHistoricalConfiguration

func (st *DBstore) GetHistoricalConfiguration(ctx context.Context, orgID int64, id int64) (*models.HistoricAlertConfiguration, error)

GetHistoricalConfiguration returns a single historical configuration based on provided org and id.

func (DBstore) GetImage

func (st DBstore) GetImage(ctx context.Context, token string) (*models.Image, error)

func (DBstore) GetImageByURL

func (st DBstore) GetImageByURL(ctx context.Context, url string) (*models.Image, error)

func (DBstore) GetImages

func (st DBstore) GetImages(ctx context.Context, tokens []string) ([]models.Image, []string, error)

func (*DBstore) GetLatestAlertmanagerConfiguration

func (st *DBstore) GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (result *models.AlertConfiguration, err error)

GetLatestAlertmanagerConfiguration returns the lastest version of the alertmanager configuration. It returns ErrNoAlertmanagerConfiguration if no configuration is found.

func (DBstore) GetNamespaceByTitle

func (st DBstore) GetNamespaceByTitle(ctx context.Context, namespace string, orgID int64, user *user.SignedInUser) (*folder.Folder, error)

GetNamespaceByTitle is a handler for retrieving a namespace by its title. Alerting rules follow a Grafana folder-like structure which we call namespaces.

func (DBstore) GetNamespaceByUID

func (st DBstore) GetNamespaceByUID(ctx context.Context, uid string, orgID int64, user *user.SignedInUser) (*folder.Folder, error)

GetNamespaceByUID is a handler for retrieving a namespace by its UID. Alerting rules follow a Grafana folder-like structure which we call namespaces.

func (DBstore) GetOrgs

func (st DBstore) GetOrgs(ctx context.Context) ([]int64, error)

func (DBstore) GetProvenance

func (st DBstore) GetProvenance(ctx context.Context, o models.Provisionable, org int64) (models.Provenance, error)

GetProvenance gets the provenance status for a provisionable object.

func (DBstore) GetProvenances

func (st DBstore) GetProvenances(ctx context.Context, org int64, resourceType string) (map[string]models.Provenance, error)

GetProvenance gets the provenance status for a provisionable object.

func (DBstore) GetRuleGroupInterval

func (st DBstore) GetRuleGroupInterval(ctx context.Context, orgID int64, namespaceUID string, ruleGroup string) (int64, error)

func (DBstore) GetUserVisibleNamespaces

func (st DBstore) GetUserVisibleNamespaces(ctx context.Context, orgID int64, user *user.SignedInUser) (map[string]*folder.Folder, error)

GetUserVisibleNamespaces returns the folders that are visible to the user and have at least one alert in it

func (*DBstore) InTransaction

func (st *DBstore) InTransaction(ctx context.Context, f func(ctx context.Context) error) error

func (DBstore) IncreaseVersionForAllRulesInNamespace

func (st DBstore) IncreaseVersionForAllRulesInNamespace(ctx context.Context, orgID int64, namespaceUID string) ([]ngmodels.AlertRuleKeyWithVersionAndPauseStatus, error)

IncreaseVersionForAllRulesInNamespace Increases version for all rules that have specified namespace. Returns all rules that belong to the namespace

func (DBstore) InsertAlertRules

func (st DBstore) InsertAlertRules(ctx context.Context, rules []ngmodels.AlertRule) (map[string]int64, error)

InsertAlertRules is a handler for creating/updating alert rules.

func (DBstore) Kind

func (st DBstore) Kind() string

Kind returns the name of the alert rule type of entity.

func (DBstore) ListAlertInstances

func (st DBstore) ListAlertInstances(ctx context.Context, cmd *models.ListAlertInstancesQuery) (result []*models.AlertInstance, err error)

ListAlertInstances is a handler for retrieving alert instances within specific organisation based on various filters.

func (DBstore) ListAlertRules

func (st DBstore) ListAlertRules(ctx context.Context, query *ngmodels.ListAlertRulesQuery) (result ngmodels.RulesGroup, err error)

ListAlertRules is a handler for retrieving alert rules of specific organisation.

func (*DBstore) MarkConfigurationAsApplied

func (st *DBstore) MarkConfigurationAsApplied(ctx context.Context, cmd *models.MarkConfigurationAsAppliedCmd) error

MarkConfigurationAsApplied sets the `last_applied` field of the last config with the given hash to the current UNIX timestamp.

func (DBstore) SaveAlertInstance

func (st DBstore) SaveAlertInstance(ctx context.Context, alertInstance models.AlertInstance) error

SaveAlertInstance is a handler for saving a new alert instance.

func (DBstore) SaveAlertmanagerConfiguration

func (st DBstore) SaveAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error

SaveAlertmanagerConfiguration creates an alertmanager configuration.

func (DBstore) SaveAlertmanagerConfigurationWithCallback

func (st DBstore) SaveAlertmanagerConfigurationWithCallback(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd, callback SaveCallback) error

SaveAlertmanagerConfigurationWithCallback creates an alertmanager configuration version and then executes a callback. If the callback results in error it rolls back the transaction.

func (DBstore) SaveImage

func (st DBstore) SaveImage(ctx context.Context, img *models.Image) error

func (DBstore) SetProvenance

func (st DBstore) SetProvenance(ctx context.Context, o models.Provisionable, org int64, p models.Provenance) error

SetProvenance changes the provenance status for a provisionable object.

func (DBstore) URLExists

func (st DBstore) URLExists(ctx context.Context, url string) (bool, error)

func (DBstore) UpdateAdminConfiguration

func (st DBstore) UpdateAdminConfiguration(cmd UpdateAdminConfigurationCmd) error

func (DBstore) UpdateAlertRules

func (st DBstore) UpdateAlertRules(ctx context.Context, rules []ngmodels.UpdateRule) error

UpdateAlertRules is a handler for updating alert rules.

func (*DBstore) UpdateAlertmanagerConfiguration

func (st *DBstore) UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error

UpdateAlertmanagerConfiguration replaces an alertmanager configuration with optimistic locking. It assumes that an existing revision of the configuration exists in the store, and will return an error otherwise.

type FakeAdminConfigStore

type FakeAdminConfigStore struct {
	Configs map[int64]*models.AdminConfiguration
	// contains filtered or unexported fields
}

func NewFakeAdminConfigStore

func NewFakeAdminConfigStore(t *testing.T) *FakeAdminConfigStore

func (*FakeAdminConfigStore) DeleteAdminConfiguration

func (f *FakeAdminConfigStore) DeleteAdminConfiguration(orgID int64) error

func (*FakeAdminConfigStore) GetAdminConfiguration

func (f *FakeAdminConfigStore) GetAdminConfiguration(orgID int64) (*models.AdminConfiguration, error)

func (*FakeAdminConfigStore) GetAdminConfigurations

func (f *FakeAdminConfigStore) GetAdminConfigurations() ([]*models.AdminConfiguration, error)

func (*FakeAdminConfigStore) UpdateAdminConfiguration

func (f *FakeAdminConfigStore) UpdateAdminConfiguration(cmd UpdateAdminConfigurationCmd) error

type FakeImageStore

type FakeImageStore struct {
	// contains filtered or unexported fields
}

func NewFakeImageStore

func NewFakeImageStore(t *testing.T, images ...*models.Image) *FakeImageStore

func (*FakeImageStore) GetImage

func (s *FakeImageStore) GetImage(_ context.Context, token string) (*models.Image, error)

func (*FakeImageStore) GetImageByURL

func (s *FakeImageStore) GetImageByURL(_ context.Context, url string) (*models.Image, error)

func (*FakeImageStore) GetImages

func (s *FakeImageStore) GetImages(_ context.Context, tokens []string) ([]models.Image, []string, error)

func (*FakeImageStore) SaveImage

func (s *FakeImageStore) SaveImage(_ context.Context, image *models.Image) error

func (*FakeImageStore) URLExists

func (s *FakeImageStore) URLExists(_ context.Context, url string) (bool, error)

type GroupDelta

type GroupDelta struct {
	GroupKey models.AlertRuleGroupKey
	// AffectedGroups contains all rules of all groups that are affected by these changes.
	// For example, during moving a rule from one group to another this map will contain all rules from two groups
	AffectedGroups map[models.AlertRuleGroupKey]models.RulesGroup
	New            []*models.AlertRule
	Update         []RuleDelta
	Delete         []*models.AlertRule
}

func CalculateChanges

func CalculateChanges(ctx context.Context, ruleReader RuleReader, groupKey models.AlertRuleGroupKey, submittedRules []*models.AlertRuleWithOptionals) (*GroupDelta, error)

CalculateChanges calculates the difference between rules in the group in the database and the submitted rules. If a submitted rule has UID it tries to find it in the database (in other groups). returns a list of rules that need to be added, updated and deleted. Deleted considered rules in the database that belong to the group but do not exist in the list of submitted rules.

func UpdateCalculatedRuleFields

func UpdateCalculatedRuleFields(ch *GroupDelta) *GroupDelta

UpdateCalculatedRuleFields refreshes the calculated fields in a set of alert rule changes. This may generate new changes to keep a group consistent, such as versions or rule indexes.

func (*GroupDelta) IsEmpty

func (c *GroupDelta) IsEmpty() bool

type ImageAdminStore

type ImageAdminStore interface {
	ImageStore

	// DeleteExpiredImages deletes expired images. It returns the number of deleted images
	// or an error.
	DeleteExpiredImages(context.Context) (int64, error)
}

type ImageStore

type ImageStore interface {
	// GetImage returns the image with the token. It returns ErrImageNotFound
	// if the image has expired or if an image with the token does not exist.
	GetImage(ctx context.Context, token string) (*models.Image, error)

	// GetImageByURL looks for a image by its URL. It returns ErrImageNotFound
	// if the image has expired or if there is no image associated with the URL.
	GetImageByURL(ctx context.Context, url string) (*models.Image, error)

	// GetImages returns all images that match the tokens. If one or more images
	// have expired or do not exist then it also returns the unmatched tokens
	// and an ErrImageNotFound error.
	GetImages(ctx context.Context, tokens []string) ([]models.Image, []string, error)

	// SaveImage saves the image or returns an error.
	SaveImage(ctx context.Context, img *models.Image) error

	// URLExists takes a URL and returns a boolean indicating whether or not
	// we have an image for that URL.
	URLExists(ctx context.Context, url string) (bool, error)
}

type OrgStore

type OrgStore interface {
	GetOrgs(ctx context.Context) ([]int64, error)
}

type RuleDelta

type RuleDelta struct {
	Existing *models.AlertRule
	New      *models.AlertRule
	Diff     cmputil.DiffReport
}

type RuleReader

type RuleReader interface {
	ListAlertRules(ctx context.Context, query *models.ListAlertRulesQuery) (models.RulesGroup, error)
	GetAlertRulesGroupByRuleUID(ctx context.Context, query *models.GetAlertRulesGroupByRuleUIDQuery) ([]*models.AlertRule, error)
}

type SaveCallback

type SaveCallback func() error

type UpdateAdminConfigurationCmd

type UpdateAdminConfigurationCmd struct {
	AdminConfiguration *ngmodels.AdminConfiguration
}

Jump to

Keyboard shortcuts

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