SQLStore: customise the limit of retrieved datasources per organisation (#29358)
* SQLStore: customise the limit of retrieved datasources per organisation * update all suggestions regarding nil or 0 as default * Apply suggestions from code review Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * correct default.ini description + adding unittest * Apply suggestions from code review Co-authored-by: Sofia Papagiannaki <papagian@users.noreply.github.com> * modify unittest name Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> Co-authored-by: Sofia Papagiannaki <papagian@users.noreply.github.com>
This commit is contained in:
co-authored by
Emil Tullstedt
Sofia Papagiannaki
parent
ac922f4e1d
commit
375e8e4fd0
@@ -67,8 +67,12 @@ func GetDataSourceByName(query *models.GetDataSourceByNameQuery) error {
|
||||
}
|
||||
|
||||
func GetDataSources(query *models.GetDataSourcesQuery) error {
|
||||
sess := x.Limit(5000, 0).Where("org_id=?", query.OrgId).Asc("name")
|
||||
|
||||
var sess *xorm.Session
|
||||
if query.DataSourceLimit <= 0 {
|
||||
sess = x.Where("org_id=?", query.OrgId).Asc("name")
|
||||
} else {
|
||||
sess = x.Limit(query.DataSourceLimit, 0).Where("org_id=?", query.OrgId).Asc("name")
|
||||
}
|
||||
query.Result = make([]*models.DataSource, 0)
|
||||
return sess.Find(&query.Result)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
@@ -214,4 +215,75 @@ func TestDataAccess(t *testing.T) {
|
||||
|
||||
require.Equal(t, 0, len(query.Result))
|
||||
})
|
||||
|
||||
t.Run("GetDataSource", func(t *testing.T) {
|
||||
t.Run("Number of data sources returned limited to 6 per organization", func(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
datasourceLimit := 6
|
||||
for i := 0; i < datasourceLimit+1; i++ {
|
||||
err := AddDataSource(&models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "laban" + strconv.Itoa(i),
|
||||
Type: models.DS_GRAPHITE,
|
||||
Access: models.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
Database: "site",
|
||||
ReadOnly: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
query := models.GetDataSourcesQuery{OrgId: 10, DataSourceLimit: datasourceLimit}
|
||||
|
||||
err := GetDataSources(&query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, datasourceLimit, len(query.Result))
|
||||
})
|
||||
|
||||
t.Run("No limit should be applied on the returned data sources if the limit is not set", func(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
numberOfDatasource := 5100
|
||||
for i := 0; i < numberOfDatasource; i++ {
|
||||
err := AddDataSource(&models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "laban" + strconv.Itoa(i),
|
||||
Type: models.DS_GRAPHITE,
|
||||
Access: models.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
Database: "site",
|
||||
ReadOnly: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
query := models.GetDataSourcesQuery{OrgId: 10}
|
||||
|
||||
err := GetDataSources(&query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, numberOfDatasource, len(query.Result))
|
||||
})
|
||||
|
||||
t.Run("No limit should be applied on the returned data sources if the limit is negative", func(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
numberOfDatasource := 5100
|
||||
for i := 0; i < numberOfDatasource; i++ {
|
||||
err := AddDataSource(&models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "laban" + strconv.Itoa(i),
|
||||
Type: models.DS_GRAPHITE,
|
||||
Access: models.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
Database: "site",
|
||||
ReadOnly: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
query := models.GetDataSourcesQuery{OrgId: 10, DataSourceLimit: -1}
|
||||
|
||||
err := GetDataSources(&query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, numberOfDatasource, len(query.Result))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -354,7 +354,7 @@ func (ss *SQLStore) readConfig() {
|
||||
ss.dbCfg.CacheMode = sec.Key("cache_mode").MustString("private")
|
||||
}
|
||||
|
||||
// Interface of arguments for testing db
|
||||
// ITestDB is an interface of arguments for testing db
|
||||
type ITestDB interface {
|
||||
Helper()
|
||||
Fatalf(format string, args ...interface{})
|
||||
|
||||
Reference in New Issue
Block a user