Skip to content

Commit

Permalink
fix an issue with prettify query
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryk-dk committed Jun 20, 2024
1 parent bc562ef commit edf8444
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## tip

* BUGFIX: fix an issue with prettify query if the query includes Grafana variables in the lookbehind window. See [this issue](https://github.com/VictoriaMetrics/grafana-datasource/issues/166).

## [v0.8.2](https://github.com/VictoriaMetrics/grafana-datasource/releases/tag/v0.8.2)

* BUGFIX: fix parsing of label names with special characters for the query builder. See [this issue](https://github.com/VictoriaMetrics/grafana-datasource/issues/131#issuecomment-2105662179).
Expand Down
26 changes: 24 additions & 2 deletions src/components/PrettifyQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ enum ResponseStatus {
Error = 'error'
}

const GRAFANA_VARIABLES = [
"$__interval",
"$__interval_ms",
"$__range",
"$__range_s",
"$__range_ms",
"$__rate_interval",
]

const DEFAULT_LOOKBEHIND_WINDOW = "1i"

const PrettifyQuery: FC<Props> = ({
datasource,
query,
Expand All @@ -26,10 +37,21 @@ const PrettifyQuery: FC<Props> = ({
const handleClickPrettify = async () => {
setLoading(true)
try {
const response = await datasource.prettifyRequest(query.expr)
let { expr } = query;
const grafanaVariable = GRAFANA_VARIABLES.find(variable => expr.includes(variable));
if (grafanaVariable) {
const regex = new RegExp(`\\${grafanaVariable}`, 'g');
expr = expr.replace(regex, DEFAULT_LOOKBEHIND_WINDOW);
}
const response = await datasource.prettifyRequest(expr);
const { data, status } = response
if (data?.status === ResponseStatus.Success) {
onChange({ ...query, expr: data.query });
let { query } = data;
if (grafanaVariable) {
const regex = new RegExp(DEFAULT_LOOKBEHIND_WINDOW, 'g');
query = query.replace(regex, grafanaVariable);
}
onChange({ ...query, expr: query });
} else {
console.error(`Error requesting /prettify-query, status: ${status}`)
}
Expand Down

0 comments on commit edf8444

Please sign in to comment.