@perses-dev/plugin-system 0.0.0-snapshot-time-range-height-80d08fc
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/LICENSE +201 -0
- package/dist/cjs/components/CalculationSelector/CalculationSelector.js +52 -0
- package/dist/cjs/components/CalculationSelector/CalculationSelector.test.js +65 -0
- package/dist/cjs/components/CalculationSelector/index.js +28 -0
- package/dist/cjs/components/DatasourceSelect.js +76 -0
- package/dist/cjs/components/OptionsEditorTabs/OptionsEditorTabs.js +102 -0
- package/dist/cjs/components/OptionsEditorTabs/OptionsEditorTabs.test.js +167 -0
- package/dist/cjs/components/OptionsEditorTabs/TabPanel.js +37 -0
- package/dist/cjs/components/OptionsEditorTabs/index.js +28 -0
- package/dist/cjs/components/PluginEditor/PluginEditor.js +67 -0
- package/dist/cjs/components/PluginEditor/PluginEditor.test.js +150 -0
- package/dist/cjs/components/PluginEditor/index.js +29 -0
- package/dist/cjs/components/PluginEditor/plugin-editor-api.js +101 -0
- package/dist/cjs/components/PluginKindSelect.js +41 -0
- package/dist/cjs/components/PluginKindSelect.test.js +86 -0
- package/dist/cjs/components/PluginRegistry/PluginRegistry.js +85 -0
- package/dist/cjs/components/PluginRegistry/PluginRegistry.test.js +144 -0
- package/dist/cjs/components/PluginRegistry/index.js +28 -0
- package/dist/cjs/components/PluginRegistry/plugin-indexes.js +78 -0
- package/dist/cjs/components/PluginSpecEditor.js +43 -0
- package/dist/cjs/components/PluginSpecEditor.test.js +70 -0
- package/dist/cjs/components/TimeSeriesQueryEditor.js +42 -0
- package/dist/cjs/components/index.js +35 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/model/calculations.js +77 -0
- package/dist/cjs/model/datasource.js +16 -0
- package/dist/cjs/model/index.js +35 -0
- package/dist/cjs/model/panels.js +16 -0
- package/dist/cjs/model/plugin-base.js +22 -0
- package/dist/cjs/model/plugin-loading.js +38 -0
- package/dist/cjs/model/plugins.js +16 -0
- package/dist/cjs/model/time-series-queries.js +16 -0
- package/dist/cjs/model/variables.js +16 -0
- package/dist/cjs/runtime/TimeRangeProvider/TimeRangeProvider.js +118 -0
- package/dist/cjs/runtime/TimeRangeProvider/index.js +29 -0
- package/dist/cjs/runtime/TimeRangeProvider/query-params.js +161 -0
- package/dist/cjs/runtime/datasources.js +44 -0
- package/dist/cjs/runtime/index.js +32 -0
- package/dist/cjs/runtime/plugin-registry.js +75 -0
- package/dist/cjs/runtime/template-variables.js +57 -0
- package/dist/cjs/runtime/time-series-queries.js +140 -0
- package/dist/cjs/test/index.js +28 -0
- package/dist/cjs/test/render.js +52 -0
- package/dist/cjs/test/setup-tests.js +20 -0
- package/dist/cjs/test/test-plugins/bert/index.js +77 -0
- package/dist/cjs/test/test-plugins/ernie/index.js +58 -0
- package/dist/cjs/test/test-plugins/index.js +77 -0
- package/dist/cjs/test-utils/index.js +28 -0
- package/dist/cjs/test-utils/mock-plugin-registry.js +68 -0
- package/dist/components/CalculationSelector/CalculationSelector.d.ts +8 -0
- package/dist/components/CalculationSelector/CalculationSelector.d.ts.map +1 -0
- package/dist/components/CalculationSelector/CalculationSelector.js +46 -0
- package/dist/components/CalculationSelector/CalculationSelector.js.map +1 -0
- package/dist/components/CalculationSelector/CalculationSelector.test.d.ts +2 -0
- package/dist/components/CalculationSelector/CalculationSelector.test.d.ts.map +1 -0
- package/dist/components/CalculationSelector/CalculationSelector.test.js +58 -0
- package/dist/components/CalculationSelector/CalculationSelector.test.js.map +1 -0
- package/dist/components/CalculationSelector/index.d.ts +2 -0
- package/dist/components/CalculationSelector/index.d.ts.map +1 -0
- package/dist/components/CalculationSelector/index.js +15 -0
- package/dist/components/CalculationSelector/index.js.map +1 -0
- package/dist/components/DatasourceSelect.d.ts +16 -0
- package/dist/components/DatasourceSelect.d.ts.map +1 -0
- package/dist/components/DatasourceSelect.js +73 -0
- package/dist/components/DatasourceSelect.js.map +1 -0
- package/dist/components/OptionsEditorTabs/OptionsEditorTabs.d.ts +34 -0
- package/dist/components/OptionsEditorTabs/OptionsEditorTabs.d.ts.map +1 -0
- package/dist/components/OptionsEditorTabs/OptionsEditorTabs.js +96 -0
- package/dist/components/OptionsEditorTabs/OptionsEditorTabs.js.map +1 -0
- package/dist/components/OptionsEditorTabs/OptionsEditorTabs.test.d.ts +2 -0
- package/dist/components/OptionsEditorTabs/OptionsEditorTabs.test.d.ts.map +1 -0
- package/dist/components/OptionsEditorTabs/OptionsEditorTabs.test.js +160 -0
- package/dist/components/OptionsEditorTabs/OptionsEditorTabs.test.js.map +1 -0
- package/dist/components/OptionsEditorTabs/TabPanel.d.ts +9 -0
- package/dist/components/OptionsEditorTabs/TabPanel.d.ts.map +1 -0
- package/dist/components/OptionsEditorTabs/TabPanel.js +31 -0
- package/dist/components/OptionsEditorTabs/TabPanel.js.map +1 -0
- package/dist/components/OptionsEditorTabs/index.d.ts +2 -0
- package/dist/components/OptionsEditorTabs/index.d.ts.map +1 -0
- package/dist/components/OptionsEditorTabs/index.js +15 -0
- package/dist/components/OptionsEditorTabs/index.js.map +1 -0
- package/dist/components/PluginEditor/PluginEditor.d.ts +12 -0
- package/dist/components/PluginEditor/PluginEditor.d.ts.map +1 -0
- package/dist/components/PluginEditor/PluginEditor.js +68 -0
- package/dist/components/PluginEditor/PluginEditor.js.map +1 -0
- package/dist/components/PluginEditor/PluginEditor.test.d.ts +2 -0
- package/dist/components/PluginEditor/PluginEditor.test.d.ts.map +1 -0
- package/dist/components/PluginEditor/PluginEditor.test.js +143 -0
- package/dist/components/PluginEditor/PluginEditor.test.js.map +1 -0
- package/dist/components/PluginEditor/index.d.ts +3 -0
- package/dist/components/PluginEditor/index.d.ts.map +1 -0
- package/dist/components/PluginEditor/index.js +16 -0
- package/dist/components/PluginEditor/index.js.map +1 -0
- package/dist/components/PluginEditor/plugin-editor-api.d.ts +30 -0
- package/dist/components/PluginEditor/plugin-editor-api.d.ts.map +1 -0
- package/dist/components/PluginEditor/plugin-editor-api.js +100 -0
- package/dist/components/PluginEditor/plugin-editor-api.js.map +1 -0
- package/dist/components/PluginKindSelect.d.ts +12 -0
- package/dist/components/PluginKindSelect.d.ts.map +1 -0
- package/dist/components/PluginKindSelect.js +38 -0
- package/dist/components/PluginKindSelect.js.map +1 -0
- package/dist/components/PluginKindSelect.test.d.ts +2 -0
- package/dist/components/PluginKindSelect.test.d.ts.map +1 -0
- package/dist/components/PluginKindSelect.test.js +79 -0
- package/dist/components/PluginKindSelect.test.js.map +1 -0
- package/dist/components/PluginRegistry/PluginRegistry.d.ts +13 -0
- package/dist/components/PluginRegistry/PluginRegistry.d.ts.map +1 -0
- package/dist/components/PluginRegistry/PluginRegistry.js +82 -0
- package/dist/components/PluginRegistry/PluginRegistry.js.map +1 -0
- package/dist/components/PluginRegistry/PluginRegistry.test.d.ts +2 -0
- package/dist/components/PluginRegistry/PluginRegistry.test.d.ts.map +1 -0
- package/dist/components/PluginRegistry/PluginRegistry.test.js +137 -0
- package/dist/components/PluginRegistry/PluginRegistry.test.js.map +1 -0
- package/dist/components/PluginRegistry/index.d.ts +2 -0
- package/dist/components/PluginRegistry/index.d.ts.map +1 -0
- package/dist/components/PluginRegistry/index.js +15 -0
- package/dist/components/PluginRegistry/index.js.map +1 -0
- package/dist/components/PluginRegistry/plugin-indexes.d.ts +14 -0
- package/dist/components/PluginRegistry/plugin-indexes.d.ts.map +1 -0
- package/dist/components/PluginRegistry/plugin-indexes.js +70 -0
- package/dist/components/PluginRegistry/plugin-indexes.js.map +1 -0
- package/dist/components/PluginSpecEditor.d.ts +9 -0
- package/dist/components/PluginSpecEditor.d.ts.map +1 -0
- package/dist/components/PluginSpecEditor.js +37 -0
- package/dist/components/PluginSpecEditor.js.map +1 -0
- package/dist/components/PluginSpecEditor.test.d.ts +2 -0
- package/dist/components/PluginSpecEditor.test.d.ts.map +1 -0
- package/dist/components/PluginSpecEditor.test.js +63 -0
- package/dist/components/PluginSpecEditor.test.js.map +1 -0
- package/dist/components/TimeSeriesQueryEditor.d.ts +14 -0
- package/dist/components/TimeSeriesQueryEditor.d.ts.map +1 -0
- package/dist/components/TimeSeriesQueryEditor.js +38 -0
- package/dist/components/TimeSeriesQueryEditor.js.map +1 -0
- package/dist/components/index.d.ts +9 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +22 -0
- package/dist/components/index.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/model/calculations.d.ts +21 -0
- package/dist/model/calculations.d.ts.map +1 -0
- package/dist/model/calculations.js +64 -0
- package/dist/model/calculations.js.map +1 -0
- package/dist/model/datasource.d.ts +12 -0
- package/dist/model/datasource.d.ts.map +1 -0
- package/dist/model/datasource.js +15 -0
- package/dist/model/datasource.js.map +1 -0
- package/dist/model/index.d.ts +9 -0
- package/dist/model/index.d.ts.map +1 -0
- package/dist/model/index.js +22 -0
- package/dist/model/index.js.map +1 -0
- package/dist/model/panels.d.ts +20 -0
- package/dist/model/panels.d.ts.map +1 -0
- package/dist/model/panels.js +15 -0
- package/dist/model/panels.js.map +1 -0
- package/dist/model/plugin-base.d.ts +22 -0
- package/dist/model/plugin-base.d.ts.map +1 -0
- package/dist/model/plugin-base.js +15 -0
- package/dist/model/plugin-base.js.map +1 -0
- package/dist/model/plugin-loading.d.ts +22 -0
- package/dist/model/plugin-loading.d.ts.map +1 -0
- package/dist/model/plugin-loading.js +35 -0
- package/dist/model/plugin-loading.js.map +1 -0
- package/dist/model/plugins.d.ts +54 -0
- package/dist/model/plugins.d.ts.map +1 -0
- package/dist/model/plugins.js +15 -0
- package/dist/model/plugins.js.map +1 -0
- package/dist/model/time-series-queries.d.ts +42 -0
- package/dist/model/time-series-queries.d.ts.map +1 -0
- package/dist/model/time-series-queries.js +15 -0
- package/dist/model/time-series-queries.js.map +1 -0
- package/dist/model/variables.d.ts +35 -0
- package/dist/model/variables.d.ts.map +1 -0
- package/dist/model/variables.js +15 -0
- package/dist/model/variables.js.map +1 -0
- package/dist/runtime/TimeRangeProvider/TimeRangeProvider.d.ts +25 -0
- package/dist/runtime/TimeRangeProvider/TimeRangeProvider.d.ts.map +1 -0
- package/dist/runtime/TimeRangeProvider/TimeRangeProvider.js +69 -0
- package/dist/runtime/TimeRangeProvider/TimeRangeProvider.js.map +1 -0
- package/dist/runtime/TimeRangeProvider/index.d.ts +3 -0
- package/dist/runtime/TimeRangeProvider/index.d.ts.map +1 -0
- package/dist/runtime/TimeRangeProvider/index.js +16 -0
- package/dist/runtime/TimeRangeProvider/index.js.map +1 -0
- package/dist/runtime/TimeRangeProvider/query-params.d.ts +25 -0
- package/dist/runtime/TimeRangeProvider/query-params.d.ts.map +1 -0
- package/dist/runtime/TimeRangeProvider/query-params.js +153 -0
- package/dist/runtime/TimeRangeProvider/query-params.js.map +1 -0
- package/dist/runtime/datasources.d.ts +25 -0
- package/dist/runtime/datasources.d.ts.map +1 -0
- package/dist/runtime/datasources.js +34 -0
- package/dist/runtime/datasources.js.map +1 -0
- package/dist/runtime/index.d.ts +6 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +19 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/plugin-registry.d.ts +32 -0
- package/dist/runtime/plugin-registry.d.ts.map +1 -0
- package/dist/runtime/plugin-registry.js +69 -0
- package/dist/runtime/plugin-registry.js.map +1 -0
- package/dist/runtime/template-variables.d.ts +17 -0
- package/dist/runtime/template-variables.d.ts.map +1 -0
- package/dist/runtime/template-variables.js +44 -0
- package/dist/runtime/template-variables.js.map +1 -0
- package/dist/runtime/time-series-queries.d.ts +13 -0
- package/dist/runtime/time-series-queries.d.ts.map +1 -0
- package/dist/runtime/time-series-queries.js +132 -0
- package/dist/runtime/time-series-queries.js.map +1 -0
- package/dist/test/index.d.ts +2 -0
- package/dist/test/index.d.ts.map +1 -0
- package/dist/test/index.js +15 -0
- package/dist/test/index.js.map +1 -0
- package/dist/test/render.d.ts +13 -0
- package/dist/test/render.d.ts.map +1 -0
- package/dist/test/render.js +49 -0
- package/dist/test/render.js.map +1 -0
- package/dist/test/setup-tests.d.ts +2 -0
- package/dist/test/setup-tests.d.ts.map +1 -0
- package/dist/test/setup-tests.js +18 -0
- package/dist/test/setup-tests.js.map +1 -0
- package/dist/test/test-plugins/bert/index.d.ts +8 -0
- package/dist/test/test-plugins/bert/index.d.ts.map +1 -0
- package/dist/test/test-plugins/bert/index.js +66 -0
- package/dist/test/test-plugins/bert/index.js.map +1 -0
- package/dist/test/test-plugins/ernie/index.d.ts +5 -0
- package/dist/test/test-plugins/ernie/index.d.ts.map +1 -0
- package/dist/test/test-plugins/ernie/index.js +53 -0
- package/dist/test/test-plugins/ernie/index.js.map +1 -0
- package/dist/test/test-plugins/index.d.ts +6 -0
- package/dist/test/test-plugins/index.d.ts.map +1 -0
- package/dist/test/test-plugins/index.js +29 -0
- package/dist/test/test-plugins/index.js.map +1 -0
- package/dist/test-utils/index.d.ts +2 -0
- package/dist/test-utils/index.d.ts.map +1 -0
- package/dist/test-utils/index.js +15 -0
- package/dist/test-utils/index.js.map +1 -0
- package/dist/test-utils/mock-plugin-registry.d.ts +20 -0
- package/dist/test-utils/mock-plugin-registry.d.ts.map +1 -0
- package/dist/test-utils/mock-plugin-registry.js +62 -0
- package/dist/test-utils/mock-plugin-registry.js.map +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Copyright 2022 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 { useMemo, useCallback, useEffect, useState } from 'react';
|
|
14
|
+
import { useQueryParams } from 'use-query-params';
|
|
15
|
+
import { getUnixTime, isDate } from 'date-fns';
|
|
16
|
+
import { isRelativeTimeRange, isDurationString } from '@perses-dev/core';
|
|
17
|
+
/* Interprets an encoded string and returns either the string or null/undefined if not available */ function getEncodedValue(input, allowEmptyString) {
|
|
18
|
+
if (input == null) {
|
|
19
|
+
return input;
|
|
20
|
+
}
|
|
21
|
+
// '' or []
|
|
22
|
+
if (input.length === 0 && (!allowEmptyString || allowEmptyString && input !== '')) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const str = input instanceof Array ? input[0] : input;
|
|
26
|
+
if (str == null) {
|
|
27
|
+
return str;
|
|
28
|
+
}
|
|
29
|
+
if (!allowEmptyString && str === '') {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return str;
|
|
33
|
+
}
|
|
34
|
+
/* Encodes individual TimeRangeValue as a string, depends on whether start is relative or absolute */ export function encodeTimeRangeValue(timeOptionValue) {
|
|
35
|
+
if (!timeOptionValue) {
|
|
36
|
+
return timeOptionValue;
|
|
37
|
+
}
|
|
38
|
+
if (typeof timeOptionValue === 'string') {
|
|
39
|
+
if (isDurationString(timeOptionValue)) {
|
|
40
|
+
return timeOptionValue;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return (getUnixTime(timeOptionValue) * 1000).toString();
|
|
44
|
+
}
|
|
45
|
+
/* Converts param input to supported relative or absolute time range format */ export function decodeTimeRangeValue(input) {
|
|
46
|
+
const paramString = getEncodedValue(input);
|
|
47
|
+
if (paramString == null) return paramString;
|
|
48
|
+
return isDurationString(paramString) ? paramString : new Date(Number(paramString));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Custom TimeRangeValue param type
|
|
52
|
+
* See: https://github.com/pbeshai/use-query-params/tree/master/packages/serialize-query-params#param-types
|
|
53
|
+
*/ export const TimeRangeParam = {
|
|
54
|
+
encode: encodeTimeRangeValue,
|
|
55
|
+
decode: decodeTimeRangeValue,
|
|
56
|
+
equals: (valueA, valueB)=>{
|
|
57
|
+
if (valueA === valueB) return true;
|
|
58
|
+
if (valueA == null || valueB == null) return valueA === valueB;
|
|
59
|
+
return valueA.valueOf() === valueB.valueOf();
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
export const timeRangeQueryConfig = {
|
|
63
|
+
start: TimeRangeParam,
|
|
64
|
+
end: TimeRangeParam
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Gets the initial time range taking into account URL params and dashboard JSON duration
|
|
68
|
+
* Sets start query param if it is empty on page load
|
|
69
|
+
*/ export function useInitialTimeRange(dashboardDuration) {
|
|
70
|
+
const [query] = useQueryParams(timeRangeQueryConfig, {
|
|
71
|
+
updateType: 'replaceIn'
|
|
72
|
+
});
|
|
73
|
+
const { start , end } = query;
|
|
74
|
+
return useMemo(()=>{
|
|
75
|
+
let initialTimeRange = {
|
|
76
|
+
pastDuration: dashboardDuration
|
|
77
|
+
};
|
|
78
|
+
if (!start) {
|
|
79
|
+
return initialTimeRange;
|
|
80
|
+
}
|
|
81
|
+
const startStr = start.toString();
|
|
82
|
+
if (isDurationString(startStr)) {
|
|
83
|
+
initialTimeRange = {
|
|
84
|
+
pastDuration: startStr
|
|
85
|
+
};
|
|
86
|
+
} else if (isDate(start) && isDate(end)) {
|
|
87
|
+
initialTimeRange = {
|
|
88
|
+
start: start,
|
|
89
|
+
end: end
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return initialTimeRange;
|
|
93
|
+
}, [
|
|
94
|
+
start,
|
|
95
|
+
end,
|
|
96
|
+
dashboardDuration
|
|
97
|
+
]);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns time range getter and setter, set enabledURLParams to false to disable query string serialization
|
|
101
|
+
*/ export function useSetTimeRangeParams(initialTimeRange, enabledURLParams = true) {
|
|
102
|
+
const [query, setQuery] = useQueryParams(timeRangeQueryConfig, {
|
|
103
|
+
updateType: 'replaceIn'
|
|
104
|
+
});
|
|
105
|
+
// determine whether initial param had previously been populated to fix back btn
|
|
106
|
+
const [paramsLoaded, setParamsLoaded] = useState(false);
|
|
107
|
+
// optional fallback when app does not want query string as source of truth
|
|
108
|
+
// this occurs when enabledURLParams is set to false on TimeRangeProvider
|
|
109
|
+
const [timeRangeState, setTimeRangeState] = useState(initialTimeRange);
|
|
110
|
+
const { start } = query;
|
|
111
|
+
useEffect(()=>{
|
|
112
|
+
// when dashboard loaded with no params, default to dashboard duration
|
|
113
|
+
if (enabledURLParams && !paramsLoaded && !start) {
|
|
114
|
+
if (isRelativeTimeRange(initialTimeRange)) {
|
|
115
|
+
setQuery({
|
|
116
|
+
start: initialTimeRange.pastDuration,
|
|
117
|
+
end: undefined
|
|
118
|
+
});
|
|
119
|
+
setParamsLoaded(true);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}, [
|
|
123
|
+
initialTimeRange,
|
|
124
|
+
enabledURLParams,
|
|
125
|
+
paramsLoaded,
|
|
126
|
+
start,
|
|
127
|
+
setQuery
|
|
128
|
+
]);
|
|
129
|
+
const setTimeRange = useCallback((value)=>{
|
|
130
|
+
if (isRelativeTimeRange(value)) {
|
|
131
|
+
setQuery({
|
|
132
|
+
start: value.pastDuration,
|
|
133
|
+
end: undefined
|
|
134
|
+
});
|
|
135
|
+
} else {
|
|
136
|
+
setQuery(value);
|
|
137
|
+
}
|
|
138
|
+
}, [
|
|
139
|
+
setQuery
|
|
140
|
+
]);
|
|
141
|
+
if (!enabledURLParams) {
|
|
142
|
+
return {
|
|
143
|
+
timeRange: timeRangeState,
|
|
144
|
+
setTimeRange: setTimeRangeState
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
timeRange: initialTimeRange,
|
|
149
|
+
setTimeRange: setTimeRange
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
//# sourceMappingURL=query-params.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/runtime/TimeRangeProvider/query-params.ts"],"sourcesContent":["// Copyright 2022 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 { useMemo, useCallback, useEffect, useState } from 'react';\nimport { useQueryParams, QueryParamConfig } from 'use-query-params';\nimport { getUnixTime, isDate } from 'date-fns';\nimport {\n TimeRangeValue,\n isRelativeTimeRange,\n isDurationString,\n DurationString,\n AbsoluteTimeRange,\n} from '@perses-dev/core';\nimport { TimeRange } from './TimeRangeProvider';\n\nexport type TimeOptionValue = Date | DurationString | null | undefined;\n\n/* Interprets an encoded string and returns either the string or null/undefined if not available */\nfunction getEncodedValue(\n input: string | Array<string | null> | null | undefined,\n allowEmptyString?: boolean\n): string | null | undefined {\n if (input == null) {\n return input;\n }\n // '' or []\n if (input.length === 0 && (!allowEmptyString || (allowEmptyString && input !== ''))) {\n return null;\n }\n\n const str = input instanceof Array ? input[0] : input;\n if (str == null) {\n return str;\n }\n if (!allowEmptyString && str === '') {\n return null;\n }\n\n return str;\n}\n\n/* Encodes individual TimeRangeValue as a string, depends on whether start is relative or absolute */\nexport function encodeTimeRangeValue(timeOptionValue: TimeOptionValue): string | null | undefined {\n if (!timeOptionValue) {\n return timeOptionValue;\n }\n\n if (typeof timeOptionValue === 'string') {\n if (isDurationString(timeOptionValue)) {\n return timeOptionValue;\n }\n }\n return (getUnixTime(timeOptionValue) * 1000).toString();\n}\n\n/* Converts param input to supported relative or absolute time range format */\nexport function decodeTimeRangeValue(\n input: string | Array<string | null> | null | undefined\n): Date | DurationString | null | undefined {\n const paramString = getEncodedValue(input);\n if (paramString == null) return paramString;\n return isDurationString(paramString) ? paramString : new Date(Number(paramString));\n}\n\n/**\n * Custom TimeRangeValue param type\n * See: https://github.com/pbeshai/use-query-params/tree/master/packages/serialize-query-params#param-types\n */\nexport const TimeRangeParam: QueryParamConfig<TimeOptionValue, TimeOptionValue> = {\n encode: encodeTimeRangeValue,\n decode: decodeTimeRangeValue,\n equals: (valueA: TimeOptionValue, valueB: TimeOptionValue) => {\n if (valueA === valueB) return true;\n if (valueA == null || valueB == null) return valueA === valueB;\n return valueA.valueOf() === valueB.valueOf();\n },\n};\n\nexport const timeRangeQueryConfig = {\n start: TimeRangeParam,\n end: TimeRangeParam,\n};\n\n/**\n * Gets the initial time range taking into account URL params and dashboard JSON duration\n * Sets start query param if it is empty on page load\n */\nexport function useInitialTimeRange(dashboardDuration: DurationString): TimeRangeValue {\n const [query] = useQueryParams(timeRangeQueryConfig, { updateType: 'replaceIn' });\n const { start, end } = query;\n return useMemo(() => {\n let initialTimeRange: TimeRangeValue = { pastDuration: dashboardDuration };\n if (!start) {\n return initialTimeRange;\n }\n const startStr = start.toString();\n if (isDurationString(startStr)) {\n initialTimeRange = { pastDuration: startStr };\n } else if (isDate(start) && isDate(end)) {\n initialTimeRange = { start: start, end: end } as AbsoluteTimeRange;\n }\n return initialTimeRange;\n }, [start, end, dashboardDuration]);\n}\n\n/**\n * Returns time range getter and setter, set enabledURLParams to false to disable query string serialization\n */\nexport function useSetTimeRangeParams(\n initialTimeRange: TimeRangeValue,\n enabledURLParams = true\n): Pick<TimeRange, 'timeRange' | 'setTimeRange'> {\n const [query, setQuery] = useQueryParams(timeRangeQueryConfig, { updateType: 'replaceIn' });\n\n // determine whether initial param had previously been populated to fix back btn\n const [paramsLoaded, setParamsLoaded] = useState<boolean>(false);\n\n // optional fallback when app does not want query string as source of truth\n // this occurs when enabledURLParams is set to false on TimeRangeProvider\n const [timeRangeState, setTimeRangeState] = useState<TimeRangeValue>(initialTimeRange);\n\n const { start } = query;\n\n useEffect(() => {\n // when dashboard loaded with no params, default to dashboard duration\n if (enabledURLParams && !paramsLoaded && !start) {\n if (isRelativeTimeRange(initialTimeRange)) {\n setQuery({ start: initialTimeRange.pastDuration, end: undefined });\n setParamsLoaded(true);\n }\n }\n }, [initialTimeRange, enabledURLParams, paramsLoaded, start, setQuery]);\n\n const setTimeRange: TimeRange['setTimeRange'] = useCallback(\n (value: TimeRangeValue) => {\n if (isRelativeTimeRange(value)) {\n setQuery({ start: value.pastDuration, end: undefined });\n } else {\n setQuery(value);\n }\n },\n [setQuery]\n );\n\n if (!enabledURLParams) {\n return { timeRange: timeRangeState, setTimeRange: setTimeRangeState };\n }\n return { timeRange: initialTimeRange, setTimeRange: setTimeRange };\n}\n"],"names":["useMemo","useCallback","useEffect","useState","useQueryParams","getUnixTime","isDate","isRelativeTimeRange","isDurationString","getEncodedValue","input","allowEmptyString","length","str","Array","encodeTimeRangeValue","timeOptionValue","toString","decodeTimeRangeValue","paramString","Date","Number","TimeRangeParam","encode","decode","equals","valueA","valueB","valueOf","timeRangeQueryConfig","start","end","useInitialTimeRange","dashboardDuration","query","updateType","initialTimeRange","pastDuration","startStr","useSetTimeRangeParams","enabledURLParams","setQuery","paramsLoaded","setParamsLoaded","timeRangeState","setTimeRangeState","undefined","setTimeRange","value","timeRange"],"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,SAASA,OAAO,EAAEC,WAAW,EAAEC,SAAS,EAAEC,QAAQ,QAAQ,OAAO,CAAC;AAClE,SAASC,cAAc,QAA0B,kBAAkB,CAAC;AACpE,SAASC,WAAW,EAAEC,MAAM,QAAQ,UAAU,CAAC;AAC/C,SAEEC,mBAA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AAW,CACzD,CAAC+C,KAAqB,GAAK;QACzB,IAAIzC,mBAAmB,CAACyC,KAAK,CAAC,EAAE;YAC9BP,QAAQ,CAAC;gBAAEX,KAAK,EAAEkB,KAAK,CAACX,YAAY;gBAAEN,GAAG,EAAEe,SAAS;aAAE,CAAC,CAAC;QAC1D,OAAO;YACLL,QAAQ,CAACO,KAAK,CAAC,CAAC;QAClB,CAAC;IACH,CAAC,EACD;QAACP,QAAQ;KAAC,CACX,AAAC;IAEF,IAAI,CAACD,gBAAgB,EAAE;QACrB,OAAO;YAAES,SAAS,EAAEL,cAAc;YAAEG,YAAY,EAAEF,iBAAiB;SAAE,CAAC;IACxE,CAAC;IACD,OAAO;QAAEI,SAAS,EAAEb,gBAAgB;QAAEW,YAAY,EAAEA,YAAY;KAAE,CAAC;AACrE,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { DatasourceSelector, DatasourceSpec } from '@perses-dev/core';
|
|
3
|
+
export interface DatasourceStore {
|
|
4
|
+
getDatasource(selector: DatasourceSelector): Promise<DatasourceSpec>;
|
|
5
|
+
/**
|
|
6
|
+
* Given a DatasourceSelector, gets a `Client` object from the corresponding Datasource plugin.
|
|
7
|
+
*/
|
|
8
|
+
getDatasourceClient<Client>(selector: DatasourceSelector): Promise<Client>;
|
|
9
|
+
/**
|
|
10
|
+
* Gets a list of datasource metadata for a plugin kind.
|
|
11
|
+
*/
|
|
12
|
+
listDatasourceMetadata(datasourcePluginKind: string): Promise<DatasourceMetadata[]>;
|
|
13
|
+
}
|
|
14
|
+
export interface DatasourceMetadata {
|
|
15
|
+
name: string;
|
|
16
|
+
selector: DatasourceSelector;
|
|
17
|
+
}
|
|
18
|
+
export declare const DatasourceStoreContext: import("react").Context<DatasourceStore | undefined>;
|
|
19
|
+
export declare function useDatasourceStore(): DatasourceStore;
|
|
20
|
+
/**
|
|
21
|
+
* Lists all available Datasource instances for a given datasource plugin kind. Returns a list with the name of that
|
|
22
|
+
* instance, as well as its DatasourceSelector for referencing it.
|
|
23
|
+
*/
|
|
24
|
+
export declare function useListDatasources(datasourcePluginKind: string): import("@tanstack/react-query").UseQueryResult<DatasourceMetadata[], unknown>;
|
|
25
|
+
//# sourceMappingURL=datasources.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"datasources.d.ts","sourceRoot":"","sources":["../../src/runtime/datasources.ts"],"names":[],"mappings":";AAaA,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAItE,MAAM,WAAW,eAAe;IAE9B,aAAa,CAAC,QAAQ,EAAE,kBAAkB,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IAErE;;OAEG;IACH,mBAAmB,CAAC,MAAM,EAAE,QAAQ,EAAE,kBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE3E;;OAEG;IACH,sBAAsB,CAAC,oBAAoB,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC,CAAC;CACrF;AAED,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,kBAAkB,CAAC;CAC9B;AAED,eAAO,MAAM,sBAAsB,sDAAwD,CAAC;AAE5F,wBAAgB,kBAAkB,oBAMjC;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,oBAAoB,EAAE,MAAM,iFAG9D"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Copyright 2022 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 { useQuery } from '@tanstack/react-query';
|
|
14
|
+
import { createContext, useContext } from 'react';
|
|
15
|
+
export const DatasourceStoreContext = createContext(undefined);
|
|
16
|
+
export function useDatasourceStore() {
|
|
17
|
+
const ctx = useContext(DatasourceStoreContext);
|
|
18
|
+
if (ctx === undefined) {
|
|
19
|
+
throw new Error('No DatasourceStoreContext found. Did you forget a Provider?');
|
|
20
|
+
}
|
|
21
|
+
return ctx;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Lists all available Datasource instances for a given datasource plugin kind. Returns a list with the name of that
|
|
25
|
+
* instance, as well as its DatasourceSelector for referencing it.
|
|
26
|
+
*/ export function useListDatasources(datasourcePluginKind) {
|
|
27
|
+
const { listDatasourceMetadata } = useDatasourceStore();
|
|
28
|
+
return useQuery([
|
|
29
|
+
'listDatasourceMetadata',
|
|
30
|
+
datasourcePluginKind
|
|
31
|
+
], ()=>listDatasourceMetadata(datasourcePluginKind));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=datasources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/datasources.ts"],"sourcesContent":["// Copyright 2022 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 { DatasourceSelector, DatasourceSpec } from '@perses-dev/core';\nimport { useQuery } from '@tanstack/react-query';\nimport { createContext, useContext } from 'react';\n\nexport interface DatasourceStore {\n // TODO: Do we even need this method?\n getDatasource(selector: DatasourceSelector): Promise<DatasourceSpec>;\n\n /**\n * Given a DatasourceSelector, gets a `Client` object from the corresponding Datasource plugin.\n */\n getDatasourceClient<Client>(selector: DatasourceSelector): Promise<Client>;\n\n /**\n * Gets a list of datasource metadata for a plugin kind.\n */\n listDatasourceMetadata(datasourcePluginKind: string): Promise<DatasourceMetadata[]>;\n}\n\nexport interface DatasourceMetadata {\n name: string;\n selector: DatasourceSelector;\n}\n\nexport const DatasourceStoreContext = createContext<DatasourceStore | undefined>(undefined);\n\nexport function useDatasourceStore() {\n const ctx = useContext(DatasourceStoreContext);\n if (ctx === undefined) {\n throw new Error('No DatasourceStoreContext found. Did you forget a Provider?');\n }\n return ctx;\n}\n\n/**\n * Lists all available Datasource instances for a given datasource plugin kind. Returns a list with the name of that\n * instance, as well as its DatasourceSelector for referencing it.\n */\nexport function useListDatasources(datasourcePluginKind: string) {\n const { listDatasourceMetadata } = useDatasourceStore();\n return useQuery(['listDatasourceMetadata', datasourcePluginKind], () => listDatasourceMetadata(datasourcePluginKind));\n}\n"],"names":["useQuery","createContext","useContext","DatasourceStoreContext","undefined","useDatasourceStore","ctx","Error","useListDatasources","datasourcePluginKind","listDatasourceMetadata"],"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;AAGjC,SAASA,QAAQ,QAAQ,uBAAuB,CAAC;AACjD,SAASC,aAAa,EAAEC,UAAU,QAAQ,OAAO,CAAC;AAsBlD,OAAO,MAAMC,sBAAsB,GAAGF,aAAa,CAA8BG,SAAS,CAAC,CAAC;AAE5F,OAAO,SAASC,kBAAkB,GAAG;IACnC,MAAMC,GAAG,GAAGJ,UAAU,CAACC,sBAAsB,CAAC,AAAC;IAC/C,IAAIG,GAAG,KAAKF,SAAS,EAAE;QACrB,MAAM,IAAIG,KAAK,CAAC,6DAA6D,CAAC,CAAC;IACjF,CAAC;IACD,OAAOD,GAAG,CAAC;AACb,CAAC;AAED;;;CAGC,GACD,OAAO,SAASE,kBAAkB,CAACC,oBAA4B,EAAE;IAC/D,MAAM,EAAEC,sBAAsB,CAAA,EAAE,GAAGL,kBAAkB,EAAE,AAAC;IACxD,OAAOL,QAAQ,CAAC;QAAC,wBAAwB;QAAES,oBAAoB;KAAC,EAAE,IAAMC,sBAAsB,CAACD,oBAAoB,CAAC,CAAC,CAAC;AACxH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAaA,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Copyright 2022 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
|
+
export * from './datasources';
|
|
14
|
+
export * from './plugin-registry';
|
|
15
|
+
export * from './template-variables';
|
|
16
|
+
export * from './TimeRangeProvider';
|
|
17
|
+
export * from './time-series-queries';
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/index.ts"],"sourcesContent":["// Copyright 2022 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\nexport * from './datasources';\nexport * from './plugin-registry';\nexport * from './template-variables';\nexport * from './TimeRangeProvider';\nexport * from './time-series-queries';\n"],"names":[],"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,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { UseQueryOptions } from '@tanstack/react-query';
|
|
3
|
+
import { DefaultPluginKinds, PluginImplementation, PluginMetadata, PluginType } from '../model';
|
|
4
|
+
export interface PluginRegistryContextType {
|
|
5
|
+
getPlugin<T extends PluginType>(pluginType: T, kind: string): Promise<PluginImplementation<T>>;
|
|
6
|
+
listPluginMetadata(pluginType: PluginType): Promise<PluginMetadata[]>;
|
|
7
|
+
defaultPluginKinds?: DefaultPluginKinds;
|
|
8
|
+
}
|
|
9
|
+
export declare const PluginRegistryContext: import("react").Context<PluginRegistryContextType | undefined>;
|
|
10
|
+
/**
|
|
11
|
+
* Use the PluginRegistry context directly. This is meant as an escape hatch for custom async flows. You should probably
|
|
12
|
+
* be using `usePlugin` or `useListPluginMetadata` instead.
|
|
13
|
+
*/
|
|
14
|
+
export declare function usePluginRegistry(): PluginRegistryContextType;
|
|
15
|
+
declare type UsePluginOptions<T extends PluginType> = Omit<UseQueryOptions<PluginImplementation<T>, Error, PluginImplementation<T>, [string, PluginType, string]>, 'queryKey' | 'queryFn'>;
|
|
16
|
+
/**
|
|
17
|
+
* Loads a plugin and returns the plugin implementation, along with loading/error state.
|
|
18
|
+
*/
|
|
19
|
+
export declare function usePlugin<T extends PluginType>(pluginType: T, kind: string, options?: UsePluginOptions<T>): import("@tanstack/react-query").UseQueryResult<PluginImplementation<T>, Error>;
|
|
20
|
+
/**
|
|
21
|
+
* Loads a list of plugins and returns the plugin implementation, along with loading/error state.
|
|
22
|
+
*/
|
|
23
|
+
export declare function usePlugins<T extends PluginType>(pluginType: T, plugins: Array<{
|
|
24
|
+
kind: string;
|
|
25
|
+
}>): import("@tanstack/react-query").UseQueryResult<PluginImplementation<T>, unknown>[];
|
|
26
|
+
declare type UseListPluginMetadataOptions = Omit<UseQueryOptions<PluginMetadata[], Error, PluginMetadata[], [string, PluginType]>, 'queryKey' | 'queryFn'>;
|
|
27
|
+
/**
|
|
28
|
+
* Gets a list of plugin metadata for the specified plugin type and returns it, along with loading/error state.
|
|
29
|
+
*/
|
|
30
|
+
export declare function useListPluginMetadata(pluginType: PluginType, options?: UseListPluginMetadataOptions): import("@tanstack/react-query").UseQueryResult<PluginMetadata[], Error>;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=plugin-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin-registry.d.ts","sourceRoot":"","sources":["../../src/runtime/plugin-registry.ts"],"names":[],"mappings":";AAcA,OAAO,EAAwB,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC9E,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAEhG,MAAM,WAAW,yBAAyB;IACxC,SA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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 { createContext, useContext } from 'react';
|
|
14
|
+
import { useQuery, useQueries } from '@tanstack/react-query';
|
|
15
|
+
export const PluginRegistryContext = createContext(undefined);
|
|
16
|
+
/**
|
|
17
|
+
* Use the PluginRegistry context directly. This is meant as an escape hatch for custom async flows. You should probably
|
|
18
|
+
* be using `usePlugin` or `useListPluginMetadata` instead.
|
|
19
|
+
*/ export function usePluginRegistry() {
|
|
20
|
+
const ctx = useContext(PluginRegistryContext);
|
|
21
|
+
if (ctx === undefined) {
|
|
22
|
+
throw new Error('PluginRegistryContext not found. Did you forget a provider?');
|
|
23
|
+
}
|
|
24
|
+
return ctx;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Loads a plugin and returns the plugin implementation, along with loading/error state.
|
|
28
|
+
*/ export function usePlugin(pluginType, kind, options) {
|
|
29
|
+
var ref;
|
|
30
|
+
// We never want to ask for a plugin when the kind isn't set yet, so disable those queries automatically
|
|
31
|
+
options = {
|
|
32
|
+
...options,
|
|
33
|
+
enabled: ((ref = options === null || options === void 0 ? void 0 : options.enabled) !== null && ref !== void 0 ? ref : true) && kind !== ''
|
|
34
|
+
};
|
|
35
|
+
const { getPlugin } = usePluginRegistry();
|
|
36
|
+
return useQuery([
|
|
37
|
+
'getPlugin',
|
|
38
|
+
pluginType,
|
|
39
|
+
kind
|
|
40
|
+
], ()=>getPlugin(pluginType, kind), options);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Loads a list of plugins and returns the plugin implementation, along with loading/error state.
|
|
44
|
+
*/ export function usePlugins(pluginType, plugins) {
|
|
45
|
+
const { getPlugin } = usePluginRegistry();
|
|
46
|
+
return useQueries({
|
|
47
|
+
queries: plugins.map((p)=>{
|
|
48
|
+
return {
|
|
49
|
+
queryKey: [
|
|
50
|
+
'getPlugin',
|
|
51
|
+
pluginType,
|
|
52
|
+
p.kind
|
|
53
|
+
],
|
|
54
|
+
queryFn: ()=>getPlugin(pluginType, p.kind)
|
|
55
|
+
};
|
|
56
|
+
})
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Gets a list of plugin metadata for the specified plugin type and returns it, along with loading/error state.
|
|
61
|
+
*/ export function useListPluginMetadata(pluginType, options) {
|
|
62
|
+
const { listPluginMetadata } = usePluginRegistry();
|
|
63
|
+
return useQuery([
|
|
64
|
+
'listPluginMetadata',
|
|
65
|
+
pluginType
|
|
66
|
+
], ()=>listPluginMetadata(pluginType), options);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=plugin-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/plugin-registry.ts"],"sourcesContent":["// Copyright 2022 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 { createContext, useContext } from 'react';\nimport { useQuery, useQueries, UseQueryOptions } from '@tanstack/react-query';\nimport { DefaultPluginKinds, PluginImplementation, PluginMetadata, PluginType } from '../model';\n\nexport interface PluginRegistryContextType {\n getPlugin<T extends PluginType>(pluginType: T, kind: string): Promise<PluginImplementation<T>>;\n listPluginMetadata(pluginType: PluginType): Promise<PluginMetadata[]>;\n defaultPluginKinds?: DefaultPluginKinds;\n}\n\nexport const PluginRegistryContext = createContext<PluginRegistryContextType | undefined>(undefined);\n\n/**\n * Use the PluginRegistry context directly. This is meant as an escape hatch for custom async flows. You should probably\n * be using `usePlugin` or `useListPluginMetadata` instead.\n */\nexport function usePluginRegistry() {\n const ctx = useContext(PluginRegistryContext);\n if (ctx === undefined) {\n throw new Error('PluginRegistryContext not found. Did you forget a provider?');\n }\n return ctx;\n}\n\n// Allows consumers to pass useQuery options from react-query when loading a plugin\ntype UsePluginOptions<T extends PluginType> = Omit<\n UseQueryOptions<PluginImplementation<T>, Error, PluginImplementation<T>, [string, PluginType, string]>,\n 'queryKey' | 'queryFn'\n>;\n\n/**\n * Loads a plugin and returns the plugin implementation, along with loading/error state.\n */\nexport function usePlugin<T extends PluginType>(pluginType: T, kind: string, options?: UsePluginOptions<T>) {\n // We never want to ask for a plugin when the kind isn't set yet, so disable those queries automatically\n options = {\n ...options,\n enabled: (options?.enabled ?? true) && kind !== '',\n };\n const { getPlugin } = usePluginRegistry();\n return useQuery(['getPlugin', pluginType, kind], () => getPlugin(pluginType, kind), options);\n}\n\n/**\n * Loads a list of plugins and returns the plugin implementation, along with loading/error state.\n */\nexport function usePlugins<T extends PluginType>(pluginType: T, plugins: Array<{ kind: string }>) {\n const { getPlugin } = usePluginRegistry();\n return useQueries({\n queries: plugins.map((p) => {\n return {\n queryKey: ['getPlugin', pluginType, p.kind],\n queryFn: () => getPlugin(pluginType, p.kind),\n };\n }),\n });\n}\n\n// Allow consumers to pass useQuery options from react-query when listing metadata\ntype UseListPluginMetadataOptions = Omit<\n UseQueryOptions<PluginMetadata[], Error, PluginMetadata[], [string, PluginType]>,\n 'queryKey' | 'queryFn'\n>;\n\n/**\n * Gets a list of plugin metadata for the specified plugin type and returns it, along with loading/error state.\n */\nexport function useListPluginMetadata(pluginType: PluginType, options?: UseListPluginMetadataOptions) {\n const { listPluginMetadata } = usePluginRegistry();\n return useQuery(['listPluginMetadata', pluginType], () => listPluginMetadata(pluginType), options);\n}\n"],"names":["createContext","useContext","useQuery","useQueries","PluginRegistryContext","undefined","usePluginRegistry","ctx","Error","usePlugin","pluginType","kind","options","enabled","getPlugin","usePlugins","plugins","queries","map","p","queryKey","queryFn","useListPluginMetadata","listPluginMetadata"],"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,SAASA,aAAa,EAAEC,UAAU,QAAQ,OAAO,CAAC;AAClD,SAASC,QAAQ,EAAEC,UAAU,QAAyB,uBAAuB,CAAC;AAS9E,OAAO,MAAMC,qBAAqB,GAAGJ,aAAa,CAAwCK,SAAS,CAAC,CAAC;AAErG;;;CAGC,GACD,OAAO,SAASC,iBAAiB,GAAG;IAClC,MAAMC,GAAG,GAAGN,UAAU,CAACG,qBAAqB,CAAC,AAAC;IAC9C,IAAIG,GAAG,KAAKF,SAAS,EAAE;QACrB,MAAM,IAAIG,KAAK,CAAC,6DAA6D,CAAC,CAAC;IACjF,CAAC;IACD,OAAOD,GAAG,CAAC;AACb,CAAC;AAQD;;CAEC,GACD,OAAO,SAASE,SAAS,CAAuBC,UAAa,EAAEC,IAAY,EAAEC,OAA6B,EAAE;QAI9FA,GAAgB;IAH5B,wGAAwG;IACxGA,OAAO,GAAG;QACR,GAAGA,OAAO;QACVC,OAAO,EAAE,AAACD,CAAAA,CAAAA,GAAgB,GAAhBA,OAAO,aAAPA,OAAO,WAAS,GAAhBA,KAAAA,CAAgB,GAAhBA,OAAO,CAAEC,OAAO,cAAhBD,GAAgB,cAAhBA,GAAgB,GAAI,IAAI,CAAA,IAAKD,IAAI,KAAK,EAAE;KACnD,CAAC;IACF,MAAM,EAAEG,SAAS,CAAA,EAAE,GAAGR,iBAAiB,EAAE,AAAC;IAC1C,OAAOJ,QAAQ,CAAC;QAAC,WAAW;QAAEQ,UAAU;QAAEC,IAAI;KAAC,EAAE,IAAMG,SAAS,CAACJ,UAAU,EAAEC,IAAI,CAAC,EAAEC,OAAO,CAAC,CAAC;AAC/F,CAAC;AAED;;CAEC,GACD,OAAO,SAASG,UAAU,CAAuBL,UAAa,EAAEM,OAAgC,EAAE;IAChG,MAAM,EAAEF,SAAS,CAAA,EAAE,GAAGR,iBAAiB,EAAE,AAAC;IAC1C,OAAOH,UAAU,CAAC;QAChBc,OAAO,EAAED,OAAO,CAACE,GAAG,CAAC,CAACC,CAAC,GAAK;YAC1B,OAAO;gBACLC,QAAQ,EAAE;oBAAC,WAAW;oBAAEV,UAAU;oBAAES,CAAC,CAACR,IAAI;iBAAC;gBAC3CU,OAAO,EAAE,IAAMP,SAAS,CAACJ,UAAU,EAAES,CAAC,CAACR,IAAI,CAAC;aAC7C,CAAC;QACJ,CAAC,CAAC;KACH,CAAC,CAAC;AACL,CAAC;AAQD;;CAEC,GACD,OAAO,SAASW,qBAAqB,CAACZ,UAAsB,EAAEE,OAAsC,EAAE;IACpG,MAAM,EAAEW,kBAAkB,CAAA,EAAE,GAAGjB,iBAAiB,EAAE,AAAC;IACnD,OAAOJ,QAAQ,CAAC;QAAC,oBAAoB;QAAEQ,UAAU;KAAC,EAAE,IAAMa,kBAAkB,CAACb,UAAU,CAAC,EAAEE,OAAO,CAAC,CAAC;AACrG,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { VariableName, VariableValue } from '@perses-dev/core';
|
|
3
|
+
import { VariableOption } from '../model';
|
|
4
|
+
export declare const DEFAULT_ALL_VALUE: "$__all";
|
|
5
|
+
export declare type VariableState = {
|
|
6
|
+
value: VariableValue;
|
|
7
|
+
options?: VariableOption[];
|
|
8
|
+
loading: boolean;
|
|
9
|
+
error?: Error;
|
|
10
|
+
};
|
|
11
|
+
export declare type VariableStateMap = Record<VariableName, VariableState>;
|
|
12
|
+
export declare type TemplateVariableSrv = {
|
|
13
|
+
state: VariableStateMap;
|
|
14
|
+
};
|
|
15
|
+
export declare const TemplateVariableContext: import("react").Context<TemplateVariableSrv | undefined>;
|
|
16
|
+
export declare function useTemplateVariableValues(names?: string[]): VariableStateMap;
|
|
17
|
+
//# sourceMappingURL=template-variables.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"template-variables.d.ts","sourceRoot":"","sources":["../../src/runtime/template-variables.ts"],"names":[],"mappings":";AAcA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE1C,eAAO,MAAM,iBAAiB,UAAoB,CAAC;AAEnD,oBAAY,aAAa,GAAG;IAC1B,KAAK,EAAE,aAAa,CAAC;IACrB,OAAO,CAAC,EAAE,cAAc,EAAE,CAAC;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,CAAC;AAEF,oBAAY,gBAAgB,GAAG,MAAM,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;AAEnE,oBAAY,mBAAmB,GAAG;IAChC,KAAK,EAAE,gBAAgB,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,uBAAuB,0DAA4D,CAAC;AAUjG,wBAAgB,yBAAyB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,oBAmBzD"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Copyright 2022 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 { createContext, useContext, useMemo } from 'react';
|
|
14
|
+
export const DEFAULT_ALL_VALUE = '$__all';
|
|
15
|
+
export const TemplateVariableContext = createContext(undefined);
|
|
16
|
+
function useTemplateVariableContext() {
|
|
17
|
+
const ctx = useContext(TemplateVariableContext);
|
|
18
|
+
if (ctx === undefined) {
|
|
19
|
+
throw new Error('No TemplateVariableContext found. Did you forget a Provider?');
|
|
20
|
+
}
|
|
21
|
+
return ctx;
|
|
22
|
+
}
|
|
23
|
+
export function useTemplateVariableValues(names) {
|
|
24
|
+
const { state } = useTemplateVariableContext();
|
|
25
|
+
const values = useMemo(()=>{
|
|
26
|
+
const values = {};
|
|
27
|
+
names === null || names === void 0 ? void 0 : names.forEach((name)=>{
|
|
28
|
+
const s = state[name];
|
|
29
|
+
if (s) {
|
|
30
|
+
values[name] = s;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return values;
|
|
34
|
+
}, [
|
|
35
|
+
state,
|
|
36
|
+
names
|
|
37
|
+
]);
|
|
38
|
+
if (names === undefined) {
|
|
39
|
+
return state;
|
|
40
|
+
}
|
|
41
|
+
return values;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//# sourceMappingURL=template-variables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/template-variables.ts"],"sourcesContent":["// Copyright 2022 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 { createContext, useContext, useMemo } from 'react';\nimport { VariableName, VariableValue } from '@perses-dev/core';\nimport { VariableOption } from '../model';\n\nexport const DEFAULT_ALL_VALUE = '$__all' as const;\n\nexport type VariableState = {\n value: VariableValue;\n options?: VariableOption[];\n loading: boolean;\n error?: Error;\n};\n\nexport type VariableStateMap = Record<VariableName, VariableState>;\n\nexport type TemplateVariableSrv = {\n state: VariableStateMap;\n};\n\nexport const TemplateVariableContext = createContext<TemplateVariableSrv | undefined>(undefined);\n\nfunction useTemplateVariableContext() {\n const ctx = useContext(TemplateVariableContext);\n if (ctx === undefined) {\n throw new Error('No TemplateVariableContext found. Did you forget a Provider?');\n }\n return ctx;\n}\n\nexport function useTemplateVariableValues(names?: string[]) {\n const { state } = useTemplateVariableContext();\n\n const values = useMemo(() => {\n const values: VariableStateMap = {};\n names?.forEach((name) => {\n const s = state[name];\n if (s) {\n values[name] = s;\n }\n });\n return values;\n }, [state, names]);\n\n if (names === undefined) {\n return state;\n }\n\n return values;\n}\n"],"names":["createContext","useContext","useMemo","DEFAULT_ALL_VALUE","TemplateVariableContext","undefined","useTemplateVariableContext","ctx","Error","useTemplateVariableValues","names","state","values","forEach","name","s"],"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,SAASA,aAAa,EAAEC,UAAU,EAAEC,OAAO,QAAQ,OAAO,CAAC;AAI3D,OAAO,MAAMC,iBAAiB,GAAG,QAAQ,AAAS,CAAC;AAenD,OAAO,MAAMC,uBAAuB,GAAGJ,aAAa,CAAkCK,SAAS,CAAC,CAAC;AAEjG,SAASC,0BAA0B,GAAG;IACpC,MAAMC,GAAG,GAAGN,UAAU,CAACG,uBAAuB,CAAC,AAAC;IAChD,IAAIG,GAAG,KAAKF,SAAS,EAAE;QACrB,MAAM,IAAIG,KAAK,CAAC,8DAA8D,CAAC,CAAC;IAClF,CAAC;IACD,OAAOD,GAAG,CAAC;AACb,CAAC;AAED,OAAO,SAASE,yBAAyB,CAACC,KAAgB,EAAE;IAC1D,MAAM,EAAEC,KAAK,CAAA,EAAE,GAAGL,0BAA0B,EAAE,AAAC;IAE/C,MAAMM,MAAM,GAAGV,OAAO,CAAC,IAAM;QAC3B,MAAMU,MAAM,GAAqB,EAAE,AAAC;QACpCF,KAAK,aAALA,KAAK,WAAS,GAAdA,KAAAA,CAAc,GAAdA,KAAK,CAAEG,OAAO,CAAC,CAACC,IAAI,GAAK;YACvB,MAAMC,CAAC,GAAGJ,KAAK,CAACG,IAAI,CAAC,AAAC;YACtB,IAAIC,CAAC,EAAE;gBACLH,MAAM,CAACE,IAAI,CAAC,GAAGC,CAAC,CAAC;YACnB,CAAC;QACH,CAAC,CAAC,CAAC;QACH,OAAOH,MAAM,CAAC;IAChB,CAAC,EAAE;QAACD,KAAK;QAAED,KAAK;KAAC,CAAC,AAAC;IAEnB,IAAIA,KAAK,KAAKL,SAAS,EAAE;QACvB,OAAOM,KAAK,CAAC;IACf,CAAC;IAED,OAAOC,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { TimeSeriesQueryDefinition } from '@perses-dev/core';
|
|
2
|
+
export interface UseTimeSeriesQueryOptions {
|
|
3
|
+
suggestedStepMs?: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Runs a time series query using a plugin and returns the results.
|
|
7
|
+
*/
|
|
8
|
+
export declare const useTimeSeriesQuery: (definition: TimeSeriesQueryDefinition, options?: UseTimeSeriesQueryOptions) => import("@tanstack/react-query").UseQueryResult<import("../model").TimeSeriesData, unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* Runs multiple time series queries using plugins and returns the results.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useTimeSeriesQueries(definitions: TimeSeriesQueryDefinition[], options?: UseTimeSeriesQueryOptions): import("@tanstack/react-query").UseQueryResult<import("../model").TimeSeriesData, unknown>[];
|
|
13
|
+
//# sourceMappingURL=time-series-queries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time-series-queries.d.ts","sourceRoot":"","sources":["../../src/runtime/time-series-queries.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAC;AAU7D,MAAM,WAAW,yBAAyB;IACxC,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAkDD;;GAEG;AACH,eAAO,MAAM,kBAAkB,eAAgB,yBAAyB,YAAY,yBAAyB,+FAkB5G,CAAC;AAEF;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,yBAAyB,EAAE,EAAE,OAAO,CAAC,EAAE,yBAAyB,gGA0BjH"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Copyright 2022 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 { useQuery, useQueries } from '@tanstack/react-query';
|
|
14
|
+
import { useTemplateVariableValues } from './template-variables';
|
|
15
|
+
import { useTimeRange } from './TimeRangeProvider';
|
|
16
|
+
import { useDatasourceStore } from './datasources';
|
|
17
|
+
import { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';
|
|
18
|
+
/**
|
|
19
|
+
* Returns a serialized string of the current state of variable values.
|
|
20
|
+
*/ function getVariableValuesKey(v) {
|
|
21
|
+
return Object.values(v).map((v)=>JSON.stringify(v.value)).join(',');
|
|
22
|
+
}
|
|
23
|
+
function filterVariableStateMap(v, names) {
|
|
24
|
+
if (!names) {
|
|
25
|
+
return v;
|
|
26
|
+
}
|
|
27
|
+
return Object.fromEntries(Object.entries(v).filter(([name])=>names.includes(name)));
|
|
28
|
+
}
|
|
29
|
+
function getQueryOptions({ plugin , definition , context }) {
|
|
30
|
+
const { timeRange , datasourceStore , suggestedStepMs , variableState , refreshKey } = context;
|
|
31
|
+
const dependencies = (plugin === null || plugin === void 0 ? void 0 : plugin.dependsOn) ? plugin.dependsOn(definition.spec.plugin.spec, context) : {};
|
|
32
|
+
const variableDependencies = dependencies === null || dependencies === void 0 ? void 0 : dependencies.variables;
|
|
33
|
+
// Determine queryKey
|
|
34
|
+
const filteredVariabledState = filterVariableStateMap(variableState, variableDependencies);
|
|
35
|
+
const variablesValueKey = getVariableValuesKey(filteredVariabledState);
|
|
36
|
+
const queryKey = [
|
|
37
|
+
definition,
|
|
38
|
+
timeRange,
|
|
39
|
+
datasourceStore,
|
|
40
|
+
suggestedStepMs,
|
|
41
|
+
variablesValueKey,
|
|
42
|
+
refreshKey
|
|
43
|
+
];
|
|
44
|
+
// Determine queryEnabled
|
|
45
|
+
let waitToLoad = false;
|
|
46
|
+
if (variableDependencies) {
|
|
47
|
+
waitToLoad = variableDependencies.some((v)=>{
|
|
48
|
+
var ref;
|
|
49
|
+
return (ref = variableState[v]) === null || ref === void 0 ? void 0 : ref.loading;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const queryEnabled = plugin !== undefined && !waitToLoad;
|
|
53
|
+
return {
|
|
54
|
+
queryKey,
|
|
55
|
+
queryEnabled
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Runs a time series query using a plugin and returns the results.
|
|
60
|
+
*/ export const useTimeSeriesQuery = (definition, options)=>{
|
|
61
|
+
const { data: plugin } = usePlugin('TimeSeriesQuery', definition.spec.plugin.kind);
|
|
62
|
+
const context = useTimeSeriesQueryContext();
|
|
63
|
+
const { queryEnabled , queryKey } = getQueryOptions({
|
|
64
|
+
plugin,
|
|
65
|
+
definition,
|
|
66
|
+
context
|
|
67
|
+
});
|
|
68
|
+
return useQuery({
|
|
69
|
+
enabled: queryEnabled,
|
|
70
|
+
queryKey: queryKey,
|
|
71
|
+
queryFn: ()=>{
|
|
72
|
+
// The 'enabled' option should prevent this from happening, but make TypeScript happy by checking
|
|
73
|
+
if (plugin === undefined) {
|
|
74
|
+
throw new Error('Expected plugin to be loaded');
|
|
75
|
+
}
|
|
76
|
+
// Keep options out of query key so we don't re-run queries because suggested step changes
|
|
77
|
+
const ctx = {
|
|
78
|
+
...context,
|
|
79
|
+
suggestedStepMs: options === null || options === void 0 ? void 0 : options.suggestedStepMs
|
|
80
|
+
};
|
|
81
|
+
return plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Runs multiple time series queries using plugins and returns the results.
|
|
87
|
+
*/ export function useTimeSeriesQueries(definitions, options) {
|
|
88
|
+
const { getPlugin } = usePluginRegistry();
|
|
89
|
+
const context = useTimeSeriesQueryContext();
|
|
90
|
+
const pluginLoaderResponse = usePlugins('TimeSeriesQuery', definitions.map((d)=>({
|
|
91
|
+
kind: d.spec.plugin.kind
|
|
92
|
+
})));
|
|
93
|
+
return useQueries({
|
|
94
|
+
queries: definitions.map((definition, idx)=>{
|
|
95
|
+
var ref;
|
|
96
|
+
const plugin = (ref = pluginLoaderResponse[idx]) === null || ref === void 0 ? void 0 : ref.data;
|
|
97
|
+
const { queryEnabled , queryKey } = getQueryOptions({
|
|
98
|
+
plugin,
|
|
99
|
+
definition,
|
|
100
|
+
context
|
|
101
|
+
});
|
|
102
|
+
return {
|
|
103
|
+
enabled: queryEnabled,
|
|
104
|
+
queryKey: queryKey,
|
|
105
|
+
queryFn: async ()=>{
|
|
106
|
+
// Keep options out of query key so we don't re-run queries because suggested step changes
|
|
107
|
+
const ctx = {
|
|
108
|
+
...context,
|
|
109
|
+
suggestedStepMs: options === null || options === void 0 ? void 0 : options.suggestedStepMs
|
|
110
|
+
};
|
|
111
|
+
const plugin = await getPlugin('TimeSeriesQuery', definition.spec.plugin.kind);
|
|
112
|
+
const data = await plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);
|
|
113
|
+
return data;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
})
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function useTimeSeriesQueryContext() {
|
|
120
|
+
// Build the context object from data available at runtime
|
|
121
|
+
const { absoluteTimeRange , refreshKey } = useTimeRange();
|
|
122
|
+
const variableState = useTemplateVariableValues();
|
|
123
|
+
const datasourceStore = useDatasourceStore();
|
|
124
|
+
return {
|
|
125
|
+
timeRange: absoluteTimeRange,
|
|
126
|
+
variableState,
|
|
127
|
+
datasourceStore,
|
|
128
|
+
refreshKey
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
//# sourceMappingURL=time-series-queries.js.map
|