mirror of
https://github.com/grafana/grafana.git
synced 2024-12-02 05:29:42 -06:00
946da57b6a
* Allow creating correlations for provisioned data sources * Update docs * Fix linting * Add missing props * Add missing props * Fix linting * Fix linting * Clarify error name * Removed error handling for a non-existing use case * Create a list of deleted data datasources based on all configs * Add org_id to correlations * Add tests * Allow org_id to be null in case org_id=0 is used * Create organization to ensure stable id is generated * Fix linting * Ensure backwards compatibility * Add deprecation information * Update comments * Override existing datasSource variable so the UID is retrieved correctly * Migrate correlations indices * Default org_id when migrating * Remove redundant default * Make PK non-nullable * Post merge fixes * Separate data sources / correlations provisioning * Adjust comments * Store new data sources in spy store so it can be used to test correlations as well * Fix linting * Update tests * Ensure response is closed * Avoid creating duplicates during provisioning * Fix updating provisioned column and update tests * Rename error message * Fix linting errors * Fix linting errors and rename variable * Update test * Update pkg/services/sqlstore/migrations/correlations_mig.go Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Remove unused error * Fix lining --------- Co-authored-by: Giordano Ricci <me@giordanoricci.com>
153 lines
4.8 KiB
Go
153 lines
4.8 KiB
Go
package correlations
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/events"
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
var (
|
|
logger = log.New("correlations")
|
|
)
|
|
|
|
func ProvideService(sqlStore db.DB, routeRegister routing.RouteRegister, ds datasources.DataSourceService, ac accesscontrol.AccessControl, bus bus.Bus, qs quota.Service, cfg *setting.Cfg,
|
|
) (*CorrelationsService, error) {
|
|
s := &CorrelationsService{
|
|
SQLStore: sqlStore,
|
|
RouteRegister: routeRegister,
|
|
log: logger,
|
|
DataSourceService: ds,
|
|
AccessControl: ac,
|
|
QuotaService: qs,
|
|
}
|
|
|
|
s.registerAPIEndpoints()
|
|
|
|
bus.AddEventListener(s.handleDatasourceDeletion)
|
|
|
|
defaultLimits, err := readQuotaConfig(cfg)
|
|
if err != nil {
|
|
return s, err
|
|
}
|
|
|
|
if err := qs.RegisterQuotaReporter("a.NewUsageReporter{
|
|
TargetSrv: QuotaTargetSrv,
|
|
DefaultLimits: defaultLimits,
|
|
Reporter: s.Usage,
|
|
}); err != nil {
|
|
return s, err
|
|
}
|
|
|
|
return s, nil
|
|
}
|
|
|
|
type Service interface {
|
|
CreateCorrelation(ctx context.Context, cmd CreateCorrelationCommand) (Correlation, error)
|
|
CreateOrUpdateCorrelation(ctx context.Context, cmd CreateCorrelationCommand) error
|
|
DeleteCorrelation(ctx context.Context, cmd DeleteCorrelationCommand) error
|
|
DeleteCorrelationsBySourceUID(ctx context.Context, cmd DeleteCorrelationsBySourceUIDCommand) error
|
|
DeleteCorrelationsByTargetUID(ctx context.Context, cmd DeleteCorrelationsByTargetUIDCommand) error
|
|
}
|
|
|
|
type CorrelationsService struct {
|
|
SQLStore db.DB
|
|
RouteRegister routing.RouteRegister
|
|
log log.Logger
|
|
DataSourceService datasources.DataSourceService
|
|
AccessControl accesscontrol.AccessControl
|
|
QuotaService quota.Service
|
|
}
|
|
|
|
func (s CorrelationsService) CreateCorrelation(ctx context.Context, cmd CreateCorrelationCommand) (Correlation, error) {
|
|
quotaReached, err := s.QuotaService.CheckQuotaReached(ctx, QuotaTargetSrv, nil)
|
|
if err != nil {
|
|
logger.Warn("Error getting correlation quota.", "error", err)
|
|
return Correlation{}, ErrCorrelationsQuotaFailed
|
|
}
|
|
if quotaReached {
|
|
return Correlation{}, ErrCorrelationsQuotaReached
|
|
}
|
|
|
|
return s.createCorrelation(ctx, cmd)
|
|
}
|
|
|
|
func (s CorrelationsService) CreateOrUpdateCorrelation(ctx context.Context, cmd CreateCorrelationCommand) error {
|
|
return s.createOrUpdateCorrelation(ctx, cmd)
|
|
}
|
|
|
|
func (s CorrelationsService) DeleteCorrelation(ctx context.Context, cmd DeleteCorrelationCommand) error {
|
|
return s.deleteCorrelation(ctx, cmd)
|
|
}
|
|
|
|
func (s CorrelationsService) UpdateCorrelation(ctx context.Context, cmd UpdateCorrelationCommand) (Correlation, error) {
|
|
return s.updateCorrelation(ctx, cmd)
|
|
}
|
|
|
|
func (s CorrelationsService) GetCorrelation(ctx context.Context, cmd GetCorrelationQuery) (Correlation, error) {
|
|
return s.getCorrelation(ctx, cmd)
|
|
}
|
|
|
|
func (s CorrelationsService) GetCorrelationsBySourceUID(ctx context.Context, cmd GetCorrelationsBySourceUIDQuery) ([]Correlation, error) {
|
|
return s.getCorrelationsBySourceUID(ctx, cmd)
|
|
}
|
|
|
|
func (s CorrelationsService) GetCorrelations(ctx context.Context, cmd GetCorrelationsQuery) (GetCorrelationsResponseBody, error) {
|
|
return s.getCorrelations(ctx, cmd)
|
|
}
|
|
|
|
func (s CorrelationsService) DeleteCorrelationsBySourceUID(ctx context.Context, cmd DeleteCorrelationsBySourceUIDCommand) error {
|
|
return s.deleteCorrelationsBySourceUID(ctx, cmd)
|
|
}
|
|
|
|
func (s CorrelationsService) DeleteCorrelationsByTargetUID(ctx context.Context, cmd DeleteCorrelationsByTargetUIDCommand) error {
|
|
return s.deleteCorrelationsByTargetUID(ctx, cmd)
|
|
}
|
|
|
|
func (s CorrelationsService) handleDatasourceDeletion(ctx context.Context, event *events.DataSourceDeleted) error {
|
|
return s.SQLStore.InTransaction(ctx, func(ctx context.Context) error {
|
|
if err := s.deleteCorrelationsBySourceUID(ctx, DeleteCorrelationsBySourceUIDCommand{
|
|
SourceUID: event.UID,
|
|
OrgId: event.OrgID,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.deleteCorrelationsByTargetUID(ctx, DeleteCorrelationsByTargetUIDCommand{
|
|
TargetUID: event.UID,
|
|
OrgId: event.OrgID,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (s *CorrelationsService) Usage(ctx context.Context, scopeParams *quota.ScopeParameters) (*quota.Map, error) {
|
|
return s.CountCorrelations(ctx)
|
|
}
|
|
|
|
func readQuotaConfig(cfg *setting.Cfg) (*quota.Map, error) {
|
|
limits := "a.Map{}
|
|
|
|
if cfg == nil {
|
|
return limits, nil
|
|
}
|
|
|
|
globalQuotaTag, err := quota.NewTag(QuotaTargetSrv, QuotaTarget, quota.GlobalScope)
|
|
if err != nil {
|
|
return limits, err
|
|
}
|
|
|
|
limits.Set(globalQuotaTag, cfg.Quota.Global.Correlations)
|
|
return limits, nil
|
|
}
|