mirror of https://github.com/grafana/grafana
SQL Datasources: Update Max Connection and Max Idle Connection Defaults to 100 and add auto mode (#65834)
* Update connection configuration for SQL datasources * Working auto state for connection numbers * Add migration * Use defaults from constants file * Remove dead code * Add tests and restructure useMigrateDatabaseField * Update function names * Update docs * Make sure we don't continually issue updates * Update docs * Use onOptionsChnage in ConnectionLimits * Update docs * Clean up docs * Update migration * Fix default values in docs * Fix spacing issue * Fix test * Update default values for SQL connections * Include consts * Allow override for default SQL datasource connection parameters * Fix linter errors * Remove extra @ts-ignore * Centralize logic for default values * Remove debugging * Remove unecessary function * Update configuration docs * minor suggested change * Fix comment misspelling * Remove unecessary default setting code * Update docs to indicate that code was included for backport version * Remove dead code --------- Co-authored-by: lwandz13 <larissa.wandzura@grafana.com>pull/66654/head
parent
2509dec0cb
commit
92d92187d9
@ -1,25 +0,0 @@ |
||||
import { useEffect } from 'react'; |
||||
|
||||
import { DataSourceJsonData, DataSourcePluginOptionsEditorProps } from '@grafana/data'; |
||||
import { logDebug } from '@grafana/runtime'; |
||||
|
||||
import { SQLOptions } from '../../types'; |
||||
|
||||
/** |
||||
* Moves the database field from the options object to jsonData.database and empties the database field. |
||||
*/ |
||||
export function useMigrateDatabaseField<T extends DataSourceJsonData = SQLOptions, S = {}>({ |
||||
onOptionsChange, |
||||
options, |
||||
}: DataSourcePluginOptionsEditorProps<T, S>) { |
||||
useEffect(() => { |
||||
if (options.database) { |
||||
logDebug(`Migrating from options.database with value ${options.database} for ${options.name}`); |
||||
onOptionsChange({ |
||||
...options, |
||||
database: '', |
||||
jsonData: { ...options.jsonData, database: options.database }, |
||||
}); |
||||
} |
||||
}, [onOptionsChange, options]); |
||||
} |
||||
@ -0,0 +1,71 @@ |
||||
import { renderHook } from '@testing-library/react-hooks'; |
||||
|
||||
import { DataSourceSettings } from '@grafana/data'; |
||||
|
||||
import { SQLConnectionDefaults } from '../../constants'; |
||||
import { SQLOptions } from '../../types'; |
||||
|
||||
import { useMigrateDatabaseFields } from './useMigrateDatabaseFields'; |
||||
|
||||
describe('Database Field Migration', () => { |
||||
let defaultProps = { |
||||
options: { |
||||
database: 'testDatabase', |
||||
id: 1, |
||||
uid: 'unique-id', |
||||
orgId: 1, |
||||
name: 'Datasource Name', |
||||
type: 'postgres', |
||||
typeName: 'Postgres', |
||||
typeLogoUrl: 'http://example.com/logo.png', |
||||
access: 'access', |
||||
url: 'http://example.com', |
||||
user: 'user', |
||||
basicAuth: true, |
||||
basicAuthUser: 'user', |
||||
isDefault: false, |
||||
secureJsonFields: {}, |
||||
readOnly: false, |
||||
withCredentials: false, |
||||
jsonData: { |
||||
tlsAuth: false, |
||||
tlsAuthWithCACert: false, |
||||
timezone: 'America/Chicago', |
||||
tlsSkipVerify: false, |
||||
user: 'user', |
||||
}, |
||||
}, |
||||
}; |
||||
it('should migrate the old database field to be included in jsonData', () => { |
||||
const props = { |
||||
...defaultProps, |
||||
onOptionsChange: (options: DataSourceSettings) => { |
||||
const jsonData = options.jsonData as SQLOptions; |
||||
expect(options.database).toBe(''); |
||||
expect(jsonData.database).toBe('testDatabase'); |
||||
}, |
||||
}; |
||||
|
||||
// @ts-ignore Ignore this line as it's expected that
|
||||
// the database object will not be in necessary (most current) state
|
||||
const { rerender, result } = renderHook(() => useMigrateDatabaseFields(props)); |
||||
rerender(); |
||||
}); |
||||
|
||||
it('adds default max connection, max idle connection, and auto idle values when not detected', () => { |
||||
const props = { |
||||
...defaultProps, |
||||
onOptionsChange: (options: DataSourceSettings) => { |
||||
const jsonData = options.jsonData as SQLOptions; |
||||
expect(jsonData.maxOpenConns).toBe(SQLConnectionDefaults.MAX_CONNS); |
||||
expect(jsonData.maxIdleConns).toBe(Math.ceil(SQLConnectionDefaults.MAX_CONNS)); |
||||
expect(jsonData.maxIdleConnsAuto).toBe(true); |
||||
}, |
||||
}; |
||||
|
||||
// @ts-ignore Ignore this line as it's expected that
|
||||
// the database object will not be in the expected (most current) state
|
||||
const { rerender, result } = renderHook(() => useMigrateDatabaseFields(props)); |
||||
rerender(); |
||||
}); |
||||
}); |
||||
@ -0,0 +1,63 @@ |
||||
import { useEffect } from 'react'; |
||||
|
||||
import { DataSourcePluginOptionsEditorProps } from '@grafana/data'; |
||||
import { logDebug } from '@grafana/runtime'; |
||||
|
||||
import { SQLConnectionDefaults } from '../../constants'; |
||||
import { SQLOptions } from '../../types'; |
||||
|
||||
/** |
||||
* 1. Moves the database field from the options object to jsonData.database and empties the database field. |
||||
* 2. If max open connections, max idle connections, and auto idle are all undefined set these to default values. |
||||
*/ |
||||
export function useMigrateDatabaseFields<T extends SQLOptions, S = {}>({ |
||||
onOptionsChange, |
||||
options, |
||||
}: DataSourcePluginOptionsEditorProps<T, S>) { |
||||
useEffect(() => { |
||||
const jsonData = options.jsonData; |
||||
let newOptions = { ...options }; |
||||
let optionsUpdated = false; |
||||
|
||||
// Migrate the database field from the column into the jsonData object
|
||||
if (options.database) { |
||||
logDebug(`Migrating from options.database with value ${options.database} for ${options.name}`); |
||||
newOptions.database = ''; |
||||
newOptions.jsonData = { ...jsonData, database: options.database }; |
||||
optionsUpdated = true; |
||||
} |
||||
|
||||
// Set default values for max open connections, max idle connection,
|
||||
// and auto idle if they're all undefined
|
||||
if ( |
||||
jsonData.maxOpenConns === undefined && |
||||
jsonData.maxIdleConns === undefined && |
||||
jsonData.maxIdleConnsAuto === undefined |
||||
) { |
||||
// It's expected that the default will be greater than 4
|
||||
const maxOpenConns = SQLConnectionDefaults.MAX_CONNS; |
||||
const maxIdleConns = maxOpenConns; |
||||
|
||||
logDebug( |
||||
`Setting default max open connections to ${maxOpenConns} and setting max idle connection to ${maxIdleConns}` |
||||
); |
||||
|
||||
// Spread from the jsonData in new options in case
|
||||
// the database field was migrated as well
|
||||
newOptions.jsonData = { |
||||
...newOptions.jsonData, |
||||
maxOpenConns: maxOpenConns, |
||||
maxIdleConns: maxIdleConns, |
||||
maxIdleConnsAuto: true, |
||||
}; |
||||
|
||||
// Make sure we issue an update if options changed
|
||||
optionsUpdated = true; |
||||
} |
||||
|
||||
// Only issue an update if we changed options
|
||||
if (optionsUpdated) { |
||||
onOptionsChange(newOptions); |
||||
} |
||||
}, [onOptionsChange, options]); |
||||
} |
||||
Loading…
Reference in new issue