grafana/public/app/features/query-library/api/query.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

Explore: List query templates (#86897) * Create basic feature toggle * Rename context to reflect it contains query history and query library * Update icons and variants * Rename hooks * Update tests * Fix mock * Add tracking * Turn button into a toggle * Make dropdown active as well This is required to have better UI and an indication of selected state in split view * Update Query Library icon This is to make it consistent with the toolbar button * Hide query history button when query library is available This is to avoid confusing UX with 2 button triggering the drawer but with slightly different behavior * Make the drawer bigger for query library To avoid confusion for current users and test it internally a bit more it's behind a feature toggle. Bigger drawer may obstruct the view and add more friction in the UX. * Fix tests The test was failing because queryLibraryAvailable was set to true for tests. This change makes it more explicit what use case is being tested * Remove active state underline from the dropdown * Add basic types and api methods This is just moved from the app. To be cleaned up and refactored later. * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Add basic table for query templates * Add sorting * Style cells * Style table cells * Allow closing Query Library drawer from the toolbar * Remove Private Query toggle It will be moved to the kebab * Add empty state * Remove variables detection for now Just to simplify the PR, it's not needed for Explore yet. * Simplify getting useDatasource.tsx * Rename cell * Move QueryTemplatesTable to a separate folder * Use RTK Query to get list of query templates * Clean up query templates table * Simplify useDatasource hook * Add a test * Retrigger the build * Remove unused code * Small clean up * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit aa9da6e442b8508df0e234a6a093f088908958af. * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit 2e68a62ab6b5b59b3a593b53fe265a80db7dc707. * Add @reduxjs/toolkit as peer dependency * Add @reduxjs/toolkit as peer dependecy * Move reactjs/toolkit to dev dependecies * Minor clean up and use react-redux as a peer dependency * Move query library code to core features * Update code owners * Update export * Update exports * Use Redux store instead of APIProvider * Await for query templates to show during the test * Add more explicit docs that the feature is experimental --------- Co-authored-by: Kristina Durivage <kristina.durivage@grafana.com>
2024-05-14 16:05:39 +08:00
import { BaseQueryFn } from '@reduxjs/toolkit/query/react';
import { lastValueFrom } from 'rxjs';
import { BackendSrvRequest, getBackendSrv, isFetchError } from '@grafana/runtime/src/services/backendSrv';
Explore: List query templates (#86897) * Create basic feature toggle * Rename context to reflect it contains query history and query library * Update icons and variants * Rename hooks * Update tests * Fix mock * Add tracking * Turn button into a toggle * Make dropdown active as well This is required to have better UI and an indication of selected state in split view * Update Query Library icon This is to make it consistent with the toolbar button * Hide query history button when query library is available This is to avoid confusing UX with 2 button triggering the drawer but with slightly different behavior * Make the drawer bigger for query library To avoid confusion for current users and test it internally a bit more it's behind a feature toggle. Bigger drawer may obstruct the view and add more friction in the UX. * Fix tests The test was failing because queryLibraryAvailable was set to true for tests. This change makes it more explicit what use case is being tested * Remove active state underline from the dropdown * Add basic types and api methods This is just moved from the app. To be cleaned up and refactored later. * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Add basic table for query templates * Add sorting * Style cells * Style table cells * Allow closing Query Library drawer from the toolbar * Remove Private Query toggle It will be moved to the kebab * Add empty state * Remove variables detection for now Just to simplify the PR, it's not needed for Explore yet. * Simplify getting useDatasource.tsx * Rename cell * Move QueryTemplatesTable to a separate folder * Use RTK Query to get list of query templates * Clean up query templates table * Simplify useDatasource hook * Add a test * Retrigger the build * Remove unused code * Small clean up * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit aa9da6e442b8508df0e234a6a093f088908958af. * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit 2e68a62ab6b5b59b3a593b53fe265a80db7dc707. * Add @reduxjs/toolkit as peer dependency * Add @reduxjs/toolkit as peer dependecy * Move reactjs/toolkit to dev dependecies * Minor clean up and use react-redux as a peer dependency * Move query library code to core features * Update code owners * Update export * Update exports * Use Redux store instead of APIProvider * Await for query templates to show during the test * Add more explicit docs that the feature is experimental --------- Co-authored-by: Kristina Durivage <kristina.durivage@grafana.com>
2024-05-14 16:05:39 +08:00
import { DataQuerySpecResponse } from './types';
/**
* @alpha
*/
export const API_VERSION = 'peakq.grafana.app/v0alpha1';
/**
* @alpha
*/
export enum QueryTemplateKinds {
QueryTemplate = 'QueryTemplate',
}
Explore: List query templates (#86897) * Create basic feature toggle * Rename context to reflect it contains query history and query library * Update icons and variants * Rename hooks * Update tests * Fix mock * Add tracking * Turn button into a toggle * Make dropdown active as well This is required to have better UI and an indication of selected state in split view * Update Query Library icon This is to make it consistent with the toolbar button * Hide query history button when query library is available This is to avoid confusing UX with 2 button triggering the drawer but with slightly different behavior * Make the drawer bigger for query library To avoid confusion for current users and test it internally a bit more it's behind a feature toggle. Bigger drawer may obstruct the view and add more friction in the UX. * Fix tests The test was failing because queryLibraryAvailable was set to true for tests. This change makes it more explicit what use case is being tested * Remove active state underline from the dropdown * Add basic types and api methods This is just moved from the app. To be cleaned up and refactored later. * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Add basic table for query templates * Add sorting * Style cells * Style table cells * Allow closing Query Library drawer from the toolbar * Remove Private Query toggle It will be moved to the kebab * Add empty state * Remove variables detection for now Just to simplify the PR, it's not needed for Explore yet. * Simplify getting useDatasource.tsx * Rename cell * Move QueryTemplatesTable to a separate folder * Use RTK Query to get list of query templates * Clean up query templates table * Simplify useDatasource hook * Add a test * Retrigger the build * Remove unused code * Small clean up * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit aa9da6e442b8508df0e234a6a093f088908958af. * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit 2e68a62ab6b5b59b3a593b53fe265a80db7dc707. * Add @reduxjs/toolkit as peer dependency * Add @reduxjs/toolkit as peer dependecy * Move reactjs/toolkit to dev dependecies * Minor clean up and use react-redux as a peer dependency * Move query library code to core features * Update code owners * Update export * Update exports * Use Redux store instead of APIProvider * Await for query templates to show during the test * Add more explicit docs that the feature is experimental --------- Co-authored-by: Kristina Durivage <kristina.durivage@grafana.com>
2024-05-14 16:05:39 +08:00
/**
* Query Library is an experimental feature. API (including the URL path) will likely change.
*
* @alpha
*/
export const BASE_URL = `/apis/${API_VERSION}/namespaces/default/querytemplates/`;
Explore: List query templates (#86897) * Create basic feature toggle * Rename context to reflect it contains query history and query library * Update icons and variants * Rename hooks * Update tests * Fix mock * Add tracking * Turn button into a toggle * Make dropdown active as well This is required to have better UI and an indication of selected state in split view * Update Query Library icon This is to make it consistent with the toolbar button * Hide query history button when query library is available This is to avoid confusing UX with 2 button triggering the drawer but with slightly different behavior * Make the drawer bigger for query library To avoid confusion for current users and test it internally a bit more it's behind a feature toggle. Bigger drawer may obstruct the view and add more friction in the UX. * Fix tests The test was failing because queryLibraryAvailable was set to true for tests. This change makes it more explicit what use case is being tested * Remove active state underline from the dropdown * Add basic types and api methods This is just moved from the app. To be cleaned up and refactored later. * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Add basic table for query templates * Add sorting * Style cells * Style table cells * Allow closing Query Library drawer from the toolbar * Remove Private Query toggle It will be moved to the kebab * Add empty state * Remove variables detection for now Just to simplify the PR, it's not needed for Explore yet. * Simplify getting useDatasource.tsx * Rename cell * Move QueryTemplatesTable to a separate folder * Use RTK Query to get list of query templates * Clean up query templates table * Simplify useDatasource hook * Add a test * Retrigger the build * Remove unused code * Small clean up * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit aa9da6e442b8508df0e234a6a093f088908958af. * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit 2e68a62ab6b5b59b3a593b53fe265a80db7dc707. * Add @reduxjs/toolkit as peer dependency * Add @reduxjs/toolkit as peer dependecy * Move reactjs/toolkit to dev dependecies * Minor clean up and use react-redux as a peer dependency * Move query library code to core features * Update code owners * Update export * Update exports * Use Redux store instead of APIProvider * Await for query templates to show during the test * Add more explicit docs that the feature is experimental --------- Co-authored-by: Kristina Durivage <kristina.durivage@grafana.com>
2024-05-14 16:05:39 +08:00
/**
* TODO: similar code is duplicated in many places. To be unified in #86960
*/
export const baseQuery: BaseQueryFn<Pick<BackendSrvRequest, 'data' | 'method'>, DataQuerySpecResponse, Error> = async (
requestOptions
) => {
Explore: List query templates (#86897) * Create basic feature toggle * Rename context to reflect it contains query history and query library * Update icons and variants * Rename hooks * Update tests * Fix mock * Add tracking * Turn button into a toggle * Make dropdown active as well This is required to have better UI and an indication of selected state in split view * Update Query Library icon This is to make it consistent with the toolbar button * Hide query history button when query library is available This is to avoid confusing UX with 2 button triggering the drawer but with slightly different behavior * Make the drawer bigger for query library To avoid confusion for current users and test it internally a bit more it's behind a feature toggle. Bigger drawer may obstruct the view and add more friction in the UX. * Fix tests The test was failing because queryLibraryAvailable was set to true for tests. This change makes it more explicit what use case is being tested * Remove active state underline from the dropdown * Add basic types and api methods This is just moved from the app. To be cleaned up and refactored later. * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Add basic table for query templates * Add sorting * Style cells * Style table cells * Allow closing Query Library drawer from the toolbar * Remove Private Query toggle It will be moved to the kebab * Add empty state * Remove variables detection for now Just to simplify the PR, it's not needed for Explore yet. * Simplify getting useDatasource.tsx * Rename cell * Move QueryTemplatesTable to a separate folder * Use RTK Query to get list of query templates * Clean up query templates table * Simplify useDatasource hook * Add a test * Retrigger the build * Remove unused code * Small clean up * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit aa9da6e442b8508df0e234a6a093f088908958af. * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit 2e68a62ab6b5b59b3a593b53fe265a80db7dc707. * Add @reduxjs/toolkit as peer dependency * Add @reduxjs/toolkit as peer dependecy * Move reactjs/toolkit to dev dependecies * Minor clean up and use react-redux as a peer dependency * Move query library code to core features * Update code owners * Update export * Update exports * Use Redux store instead of APIProvider * Await for query templates to show during the test * Add more explicit docs that the feature is experimental --------- Co-authored-by: Kristina Durivage <kristina.durivage@grafana.com>
2024-05-14 16:05:39 +08:00
try {
const responseObservable = getBackendSrv().fetch<DataQuerySpecResponse>({
url: BASE_URL,
showErrorAlert: true,
method: requestOptions.method || 'GET',
data: requestOptions.data,
Explore: List query templates (#86897) * Create basic feature toggle * Rename context to reflect it contains query history and query library * Update icons and variants * Rename hooks * Update tests * Fix mock * Add tracking * Turn button into a toggle * Make dropdown active as well This is required to have better UI and an indication of selected state in split view * Update Query Library icon This is to make it consistent with the toolbar button * Hide query history button when query library is available This is to avoid confusing UX with 2 button triggering the drawer but with slightly different behavior * Make the drawer bigger for query library To avoid confusion for current users and test it internally a bit more it's behind a feature toggle. Bigger drawer may obstruct the view and add more friction in the UX. * Fix tests The test was failing because queryLibraryAvailable was set to true for tests. This change makes it more explicit what use case is being tested * Remove active state underline from the dropdown * Add basic types and api methods This is just moved from the app. To be cleaned up and refactored later. * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Move API utils from Query Library app to Grafana packages * Add basic table for query templates * Add sorting * Style cells * Style table cells * Allow closing Query Library drawer from the toolbar * Remove Private Query toggle It will be moved to the kebab * Add empty state * Remove variables detection for now Just to simplify the PR, it's not needed for Explore yet. * Simplify getting useDatasource.tsx * Rename cell * Move QueryTemplatesTable to a separate folder * Use RTK Query to get list of query templates * Clean up query templates table * Simplify useDatasource hook * Add a test * Retrigger the build * Remove unused code * Small clean up * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit aa9da6e442b8508df0e234a6a093f088908958af. * Update import * Add reduxjs/toolkit as a peer dependecy * Revert "Add reduxjs/toolkit as a peer dependecy" This reverts commit 2e68a62ab6b5b59b3a593b53fe265a80db7dc707. * Add @reduxjs/toolkit as peer dependency * Add @reduxjs/toolkit as peer dependecy * Move reactjs/toolkit to dev dependecies * Minor clean up and use react-redux as a peer dependency * Move query library code to core features * Update code owners * Update export * Update exports * Use Redux store instead of APIProvider * Await for query templates to show during the test * Add more explicit docs that the feature is experimental --------- Co-authored-by: Kristina Durivage <kristina.durivage@grafana.com>
2024-05-14 16:05:39 +08:00
});
return await lastValueFrom(responseObservable);
} catch (error) {
if (isFetchError(error)) {
return { error: new Error(error.data.message) };
} else if (error instanceof Error) {
return { error };
} else {
return { error: new Error('Unknown error') };
}
}
};