2020-03-25 06:25:39 -05:00
|
|
|
import React, { useState } from 'react';
|
2019-11-06 09:15:08 -06:00
|
|
|
import { css } from 'emotion';
|
2020-03-25 06:25:39 -05:00
|
|
|
import { Button, FormField, DataLinkInput, stylesFactory, Switch } from '@grafana/ui';
|
2020-02-11 06:48:36 -06:00
|
|
|
import { VariableSuggestion } from '@grafana/data';
|
2020-03-25 06:25:39 -05:00
|
|
|
import { DataSourceSelectItem } from '@grafana/data';
|
|
|
|
|
2019-11-06 09:15:08 -06:00
|
|
|
import { DerivedFieldConfig } from '../types';
|
2020-03-25 06:25:39 -05:00
|
|
|
import DataSourcePicker from 'app/core/components/Select/DataSourcePicker';
|
|
|
|
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
|
|
|
import { config } from 'app/core/config';
|
2019-11-06 09:15:08 -06:00
|
|
|
|
|
|
|
const getStyles = stylesFactory(() => ({
|
2020-03-25 06:25:39 -05:00
|
|
|
row: css`
|
2019-11-06 09:15:08 -06:00
|
|
|
display: flex;
|
2019-11-21 11:15:40 -06:00
|
|
|
align-items: baseline;
|
2019-11-06 09:15:08 -06:00
|
|
|
`,
|
|
|
|
nameField: css`
|
|
|
|
flex: 2;
|
|
|
|
`,
|
|
|
|
regexField: css`
|
|
|
|
flex: 3;
|
|
|
|
`,
|
|
|
|
}));
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
value: DerivedFieldConfig;
|
|
|
|
onChange: (value: DerivedFieldConfig) => void;
|
|
|
|
onDelete: () => void;
|
|
|
|
suggestions: VariableSuggestion[];
|
|
|
|
className?: string;
|
|
|
|
};
|
|
|
|
export const DerivedField = (props: Props) => {
|
|
|
|
const { value, onChange, onDelete, suggestions, className } = props;
|
|
|
|
const styles = getStyles();
|
2020-03-25 06:25:39 -05:00
|
|
|
const [hasIntenalLink, setHasInternalLink] = useState(!!value.datasourceName);
|
2019-11-06 09:15:08 -06:00
|
|
|
|
|
|
|
const handleChange = (field: keyof typeof value) => (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
onChange({
|
|
|
|
...value,
|
|
|
|
[field]: event.currentTarget.value,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={className}>
|
2020-03-25 06:25:39 -05:00
|
|
|
<div className={styles.row}>
|
2019-11-06 09:15:08 -06:00
|
|
|
<FormField
|
|
|
|
className={styles.nameField}
|
|
|
|
labelWidth={5}
|
|
|
|
// A bit of a hack to prevent using default value for the width from FormField
|
|
|
|
inputWidth={null}
|
|
|
|
label="Name"
|
|
|
|
type="text"
|
|
|
|
value={value.name}
|
|
|
|
onChange={handleChange('name')}
|
|
|
|
/>
|
|
|
|
<FormField
|
|
|
|
className={styles.regexField}
|
|
|
|
inputWidth={null}
|
|
|
|
label="Regex"
|
|
|
|
type="text"
|
|
|
|
value={value.matcherRegex}
|
|
|
|
onChange={handleChange('matcherRegex')}
|
|
|
|
tooltip={
|
|
|
|
'Use to parse and capture some part of the log message. You can use the captured groups in the template.'
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<Button
|
2020-03-26 05:50:27 -05:00
|
|
|
variant="destructive"
|
2019-11-06 09:15:08 -06:00
|
|
|
title="Remove field"
|
|
|
|
icon={'fa fa-times'}
|
|
|
|
onClick={event => {
|
|
|
|
event.preventDefault();
|
|
|
|
onDelete();
|
|
|
|
}}
|
2019-11-21 11:15:40 -06:00
|
|
|
className={css`
|
|
|
|
margin-left: 8px;
|
|
|
|
`}
|
2019-11-06 09:15:08 -06:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
label="URL"
|
|
|
|
labelWidth={5}
|
|
|
|
inputEl={
|
|
|
|
<DataLinkInput
|
|
|
|
placeholder={'http://example.com/${__value.raw}'}
|
|
|
|
value={value.url || ''}
|
|
|
|
onChange={newValue =>
|
|
|
|
onChange({
|
|
|
|
...value,
|
|
|
|
url: newValue,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
suggestions={suggestions}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
className={css`
|
|
|
|
width: 100%;
|
|
|
|
`}
|
|
|
|
/>
|
2020-03-25 06:25:39 -05:00
|
|
|
|
|
|
|
{config.featureToggles.tracingIntegration && (
|
|
|
|
<div className={styles.row}>
|
|
|
|
<Switch
|
|
|
|
label="Internal link"
|
|
|
|
checked={hasIntenalLink}
|
|
|
|
onChange={() => {
|
|
|
|
if (hasIntenalLink) {
|
|
|
|
onChange({
|
|
|
|
...value,
|
|
|
|
datasourceName: undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setHasInternalLink(!hasIntenalLink);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{hasIntenalLink && (
|
|
|
|
<DataSourceSection
|
|
|
|
onChange={datasourceName => {
|
|
|
|
onChange({
|
|
|
|
...value,
|
|
|
|
datasourceName,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
datasourceName={value.datasourceName}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2019-11-06 09:15:08 -06:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2020-03-25 06:25:39 -05:00
|
|
|
|
|
|
|
type DataSourceSectionProps = {
|
|
|
|
datasourceName?: string;
|
|
|
|
onChange: (name: string) => void;
|
|
|
|
};
|
|
|
|
const DataSourceSection = (props: DataSourceSectionProps) => {
|
|
|
|
const { datasourceName, onChange } = props;
|
|
|
|
const datasources: DataSourceSelectItem[] = getDatasourceSrv()
|
|
|
|
.getExternal()
|
|
|
|
.map(
|
|
|
|
(ds: any) =>
|
|
|
|
({
|
|
|
|
value: ds.name,
|
|
|
|
name: ds.name,
|
|
|
|
meta: ds.meta,
|
|
|
|
} as DataSourceSelectItem)
|
|
|
|
);
|
|
|
|
const selectedDatasource = datasourceName && datasources.find(d => d.name === datasourceName);
|
|
|
|
return (
|
|
|
|
<DataSourcePicker
|
|
|
|
onChange={newValue => {
|
|
|
|
onChange(newValue.name);
|
|
|
|
}}
|
|
|
|
datasources={datasources}
|
|
|
|
current={selectedDatasource}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|