Correlations: Only return correlation for which both source and target datasources exist (#55454)

* Correlation: only return correlation for which both source and targe ds exist

* add test
This commit is contained in:
Giordano Ricci
2022-09-26 14:05:04 +01:00
committed by GitHub
parent 018733dd24
commit d07abdd23c
2 changed files with 26 additions and 15 deletions

View File

@@ -147,14 +147,10 @@ func (s CorrelationsService) getCorrelation(ctx context.Context, cmd GetCorrelat
return ErrSourceDataSourceDoesNotExists
}
found, err := session.Where("uid = ? AND source_uid = ?", correlation.UID, correlation.SourceUID).Get(&correlation)
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
}
if err != nil {
return err
}
return err
})
@@ -166,9 +162,6 @@ func (s CorrelationsService) getCorrelation(ctx context.Context, cmd GetCorrelat
}
func (s CorrelationsService) getCorrelationsBySourceUID(ctx context.Context, cmd GetCorrelationsBySourceUIDQuery) ([]Correlation, error) {
correlationsCondiBean := Correlation{
SourceUID: cmd.SourceUID,
}
correlations := make([]Correlation, 0)
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *sqlstore.DBSession) error {
@@ -180,12 +173,7 @@ func (s CorrelationsService) getCorrelationsBySourceUID(ctx context.Context, cmd
return ErrSourceDataSourceDoesNotExists
}
err := session.Find(&correlations, correlationsCondiBean)
if err != nil {
return err
}
return err
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 {
@@ -199,7 +187,7 @@ func (s CorrelationsService) getCorrelations(ctx context.Context, cmd GetCorrela
correlations := make([]Correlation, 0)
err := s.SQLStore.WithDbSession(ctx, func(session *sqlstore.DBSession) error {
return session.Select("correlation.*").Join("", "data_source", "correlation.source_uid = data_source.uid").Where("data_source.org_id = ?", cmd.OrgId).Find(&correlations)
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