@perses-dev/plugin-system 0.51.0-beta.0 → 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 (107) hide show
  1. package/dist/cjs/components/DatasourceSelect.js +155 -71
  2. package/dist/cjs/components/MultiQueryEditor/QueryEditorContainer.js +2 -2
  3. package/dist/cjs/components/TimeRangeControls/TimeRangeControls.js +91 -1
  4. package/dist/cjs/components/Variables/VariableEditorForm/VariableEditorForm.js +11 -7
  5. package/dist/cjs/constants/user-interface-text.js +3 -1
  6. package/dist/cjs/remote/PluginRuntime.js +168 -162
  7. package/dist/cjs/runtime/TimeRangeProvider/TimeRangeSettingsProvider.js +13 -0
  8. package/dist/cjs/runtime/plugin-registry.js +13 -4
  9. package/dist/cjs/runtime/time-series-queries.js +3 -13
  10. package/dist/cjs/runtime/trace-queries.js +46 -16
  11. package/dist/cjs/{stories/shared-utils/index.js → runtime/utils.js} +21 -12
  12. package/dist/components/DatasourceEditorForm/DatasourceEditorForm.js.map +1 -1
  13. package/dist/components/DatasourceSelect.d.ts +8 -3
  14. package/dist/components/DatasourceSelect.d.ts.map +1 -1
  15. package/dist/components/DatasourceSelect.js +148 -72
  16. package/dist/components/DatasourceSelect.js.map +1 -1
  17. package/dist/components/HTTPSettingsEditor/HTTPSettingsEditor.js.map +1 -1
  18. package/dist/components/MultiQueryEditor/QueryEditorContainer.js +1 -1
  19. package/dist/components/MultiQueryEditor/QueryEditorContainer.js.map +1 -1
  20. package/dist/components/OptionsEditorTabPanel/OptionsEditorTabPanel.js.map +1 -1
  21. package/dist/components/OptionsEditorTabs/OptionsEditorTabs.js.map +1 -1
  22. package/dist/components/PluginRegistry/PluginRegistry.js.map +1 -1
  23. package/dist/components/PluginRegistry/plugin-indexes.d.ts +1 -1
  24. package/dist/components/PluginRegistry/plugin-indexes.d.ts.map +1 -1
  25. package/dist/components/PluginRegistry/plugin-indexes.js.map +1 -1
  26. package/dist/components/TimeRangeControls/TimeRangeControls.d.ts +2 -1
  27. package/dist/components/TimeRangeControls/TimeRangeControls.d.ts.map +1 -1
  28. package/dist/components/TimeRangeControls/TimeRangeControls.js +94 -2
  29. package/dist/components/TimeRangeControls/TimeRangeControls.js.map +1 -1
  30. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts.map +1 -1
  31. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js +11 -7
  32. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js.map +1 -1
  33. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.js.map +1 -1
  34. package/dist/components/Variables/variable-model.js.map +1 -1
  35. package/dist/constants/user-interface-text.d.ts +2 -0
  36. package/dist/constants/user-interface-text.d.ts.map +1 -1
  37. package/dist/constants/user-interface-text.js +3 -1
  38. package/dist/constants/user-interface-text.js.map +1 -1
  39. package/dist/model/trace-queries.d.ts +13 -1
  40. package/dist/model/trace-queries.d.ts.map +1 -1
  41. package/dist/model/trace-queries.js.map +1 -1
  42. package/dist/remote/PluginLoaderComponent.js.map +1 -1
  43. package/dist/remote/PluginRuntime.d.ts +0 -1
  44. package/dist/remote/PluginRuntime.d.ts.map +1 -1
  45. package/dist/remote/PluginRuntime.js +169 -160
  46. package/dist/remote/PluginRuntime.js.map +1 -1
  47. package/dist/remote/remotePluginLoader.js.map +1 -1
  48. package/dist/runtime/DataQueriesProvider/model.js.map +1 -1
  49. package/dist/runtime/TimeRangeProvider/TimeRangeProvider.js.map +1 -1
  50. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.d.ts +7 -0
  51. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.d.ts.map +1 -1
  52. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.js +13 -0
  53. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.js.map +1 -1
  54. package/dist/runtime/plugin-registry.d.ts +2 -2
  55. package/dist/runtime/plugin-registry.d.ts.map +1 -1
  56. package/dist/runtime/plugin-registry.js +13 -4
  57. package/dist/runtime/plugin-registry.js.map +1 -1
  58. package/dist/runtime/time-series-queries.d.ts.map +1 -1
  59. package/dist/runtime/time-series-queries.js +1 -11
  60. package/dist/runtime/time-series-queries.js.map +1 -1
  61. package/dist/runtime/trace-queries.d.ts.map +1 -1
  62. package/dist/runtime/trace-queries.js +47 -17
  63. package/dist/runtime/trace-queries.js.map +1 -1
  64. package/dist/runtime/utils.d.ts +7 -0
  65. package/dist/runtime/utils.d.ts.map +1 -0
  66. package/dist/{stories/shared-utils/index.js → runtime/utils.js} +12 -2
  67. package/dist/runtime/utils.js.map +1 -0
  68. package/dist/test-utils/mock-plugin-registry.js.map +1 -1
  69. package/package.json +7 -9
  70. package/dist/cjs/stories/shared-utils/decorators/WithDataQueries.js +0 -41
  71. package/dist/cjs/stories/shared-utils/decorators/WithPluginRegistry.js +0 -34
  72. package/dist/cjs/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.js +0 -42
  73. package/dist/cjs/stories/shared-utils/decorators/WithPluginSystemDatasourceStore.js +0 -101
  74. package/dist/cjs/stories/shared-utils/decorators/WithPluginSystemVariables.js +0 -42
  75. package/dist/cjs/stories/shared-utils/decorators/WithTimeRange.js +0 -41
  76. package/dist/cjs/stories/shared-utils/decorators/index.js +0 -35
  77. package/dist/stories/shared-utils/decorators/WithDataQueries.d.ts +0 -13
  78. package/dist/stories/shared-utils/decorators/WithDataQueries.d.ts.map +0 -1
  79. package/dist/stories/shared-utils/decorators/WithDataQueries.js +0 -33
  80. package/dist/stories/shared-utils/decorators/WithDataQueries.js.map +0 -1
  81. package/dist/stories/shared-utils/decorators/WithPluginRegistry.d.ts +0 -4
  82. package/dist/stories/shared-utils/decorators/WithPluginRegistry.d.ts.map +0 -1
  83. package/dist/stories/shared-utils/decorators/WithPluginRegistry.js +0 -26
  84. package/dist/stories/shared-utils/decorators/WithPluginRegistry.js.map +0 -1
  85. package/dist/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.d.ts +0 -13
  86. package/dist/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.d.ts.map +0 -1
  87. package/dist/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.js +0 -39
  88. package/dist/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.js.map +0 -1
  89. package/dist/stories/shared-utils/decorators/WithPluginSystemDatasourceStore.d.ts +0 -13
  90. package/dist/stories/shared-utils/decorators/WithPluginSystemDatasourceStore.d.ts.map +0 -1
  91. package/dist/stories/shared-utils/decorators/WithPluginSystemDatasourceStore.js +0 -95
  92. package/dist/stories/shared-utils/decorators/WithPluginSystemDatasourceStore.js.map +0 -1
  93. package/dist/stories/shared-utils/decorators/WithPluginSystemVariables.d.ts +0 -13
  94. package/dist/stories/shared-utils/decorators/WithPluginSystemVariables.d.ts.map +0 -1
  95. package/dist/stories/shared-utils/decorators/WithPluginSystemVariables.js +0 -39
  96. package/dist/stories/shared-utils/decorators/WithPluginSystemVariables.js.map +0 -1
  97. package/dist/stories/shared-utils/decorators/WithTimeRange.d.ts +0 -13
  98. package/dist/stories/shared-utils/decorators/WithTimeRange.d.ts.map +0 -1
  99. package/dist/stories/shared-utils/decorators/WithTimeRange.js +0 -33
  100. package/dist/stories/shared-utils/decorators/WithTimeRange.js.map +0 -1
  101. package/dist/stories/shared-utils/decorators/index.d.ts +0 -7
  102. package/dist/stories/shared-utils/decorators/index.d.ts.map +0 -1
  103. package/dist/stories/shared-utils/decorators/index.js +0 -20
  104. package/dist/stories/shared-utils/decorators/index.js.map +0 -1
  105. package/dist/stories/shared-utils/index.d.ts +0 -2
  106. package/dist/stories/shared-utils/index.d.ts.map +0 -1
  107. package/dist/stories/shared-utils/index.js.map +0 -1
