mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* bring in source from database
* bring in transformations from database
* add regex transformations to scopevar
* Consolidate types, add better example, cleanup
* Add var only if match
* Change ScopedVar to not require text, do not leak transformation-made variables between links
* Add mappings and start implementing logfmt
* Add mappings and start implementing logfmt
* Remove mappings, turn off global regex
* Add example yaml and omit transformations if empty
* Fix the yaml
* Add logfmt transformation
* Cleanup transformations and yaml
* add transformation field to FE types and use it, safeStringify logfmt values
* Add tests, only safe stringify if non-string, fix bug with safe stringify where it would return empty string with false value
* Add test for transformation field
* Do not add null transformations object
* Break out transformation logic, add tests to backend code
* Fix lint errors I understand 😅
* Fix the backend lint error
* Remove unnecessary code and mark new Transformations object as internal
* Add support for named capture groups
* Remove type assertion
* Remove variable name from transformation
* Add test for overriding regexes
* Add back variable name field, but change to mapValue
* fix go api test
* Change transformation types to enum, add better provisioning checks for bad type name and format
* Check for expression with regex transformations
228 lines
6.6 KiB
Go
228 lines
6.6 KiB
Go
package correlations
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
)
|
|
|
|
// createCorrelation adds a correlation
|
|
func (s CorrelationsService) createCorrelation(ctx context.Context, cmd CreateCorrelationCommand) (Correlation, error) {
|
|
correlation := Correlation{
|
|
UID: util.GenerateShortUID(),
|
|
SourceUID: cmd.SourceUID,
|
|
TargetUID: cmd.TargetUID,
|
|
Label: cmd.Label,
|
|
Description: cmd.Description,
|
|
Config: cmd.Config,
|
|
}
|
|
|
|
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *db.Session) error {
|
|
var err error
|
|
|
|
query := &datasources.GetDataSourceQuery{
|
|
OrgID: cmd.OrgId,
|
|
UID: cmd.SourceUID,
|
|
}
|
|
dataSource, err := s.DataSourceService.GetDataSource(ctx, query)
|
|
if err != nil {
|
|
return ErrSourceDataSourceDoesNotExists
|
|
}
|
|
|
|
if !cmd.SkipReadOnlyCheck && dataSource.ReadOnly {
|
|
return ErrSourceDataSourceReadOnly
|
|
}
|
|
|
|
if cmd.TargetUID != nil {
|
|
if _, err = s.DataSourceService.GetDataSource(ctx, &datasources.GetDataSourceQuery{
|
|
OrgID: cmd.OrgId,
|
|
UID: *cmd.TargetUID,
|
|
}); err != nil {
|
|
return ErrTargetDataSourceDoesNotExists
|
|
}
|
|
}
|
|
|
|
_, err = session.Insert(correlation)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return Correlation{}, err
|
|
}
|
|
|
|
return correlation, nil
|
|
}
|
|
|
|
func (s CorrelationsService) deleteCorrelation(ctx context.Context, cmd DeleteCorrelationCommand) error {
|
|
return s.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
|
|
query := &datasources.GetDataSourceQuery{
|
|
OrgID: cmd.OrgId,
|
|
UID: cmd.SourceUID,
|
|
}
|
|
dataSource, err := s.DataSourceService.GetDataSource(ctx, query)
|
|
if err != nil {
|
|
return ErrSourceDataSourceDoesNotExists
|
|
}
|
|
|
|
if dataSource.ReadOnly {
|
|
return ErrSourceDataSourceReadOnly
|
|
}
|
|
|
|
deletedCount, err := session.Delete(&Correlation{UID: cmd.UID, SourceUID: cmd.SourceUID})
|
|
if deletedCount == 0 {
|
|
return ErrCorrelationNotFound
|
|
}
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (s CorrelationsService) updateCorrelation(ctx context.Context, cmd UpdateCorrelationCommand) (Correlation, error) {
|
|
correlation := Correlation{
|
|
UID: cmd.UID,
|
|
SourceUID: cmd.SourceUID,
|
|
}
|
|
|
|
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *db.Session) error {
|
|
query := &datasources.GetDataSourceQuery{
|
|
OrgID: cmd.OrgId,
|
|
UID: cmd.SourceUID,
|
|
}
|
|
dataSource, err := s.DataSourceService.GetDataSource(ctx, query)
|
|
if err != nil {
|
|
return ErrSourceDataSourceDoesNotExists
|
|
}
|
|
|
|
if dataSource.ReadOnly {
|
|
return ErrSourceDataSourceReadOnly
|
|
}
|
|
|
|
found, err := session.Get(&correlation)
|
|
if !found {
|
|
return ErrCorrelationNotFound
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if cmd.Label != nil {
|
|
correlation.Label = *cmd.Label
|
|
session.MustCols("label")
|
|
}
|
|
if cmd.Description != nil {
|
|
correlation.Description = *cmd.Description
|
|
session.MustCols("description")
|
|
}
|
|
if cmd.Config != nil {
|
|
session.MustCols("config")
|
|
if cmd.Config.Field != nil {
|
|
correlation.Config.Field = *cmd.Config.Field
|
|
}
|
|
if cmd.Config.Type != nil {
|
|
correlation.Config.Type = *cmd.Config.Type
|
|
}
|
|
if cmd.Config.Target != nil {
|
|
correlation.Config.Target = *cmd.Config.Target
|
|
}
|
|
if cmd.Config.Transformations != nil {
|
|
correlation.Config.Transformations = cmd.Config.Transformations
|
|
}
|
|
}
|
|
|
|
updateCount, err := session.Where("uid = ? AND source_uid = ?", correlation.UID, correlation.SourceUID).Limit(1).Update(correlation)
|
|
if updateCount == 0 {
|
|
return ErrCorrelationNotFound
|
|
}
|
|
return err
|
|
})
|
|
|
|
if err != nil {
|
|
return Correlation{}, err
|
|
}
|
|
|
|
return correlation, nil
|
|
}
|
|
|
|
func (s CorrelationsService) getCorrelation(ctx context.Context, cmd GetCorrelationQuery) (Correlation, error) {
|
|
correlation := Correlation{
|
|
UID: cmd.UID,
|
|
SourceUID: cmd.SourceUID,
|
|
}
|
|
|
|
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *db.Session) error {
|
|
query := &datasources.GetDataSourceQuery{
|
|
OrgID: cmd.OrgId,
|
|
UID: cmd.SourceUID,
|
|
}
|
|
if _, err := s.DataSourceService.GetDataSource(ctx, query); err != nil {
|
|
return ErrSourceDataSourceDoesNotExists
|
|
}
|
|
|
|
found, err := session.Select("correlation.*").Join("", "data_source AS dss", "correlation.source_uid = dss.uid and dss.org_id = ?", cmd.OrgId).Join("", "data_source AS dst", "correlation.target_uid = dst.uid and dst.org_id = ?", cmd.OrgId).Where("correlation.uid = ? AND correlation.source_uid = ?", correlation.UID, correlation.SourceUID).Get(&correlation)
|
|
if !found {
|
|
return ErrCorrelationNotFound
|
|
}
|
|
return err
|
|
})
|
|
|
|
if err != nil {
|
|
return Correlation{}, err
|
|
}
|
|
|
|
return correlation, nil
|
|
}
|
|
|
|
func (s CorrelationsService) getCorrelationsBySourceUID(ctx context.Context, cmd GetCorrelationsBySourceUIDQuery) ([]Correlation, error) {
|
|
correlations := make([]Correlation, 0)
|
|
|
|
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *db.Session) error {
|
|
query := &datasources.GetDataSourceQuery{
|
|
OrgID: cmd.OrgId,
|
|
UID: cmd.SourceUID,
|
|
}
|
|
if _, err := s.DataSourceService.GetDataSource(ctx, query); err != nil {
|
|
return ErrSourceDataSourceDoesNotExists
|
|
}
|
|
|
|
return session.Select("correlation.*").Join("", "data_source AS dss", "correlation.source_uid = dss.uid and dss.org_id = ?", cmd.OrgId).Join("", "data_source AS dst", "correlation.target_uid = dst.uid and dst.org_id = ?", cmd.OrgId).Where("correlation.source_uid = ?", cmd.SourceUID).Find(&correlations)
|
|
})
|
|
|
|
if err != nil {
|
|
return []Correlation{}, err
|
|
}
|
|
|
|
return correlations, nil
|
|
}
|
|
|
|
func (s CorrelationsService) getCorrelations(ctx context.Context, cmd GetCorrelationsQuery) ([]Correlation, error) {
|
|
correlations := make([]Correlation, 0)
|
|
|
|
err := s.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
|
|
return session.Select("correlation.*").Join("", "data_source AS dss", "correlation.source_uid = dss.uid and dss.org_id = ?", cmd.OrgId).Join("", "data_source AS dst", "correlation.target_uid = dst.uid and dst.org_id = ?", cmd.OrgId).Find(&correlations)
|
|
})
|
|
if err != nil {
|
|
return []Correlation{}, err
|
|
}
|
|
|
|
return correlations, nil
|
|
}
|
|
|
|
func (s CorrelationsService) deleteCorrelationsBySourceUID(ctx context.Context, cmd DeleteCorrelationsBySourceUIDCommand) error {
|
|
return s.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
|
|
_, err := session.Delete(&Correlation{SourceUID: cmd.SourceUID})
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (s CorrelationsService) deleteCorrelationsByTargetUID(ctx context.Context, cmd DeleteCorrelationsByTargetUIDCommand) error {
|
|
return s.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
|
|
_, err := session.Delete(&Correlation{TargetUID: &cmd.TargetUID})
|
|
return err
|
|
})
|
|
}
|