@perses-dev/plugin-system 0.51.0-beta.1 → 0.51.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/cjs/components/DatasourceSelect.js +155 -71
  2. package/dist/cjs/components/TimeRangeControls/TimeRangeControls.js +91 -1
  3. package/dist/cjs/components/Variables/VariableEditorForm/VariableEditorForm.js +11 -7
  4. package/dist/cjs/constants/user-interface-text.js +3 -1
  5. package/dist/cjs/remote/PluginRuntime.js +168 -162
  6. package/dist/cjs/runtime/TimeRangeProvider/TimeRangeSettingsProvider.js +13 -0
  7. package/dist/cjs/runtime/time-series-queries.js +3 -13
  8. package/dist/cjs/runtime/trace-queries.js +46 -16
  9. package/dist/cjs/runtime/utils.js +39 -0
  10. package/dist/components/DatasourceSelect.d.ts +8 -3
  11. package/dist/components/DatasourceSelect.d.ts.map +1 -1
  12. package/dist/components/DatasourceSelect.js +148 -72
  13. package/dist/components/DatasourceSelect.js.map +1 -1
  14. package/dist/components/PluginRegistry/PluginRegistry.js.map +1 -1
  15. package/dist/components/PluginRegistry/plugin-indexes.d.ts +1 -1
  16. package/dist/components/PluginRegistry/plugin-indexes.d.ts.map +1 -1
  17. package/dist/components/PluginRegistry/plugin-indexes.js.map +1 -1
  18. package/dist/components/TimeRangeControls/TimeRangeControls.d.ts +2 -1
  19. package/dist/components/TimeRangeControls/TimeRangeControls.d.ts.map +1 -1
  20. package/dist/components/TimeRangeControls/TimeRangeControls.js +94 -2
  21. package/dist/components/TimeRangeControls/TimeRangeControls.js.map +1 -1
  22. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts.map +1 -1
  23. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js +11 -7
  24. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js.map +1 -1
  25. package/dist/constants/user-interface-text.d.ts +2 -0
  26. package/dist/constants/user-interface-text.d.ts.map +1 -1
  27. package/dist/constants/user-interface-text.js +3 -1
  28. package/dist/constants/user-interface-text.js.map +1 -1
  29. package/dist/model/trace-queries.d.ts +13 -1
  30. package/dist/model/trace-queries.d.ts.map +1 -1
  31. package/dist/model/trace-queries.js.map +1 -1
  32. package/dist/remote/PluginRuntime.d.ts +0 -1
  33. package/dist/remote/PluginRuntime.d.ts.map +1 -1
  34. package/dist/remote/PluginRuntime.js +169 -160
  35. package/dist/remote/PluginRuntime.js.map +1 -1
  36. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.d.ts +7 -0
  37. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.d.ts.map +1 -1
  38. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.js +13 -0
  39. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.js.map +1 -1
  40. package/dist/runtime/plugin-registry.d.ts +2 -2
  41. package/dist/runtime/plugin-registry.d.ts.map +1 -1
  42. package/dist/runtime/plugin-registry.js.map +1 -1
  43. package/dist/runtime/time-series-queries.d.ts.map +1 -1
  44. package/dist/runtime/time-series-queries.js +1 -11
  45. package/dist/runtime/time-series-queries.js.map +1 -1
  46. package/dist/runtime/trace-queries.d.ts.map +1 -1
  47. package/dist/runtime/trace-queries.js +47 -17
  48. package/dist/runtime/trace-queries.js.map +1 -1
  49. package/dist/runtime/utils.d.ts +7 -0
  50. package/dist/runtime/utils.d.ts.map +1 -0
  51. package/dist/runtime/utils.js +25 -0
  52. package/dist/runtime/utils.js.map +1 -0
  53. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/runtime/time-series-queries.ts"],"sourcesContent":["// Copyright 2024 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport {\n useQuery,\n useQueries,\n useQueryClient,\n Query,\n QueryCache,\n QueryKey,\n QueryObserverOptions,\n UseQueryResult,\n} from '@tanstack/react-query';\nimport { TimeSeriesQueryDefinition, UnknownSpec, TimeSeriesData } from '@perses-dev/core';\nimport { TimeSeriesDataQuery, TimeSeriesQueryContext, TimeSeriesQueryMode, TimeSeriesQueryPlugin } from '../model';\nimport { VariableStateMap, useAllVariableValues } from './variables';\nimport { useTimeRange } from './TimeRangeProvider';\nimport { useDatasourceStore } from './datasources';\nimport { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';\n\nexport interface UseTimeSeriesQueryOptions {\n suggestedStepMs?: number;\n mode?: TimeSeriesQueryMode;\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): string {\n return Object.values(v)\n .map((v) => JSON.stringify(v.value))\n .join(',');\n}\n\nfunction filterVariableStateMap(v: VariableStateMap, names?: string[]): VariableStateMap {\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 queryKey: QueryKey;\n queryEnabled: boolean;\n} {\n const { timeRange, datasourceStore, suggestedStepMs, mode, 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 = [\n definition,\n timeRange,\n datasourceStore,\n suggestedStepMs,\n mode,\n variablesValueKey,\n refreshKey,\n ] 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 = (\n definition: TimeSeriesQueryDefinition,\n options?: UseTimeSeriesQueryOptions,\n queryOptions?: QueryObserverOptions<TimeSeriesData>\n): UseQueryResult<TimeSeriesData> => {\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: (queryOptions?.enabled ?? true) || 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(\n definitions: TimeSeriesQueryDefinition[],\n options?: UseTimeSeriesQueryOptions,\n queryOptions?: Omit<QueryObserverOptions, 'queryKey'>\n): Array<UseQueryResult<TimeSeriesData>> {\n const { getPlugin } = usePluginRegistry();\n const context = {\n ...useTimeSeriesQueryContext(),\n // We need mode to be part query key because this drives the type of query done (instant VS range query)\n mode: options?.mode,\n };\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: (queryOptions?.enabled ?? true) && queryEnabled,\n queryKey: queryKey,\n queryFn: async (): Promise<TimeSeriesData> => {\n const ctx: TimeSeriesQueryContext = {\n ...context,\n // Keep suggested step changes out of the query key, so we don´t have to run again query when it changes\n suggestedStepMs: options?.suggestedStepMs,\n };\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 = useAllVariableValues();\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(): TimeSeriesDataQuery[] {\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): TimeSeriesDataQuery[] {\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","useAllVariableValues","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","mode","variableState","refreshKey","dependencies","dependsOn","spec","variableDependencies","variables","filteredVariabledState","variablesValueKey","queryKey","waitToLoad","some","loading","queryEnabled","undefined","useTimeSeriesQuery","options","queryOptions","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,SACEA,QAAQ,EACRC,UAAU,EACVC,cAAc,QAMT,wBAAwB;AAG/B,SAA2BC,oBAAoB,QAAQ,cAAc;AACrE,SAASC,YAAY,QAAQ,sBAAsB;AACnD,SAASC,kBAAkB,QAAQ,gBAAgB;AACnD,SAASC,SAAS,EAAEC,iBAAiB,EAAEC,UAAU,QAAQ,oBAAoB;AAO7E,OAAO,MAAMC,wBAAwB,kBAAkB;AAEvD;;CAEC,GACD,SAASC,qBAAqBC,CAAmB;IAC/C,OAAOC,OAAOC,MAAM,CAACF,GAClBG,GAAG,CAAC,CAACH,IAAMI,KAAKC,SAAS,CAACL,EAAEM,KAAK,GACjCC,IAAI,CAAC;AACV;AAEA,SAASC,uBAAuBR,CAAmB,EAAES,KAAgB;IACnE,IAAI,CAACA,OAAO;QACV,OAAOT;IACT;IACA,OAAOC,OAAOS,WAAW,CAACT,OAAOU,OAAO,CAACX,GAAGY,MAAM,CAAC,CAAC,CAACC,KAAK,GAAKJ,MAAMK,QAAQ,CAACD;AAChF;AAEA,SAASE,gBAAgB,EACvBC,MAAM,EACNC,UAAU,EACVC,OAAO,EAKR;IAIC,MAAM,EAAEC,SAAS,EAAEC,eAAe,EAAEC,eAAe,EAAEC,IAAI,EAAEC,aAAa,E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OC,2BAA2BF;AACpC;AAEA;;CAEC,GACD,OAAO,SAASE,2BAA2BC,KAAiB;IAC1D,MAAMR,UAAiC,EAAE;IAEzC,KAAK,MAAMS,SAASD,MAAME,OAAO,CAAC;QAAEC,MAAM;IAAS,GAAI;QACrD,MAAMC,YAAYH,MAAM/B,QAAQ,EAAE,CAAC,EAAE;QACrC,IAAIkC,WAAWxB,QAAQ,AAACwB,UAAUxB,IAAI,CAAYyB,UAAU,CAACrE,wBAAwB;YACnFwD,QAAQc,IAAI,CAACL;QACf;IACF;IAEA,OAAOT;AACT"}
