@perses-dev/prometheus-plugin 0.21.1 → 0.23.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.
Files changed (98) hide show
  1. package/dist/cjs/components/PromQL.js +56 -0
  2. package/dist/cjs/components/index.js +28 -0
  3. package/dist/cjs/index.js +1 -1
  4. package/dist/cjs/model/api-types.js +1 -1
  5. package/dist/cjs/model/index.js +1 -2
  6. package/dist/cjs/model/parse-sample-values.js +1 -1
  7. package/dist/cjs/model/prometheus-client.js +1 -1
  8. package/dist/cjs/model/prometheus-selectors.js +1 -1
  9. package/dist/cjs/model/templating.js +2 -2
  10. package/dist/cjs/model/time.js +1 -1
  11. package/dist/cjs/plugins/MatcherEditor.js +1 -1
  12. package/dist/cjs/plugins/prometheus-datasource.js +4 -1
  13. package/dist/cjs/plugins/prometheus-time-series-query/PrometheusTimeSeriesQuery.js +3 -3
  14. package/dist/cjs/plugins/prometheus-time-series-query/PrometheusTimeSeriesQueryEditor.js +14 -8
  15. package/dist/cjs/plugins/prometheus-time-series-query/get-time-series-data.js +9 -7
  16. package/dist/cjs/plugins/prometheus-time-series-query/index.js +1 -1
  17. package/dist/cjs/plugins/prometheus-time-series-query/query-editor-model.js +3 -3
  18. package/dist/cjs/plugins/prometheus-time-series-query/time-series-query-model.js +1 -1
  19. package/dist/cjs/plugins/prometheus-variables.js +10 -9
  20. package/dist/cjs/plugins/types.js +1 -1
  21. package/dist/cjs/plugins/variable.js +1 -1
  22. package/dist/cjs/test/setup-tests.js +1 -1
  23. package/dist/cjs/utils/index.js +28 -0
  24. package/dist/cjs/{model → utils}/utils.js +37 -2
  25. package/dist/cjs/{model → utils}/utils.test.js +47 -1
  26. package/dist/components/PromQL.d.ts +8 -0
  27. package/dist/components/PromQL.d.ts.map +1 -0
  28. package/dist/components/PromQL.js +45 -0
  29. package/dist/components/PromQL.js.map +1 -0
  30. package/dist/components/index.d.ts +2 -0
  31. package/dist/components/index.d.ts.map +1 -0
  32. package/dist/components/index.js +15 -0
  33. package/dist/components/index.js.map +1 -0
  34. package/dist/index.js +1 -1
  35. package/dist/index.js.map +1 -1
  36. package/dist/model/api-types.js +1 -1
  37. package/dist/model/api-types.js.map +1 -1
  38. package/dist/model/index.d.ts +0 -1
  39. package/dist/model/index.d.ts.map +1 -1
  40. package/dist/model/index.js +1 -2
  41. package/dist/model/index.js.map +1 -1
  42. package/dist/model/parse-sample-values.js +1 -1
  43. package/dist/model/parse-sample-values.js.map +1 -1
  44. package/dist/model/prometheus-client.d.ts +5 -0
  45. package/dist/model/prometheus-client.d.ts.map +1 -1
  46. package/dist/model/prometheus-client.js +1 -1
  47. package/dist/model/prometheus-client.js.map +1 -1
  48. package/dist/model/prometheus-selectors.js +1 -1
  49. package/dist/model/prometheus-selectors.js.map +1 -1
  50. package/dist/model/templating.js +2 -2
  51. package/dist/model/templating.js.map +1 -1
  52. package/dist/model/time.js +1 -1
  53. package/dist/model/time.js.map +1 -1
  54. package/dist/plugins/MatcherEditor.js +1 -1
  55. package/dist/plugins/MatcherEditor.js.map +1 -1
  56. package/dist/plugins/prometheus-datasource.d.ts.map +1 -1
  57. package/dist/plugins/prometheus-datasource.js +4 -1
  58. package/dist/plugins/prometheus-datasource.js.map +1 -1
  59. package/dist/plugins/prometheus-time-series-query/PrometheusTimeSeriesQuery.js +2 -2
  60. package/dist/plugins/prometheus-time-series-query/PrometheusTimeSeriesQuery.js.map +1 -1
  61. package/dist/plugins/prometheus-time-series-query/PrometheusTimeSeriesQueryEditor.d.ts.map +1 -1
  62. package/dist/plugins/prometheus-time-series-query/PrometheusTimeSeriesQueryEditor.js +15 -9
  63. package/dist/plugins/prometheus-time-series-query/PrometheusTimeSeriesQueryEditor.js.map +1 -1
  64. package/dist/plugins/prometheus-time-series-query/get-time-series-data.d.ts.map +1 -1
  65. package/dist/plugins/prometheus-time-series-query/get-time-series-data.js +8 -6
  66. package/dist/plugins/prometheus-time-series-query/get-time-series-data.js.map +1 -1
  67. package/dist/plugins/prometheus-time-series-query/index.js +1 -1
  68. package/dist/plugins/prometheus-time-series-query/index.js.map +1 -1
  69. package/dist/plugins/prometheus-time-series-query/query-editor-model.d.ts +4 -5
  70. package/dist/plugins/prometheus-time-series-query/query-editor-model.d.ts.map +1 -1
  71. package/dist/plugins/prometheus-time-series-query/query-editor-model.js +3 -3
  72. package/dist/plugins/prometheus-time-series-query/query-editor-model.js.map +1 -1
  73. package/dist/plugins/prometheus-time-series-query/time-series-query-model.js +1 -1
  74. package/dist/plugins/prometheus-time-series-query/time-series-query-model.js.map +1 -1
  75. package/dist/plugins/prometheus-variables.d.ts.map +1 -1
  76. package/dist/plugins/prometheus-variables.js +3 -2
  77. package/dist/plugins/prometheus-variables.js.map +1 -1
  78. package/dist/plugins/types.js +1 -1
  79. package/dist/plugins/types.js.map +1 -1
  80. package/dist/plugins/variable.js +1 -1
  81. package/dist/plugins/variable.js.map +1 -1
  82. package/dist/test/setup-tests.js +1 -1
  83. package/dist/test/setup-tests.js.map +1 -1
  84. package/dist/utils/index.d.ts +2 -0
  85. package/dist/utils/index.d.ts.map +1 -0
  86. package/dist/utils/index.js +15 -0
  87. package/dist/utils/index.js.map +1 -0
  88. package/dist/{model → utils}/utils.d.ts +5 -0
  89. package/dist/{model → utils}/utils.d.ts.map +1 -1
  90. package/dist/{model → utils}/utils.js +37 -1
  91. package/dist/utils/utils.js.map +1 -0
  92. package/dist/{model → utils}/utils.test.d.ts +0 -0
  93. package/dist/{model → utils}/utils.test.d.ts.map +1 -1
  94. package/dist/{model → utils}/utils.test.js +48 -2
  95. package/dist/utils/utils.test.js.map +1 -0
  96. package/package.json +6 -3
  97. package/dist/model/utils.js.map +0 -1
  98. package/dist/model/utils.test.js.map +0 -1
