query.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { DataQuery, DataSourceRef } from '@grafana/data';
  2. export const getNextRefIdChar = (queries: DataQuery[]): string => {
  3. for (let num = 0; ; num++) {
  4. const refId = getRefId(num);
  5. if (!queries.some((query) => query.refId === refId)) {
  6. return refId;
  7. }
  8. }
  9. };
  10. export function addQuery(queries: DataQuery[], query?: Partial<DataQuery>, datasource?: DataSourceRef): DataQuery[] {
  11. const q = query || {};
  12. q.refId = getNextRefIdChar(queries);
  13. q.hide = false;
  14. if (!q.datasource && datasource) {
  15. q.datasource = datasource;
  16. }
  17. return [...queries, q as DataQuery];
  18. }
  19. export function isDataQuery(url: string): boolean {
  20. if (url.indexOf('api/datasources/proxy') !== -1 || url.indexOf('api/ds/query') !== -1) {
  21. return true;
  22. }
  23. return false;
  24. }
  25. export function isLocalUrl(url: string) {
  26. return !url.match(/^http/);
  27. }
  28. function getRefId(num: number): string {
  29. const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  30. if (num < letters.length) {
  31. return letters[num];
  32. } else {
  33. return getRefId(Math.floor(num / letters.length) - 1) + letters[num % letters.length];
  34. }
  35. }
  36. /**
  37. * Returns the input value for non empty string and undefined otherwise
  38. *
  39. * It is inadvisable to set a query param to an empty string as it will be added to the URL.
  40. * It is better to keep it undefined so the param won't be added to the URL at all.
  41. */
  42. export function getQueryParamValue(value: string | undefined | null): string | undefined {
  43. return value || undefined;
  44. }