2022-07-25 09:19:07 -05:00
package correlations
import (
"context"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/sqlstore"
"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 ,
2022-09-27 04:08:02 -05:00
Config : cmd . Config ,
2022-07-25 09:19:07 -05:00
}
err := s . SQLStore . WithTransactionalDbSession ( ctx , func ( session * sqlstore . DBSession ) error {
var err error
query := & datasources . GetDataSourceQuery {
OrgId : cmd . OrgId ,
Uid : cmd . SourceUID ,
}
if err = s . DataSourceService . GetDataSource ( ctx , query ) ; err != nil {
return ErrSourceDataSourceDoesNotExists
}
if ! cmd . SkipReadOnlyCheck && query . Result . ReadOnly {
return ErrSourceDataSourceReadOnly
}
2022-10-04 03:39:55 -05:00
if cmd . TargetUID != nil {
if err = s . DataSourceService . GetDataSource ( ctx , & datasources . GetDataSourceQuery {
OrgId : cmd . OrgId ,
Uid : * cmd . TargetUID ,
} ) ; err != nil {
return ErrTargetDataSourceDoesNotExists
}
2022-07-25 09:19:07 -05:00
}
_ , err = session . Insert ( correlation )
if err != nil {
return err
}
return nil
} )
if err != nil {
return Correlation { } , err
}
return correlation , nil
}
2022-07-27 03:07:58 -05:00
func ( s CorrelationsService ) deleteCorrelation ( ctx context . Context , cmd DeleteCorrelationCommand ) error {
return s . SQLStore . WithDbSession ( ctx , func ( session * sqlstore . DBSession ) error {
query := & datasources . GetDataSourceQuery {
OrgId : cmd . OrgId ,
Uid : cmd . SourceUID ,
}
if err := s . DataSourceService . GetDataSource ( ctx , query ) ; err != nil {
return ErrSourceDataSourceDoesNotExists
}
if query . Result . ReadOnly {
return ErrSourceDataSourceReadOnly
}
deletedCount , err := session . Delete ( & Correlation { UID : cmd . UID , SourceUID : cmd . SourceUID } )
if deletedCount == 0 {
return ErrCorrelationNotFound
}
return err
} )
}
2022-08-03 08:18:51 -05:00
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 * sqlstore . DBSession ) error {
query := & datasources . GetDataSourceQuery {
OrgId : cmd . OrgId ,
Uid : cmd . SourceUID ,
}
if err := s . DataSourceService . GetDataSource ( ctx , query ) ; err != nil {
return ErrSourceDataSourceDoesNotExists
}
if query . Result . ReadOnly {
return ErrSourceDataSourceReadOnly
}
if cmd . Label == nil && cmd . Description == nil {
return ErrUpdateCorrelationEmptyParams
}
update := Correlation { }
if cmd . Label != nil {
update . Label = * cmd . Label
session . MustCols ( "label" )
}
if cmd . Description != nil {
update . Description = * cmd . Description
session . MustCols ( "description" )
}
updateCount , err := session . Where ( "uid = ? AND source_uid = ?" , correlation . UID , correlation . SourceUID ) . Limit ( 1 ) . Update ( update )
if updateCount == 0 {
return ErrCorrelationNotFound
}
if err != nil {
return err
}
found , err := session . Get ( & correlation )
if ! found {
return ErrCorrelationNotFound
}
return err
} )
if err != nil {
return Correlation { } , err
}
return correlation , nil
}
2022-08-11 10:58:11 -05:00
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 * sqlstore . DBSession ) error {
query := & datasources . GetDataSourceQuery {
OrgId : cmd . OrgId ,
Uid : cmd . SourceUID ,
}
if err := s . DataSourceService . GetDataSource ( ctx , query ) ; err != nil {
return ErrSourceDataSourceDoesNotExists
}
2022-09-26 08:05:04 -05:00
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 )
2022-08-11 10:58:11 -05:00
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 * sqlstore . DBSession ) error {
query := & datasources . GetDataSourceQuery {
OrgId : cmd . OrgId ,
Uid : cmd . SourceUID ,
}
if err := s . DataSourceService . GetDataSource ( ctx , query ) ; err != nil {
return ErrSourceDataSourceDoesNotExists
}
2022-09-26 08:05:04 -05:00
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 )
2022-08-11 10:58:11 -05:00
} )
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 * sqlstore . DBSession ) error {
2022-09-26 08:05:04 -05:00
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 )
2022-08-11 10:58:11 -05:00
} )
if err != nil {
return [ ] Correlation { } , err
}
return correlations , nil
}
2022-07-25 09:19:07 -05:00
func ( s CorrelationsService ) deleteCorrelationsBySourceUID ( ctx context . Context , cmd DeleteCorrelationsBySourceUIDCommand ) error {
return s . SQLStore . WithDbSession ( ctx , func ( session * sqlstore . DBSession ) 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 * sqlstore . DBSession ) error {
2022-10-04 03:39:55 -05:00
_ , err := session . Delete ( & Correlation { TargetUID : & cmd . TargetUID } )
2022-07-25 09:19:07 -05:00
return err
} )
}