@@ -0,0 +1,56 @@
1
+ // Copyright 2023 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, "PromQLEditor", {
18
+ enumerable: true,
19
+ get: ()=>PromQLEditor
20
+ });
21
+ const _jsxRuntime = require("react/jsx-runtime");
22
+ const _reactCodemirror = /*#__PURE__*/ _interopRequireDefault(require("@uiw/react-codemirror"));
23
+ const _codemirrorPromql = require("@prometheus-io/codemirror-promql");
24
+ const _view = require("@codemirror/view");
25
+ const _material = require("@mui/material");
26
+ const _react = require("react");
27
+ function _interopRequireDefault(obj) {
28
+ return obj && obj.__esModule ? obj : {
29
+ default: obj
30
+ };
31
+ }
32
+ function PromQLEditor({ completeConfig , ...rest }) {
33
+ const theme = (0, _material.useTheme)();
34
+ const isDarkMode = theme.palette.mode === 'dark';
35
+ const promQLExtension = (0, _react.useMemo)(()=>{
36
+ return new _codemirrorPromql.PromQLExtension().activateLinter(false).setComplete(completeConfig).asExtension();
37
+ }, [
38
+ completeConfig
39
+ ]);
40
+ return /*#__PURE__*/ (0, _jsxRuntime.jsx)(_reactCodemirror.default, {
41
+ ...rest,
42
+ style: {
43
+ border: `1px solid ${theme.palette.divider}`
44
+ },
45
+ theme: isDarkMode ? 'dark' : 'light',
46
+ basicSetup: {
47
+ highlightActiveLine: false,
48
+ highlightActiveLineGutter: false,
49
+ foldGutter: false
50
+ },
51
+ extensions: [
52
+ _view.EditorView.lineWrapping,
53
+ promQLExtension
54
+ ]
55
+ });
56
+ }
@@ -0,0 +1,28 @@
1
+ // Copyright 2023 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
+ _exportStar(require("./PromQL"), exports);
18
+ function _exportStar(from, to) {
19
+ Object.keys(from).forEach(function(k) {
20
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
21
+ enumerable: true,
22
+ get: function() {
23
+ return from[k];
24
+ }
25
+ });
26
+ });
27
+ return from;
28
+ }
package/dist/cjs/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -20,7 +20,6 @@ _exportStar(require("./prometheus-client"), exports);
20
20
  _exportStar(require("./prometheus-selectors"), exports);
