-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add useDatasourceRequest hook * Prepare components release 3.5.0 * Fix lint errors * Update README.md --------- Co-authored-by: Mikhail Volkov <[email protected]>
- Loading branch information
1 parent
3f29c24
commit 5d8c53b
Showing
7 changed files
with
269 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
packages/components/src/hooks/useDatasourceRequest.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { LoadingState } from '@grafana/data'; | ||
import { getDataSourceSrv } from '@grafana/runtime'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { useDatasourceRequest } from './useDatasourceRequest'; | ||
|
||
/** | ||
* Response | ||
* | ||
* @param response | ||
*/ | ||
export const getResponse = (response: any) => | ||
new Observable((subscriber) => { | ||
subscriber.next(response); | ||
subscriber.complete(); | ||
}); | ||
|
||
/** | ||
* Mock @grafana/runtime | ||
*/ | ||
jest.mock('@grafana/runtime', () => ({ | ||
getDataSourceSrv: jest.fn(), | ||
})); | ||
|
||
describe('Use Datasource Request', () => { | ||
it('Should run query', async () => { | ||
const dataSourceSrv = { | ||
query: jest.fn(() => | ||
getResponse({ | ||
data: { | ||
message: 'hello', | ||
}, | ||
}) | ||
), | ||
}; | ||
const getDataSourceSrvMock = jest.fn(() => dataSourceSrv); | ||
|
||
jest.mocked(getDataSourceSrv).mockImplementationOnce( | ||
() => | ||
({ | ||
get: getDataSourceSrvMock, | ||
}) as any | ||
); | ||
const { result } = renderHook(() => useDatasourceRequest()); | ||
|
||
const response = await result.current({ | ||
query: { | ||
key1: 'value1', | ||
key2: 'value2', | ||
}, | ||
datasource: 'abc', | ||
replaceVariables: jest.fn((str) => str), | ||
payload: {}, | ||
}); | ||
|
||
/** | ||
* Should get datasource | ||
*/ | ||
expect(getDataSourceSrvMock).toHaveBeenCalledWith('abc'); | ||
|
||
/** | ||
* Should pass query | ||
*/ | ||
expect(dataSourceSrv.query).toHaveBeenCalledWith({ | ||
targets: [{ key1: 'value1', key2: 'value2' }], | ||
}); | ||
|
||
/** | ||
* Should return result | ||
*/ | ||
expect(response).toEqual({ | ||
data: { | ||
message: 'hello', | ||
}, | ||
}); | ||
}); | ||
|
||
it('Should handle promise result query', async () => { | ||
const dataSourceSrv = { | ||
query: jest.fn(() => | ||
Promise.resolve({ | ||
data: { | ||
message: 'hello', | ||
}, | ||
}) | ||
), | ||
}; | ||
const getDataSourceSrvMock = jest.fn(() => dataSourceSrv); | ||
|
||
jest.mocked(getDataSourceSrv).mockImplementationOnce( | ||
() => | ||
({ | ||
get: getDataSourceSrvMock, | ||
}) as any | ||
); | ||
const { result } = renderHook(() => useDatasourceRequest()); | ||
|
||
const response = await result.current({ | ||
query: { | ||
key1: 'value1', | ||
key2: 'value2', | ||
}, | ||
datasource: 'abc', | ||
replaceVariables: jest.fn((str) => str), | ||
payload: {}, | ||
}); | ||
|
||
/** | ||
* Should get datasource | ||
*/ | ||
expect(getDataSourceSrvMock).toHaveBeenCalledWith('abc'); | ||
|
||
/** | ||
* Should pass query | ||
*/ | ||
expect(dataSourceSrv.query).toHaveBeenCalledWith({ | ||
targets: [{ key1: 'value1', key2: 'value2' }], | ||
}); | ||
|
||
/** | ||
* Should return result | ||
*/ | ||
expect(response).toEqual({ | ||
data: { | ||
message: 'hello', | ||
}, | ||
}); | ||
}); | ||
|
||
it('Should handle promise error', async () => { | ||
const dataSourceSrv = { | ||
query: jest.fn(() => | ||
Promise.resolve({ | ||
state: LoadingState.Error, | ||
}) | ||
), | ||
}; | ||
const getDataSourceSrvMock = jest.fn(() => dataSourceSrv); | ||
|
||
jest.mocked(getDataSourceSrv).mockImplementationOnce( | ||
() => | ||
({ | ||
get: getDataSourceSrvMock, | ||
}) as any | ||
); | ||
const { result } = renderHook(() => useDatasourceRequest()); | ||
|
||
const response = await result | ||
.current({ | ||
query: { | ||
key1: 'value1', | ||
key2: 'value2', | ||
}, | ||
datasource: 'abc', | ||
replaceVariables: jest.fn((str) => str), | ||
payload: {}, | ||
}) | ||
.catch(() => false); | ||
|
||
expect(response).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { DataQueryResponse, InterpolateFunction, LoadingState } from '@grafana/data'; | ||
import { getDataSourceSrv } from '@grafana/runtime'; | ||
import { useCallback } from 'react'; | ||
import { lastValueFrom } from 'rxjs'; | ||
|
||
/** | ||
* Data Source Response Error | ||
*/ | ||
export class DatasourceResponseError { | ||
public readonly message: string; | ||
|
||
constructor( | ||
public readonly error: unknown, | ||
target: string | ||
) { | ||
if (error && typeof error === 'object') { | ||
if ('message' in error && typeof error.message === 'string') { | ||
this.message = error.message; | ||
} else { | ||
this.message = JSON.stringify(error, null, 2); | ||
} | ||
} else { | ||
this.message = 'Unknown Error'; | ||
} | ||
|
||
this.message += `\nRequest: ${target}`; | ||
} | ||
} | ||
|
||
/** | ||
* Use Data Source Request | ||
*/ | ||
export const useDatasourceRequest = () => { | ||
return useCallback( | ||
async ({ | ||
query, | ||
datasource, | ||
replaceVariables, | ||
payload, | ||
}: { | ||
query: unknown; | ||
datasource: string; | ||
replaceVariables: InterpolateFunction; | ||
payload: unknown; | ||
}): Promise<DataQueryResponse> => { | ||
const ds = await getDataSourceSrv().get(datasource); | ||
|
||
/** | ||
* Replace Variables | ||
*/ | ||
const targetJson = replaceVariables(JSON.stringify(query, null, 2), { | ||
payload: { | ||
value: payload, | ||
}, | ||
}); | ||
|
||
const target = JSON.parse(targetJson); | ||
|
||
try { | ||
/** | ||
* Response | ||
*/ | ||
const response = ds.query({ | ||
targets: [target], | ||
} as never); | ||
|
||
const handleResponse = (response: DataQueryResponse) => { | ||
if (response.state && response.state === LoadingState.Error) { | ||
throw response?.errors?.[0] || response; | ||
} | ||
return response; | ||
}; | ||
|
||
/** | ||
* Handle as promise | ||
*/ | ||
if (response instanceof Promise) { | ||
return await response.then(handleResponse); | ||
} | ||
|
||
/** | ||
* Handle as observable | ||
*/ | ||
return await lastValueFrom(response).then(handleResponse); | ||
} catch (error) { | ||
throw new DatasourceResponseError(error, targetJson); | ||
} | ||
}, | ||
[] | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters