Skip to content

Commit

Permalink
feat(workspace): Workspace data source returns workspaces (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: Melissa Hilliard <[email protected]>
  • Loading branch information
cameronwaterman and MelissaHilliard authored Oct 3, 2023
1 parent ef51b3a commit a8877ba
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/core/DataSourceBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export abstract class DataSourceBase<TQuery extends DataQuery> extends DataSourc

static Workspaces: Workspace[];

async getWorkspaces() {
async getWorkspaces(): Promise<Workspace[]> {
if (DataSourceBase.Workspaces) {
return DataSourceBase.Workspaces;
}
Expand Down
23 changes: 23 additions & 0 deletions src/datasources/workspace/WorkspaceDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ 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<BackendSrv>;

beforeEach(() => {
[ds, backendSrv] = setupDataSource(WorkspaceDataSource);
});

const buildQuery = getQueryBuilder<WorkspaceQuery>()({});

describe('testDatasource', () => {
test('returns success', async () => {
backendSrv.fetch
Expand All @@ -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' }
]);
});
});
32 changes: 18 additions & 14 deletions src/datasources/workspace/WorkspaceDataSource.ts
Original file line number Diff line number Diff line change
@@ -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<WorkspaceQuery> {
Expand All @@ -14,26 +15,29 @@ export class WorkspaceDataSource extends DataSourceBase<WorkspaceQuery> {

baseUrl = this.instanceSettings.url + '/niuser/v1';

defaultQuery = {
constant: 3.14,
};

async runQuery(query: WorkspaceQuery, { range }: DataQueryRequest): Promise<DataFrameDTO> {
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<DataFrameDTO> {
return { fields: this.workspacesToFields( await this.getWorkspaces()) };
}

shouldRunQuery(query: WorkspaceQuery): boolean {
shouldRunQuery(): boolean {
return true;
}

async testDatasource(): Promise<TestingStatus> {
await this.get(this.baseUrl + '/workspaces');
return { status: 'success', message: 'Data source connected and authentication successful!' };
}

async metricFindQuery(): Promise<MetricFindValue[]> {
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) }
]
}
}
29 changes: 5 additions & 24 deletions src/datasources/workspace/components/WorkspaceQueryEditor.tsx
Original file line number Diff line number Diff line change
@@ -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<WorkspaceDataSource, WorkspaceQuery>;

export function WorkspaceQueryEditor({ query, onChange, onRunQuery }: Props) {
const onQueryTextChange = (event: ChangeEvent<HTMLInputElement>) => {
onChange({ ...query, queryText: event.target.value });
};

const onConstantChange = (event: ChangeEvent<HTMLInputElement>) => {
onChange({ ...query, constant: parseFloat(event.target.value) });
// executes the query
onRunQuery();
};

const { queryText, constant } = query;
export function WorkspaceQueryEditor({ onRunQuery }: Props) {
useEffect(onRunQuery, [onRunQuery]);

return (
<>
<InlineField label="Constant">
<Input onChange={onConstantChange} value={constant} width={8} type="number" step="0.1" />
</InlineField>
<InlineField label="Query Text" labelWidth={16} tooltip="Not used yet">
<Input onChange={onQueryTextChange} value={queryText || ''} />
</InlineField>
</>
<span>This data source returns all SystemLink workspaces and does not include a query editor.</span>
);
}
3 changes: 2 additions & 1 deletion src/datasources/workspace/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import { HttpConfigEditor } from 'core/components/HttpConfigEditor';

export const plugin = new DataSourcePlugin(WorkspaceDataSource)
.setConfigEditor(HttpConfigEditor)
.setQueryEditor(WorkspaceQueryEditor);
.setQueryEditor(WorkspaceQueryEditor)
.setVariableQueryEditor(() => null);
2 changes: 0 additions & 2 deletions src/datasources/workspace/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { DataQuery } from '@grafana/schema'

export interface WorkspaceQuery extends DataQuery {
queryText?: string;
constant: number;
}

0 comments on commit a8877ba

Please sign in to comment.