21
21
  _exportStar(require("./templating"), exports);
22
22
  _exportStar(require("./time"), exports);
23
- _exportStar(require("./utils"), exports);
24
23
  function _exportStar(from, to) {
25
24
  Object.keys(from).forEach(function(k) {
26
25
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -19,7 +19,7 @@ Object.defineProperty(exports, "useReplaceTemplateString", {
19
19
  get: ()=>useReplaceTemplateString
20
20
  });
21
21
  const _pluginSystem = require("@perses-dev/plugin-system");
22
- const _utils = require("./utils");
22
+ const _utils = require("../utils");
23
23
  function useReplaceTemplateString(templateString) {
24
24
  const templateVariablesInTemplate = (0, _utils.parseTemplateVariables)(templateString || '');
25
25
  const variables = (0, _pluginSystem.useTemplateVariableValues)(templateVariablesInTemplate);
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -31,6 +31,9 @@ const _model = require("../model");
31
31
  }
32
32
  // Could think about this becoming a class, although it definitely doesn't have to be
33
33
  return {
34
+ options: {
35
+ datasourceUrl
36
+ },
34
37
  instantQuery: (params)=>(0, _model.instantQuery)(params, {
35
38
  datasourceUrl
36
39
  }),
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -18,7 +18,7 @@ Object.defineProperty(exports, "PrometheusTimeSeriesQuery", {
18
18
  enumerable: true,
19
19
  get: ()=>PrometheusTimeSeriesQuery
20
20
  });
21
- const _model = require("../../model");
21
+ const _utils = require("../../utils");
22
22
  const _getTimeSeriesData = require("./get-time-series-data");
23
23
  const _prometheusTimeSeriesQueryEditor = require("./PrometheusTimeSeriesQueryEditor");
24
24
  const PrometheusTimeSeriesQuery = {
@@ -30,7 +30,7 @@ const PrometheusTimeSeriesQuery = {
30
30
  }),
31
31
  dependsOn: (spec)=>{
32
32
  return {
33
- variables: (0, _model.parseTemplateVariables)(spec.query)
33
+ variables: (0, _utils.parseTemplateVariables)(spec.query)
34
34
  };
35
35
  }
36
36
  };
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -23,10 +23,14 @@ const _immer = require("immer");
23
23
  const _material = require("@mui/material");
24
24
  const _pluginSystem = require("@perses-dev/plugin-system");
25
25
  const _model = require("../../model");
26
+ const _components = require("../../components");
26
27
  const _queryEditorModel = require("./query-editor-model");
27
28
  function PrometheusTimeSeriesQueryEditor(props) {
28
29
  const { onChange , value } = props;
29
30
  const { datasource } = value;
31
+ const selectedDatasource = datasource !== null && datasource !== void 0 ? datasource : _model.DEFAULT_PROM;
32
+ const { data: client } = (0, _pluginSystem.useDatasourceClient)(selectedDatasource);
33
+ const promURL = client === null || client === void 0 ? void 0 : client.options.datasourceUrl;
30
34
  const { query , handleQueryChange , handleQueryBlur } = (0, _queryEditorModel.useQueryState)(props);
31
35
  const { format , handleFormatChange , handleFormatBlur } = (0, _queryEditorModel.useFormatState)(props);
32
36
  const handleDatasourceChange = (next)=>{
@@ -43,19 +47,21 @@ function PrometheusTimeSeriesQueryEditor(props) {
43
47
  return /*#__PURE__*/ (0, _jsxRuntime.jsxs)(_material.Stack, {
44
48
  spacing: 2,
45
49
  children: [
46
- /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.TextField, {
47
- fullWidth: true,
48
- label: "Query",
50
+ /*#__PURE__*/ (0, _jsxRuntime.jsx)(_components.PromQLEditor, {
51
+ completeConfig: {
52
+ remote: {
53
+ url: promURL
54
+ }
55
+ },
49
56
  value: query,
50
57
  onChange: handleQueryChange,
51
- onBlur: handleQueryBlur,
52
- margin: "dense"
58
+ onBlur: handleQueryBlur
53
59
  }),
54
60
  /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.TextField, {
55
61
  fullWidth: true,
56
62
  label: "Series Name Format",
57
63
  value: format !== null && format !== void 0 ? format : '',
58
- onChange: handleFormatChange,
64
+ onChange: (e)=>handleFormatChange(e.target.value),
59
65
  onBlur: handleFormatBlur,
60
66
  margin: "dense"
61
67
  }),
@@ -69,7 +75,7 @@ function PrometheusTimeSeriesQueryEditor(props) {
69
75
  }),
70
76
  /*#__PURE__*/ (0, _jsxRuntime.jsx)(_pluginSystem.DatasourceSelect, {
71
77
  datasourcePluginKind: "PrometheusDatasource",
72
- value: datasource !== null && datasource !== void 0 ? datasource : _model.DEFAULT_PROM,
78
+ value: selectedDatasource,
73
79
  onChange: handleDatasourceChange,
74
80
  labelId: "prom-datasource-label",
75
81
  label: "Prometheus Datasource"
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -20,6 +20,7 @@ Object.defineProperty(exports, "getTimeSeriesData", {
20
20
  });
21
21
  const _dateFns = require("date-fns");
22
22
  const _model = require("../../model");
23
+ const _utils = require("../../utils");
23
24
  const getTimeSeriesData = async (spec, context)=>{
24
25
  var ref;
25
26
  if (spec.query === undefined || spec.query === null || spec.query === '') {
@@ -40,7 +41,7 @@ const getTimeSeriesData = async (spec, context)=>{
40
41
  end = alignedEnd;
41
42
  // Replace template variable placeholders in PromQL query
42
43
  let query = spec.query.replace('$__rate_interval', `15s`);
43
- query = (0, _model.replaceTemplateVariables)(query, context.variableState);
44
+ query = (0, _utils.replaceTemplateVariables)(query, context.variableState);
44
45
  var _datasource;
45
46
  // Get the datasource, using the default Prom Datasource if one isn't specified in the query
46
47
  const client = await context.datasourceStore.getDatasourceClient((_datasource = spec.datasource) !== null && _datasource !== void 0 ? _datasource : _model.DEFAULT_PROM);
@@ -66,13 +67,14 @@ const getTimeSeriesData = async (spec, context)=>{
66
67
  // processing until its needed
67
68
  series: result.map((value)=>{
68
69
  const { metric , values } = value;
69
- // Name the series after the metric labels or if no metric, just use the
70
- // overall query
71
- let name = Object.entries(metric).map(([labelName, labelValue])=>`${labelName}="${labelValue}"`).join(', ');
72
- if (name === '') name = query;
70
+ // Name the series after the metric labels or if no metric, use the query
71
+ let name = (0, _utils.getUniqueKeyForPrometheusResult)(metric);
72
+ if (name === '') {
73
+ name = query;
74
+ }
73
75
  // query editor allows you to define an optional series_name_format
74
76
  // property to customize legend and tooltip display
75
- const formattedName = spec.series_name_format ? (0, _model.formatSeriesName)(spec.series_name_format, metric) : name;
77
+ const formattedName = spec.series_name_format ? (0, _utils.formatSeriesName)(spec.series_name_format, metric) : name;
76
78
  return {
77
79
  name,
78
80
  values: values.map(_model.parseValueTuple),
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -39,7 +39,7 @@ function useQueryState(props) {
39
39
  }
40
40
  // Update our local state's copy as the user types
41
41
  const handleQueryChange = (e)=>{
42
- setQuery(e.target.value);
42
+ setQuery(e);
43
43
  };
44
44
  // Propagate changes to the query's value when the input is blurred to avoid constantly re-running queries in the
45
45
  // PanelPreview
@@ -66,7 +66,7 @@ function useFormatState(props) {
66
66
  }
67
67
  // Update our local state as the user types
68
68
  const handleFormatChange = (e)=>{
69
- setFormat(e.target.value);
69
+ setFormat(e);
70
70
  };
71
71
  // Propagate changes to the panel preview component when series_name_format TextField is blurred
72
72
  const handleFormatBlur = ()=>{
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -28,6 +28,7 @@ _export(exports, {
28
28
  const _jsxRuntime = require("react/jsx-runtime");
29
29
  const _material = require("@mui/material");
30
30
  const _model = require("../model");
31
+ const _utils = require("../utils");
31
32
  const _matcherEditor = require("./MatcherEditor");
32
33
  function PrometheusLabelValuesVariableEditor(props) {
33
34
  var _matchers;
@@ -140,7 +141,7 @@ const PrometheusLabelNamesVariable = {
140
141
  getVariableOptions: async (spec, ctx)=>{
141
142
  var _datasource;
142
143
  const client = await ctx.datasourceStore.getDatasourceClient((_datasource = spec.datasource) !== null && _datasource !== void 0 ? _datasource : _model.DEFAULT_PROM);
143
- const match = spec.matchers ? spec.matchers.map((m)=>(0, _model.replaceTemplateVariables)(m, ctx.variables)) : undefined;
144
+ const match = spec.matchers ? spec.matchers.map((m)=>(0, _utils.replaceTemplateVariables)(m, ctx.variables)) : undefined;
144
145
  const timeRange = (0, _model.getPrometheusTimeRange)(ctx.timeRange);
145
146
  const { data: options } = await client.labelNames({
146
147
  'match[]': match,
@@ -153,7 +154,7 @@ const PrometheusLabelNamesVariable = {
153
154
  dependsOn: (spec)=>{
154
155
  var ref;
155
156
  return {
156
- variables: ((ref = spec.matchers) === null || ref === void 0 ? void 0 : ref.map((m)=>(0, _model.parseTemplateVariables)(m)).flat()) || []
157
+ variables: ((ref = spec.matchers) === null || ref === void 0 ? void 0 : ref.map((m)=>(0, _utils.parseTemplateVariables)(m)).flat()) || []
157
158
  };
158
159
  },
159
160
  OptionsEditorComponent: PrometheusLabelNamesVariableEditor,
@@ -164,10 +165,10 @@ const PrometheusLabelValuesVariable = {
164
165
  const pluginDef = spec;
165
166
  var _datasource;
166
167
  const client = await ctx.datasourceStore.getDatasourceClient((_datasource = spec.datasource) !== null && _datasource !== void 0 ? _datasource : _model.DEFAULT_PROM);
167
- const match = pluginDef.matchers ? pluginDef.matchers.map((m)=>(0, _model.replaceTemplateVariables)(m, ctx.variables)) : undefined;
168
+ const match = pluginDef.matchers ? pluginDef.matchers.map((m)=>(0, _utils.replaceTemplateVariables)(m, ctx.variables)) : undefined;
168
169
  const timeRange = (0, _model.getPrometheusTimeRange)(ctx.timeRange);
169
170
  const { data: options } = await client.labelValues({
170
- labelName: (0, _model.replaceTemplateVariables)(pluginDef.label_name, ctx.variables),
171
+ labelName: (0, _utils.replaceTemplateVariables)(pluginDef.label_name, ctx.variables),
171
172
  'match[]': match,
172
173
  ...timeRange
173
174
  });
@@ -178,7 +179,7 @@ const PrometheusLabelValuesVariable = {
178
179
  dependsOn: (spec)=>{
179
180
  var ref;
180
181
  return {
181
- variables: ((ref = spec.matchers) === null || ref === void 0 ? void 0 : ref.map((m)=>(0, _model.parseTemplateVariables)(m)).flat().concat((0, _model.parseTemplateVariables)(spec.label_name))) || []
182
+ variables: ((ref = spec.matchers) === null || ref === void 0 ? void 0 : ref.map((m)=>(0, _utils.parseTemplateVariables)(m)).flat().concat((0, _utils.parseTemplateVariables)(spec.label_name))) || []
182
183
  };
183
184
  },
184
185
  OptionsEditorComponent: PrometheusLabelValuesVariableEditor,
@@ -192,9 +193,9 @@ const PrometheusPromQLVariable = {
192
193
  const client = await ctx.datasourceStore.getDatasourceClient((_datasource = spec.datasource) !== null && _datasource !== void 0 ? _datasource : _model.DEFAULT_PROM);
193
194
  // TODO we may want to manage a range query as well.
194
195
  const { data: options } = await client.instantQuery({
195
- query: (0, _model.replaceTemplateVariables)(spec.expr, ctx.variables)
196
+ query: (0, _utils.replaceTemplateVariables)(spec.expr, ctx.variables)
196
197
  });
197
- const labelName = (0, _model.replaceTemplateVariables)(spec.label_name, ctx.variables);
198
+ const labelName = (0, _utils.replaceTemplateVariables)(spec.label_name, ctx.variables);
198
199
  let values = [];
199
200
  if ((options === null || options === void 0 ? void 0 : options.resultType) === 'matrix') {
200
201
  values = capturingMatrix(options, labelName);
@@ -207,7 +208,7 @@ const PrometheusPromQLVariable = {
207
208
  },
208
209
  dependsOn: (spec)=>{
209
210
  return {
210
- variables: (0, _model.parseTemplateVariables)(spec.expr).concat((0, _model.parseTemplateVariables)(spec.label_name))
211
+ variables: (0, _utils.parseTemplateVariables)(spec.expr).concat((0, _utils.parseTemplateVariables)(spec.label_name))
211
212
  };
212
213
  },
213
214
  OptionsEditorComponent: PrometheusPromQLVariableEditor,
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -0,0 +1,28 @@
1
+ // Copyright 2023 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
+ _exportStar(require("./utils"), exports);
18
+ function _exportStar(from, to) {
19
+ Object.keys(from).forEach(function(k) {
20
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
21
+ enumerable: true,
22
+ get: function() {
23
+ return from[k];
24
+ }
25
+ });
26
+ });
27
+ return from;
28
+ }
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -24,7 +24,8 @@ _export(exports, {
24
24
  replaceTemplateVariables: ()=>replaceTemplateVariables,
25
25
  replaceTemplateVariable: ()=>replaceTemplateVariable,
26
26
  parseTemplateVariables: ()=>parseTemplateVariables,
27
- formatSeriesName: ()=>formatSeriesName
27
+ formatSeriesName: ()=>formatSeriesName,
28
+ getUniqueKeyForPrometheusResult: ()=>getUniqueKeyForPrometheusResult
28
29
  });
29
30
  function replaceTemplateVariables(text, variableState) {
30
31
  const variables = parseTemplateVariables(text);
@@ -67,3 +68,37 @@ function formatSeriesName(inputFormat, seriesLabels) {
67
68
  const resolveLabelsRegex = /\{\{\s*(.+?)\s*\}\}/g;
68
69
  return inputFormat.replace(resolveLabelsRegex, (_, g)=>seriesLabels[g] ? seriesLabels[g] : g);
69
70
  }
71
+ /*
72
+ * Stringifies object of labels into valid PromQL for querying metric by label
73
+ */ function stringifyPrometheusMetricLabels(labels, removeExprWrap) {
74
+ const labelStrings = [];
75
+ Object.keys(labels).sort().forEach((labelName)=>{
76
+ const labelValue = labels[labelName];
77
+ if (labelValue !== undefined) {
78
+ if (removeExprWrap) {
79
+ labelStrings.push(`"${labelName}":"${labelValue}"`);
80
+ } else {
81
+ labelStrings.push(`${labelName}="${labelValue}"`);
82
+ }
83
+ }
84
+ });
85
+ return `{${labelStrings.join(',')}}`;
86
+ }
87
+ function getUniqueKeyForPrometheusResult(metricLabels, { removeExprWrap } = {}) {
88
+ const metricNameKey = '__name__';
89
+ if (metricLabels) {
90
+ if (Object.prototype.hasOwnProperty.call(metricLabels, metricNameKey)) {
91
+ const stringifiedLabels = stringifyPrometheusMetricLabels({
92
+ ...metricLabels,
93
+ [metricNameKey]: undefined
94
+ }, removeExprWrap);
95
+ if (removeExprWrap === true) {
96
+ return `${stringifiedLabels}`;
97
+ } else {
98
+ return `${metricLabels[metricNameKey]}${stringifiedLabels}`;
99
+ }
100
+ }
101
+ return stringifyPrometheusMetricLabels(metricLabels, removeExprWrap);
102
+ }
103
+ return '';
104
+ }
@@ -1,4 +1,4 @@
1
- // Copyright 2022 The Perses Authors
1
+ // Copyright 2023 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -124,3 +124,49 @@ describe('formatSeriesName', ()=>{
124
124
  expect((0, _utils.formatSeriesName)(inputFormat, metric)).toEqual(output);
125
125
  });
126
126
  });
127
+ describe('getUniqueKeyForPrometheusResult', ()=>{
128
+ let labels = {};
129
+ beforeEach(()=>{
130
+ labels = {};
131
+ });
132
+ it('should be a formatted prometheus string', ()=>{
133
+ labels = {
134
+ ['foo']: 'bar'
135
+ };
136
+ const result = (0, _utils.getUniqueKeyForPrometheusResult)(labels);
137
+ expect(result).toEqual('{foo="bar"}');
138
+ });
139
+ it('should be formatted with "', ()=>{
140
+ labels = {
141
+ ['foo']: 'bar'
142
+ };
143
+ const result = (0, _utils.getUniqueKeyForPrometheusResult)(labels, {
144
+ removeExprWrap: true
145
+ });
146
+ expect(result).toEqual('{"foo":"bar"}');
147
+ });
148
+ it('should be formatted with __name__ removed', ()=>{
149
+ labels = {
150
+ __name__: 'node_memory_Buffers_bytes',
151
+ env: 'demo',
152
+ instance: 'demo.do.prometheus.io:9100',
153
+ job: 'node'
154
+ };
155
+ const result = (0, _utils.getUniqueKeyForPrometheusResult)(labels, {
156
+ removeExprWrap: true
157
+ });
158
+ expect(result).toEqual('{"env":"demo","instance":"demo.do.prometheus.io:9100","job":"node"}');
159
+ });
160
+ it('should return a valid query to filter by label', ()=>{
161
+ labels = {
162
+ __name__: 'node_memory_Buffers_bytes',
163
+ env: 'demo',
164
+ instance: 'demo.do.prometheus.io:9100',
165
+ job: 'node'
166
+ };
167
+ const result = (0, _utils.getUniqueKeyForPrometheusResult)(labels, {
168
+ removeExprWrap: false
169
+ });
170
+ expect(result).toEqual('node_memory_Buffers_bytes{env="demo",instance="demo.do.prometheus.io:9100",job="node"}');
171
+ });
172
+ });
@@ -0,0 +1,8 @@
1
+ /// <reference types="react" />
2
+ import { ReactCodeMirrorProps } from '@uiw/react-codemirror';
3
+ import { CompleteConfiguration } from '@prometheus-io/codemirror-promql';
4
+ export declare type PromQLEditorProps = {
5
+ completeConfig: CompleteConfiguration;
6
+ } & Omit<ReactCodeMirrorProps, 'theme' | 'extensions'>;
7
+ export declare function PromQLEditor({ completeConfig, ...rest }: PromQLEditorProps): JSX.Element;
8
+ //# sourceMappingURL=PromQL.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PromQL.d.ts","sourceRoot":"","sources":["../../src/components/PromQL.tsx"],"names":[],"mappings":";AAaA,OAAmB,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AACzE,OAAO,EAAmB,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAK1F,oBAAY,iBAAiB,GAAG;IAAE,cAAc,EAAE,qBAAqB,CAAA;CAAE,GAAG,IAAI,CAC9E,oBAAoB,EACpB,OAAO,GAAG,YAAY,CACvB,CAAC;AAEF,wBAAgB,YAAY,CAAC,EAAE,cAAc,EAAE,GAAG,IAAI,EAAE,EAAE,iBAAiB,eAqB1E"}
@@ -0,0 +1,45 @@
1
+ // Copyright 2023 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
+ import { jsx as _jsx } from "react/jsx-runtime";
14
+ import CodeMirror from '@uiw/react-codemirror';
15
+ import { PromQLExtension } from '@prometheus-io/codemirror-promql';
16
+ import { EditorView } from '@codemirror/view';
17
+ import { useTheme } from '@mui/material';
18
+ import { useMemo } from 'react';
19
+ export function PromQLEditor({ completeConfig , ...rest }) {
20
+ const theme = useTheme();
21
+ const isDarkMode = theme.palette.mode === 'dark';
22
+ const promQLExtension = useMemo(()=>{
23
+ return new PromQLExtension().activateLinter(false).setComplete(completeConfig).asExtension();
24
+ }, [
25
+ completeConfig
26
+ ]);
27
+ return /*#__PURE__*/ _jsx(CodeMirror, {
28
+ ...rest,
29
+ style: {
30
+ border: `1px solid ${theme.palette.divider}`
31
+ },
32
+ theme: isDarkMode ? 'dark' : 'light',
33
+ basicSetup: {
34
+ highlightActiveLine: false,
35
+ highlightActiveLineGutter: false,
36
+ foldGutter: false
37
+ },
38
+ extensions: [
39
+ EditorView.lineWrapping,
40
+ promQLExtension
41
+ ]
42
+ });
43
+ }
44
+
45
+ //# sourceMappingURL=PromQL.js.map