@perses-dev/plugin-system 0.24.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/runtime/time-series-queries.js +29 -6
- package/dist/model/datasource.d.ts +7 -0
- package/dist/model/datasource.d.ts.map +1 -1
- package/dist/model/datasource.js.map +1 -1
- package/dist/model/time-series-queries.d.ts +8 -1
- package/dist/model/time-series-queries.d.ts.map +1 -1
- package/dist/model/time-series-queries.js.map +1 -1
- package/dist/runtime/time-series-queries.d.ts +13 -2
- package/dist/runtime/time-series-queries.d.ts.map +1 -1
- package/dist/runtime/time-series-queries.js +30 -6
- package/dist/runtime/time-series-queries.js.map +1 -1
- package/package.json +3 -3
|
@@ -21,14 +21,18 @@ function _export(target, all) {
|
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
_export(exports, {
|
|
24
|
+
TIME_SERIES_QUERY_KEY: ()=>TIME_SERIES_QUERY_KEY,
|
|
24
25
|
useTimeSeriesQuery: ()=>useTimeSeriesQuery,
|
|
25
|
-
useTimeSeriesQueries: ()=>useTimeSeriesQueries
|
|
26
|
+
useTimeSeriesQueries: ()=>useTimeSeriesQueries,
|
|
27
|
+
useActiveTimeSeriesQueries: ()=>useActiveTimeSeriesQueries,
|
|
28
|
+
getActiveTimeSeriesQueries: ()=>getActiveTimeSeriesQueries
|
|
26
29
|
});
|
|
27
30
|
const _reactQuery = require("@tanstack/react-query");
|
|
28
31
|
const _templateVariables = require("./template-variables");
|
|
29
32
|
const _timeRangeProvider = require("./TimeRangeProvider");
|
|
30
33
|
const _datasources = require("./datasources");
|
|
31
34
|
const _pluginRegistry = require("./plugin-registry");
|
|
35
|
+
const TIME_SERIES_QUERY_KEY = 'TimeSeriesQuery';
|
|
32
36
|
/**
|
|
33
37
|
* Returns a serialized string of the current state of variable values.
|
|
34
38
|
*/ function getVariableValuesKey(v) {
|
|
@@ -70,7 +74,7 @@ function getQueryOptions({ plugin , definition , context }) {
|
|
|
70
74
|
};
|
|
71
75
|
}
|
|
72
76
|
const useTimeSeriesQuery = (definition, options)=>{
|
|
73
|
-
const { data: plugin } = (0, _pluginRegistry.usePlugin)(
|
|
77
|
+
const { data: plugin } = (0, _pluginRegistry.usePlugin)(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);
|
|
74
78
|
const context = useTimeSeriesQueryContext();
|
|
75
79
|
const { queryEnabled , queryKey } = getQueryOptions({
|
|
76
80
|
plugin,
|
|
@@ -97,7 +101,7 @@ const useTimeSeriesQuery = (definition, options)=>{
|
|
|
97
101
|
function useTimeSeriesQueries(definitions, options) {
|
|
98
102
|
const { getPlugin } = (0, _pluginRegistry.usePluginRegistry)();
|
|
99
103
|
const context = useTimeSeriesQueryContext();
|
|
100
|
-
const pluginLoaderResponse = (0, _pluginRegistry.usePlugins)(
|
|
104
|
+
const pluginLoaderResponse = (0, _pluginRegistry.usePlugins)(TIME_SERIES_QUERY_KEY, definitions.map((d)=>({
|
|
101
105
|
kind: d.spec.plugin.kind
|
|
102
106
|
})));
|
|
103
107
|
return (0, _reactQuery.useQueries)({
|
|
@@ -118,7 +122,7 @@ function useTimeSeriesQueries(definitions, options) {
|
|
|
118
122
|
...context,
|
|
119
123
|
suggestedStepMs: options === null || options === void 0 ? void 0 : options.suggestedStepMs
|
|
120
124
|
};
|
|
121
|
-
const plugin = await getPlugin(
|
|
125
|
+
const plugin = await getPlugin(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);
|
|
122
126
|
const data = await plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);
|
|
123
127
|
return data;
|
|
124
128
|
}
|
|
@@ -126,8 +130,9 @@ function useTimeSeriesQueries(definitions, options) {
|
|
|
126
130
|
})
|
|
127
131
|
});
|
|
128
132
|
}
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Build the time series query context object from data available at runtime
|
|
135
|
+
*/ function useTimeSeriesQueryContext() {
|
|
131
136
|
const { absoluteTimeRange , refreshKey } = (0, _timeRangeProvider.useTimeRange)();
|
|
132
137
|
const variableState = (0, _templateVariables.useTemplateVariableValues)();
|
|
133
138
|
const datasourceStore = (0, _datasources.useDatasourceStore)();
|
|
@@ -138,3 +143,21 @@ function useTimeSeriesQueryContext() {
|
|
|
138
143
|
refreshKey
|
|
139
144
|
};
|
|
140
145
|
}
|
|
146
|
+
function useActiveTimeSeriesQueries() {
|
|
147
|
+
const queryClient = (0, _reactQuery.useQueryClient)();
|
|
148
|
+
const queryCache = queryClient.getQueryCache();
|
|
149
|
+
return getActiveTimeSeriesQueries(queryCache);
|
|
150
|
+
}
|
|
151
|
+
function getActiveTimeSeriesQueries(cache) {
|
|
152
|
+
const queries = [];
|
|
153
|
+
for (const query of cache.findAll({
|
|
154
|
+
type: 'active'
|
|
155
|
+
})){
|
|
156
|
+
var ref;
|
|
157
|
+
const firstPart = (ref = query.queryKey) === null || ref === void 0 ? void 0 : ref[0];
|
|
158
|
+
if ((firstPart === null || firstPart === void 0 ? void 0 : firstPart.kind) && firstPart.kind.startsWith(TIME_SERIES_QUERY_KEY)) {
|
|
159
|
+
queries.push(query);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return queries;
|
|
163
|
+
}
|
|
@@ -9,4 +9,11 @@ export interface DatasourcePlugin<Spec = UnknownSpec, Client = unknown> extends
|
|
|
9
9
|
export interface DatasourceClientOptions {
|
|
10
10
|
proxyUrl?: string;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Common properties for all clients
|
|
14
|
+
*/
|
|
15
|
+
export interface DatasourceClient {
|
|
16
|
+
kind?: string;
|
|
17
|
+
healthCheck?: () => Promise<boolean>;
|
|
18
|
+
}
|
|
12
19
|
//# sourceMappingURL=datasource.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"datasource.d.ts","sourceRoot":"","sources":["../../src/model/datasource.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAEvC;;GAEG;AACH,MAAM,WAAW,gBAAgB,CAAC,IAAI,GAAG,WAAW,EAAE,MAAM,GAAG,OAAO,CAAE,SAAQ,MAAM,CAAC,IAAI,CAAC;IAC1F,YAAY,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,uBAAuB,KAAK,MAAM,CAAC;CACxE;AAED,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB"}
|
|
1
|
+
{"version":3,"file":"datasource.d.ts","sourceRoot":"","sources":["../../src/model/datasource.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAEvC;;GAEG;AACH,MAAM,WAAW,gBAAgB,CAAC,IAAI,GAAG,WAAW,EAAE,MAAM,GAAG,OAAO,CAAE,SAAQ,MAAM,CAAC,IAAI,CAAC;IAC1F,YAAY,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,uBAAuB,KAAK,MAAM,CAAC;CACxE;AAED,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAE/B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;CACtC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/model/datasource.ts"],"sourcesContent":["// Copyright 2023 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 { UnknownSpec } from '@perses-dev/core';\nimport { Plugin } from './plugin-base';\n\n/**\n * Plugin that defines options for an external system that Perses talks to for data.\n */\nexport interface DatasourcePlugin<Spec = UnknownSpec, Client = unknown> extends Plugin<Spec> {\n createClient: (spec: Spec, options: DatasourceClientOptions) => Client;\n}\n\nexport interface DatasourceClientOptions {\n proxyUrl?: string;\n}\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,
|
|
1
|
+
{"version":3,"sources":["../../src/model/datasource.ts"],"sourcesContent":["// Copyright 2023 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 { UnknownSpec } from '@perses-dev/core';\nimport { Plugin } from './plugin-base';\n\n/**\n * Plugin that defines options for an external system that Perses talks to for data.\n */\nexport interface DatasourcePlugin<Spec = UnknownSpec, Client = unknown> extends Plugin<Spec> {\n createClient: (spec: Spec, options: DatasourceClientOptions) => Client;\n}\n\nexport interface DatasourceClientOptions {\n proxyUrl?: string;\n}\n\n/**\n * Common properties for all clients\n */\nexport interface DatasourceClient {\n // TODO: set kind and define healthCheck function\n kind?: string;\n healthCheck?: () => Promise<boolean>;\n}\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,WAqBC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Query, QueryKey } from '@tanstack/react-query';
|
|
2
|
+
import { AbsoluteTimeRange, TimeSeriesValueTuple, UnknownSpec, Labels, Notice } from '@perses-dev/core';
|
|
2
3
|
import { DatasourceStore, VariableStateMap } from '../runtime';
|
|
3
4
|
import { Plugin } from './plugin-base';
|
|
4
5
|
/**
|
|
@@ -31,11 +32,17 @@ export interface TimeSeriesData {
|
|
|
31
32
|
timeRange?: AbsoluteTimeRange;
|
|
32
33
|
stepMs?: number;
|
|
33
34
|
series: TimeSeries[];
|
|
35
|
+
metadata?: TimeSeriesMetadata;
|
|
34
36
|
}
|
|
35
37
|
export interface TimeSeries {
|
|
36
38
|
name: string;
|
|
37
39
|
values: TimeSeriesValueTuple[];
|
|
38
40
|
formattedName?: string;
|
|
41
|
+
labels?: Labels;
|
|
39
42
|
}
|
|
43
|
+
export interface TimeSeriesMetadata {
|
|
44
|
+
notices?: Notice[];
|
|
45
|
+
}
|
|
46
|
+
export declare type TimeSeriesDataQuery = Query<TimeSeriesData, unknown, TimeSeriesData, QueryKey>;
|
|
40
47
|
export {};
|
|
41
48
|
//# sourceMappingURL=time-series-queries.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time-series-queries.d.ts","sourceRoot":"","sources":["../../src/model/time-series-queries.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"time-series-queries.d.ts","sourceRoot":"","sources":["../../src/model/time-series-queries.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAu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AO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,oBAAY,mBAAmB,GAAG,KAAK,CAAC,cAAc,EAAE,OAAO,EAAE,cAAc,EAAE,QAAQ,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/model/time-series-queries.ts"],"sourcesContent":["// Copyright 2023 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 { AbsoluteTimeRange, TimeSeriesValueTuple, UnknownSpec } from '@perses-dev/core';\nimport { DatasourceStore, VariableStateMap } from '../runtime';\nimport { Plugin } from './plugin-base';\n\n/**\n * An object containing all the dependencies of a TimeSeriesQuery.\n */\ntype TimeSeriesQueryPluginDependencies = {\n /**\n * Returns a list of variables name this time series query depends on.\n */\n variables?: string[];\n};\n\n/**\n * A plugin for running time series queries.\n */\nexport interface TimeSeriesQueryPlugin<Spec = UnknownSpec> extends Plugin<Spec> {\n getTimeSeriesData: (spec: Spec, ctx: TimeSeriesQueryContext) => Promise<TimeSeriesData>;\n\n dependsOn?: (spec: Spec, ctx: TimeSeriesQueryContext) => TimeSeriesQueryPluginDependencies;\n}\n\n/**\n * Context available to TimeSeriesQuery plugins at runtime.\n */\nexport interface TimeSeriesQueryContext {\n suggestedStepMs?: number;\n timeRange: AbsoluteTimeRange;\n variableState: VariableStateMap;\n datasourceStore: DatasourceStore;\n refreshKey: string;\n}\n\nexport interface TimeSeriesData {\n timeRange?: AbsoluteTimeRange;\n stepMs?: number;\n series: TimeSeries[];\n}\n\nexport interface TimeSeries {\n name: string;\n values: TimeSeriesValueTuple[];\n formattedName?: string;\n}\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,
|
|
1
|
+
{"version":3,"sources":["../../src/model/time-series-queries.ts"],"sourcesContent":["// Copyright 2023 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 { Query, QueryKey } from '@tanstack/react-query';\nimport { AbsoluteTimeRange, TimeSeriesValueTuple, UnknownSpec, Labels, Notice } from '@perses-dev/core';\nimport { DatasourceStore, VariableStateMap } from '../runtime';\nimport { Plugin } from './plugin-base';\n\n/**\n * An object containing all the dependencies of a TimeSeriesQuery.\n */\ntype TimeSeriesQueryPluginDependencies = {\n /**\n * Returns a list of variables name this time series query depends on.\n */\n variables?: string[];\n};\n\n/**\n * A plugin for running time series queries.\n */\nexport interface TimeSeriesQueryPlugin<Spec = UnknownSpec> extends Plugin<Spec> {\n getTimeSeriesData: (spec: Spec, ctx: TimeSeriesQueryContext) => Promise<TimeSeriesData>;\n\n dependsOn?: (spec: Spec, ctx: TimeSeriesQueryContext) => TimeSeriesQueryPluginDependencies;\n}\n\n/**\n * Context available to TimeSeriesQuery plugins at runtime.\n */\nexport interface TimeSeriesQueryContext {\n suggestedStepMs?: number;\n timeRange: AbsoluteTimeRange;\n variableState: VariableStateMap;\n datasourceStore: DatasourceStore;\n refreshKey: string;\n}\n\nexport interface TimeSeriesData {\n timeRange?: AbsoluteTimeRange;\n stepMs?: number;\n series: TimeSeries[];\n metadata?: TimeSeriesMetadata;\n}\n\nexport interface TimeSeries {\n name: string;\n values: TimeSeriesValueTuple[];\n formattedName?: string;\n labels?: Labels;\n}\n\nexport interface TimeSeriesMetadata {\n notices?: Notice[];\n}\n\nexport type TimeSeriesDataQuery = Query<TimeSeriesData, unknown, TimeSeriesData, QueryKey>;\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,WAqD2F"}
|
|
@@ -1,13 +1,24 @@
|
|
|
1
|
+
import { QueryCache } from '@tanstack/react-query';
|
|
1
2
|
import { TimeSeriesQueryDefinition } from '@perses-dev/core';
|
|
3
|
+
import { TimeSeriesData, TimeSeriesDataQuery } from '../model';
|
|
2
4
|
export interface UseTimeSeriesQueryOptions {
|
|
3
5
|
suggestedStepMs?: number;
|
|
4
6
|
}
|
|
7
|
+
export declare const TIME_SERIES_QUERY_KEY = "TimeSeriesQuery";
|
|
5
8
|
/**
|
|
6
9
|
* Runs a time series query using a plugin and returns the results.
|
|
7
10
|
*/
|
|
8
|
-
export declare const useTimeSeriesQuery: (definition: TimeSeriesQueryDefinition, options?: UseTimeSeriesQueryOptions) => import("@tanstack/react-query").UseQueryResult<
|
|
11
|
+
export declare const useTimeSeriesQuery: (definition: TimeSeriesQueryDefinition, options?: UseTimeSeriesQueryOptions) => import("@tanstack/react-query").UseQueryResult<TimeSeriesData, unknown>;
|
|
9
12
|
/**
|
|
10
13
|
* Runs multiple time series queries using plugins and returns the results.
|
|
11
14
|
*/
|
|
12
|
-
export declare function useTimeSeriesQueries(definitions: TimeSeriesQueryDefinition[], options?: UseTimeSeriesQueryOptions): import("@tanstack/react-query").UseQueryResult<
|
|
15
|
+
export declare function useTimeSeriesQueries(definitions: TimeSeriesQueryDefinition[], options?: UseTimeSeriesQueryOptions): import("@tanstack/react-query").UseQueryResult<TimeSeriesData, unknown>[];
|
|
16
|
+
/**
|
|
17
|
+
* Get active time series queries for query results summary
|
|
18
|
+
*/
|
|
19
|
+
export declare function useActiveTimeSeriesQueries(): TimeSeriesDataQuery[];
|
|
20
|
+
/**
|
|
21
|
+
* Filter all cached queries down to only active time series queries
|
|
22
|
+
*/
|
|
23
|
+
export declare function getActiveTimeSeriesQueries(cache: QueryCache): TimeSeriesDataQuery[];
|
|
13
24
|
//# sourceMappingURL=time-series-queries.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time-series-queries.d.ts","sourceRoot":"","sources":["../../src/runtime/time-series-queries.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,yBAAyB,
|
|
1
|
+
{"version":3,"file":"time-series-queries.d.ts","sourceRoot":"","sources":["../../src/runtime/time-series-queries.ts"],"names":[],"mappings":"AAaA,OAAO,EAA+C,UAAU,EAAY,MAAM,uBAAuB,CAAC;AAC1G,OAAO,EAAE,yBAAyB,EAAe,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAiD,MAAM,UAAU,CAAC;AAO9G,MAAM,WAAW,yBAAyB;IACxC,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,eAAO,MAAM,qBAAqB,oBAAoB,CAAC;AAmDvD;;GAEG;AACH,eAAO,MAAM,kBAAkB,eAAgB,yBAAyB,YAAY,yBAAyB,4EAkB5G,CAAC;AAEF;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,yBAAyB,EAAE,EAAE,OAAO,CAAC,EAAE,yBAAyB,6EA0BjH;AAkBD;;GAEG;AACH,wBAAgB,0BAA0B,0BAIzC;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,UAAU,yBAW3D"}
|
|
@@ -10,11 +10,12 @@
|
|
|
10
10
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
11
|
// See the License for the specific language governing permissions and
|
|
12
12
|
// limitations under the License.
|
|
13
|
-
import { useQuery, useQueries } from '@tanstack/react-query';
|
|
13
|
+
import { useQuery, useQueries, useQueryClient } from '@tanstack/react-query';
|
|
14
14
|
import { useTemplateVariableValues } from './template-variables';
|
|
15
15
|
import { useTimeRange } from './TimeRangeProvider';
|
|
16
16
|
import { useDatasourceStore } from './datasources';
|
|
17
17
|
import { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';
|
|
18
|
+
export const TIME_SERIES_QUERY_KEY = 'TimeSeriesQuery';
|
|
18
19
|
/**
|
|
19
20
|
* Returns a serialized string of the current state of variable values.
|
|
20
21
|
*/ function getVariableValuesKey(v) {
|
|
@@ -58,7 +59,7 @@ function getQueryOptions({ plugin , definition , context }) {
|
|
|
58
59
|
/**
|
|
59
60
|
* Runs a time series query using a plugin and returns the results.
|
|
60
61
|
*/ export const useTimeSeriesQuery = (definition, options)=>{
|
|
61
|
-
const { data: plugin } = usePlugin(
|
|
62
|
+
const { data: plugin } = usePlugin(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);
|
|
62
63
|
const context = useTimeSeriesQueryContext();
|
|
63
64
|
const { queryEnabled , queryKey } = getQueryOptions({
|
|
64
65
|
plugin,
|
|
@@ -87,7 +88,7 @@ function getQueryOptions({ plugin , definition , context }) {
|
|
|
87
88
|
*/ export function useTimeSeriesQueries(definitions, options) {
|
|
88
89
|
const { getPlugin } = usePluginRegistry();
|
|
89
90
|
const context = useTimeSeriesQueryContext();
|
|
90
|
-
const pluginLoaderResponse = usePlugins(
|
|
91
|
+
const pluginLoaderResponse = usePlugins(TIME_SERIES_QUERY_KEY, definitions.map((d)=>({
|
|
91
92
|
kind: d.spec.plugin.kind
|
|
92
93
|
})));
|
|
93
94
|
return useQueries({
|
|
@@ -108,7 +109,7 @@ function getQueryOptions({ plugin , definition , context }) {
|
|
|
108
109
|
...context,
|
|
109
110
|
suggestedStepMs: options === null || options === void 0 ? void 0 : options.suggestedStepMs
|
|
110
111
|
};
|
|
111
|
-
const plugin = await getPlugin(
|
|
112
|
+
const plugin = await getPlugin(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);
|
|
112
113
|
const data = await plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);
|
|
113
114
|
return data;
|
|
114
115
|
}
|
|
@@ -116,8 +117,9 @@ function getQueryOptions({ plugin , definition , context }) {
|
|
|
116
117
|
})
|
|
117
118
|
});
|
|
118
119
|
}
|
|
119
|
-
|
|
120
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Build the time series query context object from data available at runtime
|
|
122
|
+
*/ function useTimeSeriesQueryContext() {
|
|
121
123
|
const { absoluteTimeRange , refreshKey } = useTimeRange();
|
|
122
124
|
const variableState = useTemplateVariableValues();
|
|
123
125
|
const datasourceStore = useDatasourceStore();
|
|
@@ -128,5 +130,27 @@ function useTimeSeriesQueryContext() {
|
|
|
128
130
|
refreshKey
|
|
129
131
|
};
|
|
130
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Get active time series queries for query results summary
|
|
135
|
+
*/ export function useActiveTimeSeriesQueries() {
|
|
136
|
+
const queryClient = useQueryClient();
|
|
137
|
+
const queryCache = queryClient.getQueryCache();
|
|
138
|
+
return getActiveTimeSeriesQueries(queryCache);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Filter all cached queries down to only active time series queries
|
|
142
|
+
*/ export function getActiveTimeSeriesQueries(cache) {
|
|
143
|
+
const queries = [];
|
|
144
|
+
for (const query of cache.findAll({
|
|
145
|
+
type: 'active'
|
|
146
|
+
})){
|
|
147
|
+
var ref;
|
|
148
|
+
const firstPart = (ref = query.queryKey) === null || ref === void 0 ? void 0 : ref[0];
|
|
149
|
+
if ((firstPart === null || firstPart === void 0 ? void 0 : firstPart.kind) && firstPart.kind.startsWith(TIME_SERIES_QUERY_KEY)) {
|
|
150
|
+
queries.push(query);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return queries;
|
|
154
|
+
}
|
|
131
155
|
|
|
132
156
|
//# sourceMappingURL=time-series-queries.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/runtime/time-series-queries.ts"],"sourcesContent":["// Copyright 2023 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 { TimeSeriesQueryDefinition } from '@perses-dev/core';\nimport { useQuery, useQueries } from '@tanstack/react-query';\nimport { TimeSeriesQueryContext } from '../model';\nimport { TimeSeriesQueryPlugin } from '../model';\nimport { VariableStateMap } from './template-variables';\nimport { useTemplateVariableValues } from './template-variables';\nimport { useTimeRange } from './TimeRangeProvider';\nimport { useDatasourceStore } from './datasources';\nimport { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';\n\nexport interface UseTimeSeriesQueryOptions {\n suggestedStepMs?: number;\n}\n\n/**\n * Returns a serialized string of the current state of variable values.\n */\nfunction getVariableValuesKey(v: VariableStateMap) {\n return Object.values(v)\n .map((v) => JSON.stringify(v.value))\n .join(',');\n}\n\nfunction filterVariableStateMap(v: VariableStateMap, names?: string[]) {\n if (!names) {\n return v;\n }\n return Object.fromEntries(Object.entries(v).filter(([name]) => names.includes(name)));\n}\n\nfunction getQueryOptions({\n plugin,\n definition,\n context,\n}: {\n plugin?: TimeSeriesQueryPlugin;\n definition: TimeSeriesQueryDefinition;\n context: TimeSeriesQueryContext;\n}) {\n const { timeRange, datasourceStore, suggestedStepMs, variableState, refreshKey } = context;\n\n const dependencies = plugin?.dependsOn ? plugin.dependsOn(definition.spec.plugin.spec, context) : {};\n const variableDependencies = dependencies?.variables;\n\n // Determine queryKey\n const filteredVariabledState = filterVariableStateMap(variableState, variableDependencies);\n const variablesValueKey = getVariableValuesKey(filteredVariabledState);\n const queryKey = [definition, timeRange, datasourceStore, suggestedStepMs, variablesValueKey, refreshKey] as const;\n\n // Determine queryEnabled\n let waitToLoad = false;\n if (variableDependencies) {\n waitToLoad = variableDependencies.some((v) => variableState[v]?.loading);\n }\n const queryEnabled = plugin !== undefined && !waitToLoad;\n\n return {\n queryKey,\n queryEnabled,\n };\n}\n\n/**\n * Runs a time series query using a plugin and returns the results.\n */\nexport const useTimeSeriesQuery = (definition: TimeSeriesQueryDefinition, options?: UseTimeSeriesQueryOptions) => {\n const { data: plugin } = usePlugin('TimeSeriesQuery', definition.spec.plugin.kind);\n const context = useTimeSeriesQueryContext();\n\n const { queryEnabled, queryKey } = getQueryOptions({ plugin, definition, context });\n return useQuery({\n enabled: queryEnabled,\n queryKey: queryKey,\n queryFn: () => {\n // The 'enabled' option should prevent this from happening, but make TypeScript happy by checking\n if (plugin === undefined) {\n throw new Error('Expected plugin to be loaded');\n }\n // Keep options out of query key so we don't re-run queries because suggested step changes\n const ctx: TimeSeriesQueryContext = { ...context, suggestedStepMs: options?.suggestedStepMs };\n return plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);\n },\n });\n};\n\n/**\n * Runs multiple time series queries using plugins and returns the results.\n */\nexport function useTimeSeriesQueries(definitions: TimeSeriesQueryDefinition[], options?: UseTimeSeriesQueryOptions) {\n const { getPlugin } = usePluginRegistry();\n const context = useTimeSeriesQueryContext();\n\n const pluginLoaderResponse = usePlugins(\n 'TimeSeriesQuery',\n definitions.map((d) => ({ kind: d.spec.plugin.kind }))\n );\n\n return useQueries({\n queries: definitions.map((definition, idx) => {\n const plugin = pluginLoaderResponse[idx]?.data;\n const { queryEnabled, queryKey } = getQueryOptions({ plugin, definition, context });\n return {\n enabled: queryEnabled,\n queryKey: queryKey,\n queryFn: async () => {\n // Keep options out of query key so we don't re-run queries because suggested step changes\n const ctx: TimeSeriesQueryContext = { ...context, suggestedStepMs: options?.suggestedStepMs };\n const plugin = await getPlugin('TimeSeriesQuery', definition.spec.plugin.kind);\n const data = await plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);\n return data;\n },\n };\n }),\n });\n}\n\nfunction useTimeSeriesQueryContext(): TimeSeriesQueryContext {\n // Build the context object from data available at runtime\n const { absoluteTimeRange, refreshKey } = useTimeRange();\n const variableState = useTemplateVariableValues();\n const datasourceStore = useDatasourceStore();\n\n return {\n timeRange: absoluteTimeRange,\n variableState,\n datasourceStore,\n refreshKey,\n };\n}\n"],"names":["useQuery","useQueries","useTemplateVariableValues","useTimeRange","useDatasourceStore","usePlugin","usePluginRegistry","usePlugins","getVariableValuesKey","v","Object","values","map","JSON","stringify","value","join","filterVariableStateMap","names","fromEntries","entries","filter","name","includes","getQueryOptions","plugin","definition","context","timeRange","datasourceStore","suggestedStepMs","variableState","refreshKey","dependencies","dependsOn","spec","variableDependencies","variables","filteredVariabledState","variablesValueKey","queryKey","waitToLoad","some","loading","queryEnabled","undefined","useTimeSeriesQuery","options","data","kind","useTimeSeriesQueryContext","enabled","queryFn","Error","ctx","getTimeSeriesData","useTimeSeriesQueries","definitions","getPlugin","pluginLoaderResponse","d","queries","idx","absoluteTimeRange"],"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,EAAEC,UAAU,QAAQ,uBAAuB,CAAC;AAI7D,SAASC,yBAAyB,QAAQ,sBAAsB,CAAC;AACjE,SAASC,YAAY,QAAQ,qBAAqB,CAAC;AACnD,SAASC,kBAAkB,QAAQ,eAAe,CAAC;AACnD,SAASC,SAAS,EAAEC,iBAAiB,EAAEC,UAAU,QAAQ,mBAAmB,CAAC;AAM7E;;CAEC,GAC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QAAQ,EAAEA,QAAQ;gBAClBY,OAAO,EAAE,UAAY;oBACnB,0FAA0F;oBAC1F,MAAME,GAAG,GAA2B;wBAAE,GAAG3B,OAAO;wBAAEG,eAAe,EAAEiB,OAAO,aAAPA,OAAO,WAAiB,GAAxBA,KAAAA,CAAwB,GAAxBA,OAAO,CAAEjB,eAAe;qBAAE,AAAC;oBAC9F,MAAML,MAAM,GAAG,MAAMiC,SAAS,CAAC,iBAAiB,EAAEhC,UAAU,CAACS,IAAI,CAACV,MAAM,CAACwB,IAAI,CAAC,AAAC;oBAC/E,MAAMD,IAAI,GAAG,MAAMvB,MAAM,CAAC8B,iBAAiB,CAAC7B,UAAU,CAACS,IAAI,CAACV,MAAM,CAACU,IAAI,EAAEmB,GAAG,CAAC,AAAC;oBAC9E,OAAON,IAAI,CAAC;gBACd,CAAC;aACF,CAAC;QACJ,CAAC,CAAC;KACH,CAAC,CAAC;AACL,CAAC;AAED,SAASE,yBAAyB,GAA2B;IAC3D,0DAA0D;IAC1D,MAAM,EAAEa,iBAAiB,CAAA,EAAE/B,UAAU,CAAA,EAAE,GAAG7B,YAAY,EAAE,AAAC;IACzD,MAAM4B,aAAa,GAAG7B,yBAAyB,EAAE,AAAC;IAClD,MAAM2B,eAAe,GAAGzB,kBAAkB,EAAE,AAAC;IAE7C,OAAO;QACLwB,SAAS,EAAEmC,iBAAiB;QAC5BhC,aAAa;QACbF,eAAe;QACfG,UAAU;KACX,CAAC;AACJ,CAAC"}
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/time-series-queries.ts"],"sourcesContent":["// Copyright 2023 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 { useQuery, useQueries, useQueryClient, Query, QueryCache, QueryKey } from '@tanstack/react-query';\nimport { TimeSeriesQueryDefinition, UnknownSpec } from '@perses-dev/core';\nimport { TimeSeriesData, TimeSeriesDataQuery, TimeSeriesQueryContext, TimeSeriesQueryPlugin } from '../model';\nimport { VariableStateMap } from './template-variables';\nimport { useTemplateVariableValues } from './template-variables';\nimport { useTimeRange } from './TimeRangeProvider';\nimport { useDatasourceStore } from './datasources';\nimport { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';\n\nexport interface UseTimeSeriesQueryOptions {\n suggestedStepMs?: number;\n}\n\nexport const TIME_SERIES_QUERY_KEY = 'TimeSeriesQuery';\n\n/**\n * Returns a serialized string of the current state of variable values.\n */\nfunction getVariableValuesKey(v: VariableStateMap) {\n return Object.values(v)\n .map((v) => JSON.stringify(v.value))\n .join(',');\n}\n\nfunction filterVariableStateMap(v: VariableStateMap, names?: string[]) {\n if (!names) {\n return v;\n }\n return Object.fromEntries(Object.entries(v).filter(([name]) => names.includes(name)));\n}\n\nfunction getQueryOptions({\n plugin,\n definition,\n context,\n}: {\n plugin?: TimeSeriesQueryPlugin;\n definition: TimeSeriesQueryDefinition;\n context: TimeSeriesQueryContext;\n}) {\n const { timeRange, datasourceStore, suggestedStepMs, variableState, refreshKey } = context;\n\n const dependencies = plugin?.dependsOn ? plugin.dependsOn(definition.spec.plugin.spec, context) : {};\n const variableDependencies = dependencies?.variables;\n\n // Determine queryKey\n const filteredVariabledState = filterVariableStateMap(variableState, variableDependencies);\n const variablesValueKey = getVariableValuesKey(filteredVariabledState);\n const queryKey = [definition, timeRange, datasourceStore, suggestedStepMs, variablesValueKey, refreshKey] as const;\n\n // Determine queryEnabled\n let waitToLoad = false;\n if (variableDependencies) {\n waitToLoad = variableDependencies.some((v) => variableState[v]?.loading);\n }\n\n const queryEnabled = plugin !== undefined && !waitToLoad;\n\n return {\n queryKey,\n queryEnabled,\n };\n}\n\n/**\n * Runs a time series query using a plugin and returns the results.\n */\nexport const useTimeSeriesQuery = (definition: TimeSeriesQueryDefinition, options?: UseTimeSeriesQueryOptions) => {\n const { data: plugin } = usePlugin(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);\n const context = useTimeSeriesQueryContext();\n\n const { queryEnabled, queryKey } = getQueryOptions({ plugin, definition, context });\n return useQuery({\n enabled: queryEnabled,\n queryKey: queryKey,\n queryFn: () => {\n // The 'enabled' option should prevent this from happening, but make TypeScript happy by checking\n if (plugin === undefined) {\n throw new Error('Expected plugin to be loaded');\n }\n // Keep options out of query key so we don't re-run queries because suggested step changes\n const ctx: TimeSeriesQueryContext = { ...context, suggestedStepMs: options?.suggestedStepMs };\n return plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);\n },\n });\n};\n\n/**\n * Runs multiple time series queries using plugins and returns the results.\n */\nexport function useTimeSeriesQueries(definitions: TimeSeriesQueryDefinition[], options?: UseTimeSeriesQueryOptions) {\n const { getPlugin } = usePluginRegistry();\n const context = useTimeSeriesQueryContext();\n\n const pluginLoaderResponse = usePlugins(\n TIME_SERIES_QUERY_KEY,\n definitions.map((d) => ({ kind: d.spec.plugin.kind }))\n );\n\n return useQueries({\n queries: definitions.map((definition, idx) => {\n const plugin = pluginLoaderResponse[idx]?.data;\n const { queryEnabled, queryKey } = getQueryOptions({ plugin, definition, context });\n return {\n enabled: queryEnabled,\n queryKey: queryKey,\n queryFn: async () => {\n // Keep options out of query key so we don't re-run queries because suggested step changes\n const ctx: TimeSeriesQueryContext = { ...context, suggestedStepMs: options?.suggestedStepMs };\n const plugin = await getPlugin(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);\n const data = await plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);\n return data;\n },\n };\n }),\n });\n}\n\n/**\n * Build the time series query context object from data available at runtime\n */\nfunction useTimeSeriesQueryContext(): TimeSeriesQueryContext {\n const { absoluteTimeRange, refreshKey } = useTimeRange();\n const variableState = useTemplateVariableValues();\n const datasourceStore = useDatasourceStore();\n\n return {\n timeRange: absoluteTimeRange,\n variableState,\n datasourceStore,\n refreshKey,\n };\n}\n\n/**\n * Get active time series queries for query results summary\n */\nexport function useActiveTimeSeriesQueries() {\n const queryClient = useQueryClient();\n const queryCache = queryClient.getQueryCache();\n return getActiveTimeSeriesQueries(queryCache);\n}\n\n/**\n * Filter all cached queries down to only active time series queries\n */\nexport function getActiveTimeSeriesQueries(cache: QueryCache) {\n const queries: TimeSeriesDataQuery[] = [];\n\n for (const query of cache.findAll({ type: 'active' })) {\n const firstPart = query.queryKey?.[0] as UnknownSpec;\n if (firstPart?.kind && (firstPart.kind as string).startsWith(TIME_SERIES_QUERY_KEY)) {\n queries.push(query as Query<TimeSeriesData, unknown, TimeSeriesData, QueryKey>);\n }\n }\n\n return queries;\n}\n"],"names":["useQuery","useQueries","useQueryClient","useTemplateVariableValues","useTimeRange","useDatasourceStore","usePlugin","usePluginRegistry","usePlugins","TIME_SERIES_QUERY_KEY","getVariableValuesKey","v","Object","values","map","JSON","stringify","value","join","filterVariableStateMap","names","fromEntries","entries","filter","name","includes","getQueryOptions","plugin","definition","context","timeRange","datasourceStore","suggestedStepMs","variableState","refreshKey","dependencies","dependsOn","spec","variableDependencies","variables","filteredVariabledState","variablesValueKey","queryKey","waitToLoad","some","loading","queryEnabled","undefined","useTimeSeriesQuery","options","data","kind","useTimeSeriesQueryContext","enabled","queryFn","Error","ctx","getTimeSeriesData","useTimeSeriesQueries","definitions","getPlugin","pluginLoaderResponse","d","queries","idx","absoluteTimeRange","useActiveTimeSeriesQueries","queryClient","queryCache","getQueryCache","getActiveTimeSeriesQueries","cache","query","findAll","type","firstPart","startsWith","push"],"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,QAAQ,E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AAO,SAASgC,0BAA0B,GAAG;IAC3C,MAAMC,WAAW,GAAGjE,cAAc,EAAE,AAAC;IACrC,MAAMkE,UAAU,GAAGD,WAAW,CAACE,aAAa,EAAE,AAAC;IAC/C,OAAOC,0BAA0B,CAACF,UAAU,CAAC,CAAC;AAChD,CAAC;AAED;;CAEC,GACD,OAAO,SAASE,0BAA0B,CAACC,KAAiB,EAAE;IAC5D,MAAMR,OAAO,GAA0B,EAAE,AAAC;IAE1C,KAAK,MAAMS,KAAK,IAAID,KAAK,CAACE,OAAO,CAAC;QAAEC,IAAI,EAAE,QAAQ;KAAE,CAAC,CAAE;YACnCF,GAAc;QAAhC,MAAMG,SAAS,GAAGH,CAAAA,GAAc,GAAdA,KAAK,CAAC9B,QAAQ,cAAd8B,GAAc,WAAK,GAAnBA,KAAAA,CAAmB,GAAnBA,GAAc,AAAE,CAAC,CAAC,CAAC,AAAe,AAAC;QACrD,IAAIG,CAAAA,SAAS,aAATA,SAAS,WAAM,GAAfA,KAAAA,CAAe,GAAfA,SAAS,CAAExB,IAAI,CAAA,IAAI,AAACwB,SAAS,CAACxB,IAAI,CAAYyB,UAAU,CAACnE,qBAAqB,CAAC,EAAE;YACnFsD,OAAO,CAACc,IAAI,CAACL,KAAK,CAA6D,CAAC;QAClF,CAAC;IACH,CAAC;IAED,OAAOT,OAAO,CAAC;AACjB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@perses-dev/plugin-system",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "The plugin feature in Pereses",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://github.com/perses/perses/blob/main/README.md",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"lint:fix": "eslint --fix src --ext .ts,.tsx"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@perses-dev/components": "0.
|
|
32
|
-
"@perses-dev/core": "0.
|
|
31
|
+
"@perses-dev/components": "0.25.0",
|
|
32
|
+
"@perses-dev/core": "0.25.0",
|
|
33
33
|
"immer": "^9.0.15",
|
|
34
34
|
"use-immer": "^0.7.0",
|
|
35
35
|
"use-query-params": "^2.1.2"
|