-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(asset): add asset variable query with query builder (#90)
Co-authored-by: NI\akerezsi <[email protected]>
- Loading branch information
1 parent
a315a29
commit 4de79ff
Showing
11 changed files
with
160 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/datasources/asset/components/variable-editor/AssetVariableQueryEditor.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { screen, waitFor } from '@testing-library/react'; | ||
import { AssetQuery } from '../../types/types'; | ||
import { SystemMetadata } from '../../../system/types' | ||
import { AssetVariableQueryEditor } from './AssetVariableQueryEditor'; | ||
import { Workspace } from 'core/types'; | ||
import { setupRenderer } from 'test/fixtures'; | ||
import { ListAssetsDataSource } from '../../data-sources/list-assets/ListAssetsDataSource'; | ||
import { AssetDataSource } from 'datasources/asset/AssetDataSource'; | ||
|
||
const fakeSystems: SystemMetadata[] = [ | ||
{ | ||
id: '1', | ||
state: 'CONNECTED', | ||
workspace: '1', | ||
}, | ||
{ | ||
id: '2', | ||
state: 'CONNECTED', | ||
workspace: '2', | ||
}, | ||
]; | ||
|
||
const fakeWorkspaces: Workspace[] = [ | ||
{ | ||
id: '1', | ||
name: 'workspace1', | ||
default: false, | ||
enabled: true | ||
}, | ||
{ | ||
id: '2', | ||
name: 'workspace2', | ||
default: false, | ||
enabled: true | ||
}, | ||
]; | ||
|
||
class FakeAssetsSource extends ListAssetsDataSource { | ||
getWorkspaces(): Promise<Workspace[]> { | ||
return Promise.resolve(fakeWorkspaces); | ||
} | ||
querySystems(filter?: string, projection?: string[]): Promise<SystemMetadata[]> { | ||
return Promise.resolve(fakeSystems); | ||
} | ||
} | ||
|
||
class FakeAssetDataSource extends AssetDataSource { | ||
getListAssetsSource(): ListAssetsDataSource { | ||
return new FakeAssetsSource(this.instanceSettings, this.backendSrv, this.templateSrv); | ||
} | ||
} | ||
|
||
const render = setupRenderer(AssetVariableQueryEditor, FakeAssetDataSource, () => {}); | ||
|
||
it('renders the variable query builder', async () => { | ||
render({ filter: "" } as AssetQuery); | ||
|
||
await waitFor(() => expect(screen.getAllByText('Property').length).toBe(1)); | ||
await waitFor(() => expect(screen.getAllByText('Operator').length).toBe(1)); | ||
await waitFor(() => expect(screen.getAllByText('Value').length).toBe(1)); | ||
}); |
47 changes: 47 additions & 0 deletions
47
src/datasources/asset/components/variable-editor/AssetVariableQueryEditor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import { QueryEditorProps } from "@grafana/data"; | ||
import { AssetDataSourceOptions, AssetQuery } from '../../../asset/types/types'; | ||
import { AssetDataSource } from '../../AssetDataSource' | ||
import { FloatingError } from '../../../../core/errors'; | ||
import { AssetQueryBuilder } from '../editors/list-assets/query-builder/AssetQueryBuilder'; | ||
import { Workspace } from '../../../../core/types'; | ||
import { SystemMetadata } from '../../../system/types'; | ||
import { AssetVariableQuery } from '../../../asset/types/AssetVariableQuery.types'; | ||
|
||
type Props = QueryEditorProps<AssetDataSource, AssetQuery, AssetDataSourceOptions>; | ||
|
||
export function AssetVariableQueryEditor({ datasource, query, onChange }: Props) { | ||
const [workspaces, setWorkspaces] = useState<Workspace[]>([]); | ||
const [systems, setSystems] = useState<SystemMetadata[]>([]); | ||
const [areDependenciesLoaded, setAreDependenciesLoaded] = useState<boolean>(false); | ||
const assetVariableQuery = query as AssetVariableQuery; | ||
const assetListDatasource = useRef(datasource.getListAssetsSource()); | ||
|
||
useEffect(() => { | ||
Promise.all([assetListDatasource.current.areSystemsLoaded$, assetListDatasource.current.areWorkspacesLoaded$]).then(() => { | ||
setWorkspaces(Array.from(assetListDatasource.current.workspacesCache.values())); | ||
setSystems(Array.from(assetListDatasource.current.systemAliasCache.values())); | ||
setAreDependenciesLoaded(true); | ||
}); | ||
}, [datasource]); | ||
|
||
function onParameterChange(ev: CustomEvent) { | ||
if (assetVariableQuery?.filter !== ev.detail.linq) { | ||
onChange({ ...assetVariableQuery, filter: ev.detail.linq }); | ||
} | ||
} | ||
|
||
return ( | ||
<div style={{ width: "525px" }}> | ||
<AssetQueryBuilder | ||
filter={assetVariableQuery.filter} | ||
workspaces={workspaces} | ||
systems={systems} | ||
globalVariableOptions={assetListDatasource.current.globalVariableOptions} | ||
areDependenciesLoaded={areDependenciesLoaded} | ||
onChange={(event: any) => onParameterChange(event)} | ||
></AssetQueryBuilder> | ||
<FloatingError message={assetListDatasource.current.error} /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ export enum AllFieldNames { | |
BUS_TYPE = 'BusType', | ||
ASSET_TYPE = 'AssetType', | ||
} | ||
|
||
export const QUERY_LIMIT = 1000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { DataQuery } from '@grafana/schema' | ||
|
||
export interface AssetVariableQuery extends DataQuery { | ||
filter: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters