diff --git a/.gitignore b/.gitignore index df67443..bce809d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ yarn-error.log* .pnpm-debug.log* node_modules/ +# OS +.DS_Store # Runtime data pids @@ -32,4 +34,4 @@ e2e-results/ # Editor .idea -.eslintcache \ No newline at end of file +.eslintcache diff --git a/src/datasources/tag/README.md b/src/datasources/tag/README.md new file mode 100644 index 0000000..a552bc1 --- /dev/null +++ b/src/datasources/tag/README.md @@ -0,0 +1,4 @@ +# SystemLink Tag data source + +This is a plugin for the Tag Service. It allows you to: +- Populate a Stat, Gauge, table, or other panel with the current value, min, max, average values, of a tag. The tag datatype may be numeric or string. diff --git a/src/datasources/tag/TagDataSource.ts b/src/datasources/tag/TagDataSource.ts new file mode 100644 index 0000000..ac2d005 --- /dev/null +++ b/src/datasources/tag/TagDataSource.ts @@ -0,0 +1,46 @@ +import { + DataQueryRequest, + DataQueryResponse, + DataSourceApi, + DataSourceInstanceSettings, + MutableDataFrame, + FieldType, +} from '@grafana/data'; + +import { TestingStatus, getBackendSrv } from '@grafana/runtime'; + +import { TagQuery } from './types'; + +export class TagDataSource extends DataSourceApi { + baseUrl: string; + constructor(private instanceSettings: DataSourceInstanceSettings) { + super(instanceSettings); + // TODO: set base path of the service + this.baseUrl = this.instanceSettings.url + '/nifoo/v2'; + } + + async query(options: DataQueryRequest): Promise { + const { range } = options; + const from = range!.from.valueOf(); + const to = range!.to.valueOf(); + + // Return a constant for each query. + const data = options.targets.map((target) => { + return new MutableDataFrame({ + refId: target.refId, + fields: [ + { name: 'Time', values: [from, to], type: FieldType.time }, + { name: 'Value', values: [target.constant, target.constant], type: FieldType.number }, + ], + }); + }); + + return { data }; + } + + async testDatasource(): Promise { + // TODO: Implement a health and authentication check + await getBackendSrv().get(this.baseUrl + '/bar'); + return { status: 'success', message: 'Data source connected and authentication successful!' }; + } +} diff --git a/src/datasources/tag/components/TagQueryEditor.tsx b/src/datasources/tag/components/TagQueryEditor.tsx new file mode 100644 index 0000000..a32aeab --- /dev/null +++ b/src/datasources/tag/components/TagQueryEditor.tsx @@ -0,0 +1,32 @@ +import React, { ChangeEvent } from 'react'; +import { InlineField, Input } from '@grafana/ui'; +import { QueryEditorProps } from '@grafana/data'; +import { TagDataSource } from '../TagDataSource'; +import { TagQuery } from '../types'; + +type Props = QueryEditorProps; + +export function TagQueryEditor({ 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; + + return ( +
+ + + + + + +
+ ); +} diff --git a/src/datasources/tag/img/logo-ni.svg b/src/datasources/tag/img/logo-ni.svg new file mode 100644 index 0000000..76f8758 --- /dev/null +++ b/src/datasources/tag/img/logo-ni.svg @@ -0,0 +1,11 @@ + + + 9B441361-9BF9-4E57-A772-8A5F7846467E + + + + + + \ No newline at end of file diff --git a/src/datasources/tag/module.ts b/src/datasources/tag/module.ts new file mode 100644 index 0000000..f59b1e7 --- /dev/null +++ b/src/datasources/tag/module.ts @@ -0,0 +1,8 @@ +import { DataSourcePlugin } from '@grafana/data'; +import { TagDataSource } from './TagDataSource'; +import { TagQueryEditor } from './components/TagQueryEditor'; +import { HttpConfigEditor } from 'core/HttpConfigEditor'; + +export const plugin = new DataSourcePlugin(TagDataSource) + .setConfigEditor(HttpConfigEditor) + .setQueryEditor(TagQueryEditor); diff --git a/src/datasources/tag/plugin.json b/src/datasources/tag/plugin.json new file mode 100644 index 0000000..dd199d8 --- /dev/null +++ b/src/datasources/tag/plugin.json @@ -0,0 +1,15 @@ +{ + "type": "datasource", + "name": "SystemLink Tags", + "id": "ni-sltag-datasource", + "metrics": true, + "info": { + "author": { + "name": "NI" + }, + "logos": { + "small": "img/logo-ni.svg", + "large": "img/logo-ni.svg" + } + } +} diff --git a/src/datasources/tag/types.ts b/src/datasources/tag/types.ts new file mode 100644 index 0000000..e23c2c5 --- /dev/null +++ b/src/datasources/tag/types.ts @@ -0,0 +1,6 @@ +import { DataQuery } from '@grafana/schema' + +export interface TagQuery extends DataQuery { + queryText?: string; + constant: number; +}