Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMM-12377 Custom Labels #682

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { css } from '@emotion/css';

export const styles = {
QueryBuilder: css`
min-width: 500px;

& > div {
background-color: transparent;
padding: 0;
padding-bottom: 10px;
}

& > div:first-child label {
display: none;
}

div[data-testid='prometheus-dimensions-filter-item'] > div > div:nth-child(2) {
background-color: transparent;
pointer-events: none;
border: none;
cursor: initial;
padding: 0 10px;

* {
margin: 0;
padding: 0;
}

/* caret */
& > div > div:nth-child(2) {
display: none;
}
}
`,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { FC, useMemo, useState } from 'react';
import { useField } from 'react-final-form';

import { getDataSourceSrv } from '@grafana/runtime';
import { PrometheusDatasource } from 'app/plugins/datasource/prometheus/datasource';
import { promQueryModeller } from 'app/plugins/datasource/prometheus/querybuilder/PromQueryModeller';
import { PromQueryBuilder } from 'app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilder';
import { buildVisualQueryFromString } from 'app/plugins/datasource/prometheus/querybuilder/parsing';
import { PromVisualQuery } from 'app/plugins/datasource/prometheus/querybuilder/types';
import { PromQuery } from 'app/plugins/datasource/prometheus/types';

import { styles } from './CustomLabels.styles';

const CustomLabels: FC = () => {
const { input } = useField('custom_labels');
const source = getDataSourceSrv().getInstanceSettings();
const datasource = source ? new PrometheusDatasource(source) : undefined;
const [query, setQuery] = useState<PromQuery>({
refId: '',
expr: input.value,
});
const visualQuery = useMemo(() => buildVisualQueryFromString(query.expr).query, [query.expr]);

if (!datasource) {
return null;
}

const handleQueryChange = (visualQuery: PromVisualQuery) => {
const expr = promQueryModeller.renderQuery(visualQuery);
const labels = visualQuery.labels;

input.onChange(labels.map(({ label, value }) => label + ':' + value).join('\n'));

setQuery((prev) => ({ ...prev, expr: expr || '' }));
};

return (
<div className={styles.QueryBuilder}>
<PromQueryBuilder
datasource={datasource}
onChange={handleQueryChange}
onRunQuery={console.log}
query={visualQuery}
showExplain={false}
hideMetric
hideOperations
/>
</div>
);
};

export default CustomLabels;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TextInputField } from 'app/percona/shared/components/Form/TextInput';
import { TextareaInputField } from 'app/percona/shared/components/Form/TextareaInput';
import Validators from 'app/percona/shared/helpers/validators';

import CustomLabels from '../CustomLabels/CustomLabels';
import { Messages } from '../FormParts.messages';
import { getStyles } from '../FormParts.styles';

Expand Down Expand Up @@ -47,6 +48,7 @@ export const LabelsFormPart: FC = () => {
placeholder={Messages.form.placeholders.labels.customLabels}
validators={customLabelsValidators}
/>
<CustomLabels />
</div>
);
};
Loading