@@ -4,7 +4,7 @@ import { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
4
4
  import { DefaultPluginKinds, PluginImplementation, PluginMetadataWithModule, PluginType } from '../model';
5
5
  export interface PluginRegistryContextType {
6
6
  getPlugin<T extends PluginType>(kind: T, name: string): Promise<PluginImplementation<T>>;
7
- listPluginMetadata(pluginTypes: string[]): Promise<PluginMetadataWithModule[]>;
7
+ listPluginMetadata(pluginTypes: PluginType[]): Promise<PluginMetadataWithModule[]>;
8
8
  defaultPluginKinds?: DefaultPluginKinds;
9
9
  }
10
10
  export declare const PluginRegistryContext: import("react").Context<PluginRegistryContextType | undefined>;
@@ -28,7 +28,7 @@ type UseListPluginMetadataOptions = Omit<UseQueryOptions<PluginMetadataWithModul
28
28
  /**
29
29
  * Gets a list of plugin metadata for the specified plugin type and returns it, along with loading/error state.
30
30
  */
31
- export declare function useListPluginMetadata(pluginTypes: string[], options?: UseListPluginMetadataOptions): UseQueryResult<PluginMetadataWithModule[]>;
31
+ export declare function useListPluginMetadata(pluginTypes: PluginType[], options?: UseListPluginMetadataOptions): UseQueryResult<PluginMetadataWithModule[]>;
32
32
  export declare function usePluginBuiltinVariableDefinitions(): UseQueryResult<BuiltinVariableDefinition[]>;
33
33
  export {};
34
34
  //# sourceMappingURL=plugin-registry.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin-registry.d.ts","sourceRoot":"","sources":["../../src/runtime/plugin-registry.ts"],"names":[],"mappings":";AAaA,OAAO,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAwB,eAAe,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE9F,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE1G,MAAM,WAAW,yBAAyB;IACxC,SAAS,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC;IACzF,kBAAkB,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,wBAAwB,EAAE,CAAC,CAAC;IAC/E,kBAAkB,CAAC,EAAE,kBAAkB,CAA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AAU,EAAE,CAAC,EACb,OAAO,EAAE,KAAK,CAAC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,GAC/B,KAAK,CAAC,cAAc,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAoBhD;AAGD,KAAK,4BAA4B,GAAG,IAAI,CACtC,eAAe,CAAC,wBAAwB,EAAE,EAAE,KAAK,EAAE,wBAAwB,EAAE,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,EAClG,UAAU,GAAG,SAAS,CACvB,CAAC;AAEF;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,UAAU,EAAE,EACzB,OAAO,CAAC,EAAE,4BAA4B,GACrC,cAAc,CAAC,wBAAwB,EAAE,CAAC,CAO5C;AAED,wBAAgB,mCAAmC,IAAI,cAAc,CAAC,yBAAyB,EAAE,CAAC,CAkBjG"}
@@ -46,18 +46,27 @@ export const PluginRegistryContext = createContext(undefined);
46
46
  * Loads a list of plugins and returns the plugin implementation, along with loading/error state.
47
47
  */ export function usePlugins(pluginType, plugins) {
48
48
  const { getPlugin } = usePluginRegistry();
49
- return useQueries({
50
- queries: plugins.map((p)=>{
49
+ // useQueries() does not support queries with duplicate keys, therefore we de-duplicate the plugin kinds before running useQueries()
50
+ // This resolves the following warning in the JS console: "[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior."
51
+ // https://github.com/TanStack/query/issues/8224#issuecomment-2523554831
52
+ // https://github.com/TanStack/query/issues/4187#issuecomment-1256336901
53
+ const kinds = [
54
+ ...new Set(plugins.map((p)=>p.kind))
55
+ ];
56
+ const result = useQueries({
57
+ queries: kinds.map((kind)=>{
51
58
  return {
52
59
  queryKey: [
53
60
  'getPlugin',
54
61
  pluginType,
55
- p.kind
62
+ kind
56
63
  ],
57
- queryFn: ()=>getPlugin(pluginType, p.kind)
64
+ queryFn: ()=>getPlugin(pluginType, kind)
58
65
  };
59
66
  })
60
67
  });
68
+ // Re-assemble array in original order
69
+ return plugins.map((p)=>result[kinds.indexOf(p.kind)]);
61
70
  }
62
71
  /**
63
72
  * Gets a list of plugin metadata for the specified plugin type and returns it, along with loading/error state.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/runtime/plugin-registry.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 { BuiltinVariableDefinition } from '@perses-dev/core';\nimport { useQueries, useQuery, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';\nimport { createContext, useContext } from 'react';\nimport { DefaultPluginKinds, PluginImplementation, PluginMetadataWithModule, PluginType } from '../model';\n\nexport interface PluginRegistryContextType {\n getPlugin<T extends PluginType>(kind: T, name: string): Promise<PluginImplementation<T>>;\n listPluginMetadata(pluginTypes: string[]): Promise<PluginMetadataWithModule[]>;\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(): PluginRegistryContextType {\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 | undefined, 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>(\n pluginType: T | undefined,\n kind: string,\n options?: UsePluginOptions<T>\n): UseQueryResult<PluginImplementation<T>, Error> {\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) && pluginType !== undefined && kind !== '',\n };\n const { getPlugin } = usePluginRegistry();\n return useQuery({\n queryKey: ['getPlugin', pluginType, kind],\n queryFn: () => getPlugin(pluginType!, kind),\n ...options,\n });\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>(\n pluginType: T,\n plugins: Array<{ kind: string }>\n): Array<UseQueryResult<PluginImplementation<T>>> {\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<PluginMetadataWithModule[], Error, PluginMetadataWithModule[], [string, string[]]>,\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(\n pluginTypes: string[],\n options?: UseListPluginMetadataOptions\n): UseQueryResult<PluginMetadataWithModule[]> {\n const { listPluginMetadata } = usePluginRegistry();\n return useQuery({\n queryKey: ['listPluginMetadata', pluginTypes],\n queryFn: () => listPluginMetadata(pluginTypes),\n ...options,\n });\n}\n\nexport function usePluginBuiltinVariableDefinitions(): UseQueryResult<BuiltinVariableDefinition[]> {\n const { getPlugin, listPluginMetadata } = usePluginRegistry();\n\n return useQuery({\n queryKey: ['usePluginBuiltinVariableDefinitions'],\n queryFn: async () => {\n const datasources = await listPluginMetadata(['Datasource']);\n const datasourceNames = new Set(datasources.map((datasource) => datasource.spec.name));\n const result: BuiltinVariableDefinition[] = [];\n for (const name of datasourceNames) {\n const plugin = await getPlugin('Datasource', name);\n if (plugin.getBuiltinVariableDefinitions) {\n plugin.getBuiltinVariableDefinitions().forEach((definition) => result.push(definition));\n }\n }\n return result;\n },\n });\n}\n"],"names":["useQueries","useQuery","createContext","useContext","PluginRegistryContext","undefined","usePluginRegistry","ctx","Error","usePlugin","pluginType","kind","options","enabled","getPlugin","queryKey","queryFn","usePlugins","plugins","queries","map","p","useListPluginMetadata","pluginTypes","listPluginMetadata","usePluginBuiltinVariableDefinitions","datasources","datasourceNames","Set","datasource","spec","name","result","plugin","getBuiltinVariableDefinitions","forEach","definition","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;AAGjC,SAASA,UAAU,EAAEC,QAAQ,QAAyC,wBAAwB;AAC9F,SAASC,aAAa,EAAEC,UAAU,QAAQ,QAAQ;AASlD,OAAO,MAAMC,wBAAwBF,cAAqDG,WAAW;AAErG;;;CAGC,GACD,OAAO,SAASC;IACd,MAAMC,MAAMJ,WAAWC;IACvB,IAAIG,QAAQF,WAAW;QACrB,MAAM,IAAIG,MAAM;IAClB;IACA,OAAOD;AACT;AAQA;;CAEC,GACD,OAAO,SAASE,UACdC,UAAyB,EACzBC,IAAY,EACZC,OAA6B;IAE7B,wGAAwG;IACxGA,UAAU;QACR,GAAGA,OAAO;QACVC,SAAS,AAACD,CAAAA,SAASC,WAAW,IAAG,KAAMH,eAAeL,aAAaM,SAAS;IAC9E;IACA,MAAM,EAAEG,SAAS,EAAE,GAAGR;IACtB,OAAOL,SAAS;QACdc,UAAU;YAAC;YAAaL;YAAYC;SAAK;QACzCK,SAAS,IAAMF,UAAUJ,YAAaC;QACtC,GAAGC,OAAO;IACZ;AACF;AAEA;;CAEC,GACD,OAAO,SAASK,WACdP,UAAa,EACbQ,OAAgC;IAEhC,MAAM,EAAEJ,SAAS,EAAE,GAAGR;IACtB,OAAON,WAAW;QAChBmB,SAASD,QAAQE,GAAG,CAAC,CAACC;YACpB,OAAO;gBACLN,UAAU;oBAAC;oBAAaL;oBAAYW,EAAEV,IAAI;iBAAC;gBAC3CK,SAAS,IAAMF,UAAUJ,YAAYW,EAAEV,IAAI;YAC7C;QACF;IACF;AACF;AAQA;;CAEC,GACD,OAAO,SAASW,sBACdC,WAAqB,EACrBX,OAAsC;IAEtC,MAAM,EAAEY,kBAAkB,EAAE,GAAGlB;IAC/B,OAAOL,SAAS;QACdc,UAAU;YAAC;YAAsBQ;SAAY;QAC7CP,SAAS,IAAMQ,mBAAmBD;QAClC,GAAGX,OAAO;IACZ;AACF;AAEA,OAAO,SAASa;IACd,MAAM,EAAEX,SAAS,EAAEU,kBAAkB,EAAE,GAAGlB;IAE1C,OAAOL,SAAS;QACdc,UAAU;YAAC;SAAsC;QACjDC,SAAS;YACP,MAAMU,cAAc,MAAMF,mBAAmB;gBAAC;aAAa;YAC3D,MAAMG,kBAAkB,IAAIC,IAAIF,YAAYN,GAAG,CAAC,CAACS,aAAeA,WAAWC,IAAI,CAACC,IAAI;YACpF,MAAMC,SAAsC,EAAE;YAC9C,KAAK,MAAMD,QAAQJ,gBAAiB;gBAClC,MAAMM,SAAS,MAAMnB,UAAU,cAAciB;gBAC7C,IAAIE,OAAOC,6BAA6B,EAAE;oBACxCD,OAAOC,6BAA6B,GAAGC,OAAO,CAAC,CAACC,aAAeJ,OAAOK,IAAI,CAACD;gBAC7E;YACF;YACA,OAAOJ;QACT;IACF;AACF"}
1
+ {"version":3,"sources":["../../src/runtime/plugin-registry.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 { BuiltinVariableDefinition } from '@perses-dev/core';\nimport { useQueries, useQuery, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';\nimport { createContext, useContext } from 'react';\nimport { DefaultPluginKinds, PluginImplementation, PluginMetadataWithModule, PluginType } from '../model';\n\nexport interface PluginRegistryContextType {\n getPlugin<T extends PluginType>(kind: T, name: string): Promise<PluginImplementation<T>>;\n listPluginMetadata(pluginTypes: PluginType[]): Promise<PluginMetadataWithModule[]>;\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(): PluginRegistryContextType {\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 | undefined, 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>(\n pluginType: T | undefined,\n kind: string,\n options?: UsePluginOptions<T>\n): UseQueryResult<PluginImplementation<T>, Error> {\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) && pluginType !== undefined && kind !== '',\n };\n const { getPlugin } = usePluginRegistry();\n return useQuery({\n queryKey: ['getPlugin', pluginType, kind],\n queryFn: () => getPlugin(pluginType!, kind),\n ...options,\n });\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>(\n pluginType: T,\n plugins: Array<{ kind: string }>\n): Array<UseQueryResult<PluginImplementation<T>>> {\n const { getPlugin } = usePluginRegistry();\n\n // useQueries() does not support queries with duplicate keys, therefore we de-duplicate the plugin kinds before running useQueries()\n // This resolves the following warning in the JS console: \"[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.\"\n // https://github.com/TanStack/query/issues/8224#issuecomment-2523554831\n // https://github.com/TanStack/query/issues/4187#issuecomment-1256336901\n const kinds = [...new Set(plugins.map((p) => p.kind))];\n\n const result: Array<UseQueryResult<PluginImplementation<T>>> = useQueries({\n queries: kinds.map((kind) => {\n return {\n queryKey: ['getPlugin', pluginType, kind],\n queryFn: () => getPlugin(pluginType, kind),\n };\n }),\n });\n\n // Re-assemble array in original order\n return plugins.map((p) => result[kinds.indexOf(p.kind)]!);\n}\n\n// Allow consumers to pass useQuery options from react-query when listing metadata\ntype UseListPluginMetadataOptions = Omit<\n UseQueryOptions<PluginMetadataWithModule[], Error, PluginMetadataWithModule[], [string, string[]]>,\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(\n pluginTypes: PluginType[],\n options?: UseListPluginMetadataOptions\n): UseQueryResult<PluginMetadataWithModule[]> {\n const { listPluginMetadata } = usePluginRegistry();\n return useQuery({\n queryKey: ['listPluginMetadata', pluginTypes],\n queryFn: () => listPluginMetadata(pluginTypes),\n ...options,\n });\n}\n\nexport function usePluginBuiltinVariableDefinitions(): UseQueryResult<BuiltinVariableDefinition[]> {\n const { getPlugin, listPluginMetadata } = usePluginRegistry();\n\n return useQuery({\n queryKey: ['usePluginBuiltinVariableDefinitions'],\n queryFn: async () => {\n const datasources = await listPluginMetadata(['Datasource']);\n const datasourceNames = new Set(datasources.map((datasource) => datasource.spec.name));\n const result: BuiltinVariableDefinition[] = [];\n for (const name of datasourceNames) {\n const plugin = await getPlugin('Datasource', name);\n if (plugin.getBuiltinVariableDefinitions) {\n plugin.getBuiltinVariableDefinitions().forEach((definition) => result.push(definition));\n }\n }\n return result;\n },\n });\n}\n"],"names":["useQueries","useQuery","createContext","useContext","PluginRegistryContext","undefined","usePluginRegistry","ctx","Error","usePlugin","pluginType","kind","options","enabled","getPlugin","queryKey","queryFn","usePlugins","plugins","kinds","Set","map","p","result","queries","indexOf","useListPluginMetadata","pluginTypes","listPluginMetadata","usePluginBuiltinVariableDefinitions","datasources","datasourceNames","datasource","spec","name","plugin","getBuiltinVariableDefinitions","forEach","definition","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;AAGjC,SAASA,UAAU,EAAEC,QAAQ,QAAyC,wBAAwB;AAC9F,SAASC,aAAa,EAAEC,UAAU,QAAQ,QAAQ;AASlD,OAAO,MAAMC,wBAAwBF,cAAqDG,WAAW;AAErG;;;CAGC,GACD,OAAO,SAASC;IACd,MAAMC,MAAMJ,WAAWC;IACvB,IAAIG,QAAQF,WAAW;QACrB,MAAM,IAAIG,MAAM;IAClB;IACA,OAAOD;AACT;AAQA;;CAEC,GACD,OAAO,SAASE,UACdC,UAAyB,EACzBC,IAAY,EACZC,OAA6B;IAE7B,wGAAwG;IACxGA,UAAU;QACR,GAAGA,OAAO;QACVC,SAAS,AAACD,CAAAA,SAASC,WAAW,IAAG,KAAMH,eAAeL,aAAaM,SAAS;IAC9E;IACA,MAAM,EAAEG,SAAS,EAAE,GAAGR;IACtB,OAAOL,SAAS;QACdc,UAAU;YAAC;YAAaL;YAAYC;SAAK;QACzCK,SAAS,IAAMF,UAAUJ,YAAaC;QACtC,GAAGC,OAAO;IACZ;AACF;AAEA;;CAEC,GACD,OAAO,SAASK,WACdP,UAAa,EACbQ,OAAgC;IAEhC,MAA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
@@ -1 +1 @@
1
- {"version":3,"file":"time-series-queries.d.ts","sourceRoot":"","sources":["../../src/runtime/time-series-queries.ts"],"names":[],"mappings":"AAaA,OAAO,EAKL,UAAU,EAEV,oBAAoB,EACpB,cAAc,EACf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,yBAAyB,EAAe,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAA0B,mBAAmB,EAAyB,MAAM,UAAU,CAAC;AAMnH,MAAM,WAAW,yBAAyB;IACxC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,IAAI,CAAC,EAAE,mBAAmB,CAAC;CAC5B;AAED,eAAO,MAAM,qBAAqB,oBAAoB,CAAC;AA8DvD;;GAEG;AACH,eAAO,MAAM,kBAAkB,eACjB,yBAAyB,YAC3B,yBAAyB,iBACpB,qBAAqB,cAAc,CAAC,KAClD,eAAe,cAAc,CAkB/B,CAAC;AAEF;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,yBAAyB,EAAE,EACxC,OAAO,CAAC,EAAE,yBAAyB,EACnC,YAAY,CAAC,EAAE,IAAI,CAAC,oBAAoB,EAAE,UAAU,CAAC,GACpD,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAiCvC;AAkBD;;GAEG;AACH,wBAAgB,0BAA0B,IAAI,mBAAmB,EAAE,CAIlE;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,UAAU,GAAG,mBAAmB,EAAE,CAWnF"}
1
+ {"version":3,"file":"time-series-queries.d.ts","sourceRoot":"","sources":["../../src/runtime/time-series-queries.ts"],"names":[],"mappings":"AAaA,OAAO,EAKL,UAAU,EAEV,oBAAoB,EACpB,cAAc,EACf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,yBAAyB,EAAe,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAA0B,mBAAmB,EAAyB,MAAM,UAAU,CAAC;AAOnH,MAAM,WAAW,yBAAyB;IACxC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,IAAI,CAAC,EAAE,mBAAmB,CAAC;CAC5B;AAED,eAAO,MAAM,qBAAqB,oBAAoB,CAAC;AA8CvD;;GAEG;AACH,eAAO,MAAM,kBAAkB,eACjB,yBAAyB,YAC3B,yBAAyB,iBACpB,qBAAqB,cAAc,CAAC,KAClD,eAAe,cAAc,CAkB/B,CAAC;AAEF;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,yBAAyB,EAAE,EACxC,OAAO,CAAC,EAAE,yBAAyB,EACnC,YAAY,CAAC,EAAE,IAAI,CAAC,oBAAoB,EAAE,UAAU,CAAC,GACpD,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAiCvC;AAkBD;;GAEG;AACH,wBAAgB,0BAA0B,IAAI,mBAAmB,EAAE,CAIlE;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,UAAU,GAAG,mBAAmB,EAAE,CAWnF"}
@@ -15,18 +15,8 @@ import { useAllVariableValues } from './variables';
15
15
  import { useTimeRange } from './TimeRangeProvider';
16
16
  import { useDatasourceStore } from './datasources';
17
17
  import { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';
18
+ import { filterVariableStateMap, getVariableValuesKey } from './utils';
18
19
  export const TIME_SERIES_QUERY_KEY = 'TimeSeriesQuery';
19
- /**
20
- * Returns a serialized string of the current state of variable values.
21
- */ function getVariableValuesKey(v) {
22
- return Object.values(v).map((v)=>JSON.stringify(v.value)).join(',');
23
- }
24
- function filterVariableStateMap(v, names) {
25
- if (!names) {
26
- return v;
27
- }
28
- return Object.fromEntries(Object.entries(v).filter(([name])=>names.includes(name)));
29
- }
30
20
  function getQueryOptions({ plugin, definition, context }) {
31
21
  const { timeRange, datasourceStore, suggestedStepMs, mode, variableState, refreshKey } = context;
32
22
  const dependencies = plugin?.dependsOn ? plugin.dependsOn(definition.spec.plugin.spec, context) : {};
@@ -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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ACzE,OAAON;gBACT;YACF;QACF;IACF;AACF;AAEA;;CAEC,GACD,SAASE;IACP,MAAM,EAAEa,iBAAiB,EAAEhC,UAAU,EAAE,GAAG/B;IAC1C,MAAM8B,gBAAgB/B;IACtB,MAAM4B,kBAAkB1B;IAExB,OAAO;QACLyB,WAAWqC;QACXjC;QACAH;QACAI;IACF;AACF;AAEA;;CAEC,GACD,OAAO,SAASiC;IACd,MAAMC,cAAcnE;IACpB,MAAMoE,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,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,SAAS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,WAAWU,IAAI,CAACX,MAAM,CAACW,IAAI,EAAEqB;oBACzE,OAAON;gBACT;YACF;QACF;IACF;AACF;AAEA;;CAEC,GACD,SAASE;IACP,MAAM,EAAEc,iBAAiB,EAAElC,UAAU,EAAE,GAAGjB;IAC1C,MAAMgB,gBAAgBjB;IACtB,MAAMc,kBAAkBZ;IAExB,OAAO;QACLW,WAAWuC;QACXnC;QACAH;QACAI;IACF;AACF;AAEA;;CAEC,GACD,OAAO,SAASmC;IACd,MAAMC,cAAcvD;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;AAC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AS,MAAMR,UAAUT,iBAAiByB;oBAChD,MAAMJ,OAAO,MAAMJ,OAAOW,YAAY,CAACT,WAAWH,IAAI,CAACC,MAAM,CAACD,IAAI,EAAEN;oBACpE,OAAOW;gBACT;YACF;QACF;IACF;AACF;AAEA,SAASG,gBAAgB,EACvBP,MAAM,EACNE,UAAU,EACVT,OAAO,EAKR;IAIC,MAAM,EAAEmB,eAAe,EAAEC,aAAa,EAAEC,iBAAiB,EAAE,GAAGrB;IAE9D,MAAMsB,eAAef,QAAQgB,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"}
@@ -10,6 +10,16 @@
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
- export * from './decorators';
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
+ }
14
24
 
15
- //# sourceMappingURL=index.js.map
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"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/test-utils/mock-plugin-registry.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 { UnknownSpec } from '@perses-dev/core';\nimport { PluginRegistryProps } from '../components';\nimport { PluginModuleResource, Plugin, PluginLoader, PluginImplementation, PluginType } from '../model';\n\nexport type MockPlugin = {\n [T in PluginType]: {\n kind: T;\n spec: { name: string };\n plugin: PluginImplementation<T>;\n };\n}[PluginType];\n\n/**\n * Helper for mocking `PluginRegistry` data during tests. Returns props that can be spread on the `PluginRegistry`\n * component so that it will load the mock plugins you provide.\n */\nexport function mockPluginRegistry(...mockPlugins: MockPlugin[]): Omit<PluginRegistryProps, 'children'> {\n const mockPluginResource: PluginModuleResource = {\n kind: 'PluginModule',\n metadata: {\n name: 'Fake Plugin Module for Tests',\n version: '0',\n },\n spec: {\n // Add metadata for all mock plugins\n plugins: mockPlugins.map(({ kind, spec: { name } }) => ({\n kind,\n spec: {\n name,\n display: {\n name: getMockPluginName(kind, name),\n },\n },\n })),\n },\n };\n\n const mockPluginModule: Record<string, Plugin<UnknownSpec>> = {};\n for (const mockPlugin of mockPlugins) {\n // \"Export\" on the module under the same name as the kind the plugin handles\n mockPluginModule[mockPlugin.spec.name] = mockPlugin.plugin;\n }\n\n const pluginLoader: PluginLoader = {\n getInstalledPlugins() {\n return Promise.resolve([mockPluginResource]);\n },\n importPluginModule(/* resource */) {\n return Promise.resolve(mockPluginModule);\n },\n };\n\n return {\n pluginLoader,\n defaultPluginKinds: {\n TimeSeriesQuery: 'PrometheusTimeSeriesQuery',\n },\n };\n}\n\n/**\n * The function that's used to generate the display name of mocked plugins in mockPluginRegistry. Can be useful if you\n * need to interact with some UI component that's displaying it.\n */\nexport function getMockPluginName(kind: PluginType, name: string): string {\n return `${kind} Plugin for ${name}`;\n}\n"],"names":["mockPluginRegistry","mockPlugins","mockPluginResource","kind","metadata","name","version","spec","plugins","map","display","getMockPluginName","mockPluginModule","mockPlugin","plugin","pluginLoader","getInstalledPlugins","Promise","resolve","importPluginModule","defaultPluginKinds","TimeSeriesQuery"],"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;AAcjC;;;CAGC,GACD,OAAO,SAASA,mBAAmB,GAAGC,WAAyB;IAC7D,MAAMC,qBAA2C;QAC/CC,MAAM;QACNC,UAAU;YACRC,MAAM;YACNC,SAAS;QACX;QACAC,MAAM;YACJ,oCAAoC;YACpCC,SAASP,YAAYQ,GAAG,CAAC,CAAC,EAAEN,IAAI,EAAEI,MAAM,EAAEF,IAAI,EAAE,EAAE,GAAM,CAAA;oBACtDF;oBACAI,MAAM;wBACJF;wBACAK,SAAS;4BACPL,MAAMM,kBAAkBR,MAAME;wBAChC;oBACF;gBACF,CAAA;QACF;IACF;IAEA,MAAMO,mBAAwD,CAAC;IAC/D,KAAK,MAAMC,cAAcZ,YAAa;QACpC,4EAA4E;QAC5EW,gBAAgB,CAACC,WAAWN,IAAI,CAACF,IAAI,CAAC,GAAGQ,WAAWC,MAAM;IAC5D;IAEA,MAAMC,eAA6B;QACjCC;YACE,OAAOC,QAAQC,OAAO,CAAC;gBAAChB;aAAmB;QAC7C;QACAiB;YACE,OAAOF,QAAQC,OAAO,CAACN;QACzB;IACF;IAEA,OAAO;QACLG;QACAK,oBAAoB;YAClBC,iBAAiB;QACnB;IACF;AACF;AAEA;;;CAGC,GACD,OAAO,SAASV,kBAAkBR,IAAgB,EAAEE,IAAY;IAC9D,OAAO,CAAC,EAAEF,KAAK,YAAY,EAAEE,KAAK,CAAC;AACrC"}
1
+ {"version":3,"sources":["../../src/test-utils/mock-plugin-registry.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 { UnknownSpec } from '@perses-dev/core';\nimport { PluginRegistryProps } from '../components';\nimport { PluginModuleResource, Plugin, PluginLoader, PluginImplementation, PluginType } from '../model';\n\nexport type MockPlugin = {\n [T in PluginType]: {\n kind: T;\n spec: { name: string };\n plugin: PluginImplementation<T>;\n };\n}[PluginType];\n\n/**\n * Helper for mocking `PluginRegistry` data during tests. Returns props that can be spread on the `PluginRegistry`\n * component so that it will load the mock plugins you provide.\n */\nexport function mockPluginRegistry(...mockPlugins: MockPlugin[]): Omit<PluginRegistryProps, 'children'> {\n const mockPluginResource: PluginModuleResource = {\n kind: 'PluginModule',\n metadata: {\n name: 'Fake Plugin Module for Tests',\n version: '0',\n },\n spec: {\n // Add metadata for all mock plugins\n plugins: mockPlugins.map(({ kind, spec: { name } }) => ({\n kind,\n spec: {\n name,\n display: {\n name: getMockPluginName(kind, name),\n },\n },\n })),\n },\n };\n\n const mockPluginModule: Record<string, Plugin<UnknownSpec>> = {};\n for (const mockPlugin of mockPlugins) {\n // \"Export\" on the module under the same name as the kind the plugin handles\n mockPluginModule[mockPlugin.spec.name] = mockPlugin.plugin;\n }\n\n const pluginLoader: PluginLoader = {\n getInstalledPlugins() {\n return Promise.resolve([mockPluginResource]);\n },\n importPluginModule(/* resource */) {\n return Promise.resolve(mockPluginModule);\n },\n };\n\n return {\n pluginLoader,\n defaultPluginKinds: {\n TimeSeriesQuery: 'PrometheusTimeSeriesQuery',\n },\n };\n}\n\n/**\n * The function that's used to generate the display name of mocked plugins in mockPluginRegistry. Can be useful if you\n * need to interact with some UI component that's displaying it.\n */\nexport function getMockPluginName(kind: PluginType, name: string): string {\n return `${kind} Plugin for ${name}`;\n}\n"],"names":["mockPluginRegistry","mockPlugins","mockPluginResource","kind","metadata","name","version","spec","plugins","map","display","getMockPluginName","mockPluginModule","mockPlugin","plugin","pluginLoader","getInstalledPlugins","Promise","resolve","importPluginModule","defaultPluginKinds","TimeSeriesQuery"],"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;AAcjC;;;CAGC,GACD,OAAO,SAASA,mBAAmB,GAAGC,WAAyB;IAC7D,MAAMC,qBAA2C;QAC/CC,MAAM;QACNC,UAAU;YACRC,MAAM;YACNC,SAAS;QACX;QACAC,MAAM;YACJ,oCAAoC;YACpCC,SAASP,YAAYQ,GAAG,CAAC,CAAC,EAAEN,IAAI,EAAEI,MAAM,EAAEF,IAAI,EAAE,EAAE,GAAM,CAAA;oBACtDF;oBACAI,MAAM;wBACJF;wBACAK,SAAS;4BACPL,MAAMM,kBAAkBR,MAAME;wBAChC;oBACF;gBACF,CAAA;QACF;IACF;IAEA,MAAMO,mBAAwD,CAAC;IAC/D,KAAK,MAAMC,cAAcZ,YAAa;QACpC,4EAA4E;QAC5EW,gBAAgB,CAACC,WAAWN,IAAI,CAACF,IAAI,CAAC,GAAGQ,WAAWC,MAAM;IAC5D;IAEA,MAAMC,eAA6B;QACjCC;YACE,OAAOC,QAAQC,OAAO,CAAC;gBAAChB;aAAmB;QAC7C;QACAiB;YACE,OAAOF,QAAQC,OAAO,CAACN;QACzB;IACF;IAEA,OAAO;QACLG;QACAK,oBAAoB;YAClBC,iBAAiB;QACnB;IACF;AACF;AAEA;;;CAGC,GACD,OAAO,SAASV,kBAAkBR,IAAgB,EAAEE,IAAY;IAC9D,OAAO,GAAGF,KAAK,YAAY,EAAEE,MAAM;AACrC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@perses-dev/plugin-system",
3
- "version": "0.51.0-beta.0",
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,18 +29,16 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@module-federation/enhanced": "^0.8.9",
32
- "@perses-dev/components": "0.51.0-beta.0",
33
- "@perses-dev/core": "0.51.0-beta.0",
34
- "date-fns": "^2.30.0",
35
- "immer": "^9.0.15",
32
+ "@perses-dev/components": "0.51.0-rc.0",
33
+ "@perses-dev/core": "0.51.0-rc.0",
34
+ "date-fns": "^4.1.0",
35
+ "date-fns-tz": "^3.2.0",
36
+ "immer": "^10.1.1",
36
37
  "react-hook-form": "^7.46.1",
37
- "use-immer": "^0.7.0",
38
+ "use-immer": "^0.11.0",
38
39
  "use-query-params": "^2.1.2",
39
40
  "zod": "^3.22.2"
40
41
  },
41
- "devDependencies": {
42
- "@perses-dev/storybook": "0.51.0-beta.0"
43
- },
44
42
  "peerDependencies": {
45
43
  "@mui/material": "^6.1.10",
46
44
  "@tanstack/react-query": "^5.64.2",
@@ -1,41 +0,0 @@
1
- // Copyright 2023 The Perses Authors
2
- // Licensed under the Apache License, Version 2.0 (the "License");
3
- // you may not use this file except in compliance with the License.
4
- // You may obtain a copy of the License at
5
- //
6
- // http://www.apache.org/licenses/LICENSE-2.0
7
- //
8
- // Unless required by applicable law or agreed to in writing, software
9
- // distributed under the License is distributed on an "AS IS" BASIS,
10
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- // See the License for the specific language governing permissions and
12
- // limitations under the License.
13
- "use strict";
14
- Object.defineProperty(exports, "__esModule", {
15
- value: true
16
- });
17
- Object.defineProperty(exports, "WithDataQueries", {
18
- enumerable: true,
19
- get: function() {
20
- return WithDataQueries;
21
- }
22
- });
23
- const _jsxruntime = require("react/jsx-runtime");
24
- const _pluginsystem = require("@perses-dev/plugin-system");
25
- // Type guard because storybook types parameters as `any`
26
- function isWithDataQueriesParameter(parameter) {
27
- return !!parameter && typeof parameter === 'object' && 'props' in parameter;
28
- }
29
- const WithDataQueries = (Story, context)=>{
30
- const initParameter = context.parameters.withDataQueries;
31
- const parameter = isWithDataQueriesParameter(initParameter) ? initParameter : {
32
- props: {
33
- definitions: []
34
- }
35
- };
36
- const props = parameter?.props;
37
- return /*#__PURE__*/ (0, _jsxruntime.jsx)(_pluginsystem.DataQueriesProvider, {
38
- ...props,
39
- children: /*#__PURE__*/ (0, _jsxruntime.jsx)(Story, {})
40
- });
41
- };
@@ -1,34 +0,0 @@
1
- // Copyright 2023 The Perses Authors
2
- // Licensed under the Apache License, Version 2.0 (the "License");
3
- // you may not use this file except in compliance with the License.
4
- // You may obtain a copy of the License at
5
- //
6
- // http://www.apache.org/licenses/LICENSE-2.0
7
- //
8
- // Unless required by applicable law or agreed to in writing, software
9
- // distributed under the License is distributed on an "AS IS" BASIS,
10
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- // See the License for the specific language governing permissions and
12
- // limitations under the License.
13
- "use strict";
14
- Object.defineProperty(exports, "__esModule", {
15
- value: true
16
- });
17
- Object.defineProperty(exports, "WithPluginRegistry", {
18
- enumerable: true,
19
- get: function() {
20
- return WithPluginRegistry;
21
- }
22
- });
23
- const _jsxruntime = require("react/jsx-runtime");
24
- const _pluginsystem = require("@perses-dev/plugin-system");
25
- const WithPluginRegistry = (Story)=>{
26
- return /*#__PURE__*/ (0, _jsxruntime.jsx)(_pluginsystem.PluginRegistry, {
27
- pluginLoader: (0, _pluginsystem.remotePluginLoader)(),
28
- defaultPluginKinds: {
29
- TimeSeriesQuery: 'PrometheusTimeSeriesQuery',
30
- TraceQuery: 'TempoTraceQuery'
31
- },
32
- children: /*#__PURE__*/ (0, _jsxruntime.jsx)(Story, {})
33
- });
34
- };
@@ -1,42 +0,0 @@
1
- // Copyright 2023 The Perses Authors
2
- // Licensed under the Apache License, Version 2.0 (the "License");
3
- // you may not use this file except in compliance with the License.
4
- // You may obtain a copy of the License at
5
- //
6
- // http://www.apache.org/licenses/LICENSE-2.0
7
- //
8
- // Unless required by applicable law or agreed to in writing, software
9
- // distributed under the License is distributed on an "AS IS" BASIS,
10
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- // See the License for the specific language governing permissions and
12
- // limitations under the License.
13
- "use strict";
14
- Object.defineProperty(exports, "__esModule", {
15
- value: true
16
- });
17
- Object.defineProperty(exports, "WithPluginSystemBuiltinVariables", {
18
- enumerable: true,
19
- get: function() {
20
- return WithPluginSystemBuiltinVariables;
21
- }
22
- });
23
- const _jsxruntime = require("react/jsx-runtime");
24
- const _runtime = require("../../../runtime");
25
- // Type guard because storybook types parameters as `any`
26
- function isWithBuiltinVariableParameter(parameter) {
27
- return !!parameter && Array.isArray(parameter);
28
- }
29
- const WithPluginSystemBuiltinVariables = (Story, context)=>{
30
- const initParameter = context.parameters.withPluginSystemBuiltinVariables;
31
- const defaultValue = {
32
- variables: []
33
- };
34
- const parameter = isWithBuiltinVariableParameter(initParameter) ? initParameter : {
35
- props: defaultValue
36
- };
37
- const props = parameter?.props;
38
- return /*#__PURE__*/ (0, _jsxruntime.jsx)(_runtime.BuiltinVariableContext.Provider, {
39
- value: props,
40
- children: /*#__PURE__*/ (0, _jsxruntime.jsx)(Story, {})
41
- });
42
- };