From a8877ba22deef270d0eb8df874739eb650bfd121 Mon Sep 17 00:00:00 2001 From: Cameron Waterman Date: Tue, 3 Oct 2023 11:52:35 -0500 Subject: [PATCH] feat(workspace): Workspace data source returns workspaces (#38) Co-authored-by: Melissa Hilliard <63749096+MelissaHilliard@users.noreply.github.com> --- src/core/DataSourceBase.ts | 2 +- .../workspace/WorkspaceDataSource.test.ts | 23 +++++++++++++ .../workspace/WorkspaceDataSource.ts | 32 +++++++++++-------- .../components/WorkspaceQueryEditor.tsx | 29 +++-------------- src/datasources/workspace/module.ts | 3 +- src/datasources/workspace/types.ts | 2 -- 6 files changed, 49 insertions(+), 42 deletions(-) diff --git a/src/core/DataSourceBase.ts b/src/core/DataSourceBase.ts index 1c18430..c7caf65 100644 --- a/src/core/DataSourceBase.ts +++ b/src/core/DataSourceBase.ts @@ -57,7 +57,7 @@ export abstract class DataSourceBase extends DataSourc static Workspaces: Workspace[]; - async getWorkspaces() { + async getWorkspaces(): Promise { if (DataSourceBase.Workspaces) { return DataSourceBase.Workspaces; } diff --git a/src/datasources/workspace/WorkspaceDataSource.test.ts b/src/datasources/workspace/WorkspaceDataSource.test.ts index 529a62e..2d73b34 100644 --- a/src/datasources/workspace/WorkspaceDataSource.test.ts +++ b/src/datasources/workspace/WorkspaceDataSource.test.ts @@ -4,10 +4,12 @@ import _ from 'lodash'; import { createFetchError, createFetchResponse, + getQueryBuilder, requestMatching, setupDataSource, } from 'test/fixtures'; import { WorkspaceDataSource } from './WorkspaceDataSource'; +import { WorkspaceQuery } from './types'; let ds: WorkspaceDataSource, backendSrv: MockProxy; @@ -15,6 +17,8 @@ beforeEach(() => { [ds, backendSrv] = setupDataSource(WorkspaceDataSource); }); +const buildQuery = getQueryBuilder()({}); + describe('testDatasource', () => { test('returns success', async () => { backendSrv.fetch @@ -34,3 +38,22 @@ describe('testDatasource', () => { await expect(ds.testDatasource()).rejects.toHaveProperty('status', 400); }); }); + +describe('queries', () => { + test('returns all workspaces', async () => { + const result = await ds.query(buildQuery({})); + + expect(result.data[0]).toHaveProperty('fields', [ + { name: 'name', values: ['Default workspace', 'Other workspace'] } + ]); + }); + + test('metricFindQuery returns all workspaces', async () => { + const result = await ds.metricFindQuery(); + + expect(result).toEqual([ + { value: '1', text: 'Default workspace' }, + { value: '2', text: 'Other workspace' } + ]); + }); +}); diff --git a/src/datasources/workspace/WorkspaceDataSource.ts b/src/datasources/workspace/WorkspaceDataSource.ts index 05367ed..9e6423a 100644 --- a/src/datasources/workspace/WorkspaceDataSource.ts +++ b/src/datasources/workspace/WorkspaceDataSource.ts @@ -1,6 +1,7 @@ -import { DataFrameDTO, DataQueryRequest, DataSourceInstanceSettings, FieldType } from '@grafana/data'; +import { DataFrameDTO, DataSourceInstanceSettings, MetricFindValue } from '@grafana/data'; import { BackendSrv, TemplateSrv, TestingStatus, getBackendSrv, getTemplateSrv } from '@grafana/runtime'; import { DataSourceBase } from 'core/DataSourceBase'; +import { Workspace } from 'core/types'; import { WorkspaceQuery } from './types'; export class WorkspaceDataSource extends DataSourceBase { @@ -14,21 +15,13 @@ export class WorkspaceDataSource extends DataSourceBase { baseUrl = this.instanceSettings.url + '/niuser/v1'; - defaultQuery = { - constant: 3.14, - }; - - async runQuery(query: WorkspaceQuery, { range }: DataQueryRequest): Promise { - return { - refId: query.refId, - fields: [ - { name: 'Time', values: [range.from.valueOf(), range.to.valueOf()], type: FieldType.time }, - { name: 'Value', values: [query.constant, query.constant], type: FieldType.number }, - ], - }; + defaultQuery = {}; + + async runQuery(_query: WorkspaceQuery): Promise { + return { fields: this.workspacesToFields( await this.getWorkspaces()) }; } - shouldRunQuery(query: WorkspaceQuery): boolean { + shouldRunQuery(): boolean { return true; } @@ -36,4 +29,15 @@ export class WorkspaceDataSource extends DataSourceBase { await this.get(this.baseUrl + '/workspaces'); return { status: 'success', message: 'Data source connected and authentication successful!' }; } + + async metricFindQuery(): Promise { + const workspaces = await this.getWorkspaces(); + return workspaces.map(ws => ({ text: ws.name, value: ws.id })); + } + + private workspacesToFields(workspaces: Workspace[]): Array<{ name: string, values: string[] }> { + return [ + { name: 'name', values: workspaces.map((workspace: Workspace) => workspace.name) } + ] + } } diff --git a/src/datasources/workspace/components/WorkspaceQueryEditor.tsx b/src/datasources/workspace/components/WorkspaceQueryEditor.tsx index 1b4dfb3..fb770ed 100644 --- a/src/datasources/workspace/components/WorkspaceQueryEditor.tsx +++ b/src/datasources/workspace/components/WorkspaceQueryEditor.tsx @@ -1,33 +1,14 @@ -import React, { ChangeEvent } from 'react'; -import { Input } from '@grafana/ui'; +import React, { useEffect } from 'react'; +import { WorkspaceQuery } from '../types'; import { QueryEditorProps } from '@grafana/data'; -import { InlineField } from 'core/components/InlineField'; import { WorkspaceDataSource } from '../WorkspaceDataSource'; -import { WorkspaceQuery } from '../types'; type Props = QueryEditorProps; -export function WorkspaceQueryEditor({ query, onChange, onRunQuery }: Props) { - const onQueryTextChange = (event: ChangeEvent) => { - onChange({ ...query, queryText: event.target.value }); - }; - - const onConstantChange = (event: ChangeEvent) => { - onChange({ ...query, constant: parseFloat(event.target.value) }); - // executes the query - onRunQuery(); - }; - - const { queryText, constant } = query; +export function WorkspaceQueryEditor({ onRunQuery }: Props) { + useEffect(onRunQuery, [onRunQuery]); return ( - <> - - - - - - - + This data source returns all SystemLink workspaces and does not include a query editor. ); } diff --git a/src/datasources/workspace/module.ts b/src/datasources/workspace/module.ts index 5cf4ed4..3349c17 100644 --- a/src/datasources/workspace/module.ts +++ b/src/datasources/workspace/module.ts @@ -5,4 +5,5 @@ import { HttpConfigEditor } from 'core/components/HttpConfigEditor'; export const plugin = new DataSourcePlugin(WorkspaceDataSource) .setConfigEditor(HttpConfigEditor) - .setQueryEditor(WorkspaceQueryEditor); + .setQueryEditor(WorkspaceQueryEditor) + .setVariableQueryEditor(() => null); diff --git a/src/datasources/workspace/types.ts b/src/datasources/workspace/types.ts index 3166561..17fbe8b 100644 --- a/src/datasources/workspace/types.ts +++ b/src/datasources/workspace/types.ts @@ -1,6 +1,4 @@ import { DataQuery } from '@grafana/schema' export interface WorkspaceQuery extends DataQuery { - queryText?: string; - constant: number; }