@perses-dev/prometheus-plugin 0.49.0-rc.1 → 0.50.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/components/PromQLEditor.js +72 -99
- package/dist/cjs/components/parse.js +2 -2
- package/dist/cjs/plugins/prometheus-datasource.js +3 -3
- package/dist/cjs/plugins/prometheus-time-series-query/PrometheusTimeSeriesQueryEditor.js +1 -1
- package/dist/cjs/plugins/prometheus-time-series-query/get-time-series-data.js +4 -9
- package/dist/cjs/plugins/prometheus-time-series-query/replace-prom-builtin-variables.js +33 -0
- package/dist/components/PromQLEditor.d.ts +2 -1
- package/dist/components/PromQLEditor.d.ts.map +1 -1
- package/dist/components/PromQLEditor.js +72 -99
- package/dist/components/PromQLEditor.js.map +1 -1
- package/dist/components/parse.d.ts +2 -1
- package/dist/components/parse.d.ts.map +1 -1
- package/dist/components/parse.js +2 -2
- package/dist/components/parse.js.map +1 -1
- package/dist/components/promql/utils.d.ts.map +1 -1
- package/dist/components/promql/utils.js.map +1 -1
- package/dist/model/prometheus-client.d.ts +4 -12
- package/dist/model/prometheus-client.d.ts.map +1 -1
- package/dist/model/prometheus-client.js.map +1 -1
- package/dist/model/prometheus-selectors.d.ts.map +1 -1
- package/dist/model/prometheus-selectors.js.map +1 -1
- package/dist/model/time.d.ts.map +1 -1
- package/dist/model/time.js.map +1 -1
- package/dist/plugins/MatcherEditor.d.ts +2 -1
- package/dist/plugins/MatcherEditor.d.ts.map +1 -1
- package/dist/plugins/MatcherEditor.js.map +1 -1
- package/dist/plugins/PrometheusDatasourceEditor.d.ts +2 -1
- package/dist/plugins/PrometheusDatasourceEditor.d.ts.map +1 -1
- package/dist/plugins/PrometheusDatasourceEditor.js.map +1 -1
- package/dist/plugins/prometheus-datasource.d.ts.map +1 -1
- package/dist/plugins/prometheus-datasource.js +3 -3
- package/dist/plugins/prometheus-datasource.js.map +1 -1
- package/dist/plugins/prometheus-time-series-query/PrometheusTimeSeriesQueryEditor.d.ts +2 -1
- package/dist/plugins/prometheus-time-series-query/PrometheusTimeSeriesQueryEditor.d.ts.map +1 -1
- package/dist/plugins/prometheus-time-series-query/PrometheusTimeSeriesQueryEditor.js +1 -1
- package/dist/plugins/prometheus-time-series-query/PrometheusTimeSeriesQueryEditor.js.map +1 -1
- package/dist/plugins/prometheus-time-series-query/get-time-series-data.d.ts.map +1 -1
- package/dist/plugins/prometheus-time-series-query/get-time-series-data.js +6 -11
- package/dist/plugins/prometheus-time-series-query/get-time-series-data.js.map +1 -1
- package/dist/plugins/prometheus-time-series-query/query-editor-model.d.ts +1 -1
- package/dist/plugins/prometheus-time-series-query/query-editor-model.d.ts.map +1 -1
- package/dist/plugins/prometheus-time-series-query/query-editor-model.js.map +1 -1
- package/dist/plugins/prometheus-time-series-query/replace-prom-builtin-variables.d.ts +2 -0
- package/dist/plugins/prometheus-time-series-query/replace-prom-builtin-variables.d.ts.map +1 -0
- package/dist/plugins/prometheus-time-series-query/replace-prom-builtin-variables.js +33 -0
- package/dist/plugins/prometheus-time-series-query/replace-prom-builtin-variables.js.map +1 -0
- package/dist/plugins/prometheus-variables.d.ts.map +1 -1
- package/dist/plugins/prometheus-variables.js.map +1 -1
- package/dist/plugins/variable.d.ts.map +1 -1
- package/dist/plugins/variable.js.map +1 -1
- package/dist/utils/utils.d.ts.map +1 -1
- package/dist/utils/utils.js.map +1 -1
- package/package.json +6 -5
|
@@ -30,6 +30,7 @@ const _react = require("react");
|
|
|
30
30
|
const _components = require("@perses-dev/components");
|
|
31
31
|
const _Close = /*#__PURE__*/ _interop_require_default(require("mdi-material-ui/Close"));
|
|
32
32
|
const _pluginsystem = require("@perses-dev/plugin-system");
|
|
33
|
+
const _replaceprombuiltinvariables = require("../plugins/prometheus-time-series-query/replace-prom-builtin-variables");
|
|
33
34
|
const _parse = require("./parse");
|
|
34
35
|
const _TreeNode = /*#__PURE__*/ _interop_require_default(require("./TreeNode"));
|
|
35
36
|
function _interop_require_default(obj) {
|
|
@@ -40,33 +41,12 @@ function _interop_require_default(obj) {
|
|
|
40
41
|
const treeViewStr = 'Tree View';
|
|
41
42
|
const treeViewOpenStr = 'Open ' + treeViewStr;
|
|
42
43
|
const treeViewCloseStr = 'Close ' + treeViewStr;
|
|
43
|
-
|
|
44
|
-
// TODO: This should be removed when properly tackling query error at query editor level (https://github.com/perses/perses/issues/1419)
|
|
45
|
-
// Here this was kinda a quick-win to do it like this with the implementation of the Tree view.
|
|
46
|
-
// Once #1419 will be tackled, any error reported by the query to the parse endpoint here should be treated the same (= display
|
|
47
|
-
// the error in the debug/tree view & always let the buttons accessible to show/hide it).
|
|
48
|
-
function processError(error) {
|
|
49
|
-
// Specific errors that user should be able to hide
|
|
50
|
-
const apiNotAvailableError = '404 page not found';
|
|
51
|
-
const apiNotAvailableErrorRephrased = `${treeViewStr} is available only for datasources whose APIs comply with Prometheus 3.0 specifications`;
|
|
52
|
-
const blockedByProxyError = 'forbidden access: you are not allowed to use this endpoint "/api/v1/parse_query" with the HTTP method POST';
|
|
53
|
-
const blockedByProxyErrorRephrased = `Your datasource configuration is blocking the ${treeViewStr} feature: the datasource should allow POST requests to "/api/v1/parse_query"`;
|
|
44
|
+
function getErrMessage(error) {
|
|
54
45
|
let errorMessage = 'An unknown error occurred';
|
|
55
|
-
let isGenericError = false;
|
|
56
46
|
if (error && error instanceof Error) {
|
|
57
47
|
errorMessage = error.message.trim();
|
|
58
|
-
if (errorMessage === apiNotAvailableError) {
|
|
59
|
-
errorMessage = apiNotAvailableErrorRephrased;
|
|
60
|
-
} else if (errorMessage === blockedByProxyError) {
|
|
61
|
-
errorMessage = blockedByProxyErrorRephrased;
|
|
62
|
-
} else {
|
|
63
|
-
isGenericError = true;
|
|
64
|
-
}
|
|
65
48
|
}
|
|
66
|
-
return
|
|
67
|
-
errorMessage,
|
|
68
|
-
isGenericError
|
|
69
|
-
};
|
|
49
|
+
return errorMessage;
|
|
70
50
|
}
|
|
71
51
|
function PromQLEditor({ completeConfig, datasource, ...rest }) {
|
|
72
52
|
const theme = (0, _material.useTheme)();
|
|
@@ -77,9 +57,16 @@ function PromQLEditor({ completeConfig, datasource, ...rest }) {
|
|
|
77
57
|
}, [
|
|
78
58
|
completeConfig
|
|
79
59
|
]);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
60
|
+
let queryExpr = (0, _pluginsystem.useReplaceVariablesInString)(rest.value);
|
|
61
|
+
if (queryExpr) {
|
|
62
|
+
// TODO placeholder values for steps to be replaced with actual values
|
|
63
|
+
// Looks like providing proper values involves some refactoring: currently we'd need to rely on the timeseries query context,
|
|
64
|
+
// but these step values are actually computed independently / before the queries are getting fired, so it's useless to fire
|
|
65
|
+
// queries here, so maybe we should extract this part to independant hook(s), to be reused here?
|
|
66
|
+
queryExpr = (0, _replaceprombuiltinvariables.replacePromBuiltinVariables)(queryExpr, 12345, 12345);
|
|
67
|
+
}
|
|
68
|
+
const { data: parseQueryResponse, isLoading, error } = (0, _parse.useParseQuery)(queryExpr !== null && queryExpr !== void 0 ? queryExpr : '', datasource, isTreeViewVisible);
|
|
69
|
+
const errorMessage = (0, _react.useMemo)(()=>getErrMessage(error), [
|
|
83
70
|
error
|
|
84
71
|
]);
|
|
85
72
|
const handleShowTreeView = ()=>{
|
|
@@ -125,84 +112,70 @@ function PromQLEditor({ completeConfig, datasource, ...rest }) {
|
|
|
125
112
|
],
|
|
126
113
|
placeholder: "Example: sum(rate(http_requests_total[5m]))"
|
|
127
114
|
}),
|
|
128
|
-
queryExpr && /*#__PURE__*/ (0, _jsxruntime.
|
|
129
|
-
children:
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
children: [
|
|
143
|
-
/*#__PURE__*/ (0, _jsxruntime.jsx)(_material.Tooltip, {
|
|
144
|
-
title: isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr,
|
|
145
|
-
children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_material.IconButton, {
|
|
146
|
-
"aria-label": isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr,
|
|
147
|
-
onClick: handleShowTreeView,
|
|
115
|
+
queryExpr && /*#__PURE__*/ (0, _jsxruntime.jsxs)(_jsxruntime.Fragment, {
|
|
116
|
+
children: [
|
|
117
|
+
/*#__PURE__*/ (0, _jsxruntime.jsx)(_material.Tooltip, {
|
|
118
|
+
title: isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr,
|
|
119
|
+
children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_material.IconButton, {
|
|
120
|
+
"aria-label": isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr,
|
|
121
|
+
onClick: handleShowTreeView,
|
|
122
|
+
sx: {
|
|
123
|
+
position: 'absolute',
|
|
124
|
+
right: '5px',
|
|
125
|
+
top: '5px'
|
|
126
|
+
},
|
|
127
|
+
size: "small",
|
|
128
|
+
children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_FileTree.default, {
|
|
148
129
|
sx: {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
top: '5px'
|
|
152
|
-
},
|
|
153
|
-
size: "small",
|
|
154
|
-
children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_FileTree.default, {
|
|
155
|
-
sx: {
|
|
156
|
-
fontSize: '18px'
|
|
157
|
-
}
|
|
158
|
-
})
|
|
130
|
+
fontSize: '18px'
|
|
131
|
+
}
|
|
159
132
|
})
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
133
|
+
})
|
|
134
|
+
}),
|
|
135
|
+
isTreeViewVisible && /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
|
|
136
|
+
style: {
|
|
137
|
+
border: `1px solid ${theme.palette.divider}`,
|
|
138
|
+
position: 'relative'
|
|
139
|
+
},
|
|
140
|
+
children: [
|
|
141
|
+
/*#__PURE__*/ (0, _jsxruntime.jsx)(_material.Tooltip, {
|
|
142
|
+
title: treeViewCloseStr,
|
|
143
|
+
children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_material.IconButton, {
|
|
144
|
+
"aria-label": treeViewCloseStr,
|
|
145
|
+
onClick: ()=>setTreeViewVisible(false),
|
|
146
|
+
sx: {
|
|
147
|
+
position: 'absolute',
|
|
148
|
+
top: '5px',
|
|
149
|
+
right: '5px'
|
|
150
|
+
},
|
|
151
|
+
size: "small",
|
|
152
|
+
children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_Close.default, {
|
|
172
153
|
sx: {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
right: '5px'
|
|
176
|
-
},
|
|
177
|
-
size: "small",
|
|
178
|
-
children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_Close.default, {
|
|
179
|
-
sx: {
|
|
180
|
-
fontSize: '18px'
|
|
181
|
-
}
|
|
182
|
-
})
|
|
154
|
+
fontSize: '18px'
|
|
155
|
+
}
|
|
183
156
|
})
|
|
184
|
-
}),
|
|
185
|
-
error ? // Here the user is able to hide the error alert
|
|
186
|
-
/*#__PURE__*/ (0, _jsxruntime.jsx)(_components.ErrorAlert, {
|
|
187
|
-
error: {
|
|
188
|
-
name: `${treeViewStr} not available`,
|
|
189
|
-
message: errorMessage
|
|
190
|
-
}
|
|
191
|
-
}) : /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
|
|
192
|
-
style: {
|
|
193
|
-
padding: `${theme.spacing(1.5)} ${theme.spacing(1.5)} 0 ${theme.spacing(1.5)}`,
|
|
194
|
-
overflowX: 'auto',
|
|
195
|
-
backgroundColor: theme.palette.background.default
|
|
196
|
-
},
|
|
197
|
-
children: isLoading ? /*#__PURE__*/ (0, _jsxruntime.jsx)(_material.CircularProgress, {}) : (parseQueryResponse === null || parseQueryResponse === void 0 ? void 0 : parseQueryResponse.data) ? /*#__PURE__*/ (0, _jsxruntime.jsx)(_TreeNode.default, {
|
|
198
|
-
node: parseQueryResponse.data,
|
|
199
|
-
reverse: false
|
|
200
|
-
}) : null
|
|
201
157
|
})
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
158
|
+
}),
|
|
159
|
+
error ? // Here the user is able to hide the error alert
|
|
160
|
+
/*#__PURE__*/ (0, _jsxruntime.jsx)(_components.ErrorAlert, {
|
|
161
|
+
error: {
|
|
162
|
+
name: `${treeViewStr} not available`,
|
|
163
|
+
message: errorMessage
|
|
164
|
+
}
|
|
165
|
+
}) : /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
|
|
166
|
+
style: {
|
|
167
|
+
padding: `${theme.spacing(1.5)} ${theme.spacing(1.5)} 0 ${theme.spacing(1.5)}`,
|
|
168
|
+
overflowX: 'auto',
|
|
169
|
+
backgroundColor: theme.palette.background.default
|
|
170
|
+
},
|
|
171
|
+
children: isLoading ? /*#__PURE__*/ (0, _jsxruntime.jsx)(_material.CircularProgress, {}) : (parseQueryResponse === null || parseQueryResponse === void 0 ? void 0 : parseQueryResponse.data) ? /*#__PURE__*/ (0, _jsxruntime.jsx)(_TreeNode.default, {
|
|
172
|
+
node: parseQueryResponse.data,
|
|
173
|
+
reverse: false
|
|
174
|
+
}) : null
|
|
175
|
+
})
|
|
176
|
+
]
|
|
177
|
+
})
|
|
178
|
+
]
|
|
206
179
|
})
|
|
207
180
|
]
|
|
208
181
|
});
|
|
@@ -22,10 +22,10 @@ Object.defineProperty(exports, "useParseQuery", {
|
|
|
22
22
|
});
|
|
23
23
|
const _pluginsystem = require("@perses-dev/plugin-system");
|
|
24
24
|
const _reactquery = require("@tanstack/react-query");
|
|
25
|
-
function useParseQuery(content, datasource) {
|
|
25
|
+
function useParseQuery(content, datasource, enabled) {
|
|
26
26
|
const { data: client } = (0, _pluginsystem.useDatasourceClient)(datasource);
|
|
27
27
|
return (0, _reactquery.useQuery)({
|
|
28
|
-
enabled: !!client,
|
|
28
|
+
enabled: !!client && enabled,
|
|
29
29
|
queryKey: [
|
|
30
30
|
'parseQuery',
|
|
31
31
|
content,
|
|
@@ -82,7 +82,7 @@ const getBuiltinVariableDefinitions = ()=>{
|
|
|
82
82
|
source: 'Prometheus',
|
|
83
83
|
display: {
|
|
84
84
|
name: '__interval',
|
|
85
|
-
description: '
|
|
85
|
+
description: 'For dynamic queries that adapt across different time ranges, use $__interval instead of hardcoded intervals. It represents the actual spacing between data points: it’s calculated based on the current time range and the panel pixel width (taking the "Min step" as a lower bound).',
|
|
86
86
|
hidden: true
|
|
87
87
|
}
|
|
88
88
|
}
|
|
@@ -95,7 +95,7 @@ const getBuiltinVariableDefinitions = ()=>{
|
|
|
95
95
|
source: 'Prometheus',
|
|
96
96
|
display: {
|
|
97
97
|
name: '__interval_ms',
|
|
98
|
-
description: '
|
|
98
|
+
description: 'Same as $__interval but in milliseconds.',
|
|
99
99
|
hidden: true
|
|
100
100
|
}
|
|
101
101
|
}
|
|
@@ -108,7 +108,7 @@ const getBuiltinVariableDefinitions = ()=>{
|
|
|
108
108
|
source: 'Prometheus',
|
|
109
109
|
display: {
|
|
110
110
|
name: '__rate_interval',
|
|
111
|
-
description:
|
|
111
|
+
description: 'Use this one rather than $__interval as the range parameter of functions like rate, increase, etc. With such function it is advised to choose a range that is at least 4x the scrape interval (this is to allow for various races, and to be resilient to a failed scrape). $__rate_interval provides that, as it is defined as `max($__interval + Min Step, 4 * Min Step)`, where Min Step value should represent the scrape interval of the metrics.',
|
|
112
112
|
hidden: true
|
|
113
113
|
}
|
|
114
114
|
}
|
|
@@ -99,7 +99,7 @@ function PrometheusTimeSeriesQueryEditor(props) {
|
|
|
99
99
|
/*#__PURE__*/ (0, _jsxruntime.jsx)(_material.TextField, {
|
|
100
100
|
label: "Min Step",
|
|
101
101
|
placeholder: minStepPlaceholder,
|
|
102
|
-
helperText: "
|
|
102
|
+
helperText: "Lower bound for the step. If not provided, the scrape interval of the datasource is used.",
|
|
103
103
|
value: minStep,
|
|
104
104
|
onChange: (e)=>handleMinStepChange(e.target.value),
|
|
105
105
|
onBlur: handleMinStepBlur,
|
|
@@ -26,6 +26,7 @@ const _datefns = require("date-fns");
|
|
|
26
26
|
const _model = require("../../model");
|
|
27
27
|
const _utils = require("../../utils");
|
|
28
28
|
const _types = require("../types");
|
|
29
|
+
const _replaceprombuiltinvariables = require("./replace-prom-builtin-variables");
|
|
29
30
|
const getTimeSeriesData = async (spec, context)=>{
|
|
30
31
|
if (spec.query === undefined || spec.query === null || spec.query === '') {
|
|
31
32
|
// Do not make a request to the backend, instead return an empty TimeSeriesData
|
|
@@ -53,15 +54,9 @@ const getTimeSeriesData = async (spec, context)=>{
|
|
|
53
54
|
start = alignedStart;
|
|
54
55
|
end = alignedEnd;
|
|
55
56
|
// Replace variable placeholders in PromQL query
|
|
56
|
-
const intervalMs = step * 1000;
|
|
57
|
-
|
|
58
|
-
query = (0,
|
|
59
|
-
const scrapeIntervalMs = minStep * 1000;
|
|
60
|
-
// The $__rate_interval variable is meant to be used in the rate function.
|
|
61
|
-
// It is defined as max($__interval + Scrape interval, 4 * Scrape interval), where Scrape interval is the Min step setting (a setting per PromQL query),
|
|
62
|
-
// if any is set, and otherwise the Scrape interval as set in the Prometheus datasource
|
|
63
|
-
const rateIntervalMs = Math.max(intervalMs + scrapeIntervalMs, 4 * scrapeIntervalMs);
|
|
64
|
-
query = (0, _pluginsystem.replaceVariable)(query, '__rate_interval', (0, _core.formatDuration)((0, _core.msToPrometheusDuration)(rateIntervalMs)));
|
|
57
|
+
const intervalMs = step * 1000;
|
|
58
|
+
const minStepMs = minStep * 1000;
|
|
59
|
+
let query = (0, _replaceprombuiltinvariables.replacePromBuiltinVariables)(spec.query, minStepMs, intervalMs);
|
|
65
60
|
query = (0, _pluginsystem.replaceVariables)(query, context.variableState);
|
|
66
61
|
let seriesNameFormat = spec.seriesNameFormat;
|
|
67
62
|
// if series name format is defined, replace variable placeholders in series name format
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Copyright 2024 The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
"use strict";
|
|
14
|
+
Object.defineProperty(exports, "__esModule", {
|
|
15
|
+
value: true
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(exports, "replacePromBuiltinVariables", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function() {
|
|
20
|
+
return replacePromBuiltinVariables;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
const _pluginsystem = require("@perses-dev/plugin-system");
|
|
24
|
+
const _core = require("@perses-dev/core");
|
|
25
|
+
function replacePromBuiltinVariables(query, minStepMs, intervalMs) {
|
|
26
|
+
let updatedQuery = (0, _pluginsystem.replaceVariable)(query, '__interval_ms', intervalMs.toString());
|
|
27
|
+
updatedQuery = (0, _pluginsystem.replaceVariable)(updatedQuery, '__interval', (0, _core.formatDuration)((0, _core.msToPrometheusDuration)(intervalMs)));
|
|
28
|
+
// The $__rate_interval variable is meant to be used with the rate() promQL function.
|
|
29
|
+
// It is defined as max($__interval + Min step, 4 * Min step)
|
|
30
|
+
const rateIntervalMs = Math.max(intervalMs + minStepMs, 4 * minStepMs);
|
|
31
|
+
updatedQuery = (0, _pluginsystem.replaceVariable)(updatedQuery, '__rate_interval', (0, _core.formatDuration)((0, _core.msToPrometheusDuration)(rateIntervalMs)));
|
|
32
|
+
return updatedQuery;
|
|
33
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { ReactCodeMirrorProps } from '@uiw/react-codemirror';
|
|
2
2
|
import { CompleteConfiguration } from '@prometheus-io/codemirror-promql';
|
|
3
|
+
import { ReactElement } from 'react';
|
|
3
4
|
import { PrometheusDatasourceSelector } from '../model';
|
|
4
5
|
export type PromQLEditorProps = {
|
|
5
6
|
completeConfig: CompleteConfiguration;
|
|
6
7
|
datasource: PrometheusDatasourceSelector;
|
|
7
8
|
} & Omit<ReactCodeMirrorProps, 'theme' | 'extensions'>;
|
|
8
|
-
export declare function PromQLEditor({ completeConfig, datasource, ...rest }: PromQLEditorProps):
|
|
9
|
+
export declare function PromQLEditor({ completeConfig, datasource, ...rest }: PromQLEditorProps): ReactElement;
|
|
9
10
|
//# sourceMappingURL=PromQLEditor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PromQLEditor.d.ts","sourceRoot":"","sources":["../../src/components/PromQLEditor.tsx"],"names":[],"mappings":"AAaA,OAAmB,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AACzE,OAAO,EAAmB,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"PromQLEditor.d.ts","sourceRoot":"","sources":["../../src/components/PromQLEditor.tsx"],"names":[],"mappings":"AAaA,OAAmB,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AACzE,OAAO,EAAmB,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAI1F,OAAO,EAAE,YAAY,EAAqB,MAAM,OAAO,CAAC;AAIxD,OAAO,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAiBxD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,cAAc,EAAE,qBAAqB,CAAC;IACtC,UAAU,EAAE,4BAA4B,CAAC;CAC1C,GAAG,IAAI,CAAC,oBAAoB,EAAE,OAAO,GAAG,YAAY,CAAC,CAAC;AAEvD,wBAAgB,YAAY,CAAC,EAAE,cAAc,EAAE,UAAU,EAAE,GAAG,IAAI,EAAE,EAAE,iBAAiB,GAAG,YAAY,CAmHrG"}
|
|
@@ -20,38 +20,18 @@ import { useMemo, useState } from 'react';
|
|
|
20
20
|
import { ErrorAlert } from '@perses-dev/components';
|
|
21
21
|
import CloseIcon from 'mdi-material-ui/Close';
|
|
22
22
|
import { useReplaceVariablesInString } from '@perses-dev/plugin-system';
|
|
23
|
+
import { replacePromBuiltinVariables } from '../plugins/prometheus-time-series-query/replace-prom-builtin-variables';
|
|
23
24
|
import { useParseQuery } from './parse';
|
|
24
25
|
import TreeNode from './TreeNode';
|
|
25
26
|
const treeViewStr = 'Tree View';
|
|
26
27
|
const treeViewOpenStr = 'Open ' + treeViewStr;
|
|
27
28
|
const treeViewCloseStr = 'Close ' + treeViewStr;
|
|
28
|
-
|
|
29
|
-
// TODO: This should be removed when properly tackling query error at query editor level (https://github.com/perses/perses/issues/1419)
|
|
30
|
-
// Here this was kinda a quick-win to do it like this with the implementation of the Tree view.
|
|
31
|
-
// Once #1419 will be tackled, any error reported by the query to the parse endpoint here should be treated the same (= display
|
|
32
|
-
// the error in the debug/tree view & always let the buttons accessible to show/hide it).
|
|
33
|
-
function processError(error) {
|
|
34
|
-
// Specific errors that user should be able to hide
|
|
35
|
-
const apiNotAvailableError = '404 page not found';
|
|
36
|
-
const apiNotAvailableErrorRephrased = `${treeViewStr} is available only for datasources whose APIs comply with Prometheus 3.0 specifications`;
|
|
37
|
-
const blockedByProxyError = 'forbidden access: you are not allowed to use this endpoint "/api/v1/parse_query" with the HTTP method POST';
|
|
38
|
-
const blockedByProxyErrorRephrased = `Your datasource configuration is blocking the ${treeViewStr} feature: the datasource should allow POST requests to "/api/v1/parse_query"`;
|
|
29
|
+
function getErrMessage(error) {
|
|
39
30
|
let errorMessage = 'An unknown error occurred';
|
|
40
|
-
let isGenericError = false;
|
|
41
31
|
if (error && error instanceof Error) {
|
|
42
32
|
errorMessage = error.message.trim();
|
|
43
|
-
if (errorMessage === apiNotAvailableError) {
|
|
44
|
-
errorMessage = apiNotAvailableErrorRephrased;
|
|
45
|
-
} else if (errorMessage === blockedByProxyError) {
|
|
46
|
-
errorMessage = blockedByProxyErrorRephrased;
|
|
47
|
-
} else {
|
|
48
|
-
isGenericError = true;
|
|
49
|
-
}
|
|
50
33
|
}
|
|
51
|
-
return
|
|
52
|
-
errorMessage,
|
|
53
|
-
isGenericError
|
|
54
|
-
};
|
|
34
|
+
return errorMessage;
|
|
55
35
|
}
|
|
56
36
|
export function PromQLEditor({ completeConfig, datasource, ...rest }) {
|
|
57
37
|
const theme = useTheme();
|
|
@@ -62,9 +42,16 @@ export function PromQLEditor({ completeConfig, datasource, ...rest }) {
|
|
|
62
42
|
}, [
|
|
63
43
|
completeConfig
|
|
64
44
|
]);
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
45
|
+
let queryExpr = useReplaceVariablesInString(rest.value);
|
|
46
|
+
if (queryExpr) {
|
|
47
|
+
// TODO placeholder values for steps to be replaced with actual values
|
|
48
|
+
// Looks like providing proper values involves some refactoring: currently we'd need to rely on the timeseries query context,
|
|
49
|
+
// but these step values are actually computed independently / before the queries are getting fired, so it's useless to fire
|
|
50
|
+
// queries here, so maybe we should extract this part to independant hook(s), to be reused here?
|
|
51
|
+
queryExpr = replacePromBuiltinVariables(queryExpr, 12345, 12345);
|
|
52
|
+
}
|
|
53
|
+
const { data: parseQueryResponse, isLoading, error } = useParseQuery(queryExpr !== null && queryExpr !== void 0 ? queryExpr : '', datasource, isTreeViewVisible);
|
|
54
|
+
const errorMessage = useMemo(()=>getErrMessage(error), [
|
|
68
55
|
error
|
|
69
56
|
]);
|
|
70
57
|
const handleShowTreeView = ()=>{
|
|
@@ -110,84 +97,70 @@ export function PromQLEditor({ completeConfig, datasource, ...rest }) {
|
|
|
110
97
|
],
|
|
111
98
|
placeholder: "Example: sum(rate(http_requests_total[5m]))"
|
|
112
99
|
}),
|
|
113
|
-
queryExpr && /*#__PURE__*/
|
|
114
|
-
children:
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
children: [
|
|
128
|
-
/*#__PURE__*/ _jsx(Tooltip, {
|
|
129
|
-
title: isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr,
|
|
130
|
-
children: /*#__PURE__*/ _jsx(IconButton, {
|
|
131
|
-
"aria-label": isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr,
|
|
132
|
-
onClick: handleShowTreeView,
|
|
100
|
+
queryExpr && /*#__PURE__*/ _jsxs(_Fragment, {
|
|
101
|
+
children: [
|
|
102
|
+
/*#__PURE__*/ _jsx(Tooltip, {
|
|
103
|
+
title: isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr,
|
|
104
|
+
children: /*#__PURE__*/ _jsx(IconButton, {
|
|
105
|
+
"aria-label": isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr,
|
|
106
|
+
onClick: handleShowTreeView,
|
|
107
|
+
sx: {
|
|
108
|
+
position: 'absolute',
|
|
109
|
+
right: '5px',
|
|
110
|
+
top: '5px'
|
|
111
|
+
},
|
|
112
|
+
size: "small",
|
|
113
|
+
children: /*#__PURE__*/ _jsx(FileTreeIcon, {
|
|
133
114
|
sx: {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
top: '5px'
|
|
137
|
-
},
|
|
138
|
-
size: "small",
|
|
139
|
-
children: /*#__PURE__*/ _jsx(FileTreeIcon, {
|
|
140
|
-
sx: {
|
|
141
|
-
fontSize: '18px'
|
|
142
|
-
}
|
|
143
|
-
})
|
|
115
|
+
fontSize: '18px'
|
|
116
|
+
}
|
|
144
117
|
})
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
118
|
+
})
|
|
119
|
+
}),
|
|
120
|
+
isTreeViewVisible && /*#__PURE__*/ _jsxs("div", {
|
|
121
|
+
style: {
|
|
122
|
+
border: `1px solid ${theme.palette.divider}`,
|
|
123
|
+
position: 'relative'
|
|
124
|
+
},
|
|
125
|
+
children: [
|
|
126
|
+
/*#__PURE__*/ _jsx(Tooltip, {
|
|
127
|
+
title: treeViewCloseStr,
|
|
128
|
+
children: /*#__PURE__*/ _jsx(IconButton, {
|
|
129
|
+
"aria-label": treeViewCloseStr,
|
|
130
|
+
onClick: ()=>setTreeViewVisible(false),
|
|
131
|
+
sx: {
|
|
132
|
+
position: 'absolute',
|
|
133
|
+
top: '5px',
|
|
134
|
+
right: '5px'
|
|
135
|
+
},
|
|
136
|
+
size: "small",
|
|
137
|
+
children: /*#__PURE__*/ _jsx(CloseIcon, {
|
|
157
138
|
sx: {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
right: '5px'
|
|
161
|
-
},
|
|
162
|
-
size: "small",
|
|
163
|
-
children: /*#__PURE__*/ _jsx(CloseIcon, {
|
|
164
|
-
sx: {
|
|
165
|
-
fontSize: '18px'
|
|
166
|
-
}
|
|
167
|
-
})
|
|
139
|
+
fontSize: '18px'
|
|
140
|
+
}
|
|
168
141
|
})
|
|
169
|
-
}),
|
|
170
|
-
error ? // Here the user is able to hide the error alert
|
|
171
|
-
/*#__PURE__*/ _jsx(ErrorAlert, {
|
|
172
|
-
error: {
|
|
173
|
-
name: `${treeViewStr} not available`,
|
|
174
|
-
message: errorMessage
|
|
175
|
-
}
|
|
176
|
-
}) : /*#__PURE__*/ _jsx("div", {
|
|
177
|
-
style: {
|
|
178
|
-
padding: `${theme.spacing(1.5)} ${theme.spacing(1.5)} 0 ${theme.spacing(1.5)}`,
|
|
179
|
-
overflowX: 'auto',
|
|
180
|
-
backgroundColor: theme.palette.background.default
|
|
181
|
-
},
|
|
182
|
-
children: isLoading ? /*#__PURE__*/ _jsx(CircularProgress, {}) : (parseQueryResponse === null || parseQueryResponse === void 0 ? void 0 : parseQueryResponse.data) ? /*#__PURE__*/ _jsx(TreeNode, {
|
|
183
|
-
node: parseQueryResponse.data,
|
|
184
|
-
reverse: false
|
|
185
|
-
}) : null
|
|
186
142
|
})
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
143
|
+
}),
|
|
144
|
+
error ? // Here the user is able to hide the error alert
|
|
145
|
+
/*#__PURE__*/ _jsx(ErrorAlert, {
|
|
146
|
+
error: {
|
|
147
|
+
name: `${treeViewStr} not available`,
|
|
148
|
+
message: errorMessage
|
|
149
|
+
}
|
|
150
|
+
}) : /*#__PURE__*/ _jsx("div", {
|
|
151
|
+
style: {
|
|
152
|
+
padding: `${theme.spacing(1.5)} ${theme.spacing(1.5)} 0 ${theme.spacing(1.5)}`,
|
|
153
|
+
overflowX: 'auto',
|
|
154
|
+
backgroundColor: theme.palette.background.default
|
|
155
|
+
},
|
|
156
|
+
children: isLoading ? /*#__PURE__*/ _jsx(CircularProgress, {}) : (parseQueryResponse === null || parseQueryResponse === void 0 ? void 0 : parseQueryResponse.data) ? /*#__PURE__*/ _jsx(TreeNode, {
|
|
157
|
+
node: parseQueryResponse.data,
|
|
158
|
+
reverse: false
|
|
159
|
+
}) : null
|
|
160
|
+
})
|
|
161
|
+
]
|
|
162
|
+
})
|
|
163
|
+
]
|
|
191
164
|
})
|
|
192
165
|
]
|
|
193
166
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/components/PromQLEditor.tsx"],"sourcesContent":["// Copyright 2024 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport CodeMirror, { ReactCodeMirrorProps } from '@uiw/react-codemirror';\nimport { PromQLExtension, CompleteConfiguration } from '@prometheus-io/codemirror-promql';\nimport { EditorView } from '@codemirror/view';\nimport { useTheme, CircularProgress, InputLabel, Stack, IconButton, Tooltip } from '@mui/material';\nimport FileTreeIcon from 'mdi-material-ui/FileTree';\nimport { useMemo, useState } from 'react';\nimport { ErrorAlert } from '@perses-dev/components';\nimport CloseIcon from 'mdi-material-ui/Close';\nimport { useReplaceVariablesInString } from '@perses-dev/plugin-system';\nimport { PrometheusDatasourceSelector } from '../model';\nimport { useParseQuery } from './parse';\nimport TreeNode from './TreeNode';\n\nconst treeViewStr = 'Tree View';\nconst treeViewOpenStr = 'Open ' + treeViewStr;\nconst treeViewCloseStr = 'Close ' + treeViewStr;\n\n// Process the error to identify if it's a generic one & adjust the message in some cases\n// TODO: This should be removed when properly tackling query error at query editor level (https://github.com/perses/perses/issues/1419)\n// Here this was kinda a quick-win to do it like this with the implementation of the Tree view.\n// Once #1419 will be tackled, any error reported by the query to the parse endpoint here should be treated the same (= display\n// the error in the debug/tree view & always let the buttons accessible to show/hide it).\nfunction processError(error: unknown): { errorMessage: string; isGenericError: boolean } {\n // Specific errors that user should be able to hide\n const apiNotAvailableError = '404 page not found';\n const apiNotAvailableErrorRephrased = `${treeViewStr} is available only for datasources whose APIs comply with Prometheus 3.0 specifications`;\n\n const blockedByProxyError =\n 'forbidden access: you are not allowed to use this endpoint \"/api/v1/parse_query\" with the HTTP method POST';\n const blockedByProxyErrorRephrased = `Your datasource configuration is blocking the ${treeViewStr} feature: the datasource should allow POST requests to \"/api/v1/parse_query\"`;\n\n let errorMessage = 'An unknown error occurred';\n let isGenericError = false;\n if (error && error instanceof Error) {\n errorMessage = error.message.trim();\n if (errorMessage === apiNotAvailableError) {\n errorMessage = apiNotAvailableErrorRephrased;\n } else if (errorMessage === blockedByProxyError) {\n errorMessage = blockedByProxyErrorRephrased;\n } else {\n isGenericError = true;\n }\n }\n\n return { errorMessage, isGenericError };\n}\n\nexport type PromQLEditorProps = {\n completeConfig: CompleteConfiguration;\n datasource: PrometheusDatasourceSelector;\n} & Omit<ReactCodeMirrorProps, 'theme' | 'extensions'>;\n\nexport function PromQLEditor({ completeConfig, datasource, ...rest }: PromQLEditorProps) {\n const theme = useTheme();\n const isDarkMode = theme.palette.mode === 'dark';\n const [isTreeViewVisible, setTreeViewVisible] = useState(false);\n\n const promQLExtension = useMemo(() => {\n return new PromQLExtension().activateLinter(false).setComplete(completeConfig).asExtension();\n }, [completeConfig]);\n\n const queryExpr = useReplaceVariablesInString(rest.value);\n\n const { data: parseQueryResponse, isLoading, error } = useParseQuery(queryExpr ?? '', datasource);\n const { errorMessage, isGenericError } = useMemo(() => processError(error), [error]);\n\n const handleShowTreeView = () => {\n setTreeViewVisible(!isTreeViewVisible);\n };\n\n return (\n <Stack position=\"relative\">\n <InputLabel // reproduce the same kind of input label that regular MUI TextFields have\n shrink\n sx={{\n position: 'absolute',\n top: '-8px',\n left: '10px',\n padding: '0 4px',\n color: theme.palette.text.primary,\n zIndex: 1,\n }}\n >\n PromQL Expression\n </InputLabel>\n <CodeMirror\n {...rest}\n style={{ border: `1px solid ${theme.palette.divider}` }}\n theme={isDarkMode ? 'dark' : 'light'}\n basicSetup={{\n highlightActiveLine: false,\n highlightActiveLineGutter: false,\n foldGutter: false,\n }}\n extensions={[\n EditorView.lineWrapping,\n promQLExtension,\n EditorView.theme({\n '.cm-content': {\n paddingTop: '8px',\n paddingBottom: '8px',\n paddingRight: '40px', // offset for the tree view button\n },\n }),\n ]}\n placeholder=\"Example: sum(rate(http_requests_total[5m]))\"\n />\n {queryExpr && (\n <>\n {isGenericError ? (\n // Display the error without any close button, tree view etc when it's a generic error\n <div style={{ border: `1px solid ${theme.palette.divider}` }}>\n <ErrorAlert error={{ name: 'Parse query error', message: errorMessage }} />\n </div>\n ) : (\n // Otherwise include the logic to show/hide the tree view\n <>\n <Tooltip title={isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr}>\n <IconButton\n aria-label={isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr}\n onClick={handleShowTreeView}\n sx={{ position: 'absolute', right: '5px', top: '5px' }}\n size=\"small\"\n >\n <FileTreeIcon sx={{ fontSize: '18px' }} />\n </IconButton>\n </Tooltip>\n {isTreeViewVisible && (\n <div style={{ border: `1px solid ${theme.palette.divider}`, position: 'relative' }}>\n <Tooltip title={treeViewCloseStr}>\n <IconButton\n aria-label={treeViewCloseStr}\n onClick={() => setTreeViewVisible(false)}\n sx={{ position: 'absolute', top: '5px', right: '5px' }}\n size=\"small\"\n >\n <CloseIcon sx={{ fontSize: '18px' }} />\n </IconButton>\n </Tooltip>\n {error ? (\n // Here the user is able to hide the error alert\n <ErrorAlert\n error={{\n name: `${treeViewStr} not available`,\n message: errorMessage,\n }}\n />\n ) : (\n <div\n style={{\n padding: `${theme.spacing(1.5)} ${theme.spacing(1.5)} 0 ${theme.spacing(1.5)}`, // let paddingBottom at 0 because nodes have margin-bottom\n overflowX: 'auto',\n backgroundColor: theme.palette.background.default,\n }}\n >\n {isLoading ? (\n <CircularProgress />\n ) : parseQueryResponse?.data ? (\n <TreeNode node={parseQueryResponse.data} reverse={false} />\n ) : null}\n </div>\n )}\n </div>\n )}\n </>\n )}\n </>\n )}\n </Stack>\n );\n}\n"],"names":["CodeMirror","PromQLExtension","EditorView","useTheme","CircularProgress","InputLabel","Stack","IconButton","Tooltip","FileTreeIcon","useMemo","useState","ErrorAlert","CloseIcon","useReplaceVariablesInString","useParseQuery","TreeNode","treeViewStr","treeViewOpenStr","treeViewCloseStr","processError","error","apiNotAvailableError","apiNotAvailableErrorRephrased","blockedByProxyError","blockedByProxyErrorRephrased","errorMessage","isGenericError","Error","message","trim","PromQLEditor","completeConfig","datasource","rest","theme","isDarkMode","palette","mode","isTreeViewVisible","setTreeViewVisible","promQLExtension","activateLinter","setComplete","asExtension","queryExpr","value","data","parseQueryResponse","isLoading","handleShowTreeView","position","shrink","sx","top","left","padding","color","text","primary","zIndex","style","border","divider","basicSetup","highlightActiveLine","highlightActiveLineGutter","foldGutter","extensions","lineWrapping","paddingTop","paddingBottom","paddingRight","placeholder","div","name","title","aria-label","onClick","right","size","fontSize","spacing","overflowX","backgroundColor","background","default","node","reverse"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;;AAEjC,OAAOA,gBAA0C,wBAAwB;AACzE,SAASC,eAAe,QAA+B,mCAAmC;AAC1F,SAASC,UAAU,QAAQ,mBAAmB;AAC9C,SAASC,QAAQ,EAAEC,gBAAgB,EAAEC,UAAU,EAAEC,KAAK,EAAEC,UAAU,EAAEC,OAAO,QAAQ,gBAAgB;AACnG,OAAOC,kBAAkB,2BAA2B;AACpD,SAASC,OAAO,EAAEC,QAAQ,QAAQ,QAAQ;AAC1C,SAASC,UAAU,QAAQ,yBAAyB;AACpD,OAAOC,eAAe,wBAAwB;AAC9C,SAASC,2BAA2B,QAAQ,4BAA4B;AAExE,SAASC,aAAa,QAAQ,UAAU;AACxC,OAAOC,cAAc,aAAa;AAElC,MAAMC,cAAc;AACpB,MAAMC,kBAAkB,UAAUD;AAClC,MAAME,mBAAmB,WAAWF;AAEpC,yFAAyF;AACzF,uIAAuI;AACvI,qGAAqG;AACrG,qIAAqI;AACrI,+FAA+F;AAC/F,SAASG,aAAaC,KAAc;IAClC,mDAAmD;IACnD,MAAMC,uBAAuB;IAC7B,MAAMC,gCAAgC,CAAC,EAAEN,YAAY,uFAAuF,CAAC;IAE7I,MAAMO,sBACJ;IACF,MAAMC,+BAA+B,CAAC,8CAA8C,EAAER,YAAY,4EAA4E,CAAC;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GrC5D,QACC,gDAAgD;8CAChD,KAACT;oCACCS,OAAO;wCACLsD,MAAM,CAAC,EAAE1D,YAAY,cAAc,CAAC;wCACpCY,SAASH;oCACX;mDAGF,KAACgD;oCACCb,OAAO;wCACLL,SAAS,CAAC,EAAErB,MAAM+C,OAAO,CAAC,KAAK,CAAC,EAAE/C,MAAM+C,OAAO,CAAC,KAAK,GAAG,EAAE/C,MAAM+C,OAAO,CAAC,KAAK,CAAC;wCAC9EC,WAAW;wCACXC,iBAAiBjD,MAAME,OAAO,CAACgD,UAAU,CAACC,OAAO;oCACnD;8CAECrC,0BACC,KAAC7C,wBACC4C,CAAAA,+BAAAA,yCAAAA,mBAAoBD,IAAI,kBAC1B,KAAC/B;wCAASuE,MAAMvC,mBAAmBD,IAAI;wCAAEyC,SAAS;yCAChD;;;;;;;;;AAW1B"}
|
|
1
|
+
{"version":3,"sources":["../../src/components/PromQLEditor.tsx"],"sourcesContent":["// Copyright 2024 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport CodeMirror, { ReactCodeMirrorProps } from '@uiw/react-codemirror';\nimport { PromQLExtension, CompleteConfiguration } from '@prometheus-io/codemirror-promql';\nimport { EditorView } from '@codemirror/view';\nimport { useTheme, CircularProgress, InputLabel, Stack, IconButton, Tooltip } from '@mui/material';\nimport FileTreeIcon from 'mdi-material-ui/FileTree';\nimport { ReactElement, useMemo, useState } from 'react';\nimport { ErrorAlert } from '@perses-dev/components';\nimport CloseIcon from 'mdi-material-ui/Close';\nimport { useReplaceVariablesInString } from '@perses-dev/plugin-system';\nimport { PrometheusDatasourceSelector } from '../model';\nimport { replacePromBuiltinVariables } from '../plugins/prometheus-time-series-query/replace-prom-builtin-variables';\nimport { useParseQuery } from './parse';\nimport TreeNode from './TreeNode';\n\nconst treeViewStr = 'Tree View';\nconst treeViewOpenStr = 'Open ' + treeViewStr;\nconst treeViewCloseStr = 'Close ' + treeViewStr;\n\nfunction getErrMessage(error: unknown): string {\n let errorMessage = 'An unknown error occurred';\n if (error && error instanceof Error) {\n errorMessage = error.message.trim();\n }\n return errorMessage;\n}\n\nexport type PromQLEditorProps = {\n completeConfig: CompleteConfiguration;\n datasource: PrometheusDatasourceSelector;\n} & Omit<ReactCodeMirrorProps, 'theme' | 'extensions'>;\n\nexport function PromQLEditor({ completeConfig, datasource, ...rest }: PromQLEditorProps): ReactElement {\n const theme = useTheme();\n const isDarkMode = theme.palette.mode === 'dark';\n const [isTreeViewVisible, setTreeViewVisible] = useState(false);\n\n const promQLExtension = useMemo(() => {\n return new PromQLExtension().activateLinter(false).setComplete(completeConfig).asExtension();\n }, [completeConfig]);\n\n let queryExpr = useReplaceVariablesInString(rest.value);\n if (queryExpr) {\n // TODO placeholder values for steps to be replaced with actual values\n // Looks like providing proper values involves some refactoring: currently we'd need to rely on the timeseries query context,\n // but these step values are actually computed independently / before the queries are getting fired, so it's useless to fire\n // queries here, so maybe we should extract this part to independant hook(s), to be reused here?\n queryExpr = replacePromBuiltinVariables(queryExpr, 12345, 12345);\n }\n\n const { data: parseQueryResponse, isLoading, error } = useParseQuery(queryExpr ?? '', datasource, isTreeViewVisible);\n const errorMessage = useMemo(() => getErrMessage(error), [error]);\n\n const handleShowTreeView = (): void => {\n setTreeViewVisible(!isTreeViewVisible);\n };\n\n return (\n <Stack position=\"relative\">\n <InputLabel // reproduce the same kind of input label that regular MUI TextFields have\n shrink\n sx={{\n position: 'absolute',\n top: '-8px',\n left: '10px',\n padding: '0 4px',\n color: theme.palette.text.primary,\n zIndex: 1,\n }}\n >\n PromQL Expression\n </InputLabel>\n <CodeMirror\n {...rest}\n style={{ border: `1px solid ${theme.palette.divider}` }}\n theme={isDarkMode ? 'dark' : 'light'}\n basicSetup={{\n highlightActiveLine: false,\n highlightActiveLineGutter: false,\n foldGutter: false,\n }}\n extensions={[\n EditorView.lineWrapping,\n promQLExtension,\n EditorView.theme({\n '.cm-content': {\n paddingTop: '8px',\n paddingBottom: '8px',\n paddingRight: '40px', // offset for the tree view button\n },\n }),\n ]}\n placeholder=\"Example: sum(rate(http_requests_total[5m]))\"\n />\n {queryExpr && (\n <>\n <Tooltip title={isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr}>\n <IconButton\n aria-label={isTreeViewVisible ? treeViewCloseStr : treeViewOpenStr}\n onClick={handleShowTreeView}\n sx={{ position: 'absolute', right: '5px', top: '5px' }}\n size=\"small\"\n >\n <FileTreeIcon sx={{ fontSize: '18px' }} />\n </IconButton>\n </Tooltip>\n {isTreeViewVisible && (\n <div style={{ border: `1px solid ${theme.palette.divider}`, position: 'relative' }}>\n <Tooltip title={treeViewCloseStr}>\n <IconButton\n aria-label={treeViewCloseStr}\n onClick={() => setTreeViewVisible(false)}\n sx={{ position: 'absolute', top: '5px', right: '5px' }}\n size=\"small\"\n >\n <CloseIcon sx={{ fontSize: '18px' }} />\n </IconButton>\n </Tooltip>\n {error ? (\n // Here the user is able to hide the error alert\n <ErrorAlert\n error={{\n name: `${treeViewStr} not available`,\n message: errorMessage,\n }}\n />\n ) : (\n <div\n style={{\n padding: `${theme.spacing(1.5)} ${theme.spacing(1.5)} 0 ${theme.spacing(1.5)}`, // let paddingBottom at 0 because nodes have margin-bottom\n overflowX: 'auto',\n backgroundColor: theme.palette.background.default,\n }}\n >\n {isLoading ? (\n <CircularProgress />\n ) : parseQueryResponse?.data ? (\n <TreeNode node={parseQueryResponse.data} reverse={false} />\n ) : null}\n </div>\n )}\n </div>\n )}\n </>\n )}\n </Stack>\n );\n}\n"],"names":["CodeMirror","PromQLExtension","EditorView","useTheme","CircularProgress","InputLabel","Stack","IconButton","Tooltip","FileTreeIcon","useMemo","useState","ErrorAlert","CloseIcon","useReplaceVariablesInString","replacePromBuiltinVariables","useParseQuery","TreeNode","treeViewStr","treeViewOpenStr","treeViewCloseStr","getErrMessage","error","errorMessage","Error","message","trim","PromQLEditor","completeConfig","datasource","rest","theme","isDarkMode","palette","mode","isTreeViewVisible","setTreeViewVisible","promQLExtension","activateLinter","setComplete","asExtension","queryExpr","value","data","parseQueryResponse","isLoading","handleShowTreeView","position","shrink","sx","top","left","padding","color","text","primary","zIndex","style","border","divider","basicSetup","highlightActiveLine","highlightActiveLineGutter","foldGutter","extensions","lineWrapping","paddingTop","paddingBottom","paddingRight","placeholder","title","aria-label","onClick","right","size","fontSize","div","name","spacing","overflowX","backgroundColor","background","default","node","reverse"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;;AAEjC,OAAOA,gBAA0C,wBAAwB;AACzE,SAASC,eAAe,QAA+B,mCAAmC;AAC1F,SAASC,UAAU,QAAQ,mBAAmB;AAC9C,SAASC,QAAQ,EAAEC,gBAAgB,EAAEC,UAAU,EAAEC,KAAK,EAAEC,UAAU,EAAEC,OAAO,QAAQ,gBAAgB;AACnG,OAAOC,kBAAkB,2BAA2B;AACpD,SAAuBC,OAAO,EAAEC,QAAQ,QAAQ,QAAQ;AACxD,SAASC,UAAU,QAAQ,yBAAyB;AACpD,OAAOC,eAAe,wBAAwB;AAC9C,SAASC,2BAA2B,QAAQ,4BAA4B;AAExE,SAASC,2BAA2B,QAAQ,yEAAyE;AACrH,SAASC,aAAa,QAAQ,UAAU;AACxC,OAAOC,cAAc,aAAa;AAElC,MAAMC,cAAc;AACpB,MAAMC,kBAAkB,UAAUD;AAClC,MAAME,mBAAmB,WAAWF;AAEpC,SAASG,cAAcC,KAAc;IACnC,IAAIC,eAAe;IACnB,IAAID,S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C,OAAO;gCACnD;0CAECrC,0BACC,KAACzC,wBACCwC,CAAAA,+BAAAA,yCAAAA,mBAAoBD,IAAI,kBAC1B,KAAC1B;oCAASkE,MAAMvC,mBAAmBD,IAAI;oCAAEyC,SAAS;qCAChD;;;;;;;;AAStB"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DatasourceSelector } from '@perses-dev/core';
|
|
2
|
+
import { UseQueryResult } from '@tanstack/react-query';
|
|
2
3
|
import { ParseQueryResponse } from '../model';
|
|
3
|
-
export declare function useParseQuery(content: string, datasource: DatasourceSelector):
|
|
4
|
+
export declare function useParseQuery(content: string, datasource: DatasourceSelector, enabled?: boolean): UseQueryResult<ParseQueryResponse>;
|
|
4
5
|
//# sourceMappingURL=parse.d.ts.map
|