The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/plugins/panel/live/LivePublish.tsx

71 lines
1.8 KiB

import { useMemo } from 'react';
import { LiveChannelAddress, isValidLiveChannelAddress } from '@grafana/data';
import { Trans } from '@grafana/i18n';
import { getBackendSrv, getGrafanaLiveSrv } from '@grafana/runtime';
import { CodeEditor, Button } from '@grafana/ui';
import { MessagePublishMode } from './types';
interface Props {
height: number;
addr?: LiveChannelAddress;
mode: MessagePublishMode;
body?: string | object;
onSave: (v: string | object) => void;
}
export function LivePublish({ height, mode, body, addr, onSave }: Props) {
const txt = useMemo(() => {
if (mode === MessagePublishMode.JSON) {
return body ? JSON.stringify(body, null, 2) : '{ }';
}
return body == null ? '' : `${body}`;
}, [mode, body]);
const doSave = (v: string) => {
if (mode === MessagePublishMode.JSON) {
onSave(JSON.parse(v));
} else {
onSave(v);
}
};
const onPublishClicked = async () => {
if (mode === MessagePublishMode.Influx) {
if (addr?.scope !== 'stream') {
alert('expected stream scope!');
return;
}
return getBackendSrv().post(`api/live/push/${addr.namespace}`, body);
}
if (!isValidLiveChannelAddress(addr)) {
alert('invalid address');
return;
}
const rsp = await getGrafanaLiveSrv().publish(addr, body);
console.log('onPublishClicked (response from publish)', rsp);
};
return (
<>
<CodeEditor
height={height - 32}
language={mode === MessagePublishMode.JSON ? 'json' : 'text'}
value={txt}
onBlur={doSave}
onSave={doSave}
showMiniMap={false}
showLineNumbers={true}
/>
<div style={{ height: 32 }}>
<Button onClick={onPublishClicked}>
<Trans i18nKey="live.live-publish.publish">Publish</Trans>
</Button>
</div>
</>
);
}