import defaults from 'lodash/defaults'; import React, { PureComponent } from 'react'; import { InlineField, Select, FeatureInfoBox } from '@grafana/ui'; import { QueryEditorProps, SelectableValue, LiveChannelScope, FeatureState } from '@grafana/data'; import { getLiveMeasurements, LiveMeasurements } from '@grafana/runtime'; import { GrafanaDatasource } from '../datasource'; import { defaultQuery, GrafanaQuery, GrafanaQueryType } from '../types'; type Props = QueryEditorProps; const labelWidth = 12; export class QueryEditor extends PureComponent { queryTypes: Array> = [ { label: 'Random Walk', value: GrafanaQueryType.RandomWalk, description: 'Random signal within the selected time range', }, { label: 'Live Measurements', value: GrafanaQueryType.LiveMeasurements, description: 'Stream real-time measurements from Grafana', }, ]; onQueryTypeChange = (sel: SelectableValue) => { const { onChange, query, onRunQuery } = this.props; onChange({ ...query, queryType: sel.value! }); onRunQuery(); }; onChannelChange = (sel: SelectableValue) => { const { onChange, query, onRunQuery } = this.props; onChange({ ...query, channel: sel?.value }); onRunQuery(); }; onMeasurementNameChanged = (sel: SelectableValue) => { const { onChange, query, onRunQuery } = this.props; onChange({ ...query, measurements: { ...query.measurements, name: sel?.value, }, }); onRunQuery(); }; renderMeasurementsQuery() { let { channel, measurements } = this.props.query; const channels: Array> = []; let currentChannel = channels.find((c) => c.value === channel); if (channel && !currentChannel) { currentChannel = { value: channel, label: channel, description: `Connected to ${channel}`, }; channels.push(currentChannel); } if (!measurements) { measurements = {}; } const names: Array> = [ { value: '', label: 'All measurements', description: 'Show every measurement streamed to this channel' }, ]; let info: LiveMeasurements | undefined = undefined; if (channel) { info = getLiveMeasurements({ scope: LiveChannelScope.Grafana, namespace: 'measurements', path: channel, }); let foundName = false; if (info) { for (const name of info.getDistinctNames()) { names.push({ value: name, label: name, }); if (name === measurements.name) { foundName = true; } } } else { console.log('NO INFO for', channel); } if (measurements.name && !foundName) { names.push({ label: measurements.name, value: measurements.name, description: `Frames with name ${measurements.name}`, }); } } return ( <>
v.value === measurements?.name) || names[0]} onChange={this.onMeasurementNameChanged} allowCustomValue={true} backspaceRemovesValue={true} placeholder="Filter by name" isClearable={true} noOptionsMessage="Filter by name" formatCreateLabel={(input: string) => `Show: ${input}`} isSearchable={true} />
)}

This supports real-time event streams in Grafana core. This feature is under heavy development. Expect the interfaces and structures to change as this becomes more production ready.

); } render() { const query = defaults(this.props.query, defaultQuery); return ( <>