From 7709929391337495040715ab02ecab98f1522493 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Mon, 24 Jun 2024 14:40:24 +0200 Subject: [PATCH 1/9] Fix showing of correct URL when using query history (#884) Signed-off-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> --- .changeset/tidy-moose-tell.md | 5 +++ package.json | 1 + src/app/utils.ts | 30 +++++++++++++++- src/editors/query/query.options.tsx | 8 ++--- src/editors/query/query.url.test.tsx | 33 +++++++++++++++++ src/editors/query/query.url.tsx | 34 ++++++++++++------ yarn.lock | 53 +++++++++++++--------------- 7 files changed, 120 insertions(+), 44 deletions(-) create mode 100644 .changeset/tidy-moose-tell.md create mode 100644 src/editors/query/query.url.test.tsx diff --git a/.changeset/tidy-moose-tell.md b/.changeset/tidy-moose-tell.md new file mode 100644 index 000000000..47307e6bd --- /dev/null +++ b/.changeset/tidy-moose-tell.md @@ -0,0 +1,5 @@ +--- +'grafana-infinity-datasource': patch +--- + +Fix showing of correct URL when using query history diff --git a/package.json b/package.json index 3d0702711..d0013e615 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "mathjs": "11.7.0", "react": "18.2.0", "react-dom": "18.2.0", + "react-use": "^17.5.0", "tslib": "2.5.3", "uql": "0.0.22", "xml2js": "^0.6.2" diff --git a/src/app/utils.ts b/src/app/utils.ts index 1482017e1..d159a4262 100644 --- a/src/app/utils.ts +++ b/src/app/utils.ts @@ -1,5 +1,15 @@ import { ArrayVector, MutableDataFrame, FieldType, DataFrame, Field, Labels, TableData } from '@grafana/data'; -import type { InfinityCSVQuery, InfinityGraphQLQuery, InfinityHTMLQuery, InfinityJSONQuery, InfinityQuery, InfinityQueryWithDataSource, InfinityXMLQuery } from './../types'; +import type { + InfinityCSVQuery, + InfinityGraphQLQuery, + InfinityHTMLQuery, + InfinityJSONQuery, + InfinityQuery, + InfinityQueryWithDataSource, + InfinityQueryWithURLSource, + InfinityXMLQuery, + InfinityQueryType, +} from './../types'; export const isTableData = (res: any): res is TableData => res && res.columns; export const isDataFrame = (res: any): res is DataFrame => res && res.fields; @@ -33,6 +43,24 @@ export const isDataQuery = (query: InfinityQuery): query is InfinityQueryWithDat return false; } }; + +// We have to have query: unknown here as InfinityQuery and InfinityQueryWithURLSource are not compatible according to TypeScript +export const isInfinityQueryWithUrlSource = (query: unknown): query is InfinityQueryWithURLSource => { + // We do a basic check to ensure that query is an object and has a type property + if (!query || typeof query !== 'object' || !('type' in query)) { + return false; + } + + // we check if the query is a data query or has suitable type + if (isDataQuery(query as InfinityQuery) || query.type === 'uql' || query.type === 'groq') { + // It needs to have a source property and it should be 'url' + if ('source' in query) { + return query.source === 'url'; + } + } + + return false; +}; export const toTimeSeriesLong = (data: DataFrame[]): DataFrame[] => { if (!Array.isArray(data) || data.length === 0) { return data; diff --git a/src/editors/query/query.options.tsx b/src/editors/query/query.options.tsx index c0e6d68e2..7000bb998 100644 --- a/src/editors/query/query.options.tsx +++ b/src/editors/query/query.options.tsx @@ -11,8 +11,8 @@ import { ParseTypeEditor } from './components/ParserType'; import { GoogleSheetsEditor } from './components/GoogleSheets'; import { URL, Method } from './query.url'; import { Datasource } from './../../datasource'; -import { isDataQuery } from './../../app/utils'; -import type { EditorMode, InfinityQuery } from '../../types'; +import { isDataQuery, isInfinityQueryWithUrlSource } from './../../app/utils'; +import type { EditorMode, InfinityQuery, InfinityQueryType, InfinityQueryWithURLSource } from '../../types'; export const BasicOptions = (props: { mode: EditorMode; @@ -59,10 +59,10 @@ export const BasicOptions = (props: { Github - {isDataQuery(query) && query.source === 'url' && ( + {isInfinityQueryWithUrlSource(query) && ( - + } onChange={onChange} onRunQuery={onRunQuery} onShowUrlOptions={onShowUrlOptions} />