1
+ {"version":3,"sources":["../../src/runtime/time-series-queries.ts"],"sourcesContent":["// Copyright 2024 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport {\n useQuery,\n useQueries,\n useQueryClient,\n Query,\n QueryCache,\n QueryKey,\n QueryObserverOptions,\n UseQueryResult,\n} from '@tanstack/react-query';\nimport { TimeSeriesQueryDefinition, UnknownSpec, TimeSeriesData } from '@perses-dev/core';\nimport { TimeSeriesDataQuery, TimeSeriesQueryContext, TimeSeriesQueryMode, TimeSeriesQueryPlugin } from '../model';\nimport { useAllVariableValues } from './variables';\nimport { useTimeRange } from './TimeRangeProvider';\nimport { useDatasourceStore } from './datasources';\nimport { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';\nimport { filterVariableStateMap, getVariableValuesKey } from './utils';\n\nexport interface UseTimeSeriesQueryOptions {\n suggestedStepMs?: number;\n mode?: TimeSeriesQueryMode;\n}\n\nexport const TIME_SERIES_QUERY_KEY = 'TimeSeriesQuery';\n\nfunction getQueryOptions({\n plugin,\n definition,\n context,\n}: {\n plugin?: TimeSeriesQueryPlugin;\n definition: TimeSeriesQueryDefinition;\n context: TimeSeriesQueryContext;\n}): {\n queryKey: QueryKey;\n queryEnabled: boolean;\n} {\n const { timeRange, datasourceStore, suggestedStepMs, mode, 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 = [\n definition,\n timeRange,\n datasourceStore,\n suggestedStepMs,\n mode,\n variablesValueKey,\n refreshKey,\n ] 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 = (\n definition: TimeSeriesQueryDefinition,\n options?: UseTimeSeriesQueryOptions,\n queryOptions?: QueryObserverOptions<TimeSeriesData>\n): UseQueryResult<TimeSeriesData> => {\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: (queryOptions?.enabled ?? true) || 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(\n definitions: TimeSeriesQueryDefinition[],\n options?: UseTimeSeriesQueryOptions,\n queryOptions?: Omit<QueryObserverOptions, 'queryKey'>\n): Array<UseQueryResult<TimeSeriesData>> {\n const { getPlugin } = usePluginRegistry();\n const context = {\n ...useTimeSeriesQueryContext(),\n // We need mode to be part query key because this drives the type of query done (instant VS range query)\n mode: options?.mode,\n };\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: (queryOptions?.enabled ?? true) && queryEnabled,\n queryKey: queryKey,\n queryFn: async (): Promise<TimeSeriesData> => {\n const ctx: TimeSeriesQueryContext = {\n ...context,\n // Keep suggested step changes out of the query key, so we don´t have to run again query when it changes\n suggestedStepMs: options?.suggestedStepMs,\n };\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 = useAllVariableValues();\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(): TimeSeriesDataQuery[] {\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): TimeSeriesDataQuery[] {\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","useAllVariableValues","useTimeRange","useDatasourceStore","usePlugin","usePluginRegistry","usePlugins","filterVariableStateMap","getVariableValuesKey","TIME_SERIES_QUERY_KEY","getQueryOptions","plugin","definition","context","timeRange","datasourceStore","suggestedStepMs","mode","variableState","refreshKey","dependencies","dependsOn","spec","variableDependencies","variables","filteredVariabledState","variablesValueKey","queryKey","waitToLoad","some","v","loading","queryEnabled","undefined","useTimeSeriesQuery","options","queryOptions","data","kind","useTimeSeriesQueryContext","enabled","queryFn","Error","ctx","getTimeSeriesData","useTimeSeriesQueries","definitions","getPlugin","pluginLoaderResponse","map","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,SACEA,QAAQ,EACRC,UAAU,EACVC,cAAc,QAMT,wBAAwB;AAG/B,SAASC,oBAAoB,QAAQ,cAAc;AACnD,SAASC,YAAY,QAAQ,sBAAsB;AACnD,SAASC,kBAAkB,QAAQ,gBAAgB;AACnD,SAASC,SAAS,EAAEC,iBAAiB,EAAEC,UAAU,QAAQ,oBAAoB;AAC7E,SAASC,sBAAsB,EAAEC,oBAAoB,QAAQ,UAAU;AAOvE,OAAO,MAAMC,wBAAwB,kBAAkB;AAEvD,SAASC,gBAAgB,EACvBC,MAAM,EACNC,UAAU,EACVC,OAAO,EAKR;IAIC,MAAM,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IACpB,MAAMwD,aAAaD,YAAYE,aAAa;IAC5C,OAAOC,2BAA2BF;AACpC;AAEA;;CAEC,GACD,OAAO,SAASE,2BAA2BC,KAAiB;IAC1D,MAAMR,UAAiC,EAAE;IAEzC,KAAK,MAAMS,SAASD,MAAME,OAAO,CAAC;QAAEC,MAAM;IAAS,GAAI;QACrD,MAAMC,YAAYH,MAAMjC,QAAQ,EAAE,CAAC,EAAE;QACrC,IAAIoC,WAAWzB,QAAQ,AAACyB,UAAUzB,IAAI,CAAY0B,UAAU,CAACvD,wBAAwB;YACnF0C,QAAQc,IAAI,CAACL;QACf;IACF;IAEA,OAAOT;AACT"}
@@ -1 +1 @@
1
- {"version":3,"file":"trace-queries.d.ts","sourceRoot":"","sources":["../../src/runtime/trace-queries.tsx"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAc,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAInE,MAAM,MAAM,oBAAoB,CAAC,UAAU,GAAG,WAAW,IAAI,eAAe,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;AACvG,eAAO,MAAM,eAAe,eAAe,CAAC;AAE5C,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,iBAAiB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAM7F;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,oBAAoB,EAAE,GAAG,KAAK,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC,CA+BrG"}
1
+ {"version":3,"file":"trace-queries.d.ts","sourceRoot":"","sources":["../../src/runtime/trace-queries.tsx"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAwB,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAO7E,MAAM,MAAM,oBAAoB,CAAC,UAAU,GAAG,WAAW,IAAI,eAAe,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;AACvG,eAAO,MAAM,eAAe,eAAe,CAAC;AAE5C,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,iBAAiB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAM7F;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,oBAAoB,EAAE,GAAG,KAAK,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC,CA2BrG"}
@@ -13,8 +13,10 @@
13
13
  import { getUnixTime } from 'date-fns';
14
14
  import { useQueries } from '@tanstack/react-query';
15
15
  import { useDatasourceStore } from './datasources';
16
- import { usePluginRegistry } from './plugin-registry';
16
+ import { usePluginRegistry, usePlugins } from './plugin-registry';
17
17
  import { useTimeRange } from './TimeRangeProvider';
18
+ import { useAllVariableValues } from './variables';
19
+ import { filterVariableStateMap, getVariableValuesKey } from './utils';
18
20
  export const TRACE_QUERY_KEY = 'TraceQuery';
19
21
  export function getUnixTimeRange(timeRange) {
20
22
  const { start, end } = timeRange;
@@ -29,36 +31,64 @@ export function getUnixTimeRange(timeRange) {
29
31
  * Documentation for TraceQL: https://grafana.com/docs/tempo/latest/traceql/
30
32
  */ export function useTraceQueries(definitions) {
31
33
  const { getPlugin } = usePluginRegistry();
32
- const datasourceStore = useDatasourceStore();
33
- const { absoluteTimeRange } = useTimeRange();
34
- const context = {
35
- datasourceStore,
36
- absoluteTimeRange
37
- };
34
+ const context = useTraceQueryContext();
35
+ const pluginLoaderResponse = usePlugins('TraceQuery', definitions.map((d)=>({
36
+ kind: d.spec.plugin.kind
37
+ })));
38
38
  // useQueries() handles data fetching from query plugins (e.g. traceQL queries, promQL queries)
39
39
  // https://tanstack.com/query/v4/docs/react/reference/useQuery
40
40
  return useQueries({
41
- queries: definitions.map((definition)=>{
42
- const queryKey = [
41
+ queries: definitions.map((definition, idx)=>{
42
+ const plugin = pluginLoaderResponse[idx]?.data;
43
+ const { queryEnabled, queryKey } = getQueryOptions({
44
+ context,
43
45
  definition,
44
- datasourceStore,
45
- absoluteTimeRange
46
- ]; // `queryKey` watches and reruns `queryFn` if keys in the array change
46
+ plugin
47
+ });
47
48
  const traceQueryKind = definition?.spec?.plugin?.kind;
48
49
  return {
50
+ enabled: queryEnabled,
49
51
  queryKey: queryKey,
50
52
  queryFn: async ()=>{
51
53
  const plugin = await getPlugin(TRACE_QUERY_KEY, traceQueryKind);
52
54
  const data = await plugin.getTraceData(definition.spec.plugin.spec, context);
53
55
  return data;
54
- },
55
- // The data returned by getTraceData() contains circular dependencies (a span has a reference to the parent span, and the parent span has an array of child spans)
56
- // Therefore structuralSharing must be turned off, otherwise the query is stuck in the 'fetching' state on re-fetch.
57
- // Ref: https://github.com/TanStack/query/issues/6954#issuecomment-1962321426
58
- structuralSharing: false
56
+ }
59
57
  };
60
58
  })
61
59
  });
62
60
  }
61
+ function getQueryOptions({ plugin, definition, context }) {
62
+ const { datasourceStore, variableState, absoluteTimeRange } = context;
63
+ const dependencies = plugin?.dependsOn ? plugin.dependsOn(definition.spec.plugin.spec, context) : {};
64
+ const variableDependencies = dependencies?.variables;
65
+ const filteredVariabledState = filterVariableStateMap(variableState, variableDependencies);
66
+ const variablesValueKey = getVariableValuesKey(filteredVariabledState);
67
+ const queryKey = [
68
+ definition,
69
+ datasourceStore,
70
+ absoluteTimeRange,
71
+ variablesValueKey
72
+ ];
73
+ let waitToLoad = false;
74
+ if (variableDependencies) {
75
+ waitToLoad = variableDependencies.some((v)=>variableState[v]?.loading);
76
+ }
77
+ const queryEnabled = plugin !== undefined && !waitToLoad;
78
+ return {
79
+ queryKey,
80
+ queryEnabled
81
+ };
82
+ }
83
+ function useTraceQueryContext() {
84
+ const { absoluteTimeRange } = useTimeRange();
85
+ const variableState = useAllVariableValues();
86
+ const datasourceStore = useDatasourceStore();
87
+ return {
88
+ variableState,
89
+ datasourceStore,
90
+ absoluteTimeRange
91
+ };
92
+ }
63
93
 
64
94
  //# sourceMappingURL=trace-queries.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/runtime/trace-queries.tsx"],"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 { getUnixTime } from 'date-fns';\nimport { QueryDefinition, UnknownSpec, AbsoluteTimeRange, TraceData } from '@perses-dev/core';\nimport { useQueries, UseQueryResult } from '@tanstack/react-query';\nimport { useDatasourceStore } from './datasources';\nimport { usePluginRegistry } from './plugin-registry';\nimport { useTimeRange } from './TimeRangeProvider';\nexport type TraceQueryDefinition<PluginSpec = UnknownSpec> = QueryDefinition<'TraceQuery', PluginSpec>;\nexport const TRACE_QUERY_KEY = 'TraceQuery';\n\nexport function getUnixTimeRange(timeRange: AbsoluteTimeRange): { start: number; end: number } {\n const { start, end } = timeRange;\n return {\n start: Math.ceil(getUnixTime(start)),\n end: Math.ceil(getUnixTime(end)),\n };\n}\n\n/**\n * Run a trace query using a TraceQuery plugin and return the results\n * @param definitions: dashboard defintion for a trace query, written in Trace Query Language (TraceQL)\n * Documentation for TraceQL: https://grafana.com/docs/tempo/latest/traceql/\n */\nexport function useTraceQueries(definitions: TraceQueryDefinition[]): Array<UseQueryResult<TraceData>> {\n const { getPlugin } = usePluginRegistry();\n const datasourceStore = useDatasourceStore();\n const { absoluteTimeRange } = useTimeRange();\n\n const context = {\n datasourceStore,\n absoluteTimeRange,\n };\n\n // useQueries() handles data fetching from query plugins (e.g. traceQL queries, promQL queries)\n // https://tanstack.com/query/v4/docs/react/reference/useQuery\n return useQueries({\n queries: definitions.map((definition) => {\n const queryKey = [definition, datasourceStore, absoluteTimeRange] as const; // `queryKey` watches and reruns `queryFn` if keys in the array change\n const traceQueryKind = definition?.spec?.plugin?.kind;\n return {\n queryKey: queryKey,\n queryFn: async (): Promise<TraceData> => {\n const plugin = await getPlugin(TRACE_QUERY_KEY, traceQueryKind);\n const data = await plugin.getTraceData(definition.spec.plugin.spec, context);\n return data;\n },\n\n // The data returned by getTraceData() contains circular dependencies (a span has a reference to the parent span, and the parent span has an array of child spans)\n // Therefore structuralSharing must be turned off, otherwise the query is stuck in the 'fetching' state on re-fetch.\n // Ref: https://github.com/TanStack/query/issues/6954#issuecomment-1962321426\n structuralSharing: false,\n };\n }),\n });\n}\n"],"names":["getUnixTime","useQueries","useDatasourceStore","usePluginRegistry","useTimeRange","TRACE_QUERY_KEY","getUnixTimeRange","timeRange","start","end","Math","ceil","useTraceQueries","definitions","getPlugin","datasourceStore","absoluteTimeRange","context","queries","map","definition","queryKey","traceQueryKind","spec","plugin","kind","queryFn","data","getTraceData","structuralSharing"],"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,WAAW,QAAQ,WAAW;AAEvC,SAASC,UAAU,QAAwB,wBAAwB;AACnE,SAASC,kBAAkB,QAAQ,gBAAgB;AACnD,SAASC,iBAAiB,QAAQ,oBAAoB;AACtD,SAASC,YAAY,QAAQ,sBAAsB;AAEnD,OAAO,MAAMC,kBAAkB,aAAa;AAE5C,OAAO,SAASC,iBAAiBC,SAA4B;IAC3D,MAAM,EAAEC,KAAK,EAAEC,GAAG,EAAE,GAAGF;IACvB,OAAO;QACLC,OAAOE,KAAKC,IAAI,CAACX,YAAYQ;QAC7BC,KAAKC,KAAKC,IAAI,CAACX,YAAYS;IAC7B;AACF;AAEA;;;;CAIC,GACD,OAAO,SAASG,gBAAgBC,WAAmC;IACjE,MAAM,EAAEC,SAAS,EAAE,GAAGX;IACtB,MAAMY,kBAAkBb;IACxB,MAAM,EAAEc,iBAAiB,EAAE,GAAGZ;IAE9B,MAAMa,UAAU;QACdF;QACAC;IACF;IAEA,+FAA+F;IAC/F,8DAA8D;IAC9D,OAAOf,WAAW;QAChBiB,SAASL,YAAYM,GAAG,CAAC,CAACC;YACxB,MAAMC,WAAW;gBAACD;gBAAYL;gBAAiBC;aAAkB,EAAW,sEAAsE;YAClJ,MAAMM,iBAAiBF,YAAYG,MAAMC,QAAQC;YACjD,OAAO;gBACLJ,UAAUA;gBACVK,SAAS;oBACP,MAAMF,SAAS,MAAMV,UAAUT,iBAAiBiB;oBAChD,MAAMK,OAAO,MAAMH,OAAOI,YAAY,CAACR,WAAWG,IAAI,CAACC,MAAM,CAACD,IAAI,EAAEN;oBACpE,OAAOU;gBACT;gBAEA,kKAAkK;gBAClK,oHAAoH;gBACpH,6EAA6E;gBAC7EE,mBAAmB;YACrB;QACF;IACF;AACF"}
1
+ {"version":3,"sources":["../../src/runtime/trace-queries.tsx"],"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 { getUnixTime } from 'date-fns';\nimport { QueryDefinition, UnknownSpec, AbsoluteTimeRange, TraceData } from '@perses-dev/core';\nimport { QueryKey, useQueries, UseQueryResult } from '@tanstack/react-query';\nimport { TraceQueryContext, TraceQueryPlugin } from '../model';\nimport { useDatasourceStore } from './datasources';\nimport { usePluginRegistry, usePlugins } from './plugin-registry';\nimport { useTimeRange } from './TimeRangeProvider';\nimport { useAllVariableValues } from './variables';\nimport { filterVariableStateMap, getVariableValuesKey } from './utils';\nexport type TraceQueryDefinition<PluginSpec = UnknownSpec> = QueryDefinition<'TraceQuery', PluginSpec>;\nexport const TRACE_QUERY_KEY = 'TraceQuery';\n\nexport function getUnixTimeRange(timeRange: AbsoluteTimeRange): { start: number; end: number } {\n const { start, end } = timeRange;\n return {\n start: Math.ceil(getUnixTime(start)),\n end: Math.ceil(getUnixTime(end)),\n };\n}\n\n/**\n * Run a trace query using a TraceQuery plugin and return the results\n * @param definitions: dashboard defintion for a trace query, written in Trace Query Language (TraceQL)\n * Documentation for TraceQL: https://grafana.com/docs/tempo/latest/traceql/\n */\nexport function useTraceQueries(definitions: TraceQueryDefinition[]): Array<UseQueryResult<TraceData>> {\n const { getPlugin } = usePluginRegistry();\n const context = useTraceQueryContext();\n\n const pluginLoaderResponse = usePlugins(\n 'TraceQuery',\n definitions.map((d) => ({ kind: d.spec.plugin.kind }))\n );\n\n // useQueries() handles data fetching from query plugins (e.g. traceQL queries, promQL queries)\n // https://tanstack.com/query/v4/docs/react/reference/useQuery\n return useQueries({\n queries: definitions.map((definition, idx) => {\n const plugin = pluginLoaderResponse[idx]?.data;\n const { queryEnabled, queryKey } = getQueryOptions({ context, definition, plugin });\n const traceQueryKind = definition?.spec?.plugin?.kind;\n return {\n enabled: queryEnabled,\n queryKey: queryKey,\n queryFn: async (): Promise<TraceData> => {\n const plugin = await getPlugin(TRACE_QUERY_KEY, traceQueryKind);\n const data = await plugin.getTraceData(definition.spec.plugin.spec, context);\n return data;\n },\n };\n }),\n });\n}\n\nfunction getQueryOptions({\n plugin,\n definition,\n context,\n}: {\n plugin?: TraceQueryPlugin;\n definition: TraceQueryDefinition;\n context: TraceQueryContext;\n}): {\n queryKey: QueryKey;\n queryEnabled: boolean;\n} {\n const { datasourceStore, variableState, absoluteTimeRange } = context;\n\n const dependencies = plugin?.dependsOn ? plugin.dependsOn(definition.spec.plugin.spec, context) : {};\n const variableDependencies = dependencies?.variables;\n\n const filteredVariabledState = filterVariableStateMap(variableState, variableDependencies);\n const variablesValueKey = getVariableValuesKey(filteredVariabledState);\n const queryKey = [definition, datasourceStore, absoluteTimeRange, variablesValueKey] as const;\n\n let waitToLoad = false;\n if (variableDependencies) {\n waitToLoad = variableDependencies.some((v) => variableState[v]?.loading);\n }\n\n const queryEnabled = plugin !== undefined && !waitToLoad;\n return {\n queryKey,\n queryEnabled,\n };\n}\n\nfunction useTraceQueryContext(): TraceQueryContext {\n const { absoluteTimeRange } = useTimeRange();\n const variableState = useAllVariableValues();\n const datasourceStore = useDatasourceStore();\n\n return {\n variableState,\n datasourceStore,\n absoluteTimeRange,\n };\n}\n"],"names":["getUnixTime","useQueries","useDatasourceStore","usePluginRegistry","usePlugins","useTimeRange","useAllVariableValues","filterVariableStateMap","getVariableValuesKey","TRACE_QUERY_KEY","getUnixTimeRange","timeRange","start","end","Math","ceil","useTraceQueries","definitions","getPlugin","context","useTraceQueryContext","pluginLoaderResponse","map","d","kind","spec","plugin","queries","definition","idx","data","queryEnabled","queryKey","getQueryOptions","traceQueryKind","enabled","queryFn","getTraceData","datasourceStore","variableState","absoluteTimeRange","dependencies","dependsOn","variableDependencies","variables","filteredVariabledState","variablesValueKey","waitToLoad","some","v","loading","undefined"],"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,WAAW,QAAQ,WAAW;AAEvC,SAAmBC,UAAU,QAAwB,wBAAwB;AAE7E,SAASC,kBAAkB,QAAQ,gBAAgB;AACnD,SAASC,iBAAiB,EAAEC,UAAU,QAAQ,oBAAoB;AAClE,SAASC,YAAY,QAAQ,sBAAsB;AACnD,S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gB,YAAYhB,OAAOgB,SAAS,CAACd,WAAWH,IAAI,CAACC,MAAM,CAACD,IAAI,EAAEN,WAAW,CAAC;IACnG,MAAMwB,uBAAuBF,cAAcG;IAE3C,MAAMC,yBAAyBtC,uBAAuBgC,eAAeI;IACrE,MAAMG,oBAAoBtC,qBAAqBqC;IAC/C,MAAMb,WAAW;QAACJ;QAAYU;QAAiBE;QAAmBM;KAAkB;IAEpF,IAAIC,aAAa;IACjB,IAAIJ,sBAAsB;QACxBI,aAAaJ,qBAAqBK,IAAI,CAAC,CAACC,IAAMV,aAAa,CAACU,EAAE,EAAEC;IAClE;IAEA,MAAMnB,eAAeL,WAAWyB,aAAa,CAACJ;IAC9C,OAAO;QACLf;QACAD;IACF;AACF;AAEA,SAASX;IACP,MAAM,EAAEoB,iBAAiB,EAAE,GAAGnC;IAC9B,MAAMkC,gBAAgBjC;IACtB,MAAMgC,kBAAkBpC;IAExB,OAAO;QACLqC;QACAD;QACAE;IACF;AACF"}
@@ -0,0 +1,7 @@
1
+ import { VariableStateMap } from './variables';
2
+ export declare function filterVariableStateMap(v: VariableStateMap, names?: string[]): VariableStateMap;
3
+ /**
4
+ * Returns a serialized string of the current state of variable values.
5
+ */
6
+ export declare function getVariableValuesKey(v: VariableStateMap): string;
7
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/runtime/utils.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C,wBAAgB,sBAAsB,CAAC,CAAC,EAAE,gBAAgB,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,gBAAgB,CAK9F;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,gBAAgB,GAAG,MAAM,CAIhE"}
@@ -0,0 +1,25 @@
1
+ // Copyright 2023 The Perses Authors
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ //
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ export function filterVariableStateMap(v, names) {
14
+ if (!names) {
15
+ return v;
16
+ }
17
+ return Object.fromEntries(Object.entries(v).filter(([name])=>names.includes(name)));
18
+ }
19
+ /**
20
+ * Returns a serialized string of the current state of variable values.
21
+ */ export function getVariableValuesKey(v) {
22
+ return Object.values(v).map((v)=>JSON.stringify(v.value)).join(',');
23
+ }
24
+
25
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/runtime/utils.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 { VariableStateMap } from './variables';\n\nexport function filterVariableStateMap(v: VariableStateMap, names?: string[]): VariableStateMap {\n if (!names) {\n return v;\n }\n return Object.fromEntries(Object.entries(v).filter(([name]) => names.includes(name)));\n}\n\n/**\n * Returns a serialized string of the current state of variable values.\n */\nexport function getVariableValuesKey(v: VariableStateMap): string {\n return Object.values(v)\n .map((v) => JSON.stringify(v.value))\n .join(',');\n}\n"],"names":["filterVariableStateMap","v","names","Object","fromEntries","entries","filter","name","includes","getVariableValuesKey","values","map","JSON","stringify","value","join"],"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;AAIjC,OAAO,SAASA,uBAAuBC,CAAmB,EAAEC,KAAgB;IAC1E,IAAI,CAACA,OAAO;QACV,OAAOD;IACT;IACA,OAAOE,OAAOC,WAAW,CAACD,OAAOE,OAAO,CAACJ,GAAGK,MAAM,CAAC,CAAC,CAACC,KAAK,GAAKL,MAAMM,QAAQ,CAACD;AAChF;AAEA;;CAEC,GACD,OAAO,SAASE,qBAAqBR,CAAmB;IACtD,OAAOE,OAAOO,MAAM,CAACT,GAClBU,GAAG,CAAC,CAACV,IAAMW,KAAKC,SAAS,CAACZ,EAAEa,KAAK,GACjCC,IAAI,CAAC;AACV"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@perses-dev/plugin-system",
3
- "version": "0.51.0-beta.1",
3
+ "version": "0.51.0-rc.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",
@@ -29,8 +29,8 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@module-federation/enhanced": "^0.8.9",
32
- "@perses-dev/components": "0.51.0-beta.1",
33
- "@perses-dev/core": "0.51.0-beta.1",
32
+ "@perses-dev/components": "0.51.0-rc.0",
33
+ "@perses-dev/core": "0.51.0-rc.0",
34
34
  "date-fns": "^4.1.0",
35
35
  "date-fns-tz": "^3.2.0",
36
36
  "immer": "^10.1.1",