@perses-dev/plugin-system 0.38.0 → 0.40.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 (274) hide show
  1. package/dist/cjs/components/CalculationSelector/CalculationSelector.js +6 -4
  2. package/dist/cjs/components/CalculationSelector/index.js +10 -8
  3. package/dist/cjs/components/DatasourceEditorForm/DatasourceEditorForm.js +380 -0
  4. package/dist/cjs/components/DatasourceEditorForm/index.js +30 -0
  5. package/dist/cjs/components/DatasourceSelect.js +119 -21
  6. package/dist/cjs/components/LegendOptionsEditor/LegendOptionsEditor.js +18 -16
  7. package/dist/cjs/components/LegendOptionsEditor/index.js +10 -8
  8. package/dist/cjs/components/OptionsEditorRadios/OptionsEditorRadios.js +69 -0
  9. package/dist/cjs/components/OptionsEditorRadios/index.js +30 -0
  10. package/dist/cjs/components/{OptionsEditorTabs/TabPanel.js → OptionsEditorTabPanel/OptionsEditorTabPanel.js} +8 -6
  11. package/dist/cjs/components/OptionsEditorTabPanel/index.js +30 -0
  12. package/dist/cjs/components/OptionsEditorTabs/OptionsEditorTabs.js +10 -8
  13. package/dist/cjs/components/OptionsEditorTabs/index.js +10 -8
  14. package/dist/cjs/components/PanelSpecEditor/PanelSpecEditor.js +13 -11
  15. package/dist/cjs/components/PanelSpecEditor/index.js +10 -8
  16. package/dist/cjs/components/PluginEditor/PluginEditor.js +24 -31
  17. package/dist/cjs/components/PluginEditor/index.js +11 -9
  18. package/dist/cjs/components/PluginEditor/plugin-editor-api.js +5 -3
  19. package/dist/cjs/components/PluginKindSelect/PluginKindSelect.js +23 -12
  20. package/dist/cjs/components/PluginKindSelect/index.js +10 -8
  21. package/dist/cjs/components/PluginRegistry/PluginRegistry.js +11 -9
  22. package/dist/cjs/components/PluginRegistry/index.js +10 -8
  23. package/dist/cjs/components/PluginRegistry/plugin-indexes.js +6 -2
  24. package/dist/cjs/components/PluginSpecEditor/PluginSpecEditor.js +6 -4
  25. package/dist/cjs/components/PluginSpecEditor/index.js +10 -8
  26. package/dist/cjs/components/TimeSeriesQueryEditor/TimeSeriesQueryEditor.js +15 -13
  27. package/dist/cjs/components/TimeSeriesQueryEditor/TimeSeriesQueryInput.js +21 -19
  28. package/dist/cjs/components/TimeSeriesQueryEditor/index.js +10 -8
  29. package/dist/cjs/components/Variables/VariableEditorForm/VariableEditorForm.js +256 -166
  30. package/dist/cjs/components/Variables/VariableEditorForm/VariablePreview.js +32 -28
  31. package/dist/cjs/components/Variables/VariableEditorForm/index.js +11 -9
  32. package/dist/cjs/components/Variables/VariableEditorForm/variable-editor-form-model.js +28 -32
  33. package/dist/cjs/components/Variables/index.js +11 -9
  34. package/dist/cjs/components/Variables/variable-model.js +17 -9
  35. package/dist/cjs/components/index.js +22 -18
  36. package/dist/cjs/constants/index.js +10 -8
  37. package/dist/cjs/constants/user-interface-text.js +3 -1
  38. package/dist/cjs/index.js +16 -13
  39. package/dist/cjs/model/index.js +17 -15
  40. package/dist/cjs/model/legend.js +32 -20
  41. package/dist/cjs/model/panels.js +2 -2
  42. package/dist/cjs/model/plugin-base.js +2 -2
  43. package/dist/cjs/model/plugin-loading.js +3 -1
  44. package/dist/cjs/runtime/DataQueriesProvider/DataQueriesProvider.js +55 -35
  45. package/dist/cjs/runtime/DataQueriesProvider/index.js +11 -9
  46. package/dist/cjs/runtime/DataQueriesProvider/model.js +61 -0
  47. package/dist/cjs/runtime/TimeRangeProvider/TimeRangeProvider.js +19 -11
  48. package/dist/cjs/runtime/TimeRangeProvider/index.js +11 -9
  49. package/dist/cjs/runtime/TimeRangeProvider/query-params.js +36 -18
  50. package/dist/cjs/runtime/builtin-variables.js +81 -0
  51. package/dist/cjs/runtime/datasources.js +29 -11
  52. package/dist/cjs/runtime/index.js +16 -13
  53. package/dist/cjs/runtime/plugin-registry.js +41 -11
  54. package/dist/cjs/runtime/template-variables.js +96 -4
  55. package/dist/cjs/runtime/time-series-queries.js +39 -27
  56. package/dist/cjs/stories/shared-utils/decorators/WithDataQueries.js +7 -5
  57. package/dist/cjs/stories/shared-utils/decorators/WithPluginRegistry.js +16 -14
  58. package/dist/cjs/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.js +42 -0
  59. package/dist/cjs/stories/shared-utils/decorators/WithPluginSystemDatasourceStore.js +19 -13
  60. package/dist/cjs/stories/shared-utils/decorators/WithPluginSystemTemplateVariables.js +6 -4
  61. package/dist/cjs/stories/shared-utils/decorators/WithTimeRange.js +7 -5
  62. package/dist/cjs/stories/shared-utils/decorators/index.js +15 -12
  63. package/dist/cjs/stories/shared-utils/index.js +10 -8
  64. package/dist/cjs/test/index.js +11 -8
  65. package/dist/cjs/test/mock-data.js +57 -0
  66. package/dist/cjs/test/render.js +13 -11
  67. package/dist/cjs/test/test-plugins/bert/index.js +14 -10
  68. package/dist/cjs/test/test-plugins/ernie/index.js +13 -9
  69. package/dist/cjs/test/test-plugins/index.js +11 -9
  70. package/dist/cjs/test-utils/index.js +10 -8
  71. package/dist/cjs/test-utils/mock-plugin-registry.js +8 -4
  72. package/dist/cjs/utils/action.js +43 -0
  73. package/dist/cjs/utils/index.js +11 -8
  74. package/dist/cjs/utils/variables.js +26 -8
  75. package/dist/cjs/validation/datasource.js +30 -0
  76. package/dist/cjs/validation/index.js +32 -0
  77. package/dist/cjs/validation/resource.js +24 -0
  78. package/dist/cjs/validation/variable.js +29 -0
  79. package/dist/components/CalculationSelector/CalculationSelector.js.map +1 -1
  80. package/dist/components/CalculationSelector/index.js.map +1 -1
  81. package/dist/components/DatasourceEditorForm/DatasourceEditorForm.d.ts +15 -0
  82. package/dist/components/DatasourceEditorForm/DatasourceEditorForm.d.ts.map +1 -0
  83. package/dist/components/DatasourceEditorForm/DatasourceEditorForm.js +333 -0
  84. package/dist/components/DatasourceEditorForm/DatasourceEditorForm.js.map +1 -0
  85. package/dist/components/DatasourceEditorForm/index.d.ts +2 -0
  86. package/dist/components/DatasourceEditorForm/index.d.ts.map +1 -0
  87. package/dist/components/DatasourceEditorForm/index.js +15 -0
  88. package/dist/components/DatasourceEditorForm/index.js.map +1 -0
  89. package/dist/components/DatasourceSelect.d.ts +5 -0
  90. package/dist/components/DatasourceSelect.d.ts.map +1 -1
  91. package/dist/components/DatasourceSelect.js +102 -19
  92. package/dist/components/DatasourceSelect.js.map +1 -1
  93. package/dist/components/LegendOptionsEditor/LegendOptionsEditor.js +2 -2
  94. package/dist/components/LegendOptionsEditor/LegendOptionsEditor.js.map +1 -1
  95. package/dist/components/LegendOptionsEditor/index.js.map +1 -1
  96. package/dist/components/OptionsEditorRadios/OptionsEditorRadios.d.ts +16 -0
  97. package/dist/components/OptionsEditorRadios/OptionsEditorRadios.d.ts.map +1 -0
  98. package/dist/components/OptionsEditorRadios/OptionsEditorRadios.js +61 -0
  99. package/dist/components/OptionsEditorRadios/OptionsEditorRadios.js.map +1 -0
  100. package/dist/components/OptionsEditorRadios/index.d.ts +2 -0
  101. package/dist/components/OptionsEditorRadios/index.d.ts.map +1 -0
  102. package/dist/components/OptionsEditorRadios/index.js +15 -0
  103. package/dist/components/OptionsEditorRadios/index.js.map +1 -0
  104. package/dist/components/OptionsEditorTabPanel/OptionsEditorTabPanel.d.ts +9 -0
  105. package/dist/components/OptionsEditorTabPanel/OptionsEditorTabPanel.d.ts.map +1 -0
  106. package/dist/components/{OptionsEditorTabs/TabPanel.js → OptionsEditorTabPanel/OptionsEditorTabPanel.js} +2 -2
  107. package/dist/components/OptionsEditorTabPanel/OptionsEditorTabPanel.js.map +1 -0
  108. package/dist/components/OptionsEditorTabPanel/index.d.ts +2 -0
  109. package/dist/components/OptionsEditorTabPanel/index.d.ts.map +1 -0
  110. package/dist/components/OptionsEditorTabPanel/index.js +15 -0
  111. package/dist/components/OptionsEditorTabPanel/index.js.map +1 -0
  112. package/dist/components/OptionsEditorTabs/OptionsEditorTabs.js +2 -2
  113. package/dist/components/OptionsEditorTabs/OptionsEditorTabs.js.map +1 -1
  114. package/dist/components/OptionsEditorTabs/index.js.map +1 -1
  115. package/dist/components/PanelSpecEditor/PanelSpecEditor.js +2 -2
  116. package/dist/components/PanelSpecEditor/PanelSpecEditor.js.map +1 -1
  117. package/dist/components/PanelSpecEditor/index.js.map +1 -1
  118. package/dist/components/PluginEditor/PluginEditor.js +15 -24
  119. package/dist/components/PluginEditor/PluginEditor.js.map +1 -1
  120. package/dist/components/PluginEditor/index.js.map +1 -1
  121. package/dist/components/PluginEditor/plugin-editor-api.d.ts +1 -1
  122. package/dist/components/PluginEditor/plugin-editor-api.js +2 -2
  123. package/dist/components/PluginEditor/plugin-editor-api.js.map +1 -1
  124. package/dist/components/PluginKindSelect/PluginKindSelect.d.ts +3 -3
  125. package/dist/components/PluginKindSelect/PluginKindSelect.d.ts.map +1 -1
  126. package/dist/components/PluginKindSelect/PluginKindSelect.js +21 -12
  127. package/dist/components/PluginKindSelect/PluginKindSelect.js.map +1 -1
  128. package/dist/components/PluginKindSelect/index.js.map +1 -1
  129. package/dist/components/PluginRegistry/PluginRegistry.js +3 -3
  130. package/dist/components/PluginRegistry/PluginRegistry.js.map +1 -1
  131. package/dist/components/PluginRegistry/index.js.map +1 -1
  132. package/dist/components/PluginRegistry/plugin-indexes.js.map +1 -1
  133. package/dist/components/PluginSpecEditor/PluginSpecEditor.js.map +1 -1
  134. package/dist/components/PluginSpecEditor/index.js.map +1 -1
  135. package/dist/components/TimeSeriesQueryEditor/TimeSeriesQueryEditor.js +4 -4
  136. package/dist/components/TimeSeriesQueryEditor/TimeSeriesQueryEditor.js.map +1 -1
  137. package/dist/components/TimeSeriesQueryEditor/TimeSeriesQueryInput.js +1 -1
  138. package/dist/components/TimeSeriesQueryEditor/TimeSeriesQueryInput.js.map +1 -1
  139. package/dist/components/TimeSeriesQueryEditor/index.js.map +1 -1
  140. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts +10 -7
  141. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts.map +1 -1
  142. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js +200 -112
  143. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js.map +1 -1
  144. package/dist/components/Variables/VariableEditorForm/VariablePreview.js.map +1 -1
  145. package/dist/components/Variables/VariableEditorForm/index.js.map +1 -1
  146. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.d.ts +5 -4
  147. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.d.ts.map +1 -1
  148. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.js +22 -30
  149. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.js.map +1 -1
  150. package/dist/components/Variables/index.js.map +1 -1
  151. package/dist/components/Variables/variable-model.js +3 -3
  152. package/dist/components/Variables/variable-model.js.map +1 -1
  153. package/dist/components/index.d.ts +2 -0
  154. package/dist/components/index.d.ts.map +1 -1
  155. package/dist/components/index.js +2 -0
  156. package/dist/components/index.js.map +1 -1
  157. package/dist/constants/index.js.map +1 -1
  158. package/dist/constants/user-interface-text.js.map +1 -1
  159. package/dist/index.d.ts +1 -0
  160. package/dist/index.d.ts.map +1 -1
  161. package/dist/index.js +1 -0
  162. package/dist/index.js.map +1 -1
  163. package/dist/model/datasource.d.ts +2 -1
  164. package/dist/model/datasource.d.ts.map +1 -1
  165. package/dist/model/datasource.js.map +1 -1
  166. package/dist/model/index.js.map +1 -1
  167. package/dist/model/legend.d.ts +1 -1
  168. package/dist/model/legend.d.ts.map +1 -1
  169. package/dist/model/legend.js +14 -14
  170. package/dist/model/legend.js.map +1 -1
  171. package/dist/model/panels.js.map +1 -1
  172. package/dist/model/plugin-base.js.map +1 -1
  173. package/dist/model/plugin-loading.js.map +1 -1
  174. package/dist/runtime/DataQueriesProvider/DataQueriesProvider.d.ts +5 -5
  175. package/dist/runtime/DataQueriesProvider/DataQueriesProvider.d.ts.map +1 -1
  176. package/dist/runtime/DataQueriesProvider/DataQueriesProvider.js +40 -28
  177. package/dist/runtime/DataQueriesProvider/DataQueriesProvider.js.map +1 -1
  178. package/dist/runtime/DataQueriesProvider/index.js.map +1 -1
  179. package/dist/runtime/DataQueriesProvider/model.d.ts +17 -12
  180. package/dist/runtime/DataQueriesProvider/model.d.ts.map +1 -1
  181. package/dist/runtime/DataQueriesProvider/model.js +47 -1
  182. package/dist/runtime/DataQueriesProvider/model.js.map +1 -1
  183. package/dist/runtime/TimeRangeProvider/TimeRangeProvider.js.map +1 -1
  184. package/dist/runtime/TimeRangeProvider/index.js.map +1 -1
  185. package/dist/runtime/TimeRangeProvider/query-params.js +1 -1
  186. package/dist/runtime/TimeRangeProvider/query-params.js.map +1 -1
  187. package/dist/runtime/builtin-variables.d.ts +11 -0
  188. package/dist/runtime/builtin-variables.d.ts.map +1 -0
  189. package/dist/runtime/builtin-variables.js +59 -0
  190. package/dist/runtime/builtin-variables.js.map +1 -0
  191. package/dist/runtime/datasources.d.ts +14 -6
  192. package/dist/runtime/datasources.d.ts.map +1 -1
  193. package/dist/runtime/datasources.js +13 -6
  194. package/dist/runtime/datasources.js.map +1 -1
  195. package/dist/runtime/index.d.ts +1 -0
  196. package/dist/runtime/index.d.ts.map +1 -1
  197. package/dist/runtime/index.js +1 -0
  198. package/dist/runtime/index.js.map +1 -1
  199. package/dist/runtime/plugin-registry.d.ts +2 -0
  200. package/dist/runtime/plugin-registry.d.ts.map +1 -1
  201. package/dist/runtime/plugin-registry.js +19 -2
  202. package/dist/runtime/plugin-registry.js.map +1 -1
  203. package/dist/runtime/template-variables.d.ts +62 -2
  204. package/dist/runtime/template-variables.d.ts.map +1 -1
  205. package/dist/runtime/template-variables.js +83 -1
  206. package/dist/runtime/template-variables.js.map +1 -1
  207. package/dist/runtime/time-series-queries.d.ts +3 -3
  208. package/dist/runtime/time-series-queries.d.ts.map +1 -1
  209. package/dist/runtime/time-series-queries.js +14 -12
  210. package/dist/runtime/time-series-queries.js.map +1 -1
  211. package/dist/stories/shared-utils/decorators/WithDataQueries.js.map +1 -1
  212. package/dist/stories/shared-utils/decorators/WithPluginRegistry.js.map +1 -1
  213. package/dist/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.d.ts +13 -0
  214. package/dist/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.d.ts.map +1 -0
  215. package/dist/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.js +39 -0
  216. package/dist/stories/shared-utils/decorators/WithPluginSystemBuiltinVariables.js.map +1 -0
  217. package/dist/stories/shared-utils/decorators/WithPluginSystemDatasourceStore.d.ts.map +1 -1
  218. package/dist/stories/shared-utils/decorators/WithPluginSystemDatasourceStore.js +11 -7
  219. package/dist/stories/shared-utils/decorators/WithPluginSystemDatasourceStore.js.map +1 -1
  220. package/dist/stories/shared-utils/decorators/WithPluginSystemTemplateVariables.js.map +1 -1
  221. package/dist/stories/shared-utils/decorators/WithTimeRange.js.map +1 -1
  222. package/dist/stories/shared-utils/decorators/index.d.ts +1 -0
  223. package/dist/stories/shared-utils/decorators/index.d.ts.map +1 -1
  224. package/dist/stories/shared-utils/decorators/index.js +1 -0
  225. package/dist/stories/shared-utils/decorators/index.js.map +1 -1
  226. package/dist/stories/shared-utils/index.js.map +1 -1
  227. package/dist/test/index.d.ts +1 -0
  228. package/dist/test/index.d.ts.map +1 -1
  229. package/dist/test/index.js +1 -0
  230. package/dist/test/index.js.map +1 -1
  231. package/dist/test/mock-data.d.ts +3 -0
  232. package/dist/test/mock-data.d.ts.map +1 -0
  233. package/dist/test/mock-data.js +49 -0
  234. package/dist/test/mock-data.js.map +1 -0
  235. package/dist/test/render.js +2 -2
  236. package/dist/test/render.js.map +1 -1
  237. package/dist/test/setup-tests.js.map +1 -1
  238. package/dist/test/test-plugins/bert/index.js.map +1 -1
  239. package/dist/test/test-plugins/ernie/index.js.map +1 -1
  240. package/dist/test/test-plugins/index.js.map +1 -1
  241. package/dist/test-utils/index.js.map +1 -1
  242. package/dist/test-utils/mock-plugin-registry.js +2 -2
  243. package/dist/test-utils/mock-plugin-registry.js.map +1 -1
  244. package/dist/utils/action.d.ts +4 -0
  245. package/dist/utils/action.d.ts.map +1 -0
  246. package/dist/utils/action.js +27 -0
  247. package/dist/utils/action.js.map +1 -0
  248. package/dist/utils/index.d.ts +1 -0
  249. package/dist/utils/index.d.ts.map +1 -1
  250. package/dist/utils/index.js +1 -0
  251. package/dist/utils/index.js.map +1 -1
  252. package/dist/utils/variables.d.ts.map +1 -1
  253. package/dist/utils/variables.js +17 -5
  254. package/dist/utils/variables.js.map +1 -1
  255. package/dist/validation/datasource.d.ts +19 -0
  256. package/dist/validation/datasource.d.ts.map +1 -0
  257. package/dist/validation/datasource.js +22 -0
  258. package/dist/validation/datasource.js.map +1 -0
  259. package/dist/validation/index.d.ts +4 -0
  260. package/dist/validation/index.d.ts.map +1 -0
  261. package/dist/validation/index.js +17 -0
  262. package/dist/validation/index.js.map +1 -0
  263. package/dist/validation/resource.d.ts +3 -0
  264. package/dist/validation/resource.d.ts.map +1 -0
  265. package/dist/validation/resource.js +16 -0
  266. package/dist/validation/resource.js.map +1 -0
  267. package/dist/validation/variable.d.ts +19 -0
  268. package/dist/validation/variable.d.ts.map +1 -0
  269. package/dist/validation/variable.js +21 -0
  270. package/dist/validation/variable.js.map +1 -0
  271. package/package.json +8 -6
  272. package/dist/components/OptionsEditorTabs/TabPanel.d.ts +0 -9
  273. package/dist/components/OptionsEditorTabs/TabPanel.d.ts.map +0 -1
  274. package/dist/components/OptionsEditorTabs/TabPanel.js.map +0 -1
@@ -21,14 +21,14 @@ export function useDatasourceStore() {
21
21
  return ctx;
22
22
  }
23
23
  /**
24
- * Lists all available Datasource instances for a given datasource plugin kind. Returns a list with the name of that
25
- * instance, as well as its DatasourceSelector for referencing it.
26
- */ export function useListDatasources(datasourcePluginKind) {
27
- const { listDatasourceMetadata } = useDatasourceStore();
24
+ * Lists all available Datasource selection items for a given datasource plugin kind.
25
+ * Returns a list, with all information that can be used in a datasource selection context (group, name, selector, kind, ...)
26
+ */ export function useListDatasourceSelectItems(datasourcePluginKind) {
27
+ const { listDatasourceSelectItems } = useDatasourceStore();
28
28
  return useQuery([
29
- 'listDatasourceMetadata',
29
+ 'listDatasourceSelectItems',
30
30
  datasourcePluginKind
31
- ], ()=>listDatasourceMetadata(datasourcePluginKind));
31
+ ], ()=>listDatasourceSelectItems(datasourcePluginKind));
32
32
  }
33
33
  /**
34
34
  * Provides a convenience hook for getting a DatasourceClient for a given DatasourceSelector.
@@ -39,5 +39,12 @@ export function useDatasourceStore() {
39
39
  selector
40
40
  ], ()=>store.getDatasourceClient(selector));
41
41
  }
42
+ export function useDatasource(selector) {
43
+ const store = useDatasourceStore();
44
+ return useQuery([
45
+ 'getDatasource',
46
+ selector
47
+ ], ()=>store.getDatasource(selector));
48
+ }
42
49
 
43
50
  //# sourceMappingURL=datasources.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/runtime/datasources.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 { DatasourceSelector, DatasourceSpec } from '@perses-dev/core';\nimport { useQuery } from '@tanstack/react-query';\nimport { createContext, useContext } from 'react';\n\nexport interface DatasourceStore {\n // TODO: Do we even need this method?\n getDatasource(selector: DatasourceSelector): Promise<DatasourceSpec>;\n\n /**\n * Given a DatasourceSelector, gets a `Client` object from the corresponding Datasource plugin.\n */\n getDatasourceClient<Client>(selector: DatasourceSelector): Promise<Client>;\n\n /**\n * Gets a list of datasource metadata for a plugin kind.\n */\n listDatasourceMetadata(datasourcePluginKind: string): Promise<DatasourceMetadata[]>;\n}\n\nexport interface DatasourceMetadata {\n name: string;\n selector: DatasourceSelector;\n}\n\nexport const DatasourceStoreContext = createContext<DatasourceStore | undefined>(undefined);\n\nexport function useDatasourceStore() {\n const ctx = useContext(DatasourceStoreContext);\n if (ctx === undefined) {\n throw new Error('No DatasourceStoreContext found. Did you forget a Provider?');\n }\n return ctx;\n}\n\n/**\n * Lists all available Datasource instances for a given datasource plugin kind. Returns a list with the name of that\n * instance, as well as its DatasourceSelector for referencing it.\n */\nexport function useListDatasources(datasourcePluginKind: string) {\n const { listDatasourceMetadata } = useDatasourceStore();\n return useQuery(['listDatasourceMetadata', datasourcePluginKind], () => listDatasourceMetadata(datasourcePluginKind));\n}\n\n/**\n * Provides a convenience hook for getting a DatasourceClient for a given DatasourceSelector.\n */\nexport function useDatasourceClient<Client>(selector: DatasourceSelector) {\n const store = useDatasourceStore();\n return useQuery<Client>(['getDatasourceClient', selector], () => store.getDatasourceClient<Client>(selector));\n}\n"],"names":["useQuery","createContext","useContext","DatasourceStoreContext","undefined","useDatasourceStore","ctx","Error","useListDatasources","datasourcePluginKind","listDatasourceMetadata","useDatasourceClient","selector","store","getDatasourceClient"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAGjC,SAASA,QAAQ,QAAQ,uBAAuB,CAAC;AACjD,SAASC,aAAa,EAAEC,UAAU,QAAQ,OAAO,CAAC;AAsBlD,OAAO,MAAMC,sBAAsB,GAAGF,aAAa,CAA8BG,SAAS,CAAC,CAAC;AAE5F,OAAO,SAASC,kBAAkB,GAAG;IACnC,MAAMC,GAAG,GAAGJ,UAAU,CAACC,sBAAsB,CAAC,AAAC;IAC/C,IAAIG,GAAG,KAAKF,SAAS,EAAE;QACrB,MAAM,IAAIG,KAAK,CAAC,6DAA6D,CAAC,CAAC;IACjF,CAAC;IACD,OAAOD,GAAG,CAAC;AACb,CAAC;AAED;;;CAGC,GACD,OAAO,SAASE,kBAAkB,CAACC,oBAA4B,EAAE;IAC/D,MAAM,EAAEC,sBAAsB,CAAA,EAAE,GAAGL,kBAAkB,EAAE,AAAC;IACxD,OAAOL,QAAQ,CAAC;QAAC,wBAAwB;QAAES,oBAAoB;KAAC,EAAE,IAAMC,sBAAsB,CAACD,oBAAoB,CAAC,CAAC,CAAC;AACxH,CAAC;AAED;;CAEC,GACD,OAAO,SAASE,mBAAmB,CAASC,QAA4B,EAAE;IACxE,MAAMC,KAAK,GAAGR,kBAAkB,EAAE,AAAC;IACnC,OAAOL,QAAQ,CAAS;QAAC,qBAAqB;QAAEY,QAAQ;KAAC,EAAE,IAAMC,KAAK,CAACC,mBAAmB,CAASF,QAAQ,CAAC,CAAC,CAAC;AAChH,CAAC"}
1
+ {"version":3,"sources":["../../src/runtime/datasources.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 { DatasourceSelector, DatasourceSpec } from '@perses-dev/core';\nimport { useQuery } from '@tanstack/react-query';\nimport { createContext, useContext } from 'react';\n\nexport interface DatasourceStore {\n // TODO: Do we even need this method?\n getDatasource(selector: DatasourceSelector): Promise<DatasourceSpec>;\n\n /**\n * Given a DatasourceSelector, gets a `Client` object from the corresponding Datasource plugin.\n */\n getDatasourceClient<Client>(selector: DatasourceSelector): Promise<Client>;\n\n /**\n * Gets a list of datasource selection items for a plugin kind.\n */\n listDatasourceSelectItems(datasourcePluginKind: string): Promise<DatasourceSelectItemGroup[]>;\n}\n\nexport interface DatasourceSelectItemGroup {\n group?: string;\n editLink?: string;\n items: DatasourceSelectItem[];\n}\n\nexport interface DatasourceSelectItem {\n name: string;\n overridden?: boolean;\n overriding?: boolean;\n selector: DatasourceSelector;\n}\n\nexport const DatasourceStoreContext = createContext<DatasourceStore | undefined>(undefined);\n\nexport function useDatasourceStore() {\n const ctx = useContext(DatasourceStoreContext);\n if (ctx === undefined) {\n throw new Error('No DatasourceStoreContext found. Did you forget a Provider?');\n }\n return ctx;\n}\n\n/**\n * Lists all available Datasource selection items for a given datasource plugin kind.\n * Returns a list, with all information that can be used in a datasource selection context (group, name, selector, kind, ...)\n */\nexport function useListDatasourceSelectItems(datasourcePluginKind: string) {\n const { listDatasourceSelectItems } = useDatasourceStore();\n return useQuery(['listDatasourceSelectItems', datasourcePluginKind], () =>\n listDatasourceSelectItems(datasourcePluginKind)\n );\n}\n\n/**\n * Provides a convenience hook for getting a DatasourceClient for a given DatasourceSelector.\n */\nexport function useDatasourceClient<Client>(selector: DatasourceSelector) {\n const store = useDatasourceStore();\n return useQuery<Client>(['getDatasourceClient', selector], () => store.getDatasourceClient<Client>(selector));\n}\n\nexport function useDatasource(selector: DatasourceSelector) {\n const store = useDatasourceStore();\n return useQuery(['getDatasource', selector], () => store.getDatasource(selector));\n}\n"],"names":["useQuery","createContext","useContext","DatasourceStoreContext","undefined","useDatasourceStore","ctx","Error","useListDatasourceSelectItems","datasourcePluginKind","listDatasourceSelectItems","useDatasourceClient","selector","store","getDatasourceClient","useDatasource","getDatasource"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAGjC,SAASA,QAAQ,QAAQ,wBAAwB;AACjD,SAASC,aAAa,EAAEC,UAAU,QAAQ,QAAQ;AA8BlD,OAAO,MAAMC,yBAAyBF,cAA2CG,WAAW;AAE5F,OAAO,SAASC;IACd,MAAMC,MAAMJ,WAAWC;IACvB,IAAIG,QAAQF,WAAW;QACrB,MAAM,IAAIG,MAAM;IAClB;IACA,OAAOD;AACT;AAEA;;;CAGC,GACD,OAAO,SAASE,6BAA6BC,oBAA4B;IACvE,MAAM,EAAEC,0BAAyB,EAAE,GAAGL;IACtC,OAAOL,SAAS;QAAC;QAA6BS;KAAqB,EAAE,IACnEC,0BAA0BD;AAE9B;AAEA;;CAEC,GACD,OAAO,SAASE,oBAA4BC,QAA4B;IACtE,MAAMC,QAAQR;IACd,OAAOL,SAAiB;QAAC;QAAuBY;KAAS,EAAE,IAAMC,MAAMC,oBAA4BF;AACrG;AAEA,OAAO,SAASG,cAAcH,QAA4B;IACxD,MAAMC,QAAQR;IACd,OAAOL,SAAS;QAAC;QAAiBY;KAAS,EAAE,IAAMC,MAAMG,cAAcJ;AACzE"}
@@ -1,3 +1,4 @@
1
+ export * from './builtin-variables';
1
2
  export * from './datasources';
2
3
  export * from './plugin-registry';
3
4
  export * from './template-variables';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAaA,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC"}
@@ -10,6 +10,7 @@
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 './builtin-variables';
13
14
  export * from './datasources';
14
15
  export * from './plugin-registry';
15
16
  export * from './template-variables';
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/runtime/index.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\nexport * from './datasources';\nexport * from './plugin-registry';\nexport * from './template-variables';\nexport * from './TimeRangeProvider';\nexport * from './time-series-queries';\nexport * from './DataQueriesProvider';\n"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC"}
1
+ {"version":3,"sources":["../../src/runtime/index.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\nexport * from './builtin-variables';\nexport * from './datasources';\nexport * from './plugin-registry';\nexport * from './template-variables';\nexport * from './TimeRangeProvider';\nexport * from './time-series-queries';\nexport * from './DataQueriesProvider';\n"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,cAAc,sBAAsB;AACpC,cAAc,gBAAgB;AAC9B,cAAc,oBAAoB;AAClC,cAAc,uBAAuB;AACrC,cAAc,sBAAsB;AACpC,cAAc,wBAAwB;AACtC,cAAc,wBAAwB"}
@@ -1,5 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import { UseQueryOptions } from '@tanstack/react-query';
3
+ import { BuiltinVariableDefinition } from '@perses-dev/core';
3
4
  import { DefaultPluginKinds, PluginImplementation, PluginMetadata, PluginType } from '../model';
4
5
  export interface PluginRegistryContextType {
5
6
  getPlugin<T extends PluginType>(pluginType: T, kind: string): Promise<PluginImplementation<T>>;
@@ -28,5 +29,6 @@ declare type UseListPluginMetadataOptions = Omit<UseQueryOptions<PluginMetadata[
28
29
  * Gets a list of plugin metadata for the specified plugin type and returns it, along with loading/error state.
29
30
  */
30
31
  export declare function useListPluginMetadata(pluginType: PluginType, options?: UseListPluginMetadataOptions): import("@tanstack/react-query").UseQueryResult<PluginMetadata[], Error>;
32
+ export declare function usePluginBuiltinVariableDefinitions(): import("@tanstack/react-query").UseQueryResult<BuiltinVariableDefinition[], unknown>;
31
33
  export {};
32
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":";AAcA,OAAO,EAAwB,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC9E,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAEhG,MAAM,WAAW,yBAAyB;IACxC,SAAS,CAAC,CAAC,SAAS,UAAU,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AAM,EAAE,UAAU,CAAC,CAAC,EAChF,UAAU,GAAG,SAAS,CACvB,CAAC;AAEF;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,4BAA4B,2EAGnG;AAED,wBAAgB,mCAAmC,yFAelD"}
@@ -26,11 +26,11 @@ export const PluginRegistryContext = createContext(undefined);
26
26
  /**
27
27
  * Loads a plugin and returns the plugin implementation, along with loading/error state.
28
28
  */ export function usePlugin(pluginType, kind, options) {
29
- var ref;
29
+ var _options_enabled;
30
30
  // We never want to ask for a plugin when the kind isn't set yet, so disable those queries automatically
31
31
  options = {
32
32
  ...options,
33
- enabled: ((ref = options === null || options === void 0 ? void 0 : options.enabled) !== null && ref !== void 0 ? ref : true) && kind !== ''
33
+ enabled: ((_options_enabled = options === null || options === void 0 ? void 0 : options.enabled) !== null && _options_enabled !== void 0 ? _options_enabled : true) && kind !== ''
34
34
  };
35
35
  const { getPlugin } = usePluginRegistry();
36
36
  return useQuery([
@@ -65,5 +65,22 @@ export const PluginRegistryContext = createContext(undefined);
65
65
  pluginType
66
66
  ], ()=>listPluginMetadata(pluginType), options);
67
67
  }
68
+ export function usePluginBuiltinVariableDefinitions() {
69
+ const { getPlugin , listPluginMetadata } = usePluginRegistry();
70
+ return useQuery([
71
+ 'usePluginBuiltinVariableDefinitions'
72
+ ], async ()=>{
73
+ const datasources = await listPluginMetadata('Datasource');
74
+ const datasourceKinds = new Set(datasources.map((datasource)=>datasource.kind));
75
+ const result = [];
76
+ for (const kind of datasourceKinds){
77
+ const plugin = await getPlugin('Datasource', kind);
78
+ if (plugin.getBuiltinVariableDefinitions) {
79
+ plugin.getBuiltinVariableDefinitions().forEach((definition)=>result.push(definition));
80
+ }
81
+ }
82
+ return result;
83
+ });
84
+ }
68
85
 
69
86
  //# sourceMappingURL=plugin-registry.js.map
@@ -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 { createContext, useContext } from 'react';\nimport { useQuery, useQueries, UseQueryOptions } from '@tanstack/react-query';\nimport { DefaultPluginKinds, PluginImplementation, PluginMetadata, PluginType } from '../model';\n\nexport interface PluginRegistryContextType {\n getPlugin<T extends PluginType>(pluginType: T, kind: string): Promise<PluginImplementation<T>>;\n listPluginMetadata(pluginType: PluginType): Promise<PluginMetadata[]>;\n defaultPluginKinds: DefaultPluginKinds;\n}\n\nexport const PluginRegistryContext = createContext<PluginRegistryContextType | undefined>(undefined);\n\n/**\n * Use the PluginRegistry context directly. This is meant as an escape hatch for custom async flows. You should probably\n * be using `usePlugin` or `useListPluginMetadata` instead.\n */\nexport function usePluginRegistry() {\n const ctx = useContext(PluginRegistryContext);\n if (ctx === undefined) {\n throw new Error('PluginRegistryContext not found. Did you forget a provider?');\n }\n return ctx;\n}\n\n// Allows consumers to pass useQuery options from react-query when loading a plugin\ntype UsePluginOptions<T extends PluginType> = Omit<\n UseQueryOptions<PluginImplementation<T>, Error, PluginImplementation<T>, [string, PluginType, string]>,\n 'queryKey' | 'queryFn'\n>;\n\n/**\n * Loads a plugin and returns the plugin implementation, along with loading/error state.\n */\nexport function usePlugin<T extends PluginType>(pluginType: T, kind: string, options?: UsePluginOptions<T>) {\n // We never want to ask for a plugin when the kind isn't set yet, so disable those queries automatically\n options = {\n ...options,\n enabled: (options?.enabled ?? true) && kind !== '',\n };\n const { getPlugin } = usePluginRegistry();\n return useQuery(['getPlugin', pluginType, kind], () => getPlugin(pluginType, kind), options);\n}\n\n/**\n * Loads a list of plugins and returns the plugin implementation, along with loading/error state.\n */\nexport function usePlugins<T extends PluginType>(pluginType: T, plugins: Array<{ kind: string }>) {\n const { getPlugin } = usePluginRegistry();\n return useQueries({\n queries: plugins.map((p) => {\n return {\n queryKey: ['getPlugin', pluginType, p.kind],\n queryFn: () => getPlugin(pluginType, p.kind),\n };\n }),\n });\n}\n\n// Allow consumers to pass useQuery options from react-query when listing metadata\ntype UseListPluginMetadataOptions = Omit<\n UseQueryOptions<PluginMetadata[], Error, PluginMetadata[], [string, PluginType]>,\n 'queryKey' | 'queryFn'\n>;\n\n/**\n * Gets a list of plugin metadata for the specified plugin type and returns it, along with loading/error state.\n */\nexport function useListPluginMetadata(pluginType: PluginType, options?: UseListPluginMetadataOptions) {\n const { listPluginMetadata } = usePluginRegistry();\n return useQuery(['listPluginMetadata', pluginType], () => listPluginMetadata(pluginType), options);\n}\n"],"names":["createContext","useContext","useQuery","useQueries","PluginRegistryContext","undefined","usePluginRegistry","ctx","Error","usePlugin","pluginType","kind","options","enabled","getPlugin","usePlugins","plugins","queries","map","p","queryKey","queryFn","useListPluginMetadata","listPluginMetadata"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,aAAa,EAAEC,UAAU,QAAQ,OAAO,CAAC;AAClD,SAASC,QAAQ,EAAEC,UAAU,QAAyB,uBAAuB,CAAC;AAS9E,OAAO,MAAMC,qBAAqB,GAAGJ,aAAa,CAAwCK,SAAS,CAAC,CAAC;AAErG;;;CAGC,GACD,OAAO,SAASC,iBAAiB,GAAG;IAClC,MAAMC,GAAG,GAAGN,UAAU,CAACG,qBAAqB,CAAC,AAAC;IAC9C,IAAIG,GAAG,KAAKF,SAAS,EAAE;QACrB,MAAM,IAAIG,KAAK,CAAC,6DAA6D,CAAC,CAAC;IAC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AAAC;IACnD,OAAOJ,QAAQ,CAAC;QAAC,oBAAoB;QAAEQ,UAAU;KAAC,EAAE,IAAMa,kBAAkB,CAACb,UAAU,CAAC,EAAEE,OAAO,CAAC,CAAC;AACrG,CAAC"}
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 { createContext, useContext } from 'react';\nimport { useQuery, useQueries, UseQueryOptions } from '@tanstack/react-query';\nimport { BuiltinVariableDefinition } from '@perses-dev/core';\nimport { DefaultPluginKinds, PluginImplementation, PluginMetadata, PluginType } from '../model';\n\nexport interface PluginRegistryContextType {\n getPlugin<T extends PluginType>(pluginType: T, kind: string): Promise<PluginImplementation<T>>;\n listPluginMetadata(pluginType: PluginType): Promise<PluginMetadata[]>;\n defaultPluginKinds: DefaultPluginKinds;\n}\n\nexport const PluginRegistryContext = createContext<PluginRegistryContextType | undefined>(undefined);\n\n/**\n * Use the PluginRegistry context directly. This is meant as an escape hatch for custom async flows. You should probably\n * be using `usePlugin` or `useListPluginMetadata` instead.\n */\nexport function usePluginRegistry() {\n const ctx = useContext(PluginRegistryContext);\n if (ctx === undefined) {\n throw new Error('PluginRegistryContext not found. Did you forget a provider?');\n }\n return ctx;\n}\n\n// Allows consumers to pass useQuery options from react-query when loading a plugin\ntype UsePluginOptions<T extends PluginType> = Omit<\n UseQueryOptions<PluginImplementation<T>, Error, PluginImplementation<T>, [string, PluginType, string]>,\n 'queryKey' | 'queryFn'\n>;\n\n/**\n * Loads a plugin and returns the plugin implementation, along with loading/error state.\n */\nexport function usePlugin<T extends PluginType>(pluginType: T, kind: string, options?: UsePluginOptions<T>) {\n // We never want to ask for a plugin when the kind isn't set yet, so disable those queries automatically\n options = {\n ...options,\n enabled: (options?.enabled ?? true) && kind !== '',\n };\n const { getPlugin } = usePluginRegistry();\n return useQuery(['getPlugin', pluginType, kind], () => getPlugin(pluginType, kind), options);\n}\n\n/**\n * Loads a list of plugins and returns the plugin implementation, along with loading/error state.\n */\nexport function usePlugins<T extends PluginType>(pluginType: T, plugins: Array<{ kind: string }>) {\n const { getPlugin } = usePluginRegistry();\n return useQueries({\n queries: plugins.map((p) => {\n return {\n queryKey: ['getPlugin', pluginType, p.kind],\n queryFn: () => getPlugin(pluginType, p.kind),\n };\n }),\n });\n}\n\n// Allow consumers to pass useQuery options from react-query when listing metadata\ntype UseListPluginMetadataOptions = Omit<\n UseQueryOptions<PluginMetadata[], Error, PluginMetadata[], [string, PluginType]>,\n 'queryKey' | 'queryFn'\n>;\n\n/**\n * Gets a list of plugin metadata for the specified plugin type and returns it, along with loading/error state.\n */\nexport function useListPluginMetadata(pluginType: PluginType, options?: UseListPluginMetadataOptions) {\n const { listPluginMetadata } = usePluginRegistry();\n return useQuery(['listPluginMetadata', pluginType], () => listPluginMetadata(pluginType), options);\n}\n\nexport function usePluginBuiltinVariableDefinitions() {\n const { getPlugin, listPluginMetadata } = usePluginRegistry();\n\n return useQuery(['usePluginBuiltinVariableDefinitions'], async () => {\n const datasources = await listPluginMetadata('Datasource');\n const datasourceKinds = new Set(datasources.map((datasource) => datasource.kind));\n const result: BuiltinVariableDefinition[] = [];\n for (const kind of datasourceKinds) {\n const plugin = await getPlugin('Datasource', kind);\n if (plugin.getBuiltinVariableDefinitions) {\n plugin.getBuiltinVariableDefinitions().forEach((definition) => result.push(definition));\n }\n }\n return result;\n });\n}\n"],"names":["createContext","useContext","useQuery","useQueries","PluginRegistryContext","undefined","usePluginRegistry","ctx","Error","usePlugin","pluginType","kind","options","enabled","getPlugin","usePlugins","plugins","queries","map","p","queryKey","queryFn","useListPluginMetadata","listPluginMetadata","usePluginBuiltinVariableDefinitions","datasources","datasourceKinds","Set","datasource","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;AAEjC,SAASA,aAAa,EAAEC,UAAU,QAAQ,QAAQ;AAClD,SAASC,QAAQ,EAAEC,UAAU,QAAyB,wBAAwB;AAU9E,OAAO,MAAMC,wBAAwBJ,cAAqDK,WAAW;AAErG;;;CAGC,GACD,OAAO,SAASC;IACd,MAAMC,MAAMN,WAAWG;IACvB,IAAIG,QAAQF,WAAW;QACrB,MAAM,IAAIG,MAAM;IAClB;IACA,OAAOD;AACT;AAQA;;CAEC,GACD,OAAO,SAASE,UAAgCC,UAAa,EAAEC,IAAY,EAAEC,OAA6B;QAI5FA;IAHZ,wGAAwG;IACxGA,UAAU;QACR,GAAGA,OAAO;QACVC,SAAS,AAACD,CAAAA,CAAAA,mBAAAA,oBAAAA,qBAAAA,KAAAA,IAAAA,QAASC,qBAATD,8BAAAA,mBAAoB,IAAG,KAAMD,SAAS;IAClD;IACA,MAAM,EAAEG,UAAS,EAAE,GAAGR;IACtB,OAAOJ,SAAS;QAAC;QAAaQ;QAAYC;KAAK,EAAE,IAAMG,UAAUJ,YAAYC,OAAOC;AACtF;AAEA;;CAEC,GACD,OAAO,SAASG,WAAiCL,UAAa,EAAEM,OAAgC;IAC9F,MAAM,EAAEF,UAAS,EAAE,GAAGR;IACtB,OAAOH,WAAW;QAChBc,SAASD,QAAQE,IAAI,CAACC;YACpB,OAAO;gBACLC,UAAU;oBAAC;oBAAaV;oBAAYS,EAAER;iBAAK;gBAC3CU,SAAS,IAAMP,UAAUJ,YAAYS,EAAER;YACzC;QACF;IACF;AACF;AAQA;;CAEC,GACD,OAAO,SAASW,sBAAsBZ,UAAsB,EAAEE,OAAsC;IAClG,MAAM,EAAEW,mBAAkB,EAAE,GAAGjB;IAC/B,OAAOJ,SAAS;QAAC;QAAsBQ;KAAW,EAAE,IAAMa,mBAAmBb,aAAaE;AAC5F;AAEA,OAAO,SAASY;IACd,MAAM,EAAEV,UAAS,EAAES,mBAAkB,EAAE,GAAGjB;IAE1C,OAAOJ,SAAS;QAAC;KAAsC,EAAE;QACvD,MAAMuB,cAAc,MAAMF,mBAAmB;QAC7C,MAAMG,kBAAkB,IAAIC,IAAIF,YAAYP,IAAI,CAACU,aAAeA,WAAWjB;QAC3E,MAAMkB,SAAsC,EAAE;QAC9C,KAAK,MAAMlB,QAAQe,gBAAiB;YAClC,MAAMI,SAAS,MAAMhB,UAAU,cAAcH;YAC7C,IAAImB,OAAOC,+BAA+B;gBACxCD,OAAOC,gCAAgCC,QAAQ,CAACC,aAAeJ,OAAOK,KAAKD;YAC7E;QACF;QACA,OAAOJ;IACT;AACF"}
@@ -1,17 +1,77 @@
1
1
  /// <reference types="react" />
2
- import { VariableName, VariableValue } from '@perses-dev/core';
2
+ import { VariableValue } from '@perses-dev/core';
3
+ import { immerable } from 'immer';
3
4
  import { VariableOption } from '../model';
4
5
  export declare type VariableState = {
5
6
  value: VariableValue;
6
7
  options?: VariableOption[];
7
8
  loading: boolean;
8
9
  error?: Error;
10
+ /**
11
+ * If a local variable is overriding an external variable, local var will have the flag ``overriding=true``.
12
+ */
13
+ overriding?: boolean;
14
+ /**
15
+ * If a local variable is overriding an external variable, external var will have the flag ``overridden=true``.
16
+ */
17
+ overridden?: boolean;
18
+ defaultValue?: VariableValue;
9
19
  };
10
- export declare type VariableStateMap = Record<VariableName, VariableState>;
20
+ export declare type VariableStateMap = Record<string, VariableState>;
21
+ /**
22
+ * Structure used as key in the {@link VariableStoreStateMap}.
23
+ */
24
+ export declare type VariableStateKey = {
25
+ /**
26
+ * name of the variable we want to access in the state.
27
+ */
28
+ name: string;
29
+ /**
30
+ * source of the variable we want to access in the state.
31
+ * Defined only for external variables.
32
+ */
33
+ source?: string;
34
+ };
35
+ /**
36
+ * A state map with two entry keys, materialized by {@link VariableStateKey} structure.
37
+ */
38
+ export declare class VariableStoreStateMap {
39
+ /**
40
+ * "Immerable" is mandatory to be able to use this class in an immer context.
41
+ * Ref: https://docs.pmnd.rs/zustand/integrations/immer-middleware#gotchas
42
+ */
43
+ [immerable]: boolean;
44
+ private readonly DEFAULT_LOCAL_SOURCE_NAME;
45
+ private readonly _state;
46
+ /**
47
+ * Get variable state by key.
48
+ * @param key
49
+ */
50
+ get(key: VariableStateKey): VariableState | undefined;
51
+ /**
52
+ * Set variable state by key.
53
+ * @param key
54
+ * @param value
55
+ */
56
+ set(key: VariableStateKey, value: VariableState): VariableState | undefined;
57
+ /**
58
+ * Check presence of variable state by key.
59
+ * @param key
60
+ */
61
+ has(key: VariableStateKey): boolean;
62
+ /**
63
+ * Delete variable state by key.
64
+ * @param key
65
+ */
66
+ delete(key: VariableStateKey): boolean;
67
+ private _sourceName;
68
+ private _sourceStatesOrEmpty;
69
+ }
11
70
  export declare type TemplateVariableSrv = {
12
71
  state: VariableStateMap;
13
72
  };
14
73
  export declare const TemplateVariableContext: import("react").Context<TemplateVariableSrv | undefined>;
15
74
  export declare function useTemplateVariableValues(names?: string[]): VariableStateMap;
75
+ export declare function useVariableValues(names?: string[]): VariableStateMap;
16
76
  export declare function useReplaceVariablesInString(str: string | undefined): string | undefined;
17
77
  //# sourceMappingURL=template-variables.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"template-variables.d.ts","sourceRoot":"","sources":["../../src/runtime/template-variables.ts"],"names":[],"mappings":";AAcA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAG1C,oBAAY,aAAa,GAAG;IAC1B,KAAK,EAAE,aAAa,CAAC;IACrB,OAAO,CAAC,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;IAChC;;;OAGG;IACH,CAAC,SAAS,CAAC,UAAQ;IAEnB,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAAM;IAChD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqD;IAE5E;;;OAGG;IACH,GAAG,CAAC,GAAG,EAAE,gBAAgB,GAAG,aAAa,GAAG,SAAS;IAIrD;;;;OAIG;IACH,GAAG,CAAC,GAAG,EAAE,gBAAgB,EAAE,KAAK,EAAE,aAAa,GAAG,aAAa,GAAG,SAAS;IAS3E;;;OAGG;IACH,GAAG,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO;IAInC;;;OAGG;IACH,MAAM,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO;IAiBtC,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,oBAAoB;CAG7B;AAED,oBAAY,mBAAmB,GAAG;IAChC,KAAK,EAAE,gBAAgB,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,uBAAuB,0DAA4D,CAAC;AAUjG,wBAAgB,yBAAyB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,gBAAgB,CAmB5E;AAED,wBAAgB,iBAAiB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,gBAAgB,CAKpE;AAGD,wBAAgB,2BAA2B,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAKvF"}
@@ -10,8 +10,85 @@
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
+ function _define_property(obj, key, value) {
14
+ if (key in obj) {
15
+ Object.defineProperty(obj, key, {
16
+ value: value,
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true
20
+ });
21
+ } else {
22
+ obj[key] = value;
23
+ }
24
+ return obj;
25
+ }
13
26
  import { createContext, useContext, useMemo } from 'react';
27
+ import { immerable } from 'immer';
14
28
  import { parseTemplateVariables, replaceTemplateVariables } from '../utils';
29
+ import { useBuiltinVariableValues } from './builtin-variables';
30
+ let _immerable = immerable;
31
+ /**
32
+ * A state map with two entry keys, materialized by {@link VariableStateKey} structure.
33
+ */ export class VariableStoreStateMap {
34
+ /**
35
+ * Get variable state by key.
36
+ * @param key
37
+ */ get(key) {
38
+ return this._sourceStatesOrEmpty(key.source)[key.name];
39
+ }
40
+ /**
41
+ * Set variable state by key.
42
+ * @param key
43
+ * @param value
44
+ */ set(key, value) {
45
+ const sourceName = this._sourceName(key.source);
46
+ if (!this._state[sourceName]) {
47
+ this._state[sourceName] = {};
48
+ }
49
+ this._sourceStatesOrEmpty(key.source)[key.name] = value;
50
+ return value;
51
+ }
52
+ /**
53
+ * Check presence of variable state by key.
54
+ * @param key
55
+ */ has(key) {
56
+ return this._sourceStatesOrEmpty(key.source)[key.name] !== undefined;
57
+ }
58
+ /**
59
+ * Delete variable state by key.
60
+ * @param key
61
+ */ delete(key) {
62
+ var _Object_keys;
63
+ const result = this.has(key);
64
+ const sourceName = this._sourceName(key.source);
65
+ const sourceStates = this._state[sourceName];
66
+ // Delete var from source state
67
+ if (sourceStates) {
68
+ delete sourceStates[key.name];
69
+ }
70
+ // Delete source state from state if empty
71
+ if (((_Object_keys = Object.keys(sourceStates !== null && sourceStates !== void 0 ? sourceStates : {})) === null || _Object_keys === void 0 ? void 0 : _Object_keys.length) === 0) {
72
+ delete this._state[sourceName];
73
+ }
74
+ return result;
75
+ }
76
+ _sourceName(source) {
77
+ return source !== null && source !== void 0 ? source : this.DEFAULT_LOCAL_SOURCE_NAME;
78
+ }
79
+ _sourceStatesOrEmpty(source) {
80
+ var _this__state_this__sourceName;
81
+ return (_this__state_this__sourceName = this._state[this._sourceName(source)]) !== null && _this__state_this__sourceName !== void 0 ? _this__state_this__sourceName : {};
82
+ }
83
+ constructor(){
84
+ /**
85
+ * "Immerable" is mandatory to be able to use this class in an immer context.
86
+ * Ref: https://docs.pmnd.rs/zustand/integrations/immer-middleware#gotchas
87
+ */ _define_property(this, _immerable, true);
88
+ _define_property(this, "DEFAULT_LOCAL_SOURCE_NAME", '');
89
+ _define_property(this, "_state", {});
90
+ }
91
+ }
15
92
  export const TemplateVariableContext = createContext(undefined);
16
93
  function useTemplateVariableContext() {
17
94
  const ctx = useContext(TemplateVariableContext);
@@ -40,10 +117,15 @@ export function useTemplateVariableValues(names) {
40
117
  }
41
118
  return values;
42
119
  }
120
+ export function useVariableValues(names) {
121
+ const templateVariableValues = useTemplateVariableValues(names);
122
+ const builtinVariableValues = useBuiltinVariableValues(names);
123
+ return Object.assign(templateVariableValues, builtinVariableValues);
124
+ }
43
125
  // Convenience hook for replacing template variables in a string
44
126
  export function useReplaceVariablesInString(str) {
45
127
  const variablesInString = str ? parseTemplateVariables(str) : [];
46
- const variableValues = useTemplateVariableValues(variablesInString);
128
+ const variableValues = useVariableValues(variablesInString);
47
129
  if (!str) return undefined;
48
130
  return replaceTemplateVariables(str, variableValues);
49
131
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/runtime/template-variables.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 { createContext, useContext, useMemo } from 'react';\nimport { VariableName, VariableValue } from '@perses-dev/core';\nimport { VariableOption } from '../model';\nimport { parseTemplateVariables, replaceTemplateVariables } from '../utils';\n\nexport type VariableState = {\n value: VariableValue;\n options?: VariableOption[];\n loading: boolean;\n error?: Error;\n};\n\nexport type VariableStateMap = Record<VariableName, VariableState>;\n\nexport type TemplateVariableSrv = {\n state: VariableStateMap;\n};\n\nexport const TemplateVariableContext = createContext<TemplateVariableSrv | undefined>(undefined);\n\nfunction useTemplateVariableContext() {\n const ctx = useContext(TemplateVariableContext);\n if (ctx === undefined) {\n throw new Error('No TemplateVariableContext found. Did you forget a Provider?');\n }\n return ctx;\n}\n\nexport function useTemplateVariableValues(names?: string[]) {\n const { state } = useTemplateVariableContext();\n\n const values = useMemo(() => {\n const values: VariableStateMap = {};\n names?.forEach((name) => {\n const s = state[name];\n if (s) {\n values[name] = s;\n }\n });\n return values;\n }, [state, names]);\n\n if (names === undefined) {\n return state;\n }\n\n return values;\n}\n\n// Convenience hook for replacing template variables in a string\nexport function useReplaceVariablesInString(str: string | undefined): string | undefined {\n const variablesInString = str ? parseTemplateVariables(str) : [];\n const variableValues = useTemplateVariableValues(variablesInString);\n if (!str) return undefined;\n return replaceTemplateVariables(str, variableValues);\n}\n"],"names":["createContext","useContext","useMemo","parseTemplateVariables","replaceTemplateVariables","TemplateVariableContext","undefined","useTemplateVariableContext","ctx","Error","useTemplateVariableValues","names","state","values","forEach","name","s","useReplaceVariablesInString","str","variablesInString","variableValues"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,aAAa,EAAEC,UAAU,EAAEC,OAAO,QAAQ,OAAO,CAAC;AAG3D,SAASC,sBAAsB,EAAEC,wBAAwB,QAAQ,UAAU,CAAC;AAe5E,OAAO,MAAMC,uBAAuB,GAAGL,aAAa,CAAkCM,SAAS,CAAC,CAAC;AAEjG,SAASC,0BAA0B,GAAG;IACpC,MAAMC,GAAG,GAAGP,UAAU,CAACI,uBAAuB,CAAC,AAAC;IAChD,IAAIG,GAAG,KAAKF,SAAS,EAAE;QACrB,MAAM,IAAIG,KAAK,CAAC,8DAA8D,CAAC,CAAC;IAClF,CAAC;IACD,OAAOD,GAAG,CAAC;AACb,CAAC;AAED,OAAO,SAASE,yBAAyB,CAACC,KAAgB,EAAE;IAC1D,MAAM,EAAEC,KAAK,CAAA,EAAE,GAAGL,0BAA0B,EAAE,AAAC;IAE/C,MAAMM,MAAM,GAAGX,OAAO,CAAC,IAAM;QAC3B,MAAMW,MAAM,GAAqB,EAAE,AAAC;QACpCF,KAAK,aAALA,KAAK,WAAS,GAAdA,KAAAA,CAAc,GAAdA,KAAK,CAAEG,OAAO,CAAC,CAACC,IAAI,GAAK;YACvB,MAAMC,CAAC,GAAGJ,KAAK,CAACG,IAAI,CAAC,AAAC;YACtB,IAAIC,CAAC,EAAE;gBACLH,MAAM,CAACE,IAAI,CAAC,GAAGC,CAAC,CAAC;YACnB,CAAC;QACH,CAAC,CAAC,CAAC;QACH,OAAOH,MAAM,CAAC;IAChB,CAAC,EAAE;QAACD,KAAK;QAAED,KAAK;KAAC,CAAC,AAAC;IAEnB,IAAIA,KAAK,KAAKL,SAAS,EAAE;QACvB,OAAOM,KAAK,CAAC;IACf,CAAC;IAED,OAAOC,MAAM,CAAC;AAChB,CAAC;AAED,gEAAgE;AAChE,OAAO,SAASI,2BAA2B,CAACC,GAAuB,EAAsB;IACvF,MAAMC,iBAAiB,GAAGD,GAAG,GAAGf,sBAAsB,CAACe,GAAG,CAAC,GAAG,EAAE,AAAC;IACjE,MAAME,cAAc,GAAGV,yBAAyB,CAACS,iBAAiB,CAAC,AAAC;IACpE,IAAI,CAACD,GAAG,EAAE,OAAOZ,SAAS,CAAC;IAC3B,OAAOF,wBAAwB,CAACc,GAAG,EAAEE,cAAc,CAAC,CAAC;AACvD,CAAC"}
1
+ {"version":3,"sources":["../../src/runtime/template-variables.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 { createContext, useContext, useMemo } from 'react';\nimport { VariableValue } from '@perses-dev/core';\nimport { immerable } from 'immer';\nimport { VariableOption } from '../model';\nimport { parseTemplateVariables, replaceTemplateVariables } from '../utils';\nimport { useBuiltinVariableValues } from './builtin-variables';\n\nexport type VariableState = {\n value: VariableValue;\n options?: VariableOption[];\n loading: boolean;\n error?: Error;\n /**\n * If a local variable is overriding an external variable, local var will have the flag ``overriding=true``.\n */\n overriding?: boolean;\n /**\n * If a local variable is overriding an external variable, external var will have the flag ``overridden=true``.\n */\n overridden?: boolean;\n defaultValue?: VariableValue;\n};\n\nexport type VariableStateMap = Record<string, VariableState>;\n\n/**\n * Structure used as key in the {@link VariableStoreStateMap}.\n */\nexport type VariableStateKey = {\n /**\n * name of the variable we want to access in the state.\n */\n name: string;\n /**\n * source of the variable we want to access in the state.\n * Defined only for external variables.\n */\n source?: string;\n};\n\n/**\n * A state map with two entry keys, materialized by {@link VariableStateKey} structure.\n */\nexport class VariableStoreStateMap {\n /**\n * \"Immerable\" is mandatory to be able to use this class in an immer context.\n * Ref: https://docs.pmnd.rs/zustand/integrations/immer-middleware#gotchas\n */\n [immerable] = true;\n\n private readonly DEFAULT_LOCAL_SOURCE_NAME = '';\n private readonly _state: Record<string, Record<string, VariableState>> = {};\n\n /**\n * Get variable state by key.\n * @param key\n */\n get(key: VariableStateKey): VariableState | undefined {\n return this._sourceStatesOrEmpty(key.source)[key.name];\n }\n\n /**\n * Set variable state by key.\n * @param key\n * @param value\n */\n set(key: VariableStateKey, value: VariableState): VariableState | undefined {\n const sourceName = this._sourceName(key.source);\n if (!this._state[sourceName]) {\n this._state[sourceName] = {};\n }\n this._sourceStatesOrEmpty(key.source)[key.name] = value;\n return value;\n }\n\n /**\n * Check presence of variable state by key.\n * @param key\n */\n has(key: VariableStateKey): boolean {\n return this._sourceStatesOrEmpty(key.source)[key.name] !== undefined;\n }\n\n /**\n * Delete variable state by key.\n * @param key\n */\n delete(key: VariableStateKey): boolean {\n const result = this.has(key);\n const sourceName = this._sourceName(key.source);\n const sourceStates = this._state[sourceName];\n\n // Delete var from source state\n if (sourceStates) {\n delete sourceStates[key.name];\n }\n\n // Delete source state from state if empty\n if (Object.keys(sourceStates ?? {})?.length === 0) {\n delete this._state[sourceName];\n }\n return result;\n }\n\n private _sourceName(source: string | undefined): string {\n return source ?? this.DEFAULT_LOCAL_SOURCE_NAME;\n }\n\n private _sourceStatesOrEmpty(source: string | undefined): Record<string, VariableState> {\n return this._state[this._sourceName(source)] ?? {};\n }\n}\n\nexport type TemplateVariableSrv = {\n state: VariableStateMap;\n};\n\nexport const TemplateVariableContext = createContext<TemplateVariableSrv | undefined>(undefined);\n\nfunction useTemplateVariableContext() {\n const ctx = useContext(TemplateVariableContext);\n if (ctx === undefined) {\n throw new Error('No TemplateVariableContext found. Did you forget a Provider?');\n }\n return ctx;\n}\n\nexport function useTemplateVariableValues(names?: string[]): VariableStateMap {\n const { state } = useTemplateVariableContext();\n\n const values = useMemo(() => {\n const values: VariableStateMap = {};\n names?.forEach((name) => {\n const s = state[name];\n if (s) {\n values[name] = s;\n }\n });\n return values;\n }, [state, names]);\n\n if (names === undefined) {\n return state;\n }\n\n return values;\n}\n\nexport function useVariableValues(names?: string[]): VariableStateMap {\n const templateVariableValues = useTemplateVariableValues(names);\n const builtinVariableValues = useBuiltinVariableValues(names);\n\n return Object.assign(templateVariableValues, builtinVariableValues);\n}\n\n// Convenience hook for replacing template variables in a string\nexport function useReplaceVariablesInString(str: string | undefined): string | undefined {\n const variablesInString = str ? parseTemplateVariables(str) : [];\n const variableValues = useVariableValues(variablesInString);\n if (!str) return undefined;\n return replaceTemplateVariables(str, variableValues);\n}\n"],"names":["createContext","useContext","useMemo","immerable","parseTemplateVariables","replaceTemplateVariables","useBuiltinVariableValues","VariableStoreStateMap","get","key","_sourceStatesOrEmpty","source","name","set","value","sourceName","_sourceName","_state","has","undefined","delete","Object","result","sourceStates","keys","length","DEFAULT_LOCAL_SOURCE_NAME","TemplateVariableContext","useTemplateVariableContext","ctx","Error","useTemplateVariableValues","names","state","values","forEach","s","useVariableValues","templateVariableValues","builtinVariableValues","assign","useReplaceVariablesInString","str","variablesInString","variableValues"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;;;;;;;;;;;;;;AAEjC,SAASA,aAAa,EAAEC,UAAU,EAAEC,OAAO,QAAQ,QAAQ;AAE3D,SAASC,SAAS,QAAQ,QAAQ;AAElC,SAASC,sBAAsB,EAAEC,wBAAwB,QAAQ,WAAW;AAC5E,SAASC,wBAAwB,QAAQ,sBAAsB;IA2C5DH,aAAAA;AARH;;CAEC,GACD,OAAO,MAAMI;IAUX;;;GAGC,GACDC,IAAIC,GAAqB,EAA6B;QACpD,OAAO,IAAI,CAACC,qBAAqBD,IAAIE,OAAO,CAACF,IAAIG,KAAK;IACxD;IAEA;;;;GAIC,GACDC,IAAIJ,GAAqB,EAAEK,KAAoB,EAA6B;QAC1E,MAAMC,aAAa,IAAI,CAACC,YAAYP,IAAIE;QACxC,IAAI,CAAC,IAAI,CAACM,MAAM,CAACF,WAAW,EAAE;YAC5B,IAAI,CAACE,MAAM,CAACF,WAAW,GAAG,CAAC;QAC7B;QACA,IAAI,CAACL,qBAAqBD,IAAIE,OAAO,CAACF,IAAIG,KAAK,GAAGE;QAClD,OAAOA;IACT;IAEA;;;GAGC,GACDI,IAAIT,GAAqB,EAAW;QAClC,OAAO,IAAI,CAACC,qBAAqBD,IAAIE,OAAO,CAACF,IAAIG,KAAK,KAAKO;IAC7D;IAEA;;;GAGC,GACDC,OAAOX,GAAqB,EAAW;YAWjCY;QAVJ,MAAMC,SAAS,IA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QAAOD;KAAM;IAEjB,IAAIA,UAAUb,WAAW;QACvB,OAAOc;IACT;IAEA,OAAOC;AACT;AAEA,OAAO,SAASG,kBAAkBL,KAAgB;IAChD,MAAMM,yBAAyBP,0BAA0BC;IACzD,MAAMO,wBAAwBjC,yBAAyB0B;IAEvD,OAAOX,OAAOmB,OAAOF,wBAAwBC;AAC/C;AAEA,gEAAgE;AAChE,OAAO,SAASE,4BAA4BC,GAAuB;IACjE,MAAMC,oBAAoBD,MAAMtC,uBAAuBsC,OAAO,EAAE;IAChE,MAAME,iBAAiBP,kBAAkBM;IACzC,IAAI,CAACD,KAAK,OAAOvB;IACjB,OAAOd,yBAAyBqC,KAAKE;AACvC"}
@@ -1,4 +1,4 @@
1
- import { QueryCache } from '@tanstack/react-query';
1
+ import { QueryCache, QueryObserverOptions } from '@tanstack/react-query';
2
2
  import { TimeSeriesQueryDefinition, TimeSeriesData } from '@perses-dev/core';
3
3
  import { TimeSeriesDataQuery } from '../model';
4
4
  export interface UseTimeSeriesQueryOptions {
@@ -8,11 +8,11 @@ export declare const TIME_SERIES_QUERY_KEY = "TimeSeriesQuery";
8
8
  /**
9
9
  * Runs a time series query using a plugin and returns the results.
10
10
  */
11
- export declare const useTimeSeriesQuery: (definition: TimeSeriesQueryDefinition, options?: UseTimeSeriesQueryOptions) => import("@tanstack/react-query").UseQueryResult<TimeSeriesData, unknown>;
11
+ export declare const useTimeSeriesQuery: (definition: TimeSeriesQueryDefinition, options?: UseTimeSeriesQueryOptions, queryOptions?: QueryObserverOptions) => import("@tanstack/react-query").UseQueryResult<TimeSeriesData, unknown>;
12
12
  /**
13
13
  * Runs multiple time series queries using plugins and returns the results.
14
14
  */
15
- export declare function useTimeSeriesQueries(definitions: TimeSeriesQueryDefinition[], options?: UseTimeSeriesQueryOptions): import("@tanstack/react-query").UseQueryResult<TimeSeriesData, unknown>[];
15
+ export declare function useTimeSeriesQueries(definitions: TimeSeriesQueryDefinition[], options?: UseTimeSeriesQueryOptions, queryOptions?: QueryObserverOptions): import("@tanstack/react-query").UseQueryResult<TimeSeriesData, unknown>[];
16
16
  /**
17
17
  * Get active time series queries for query results summary
18
18
  */
@@ -1 +1 @@
1
- {"version":3,"file":"time-series-queries.d.ts","sourceRoot":"","sources":["../../src/runtime/time-series-queries.ts"],"names":[],"mappings":"AAaA,OAAO,EAA+C,UAAU,EAAY,MAAM,uBAAuB,CAAC;AAC1G,OAAO,EAAE,yBAAyB,EAAe,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAAiD,MAAM,UAAU,CAAC;AAM9F,MAAM,WAAW,yBAAyB;IACxC,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,eAAO,MAAM,qBAAqB,oBAAoB,CAAC;AAmDvD;;GAEG;AACH,eAAO,MAAM,kBAAkB,eAAgB,yBAAyB,YAAY,yBAAyB,4EAmB5G,CAAC;AAEF;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,yBAAyB,EAAE,EAAE,OAAO,CAAC,EAAE,yBAAyB,6EA2BjH;AAmBD;;GAEG;AACH,wBAAgB,0BAA0B,0BAIzC;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,UAAU,yBAW3D"}
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,EACrB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,yBAAyB,EAAe,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAAiD,MAAM,UAAU,CAAC;AAM9F,MAAM,WAAW,yBAAyB;IACxC,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,eAAO,MAAM,qBAAqB,oBAAoB,CAAC;AAmDvD;;GAEG;AACH,eAAO,MAAM,kBAAkB,eACjB,yBAAyB,YAC3B,yBAAyB,iBACpB,oBAAoB,4EAoBpC,CAAC;AAEF;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,yBAAyB,EAAE,EACxC,OAAO,CAAC,EAAE,yBAAyB,EACnC,YAAY,CAAC,EAAE,oBAAoB,6EA4BpC;AAmBD;;GAEG;AACH,wBAAgB,0BAA0B,0BAIzC;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,UAAU,yBAW3D"}
@@ -11,7 +11,7 @@
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
13
  import { useQuery, useQueries, useQueryClient } from '@tanstack/react-query';
14
- import { useTemplateVariableValues } from './template-variables';
14
+ import { useVariableValues } from './template-variables';
15
15
  import { useTimeRange } from './TimeRangeProvider';
16
16
  import { useDatasourceStore } from './datasources';
17
17
  import { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';
@@ -46,8 +46,8 @@ function getQueryOptions({ plugin , definition , context }) {
46
46
  let waitToLoad = false;
47
47
  if (variableDependencies) {
48
48
  waitToLoad = variableDependencies.some((v)=>{
49
- var ref;
50
- return (ref = variableState[v]) === null || ref === void 0 ? void 0 : ref.loading;
49
+ var _variableState_v;
50
+ return (_variableState_v = variableState[v]) === null || _variableState_v === void 0 ? void 0 : _variableState_v.loading;
51
51
  });
52
52
  }
53
53
  const queryEnabled = plugin !== undefined && !waitToLoad;
@@ -58,7 +58,7 @@ function getQueryOptions({ plugin , definition , context }) {
58
58
  }
59
59
  /**
60
60
  * Runs a time series query using a plugin and returns the results.
61
- */ export const useTimeSeriesQuery = (definition, options)=>{
61
+ */ export const useTimeSeriesQuery = (definition, options, queryOptions)=>{
62
62
  const { data: plugin } = usePlugin(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);
63
63
  const context = useTimeSeriesQueryContext();
64
64
  const { queryEnabled , queryKey } = getQueryOptions({
@@ -66,8 +66,9 @@ function getQueryOptions({ plugin , definition , context }) {
66
66
  definition,
67
67
  context
68
68
  });
69
+ var _queryOptions_enabled;
69
70
  return useQuery({
70
- enabled: queryEnabled,
71
+ enabled: ((_queryOptions_enabled = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.enabled) !== null && _queryOptions_enabled !== void 0 ? _queryOptions_enabled : true) || queryEnabled,
71
72
  queryKey: queryKey,
72
73
  refetchInterval: context.refreshIntervalInMs > 0 ? context.refreshIntervalInMs : false,
73
74
  queryFn: ()=>{
@@ -86,7 +87,7 @@ function getQueryOptions({ plugin , definition , context }) {
86
87
  };
87
88
  /**
88
89
  * Runs multiple time series queries using plugins and returns the results.
89
- */ export function useTimeSeriesQueries(definitions, options) {
90
+ */ export function useTimeSeriesQueries(definitions, options, queryOptions) {
90
91
  const { getPlugin } = usePluginRegistry();
91
92
  const context = useTimeSeriesQueryContext();
92
93
  const pluginLoaderResponse = usePlugins(TIME_SERIES_QUERY_KEY, definitions.map((d)=>({
@@ -94,15 +95,16 @@ function getQueryOptions({ plugin , definition , context }) {
94
95
  })));
95
96
  return useQueries({
96
97
  queries: definitions.map((definition, idx)=>{
97
- var ref;
98
- const plugin = (ref = pluginLoaderResponse[idx]) === null || ref === void 0 ? void 0 : ref.data;
98
+ var _pluginLoaderResponse_idx;
99
+ const plugin = (_pluginLoaderResponse_idx = pluginLoaderResponse[idx]) === null || _pluginLoaderResponse_idx === void 0 ? void 0 : _pluginLoaderResponse_idx.data;
99
100
  const { queryEnabled , queryKey } = getQueryOptions({
100
101
  plugin,
101
102
  definition,
102
103
  context
103
104
  });
105
+ var _queryOptions_enabled;
104
106
  return {
105
- enabled: queryEnabled,
107
+ enabled: ((_queryOptions_enabled = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.enabled) !== null && _queryOptions_enabled !== void 0 ? _queryOptions_enabled : true) && queryEnabled,
106
108
  queryKey: queryKey,
107
109
  refetchInterval: context.refreshIntervalInMs > 0 ? context.refreshIntervalInMs : undefined,
108
110
  queryFn: async ()=>{
@@ -123,7 +125,7 @@ function getQueryOptions({ plugin , definition , context }) {
123
125
  * Build the time series query context object from data available at runtime
124
126
  */ function useTimeSeriesQueryContext() {
125
127
  const { absoluteTimeRange , refreshKey , refreshIntervalInMs } = useTimeRange();
126
- const variableState = useTemplateVariableValues();
128
+ const variableState = useVariableValues();
127
129
  const datasourceStore = useDatasourceStore();
128
130
  return {
129
131
  timeRange: absoluteTimeRange,
@@ -147,8 +149,8 @@ function getQueryOptions({ plugin , definition , context }) {
147
149
  for (const query of cache.findAll({
148
150
  type: 'active'
149
151
  })){
150
- var ref;
151
- const firstPart = (ref = query.queryKey) === null || ref === void 0 ? void 0 : ref[0];
152
+ var _query_queryKey;
153
+ const firstPart = (_query_queryKey = query.queryKey) === null || _query_queryKey === void 0 ? void 0 : _query_queryKey[0];
152
154
  if ((firstPart === null || firstPart === void 0 ? void 0 : firstPart.kind) && firstPart.kind.startsWith(TIME_SERIES_QUERY_KEY)) {
153
155
  queries.push(query);
154
156
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/runtime/time-series-queries.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { useQuery, useQueries, useQueryClient, Query, QueryCache, QueryKey } from '@tanstack/react-query';\nimport { TimeSeriesQueryDefinition, UnknownSpec, TimeSeriesData } from '@perses-dev/core';\nimport { TimeSeriesDataQuery, TimeSeriesQueryContext, TimeSeriesQueryPlugin } from '../model';\nimport { VariableStateMap, useTemplateVariableValues } from './template-variables';\nimport { useTimeRange } from './TimeRangeProvider';\nimport { useDatasourceStore } from './datasources';\nimport { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';\n\nexport interface UseTimeSeriesQueryOptions {\n suggestedStepMs?: number;\n}\n\nexport const TIME_SERIES_QUERY_KEY = 'TimeSeriesQuery';\n\n/**\n * Returns a serialized string of the current state of variable values.\n */\nfunction getVariableValuesKey(v: VariableStateMap) {\n return Object.values(v)\n .map((v) => JSON.stringify(v.value))\n .join(',');\n}\n\nfunction filterVariableStateMap(v: VariableStateMap, names?: string[]) {\n if (!names) {\n return v;\n }\n return Object.fromEntries(Object.entries(v).filter(([name]) => names.includes(name)));\n}\n\nfunction getQueryOptions({\n plugin,\n definition,\n context,\n}: {\n plugin?: TimeSeriesQueryPlugin;\n definition: TimeSeriesQueryDefinition;\n context: TimeSeriesQueryContext;\n}) {\n const { timeRange, datasourceStore, suggestedStepMs, variableState, refreshKey } = context;\n\n const dependencies = plugin?.dependsOn ? plugin.dependsOn(definition.spec.plugin.spec, context) : {};\n const variableDependencies = dependencies?.variables;\n\n // Determine queryKey\n const filteredVariabledState = filterVariableStateMap(variableState, variableDependencies);\n const variablesValueKey = getVariableValuesKey(filteredVariabledState);\n const queryKey = [definition, timeRange, datasourceStore, suggestedStepMs, variablesValueKey, refreshKey] as const;\n\n // Determine queryEnabled\n let waitToLoad = false;\n if (variableDependencies) {\n waitToLoad = variableDependencies.some((v) => variableState[v]?.loading);\n }\n\n const queryEnabled = plugin !== undefined && !waitToLoad;\n\n return {\n queryKey,\n queryEnabled,\n };\n}\n\n/**\n * Runs a time series query using a plugin and returns the results.\n */\nexport const useTimeSeriesQuery = (definition: TimeSeriesQueryDefinition, options?: UseTimeSeriesQueryOptions) => {\n const { data: plugin } = usePlugin(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);\n const context = useTimeSeriesQueryContext();\n\n const { queryEnabled, queryKey } = getQueryOptions({ plugin, definition, context });\n return useQuery({\n enabled: queryEnabled,\n queryKey: queryKey,\n refetchInterval: context.refreshIntervalInMs > 0 ? context.refreshIntervalInMs : false,\n queryFn: () => {\n // The 'enabled' option should prevent this from happening, but make TypeScript happy by checking\n if (plugin === undefined) {\n throw new Error('Expected plugin to be loaded');\n }\n // Keep options out of query key so we don't re-run queries because suggested step changes\n const ctx: TimeSeriesQueryContext = { ...context, suggestedStepMs: options?.suggestedStepMs };\n return plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);\n },\n });\n};\n\n/**\n * Runs multiple time series queries using plugins and returns the results.\n */\nexport function useTimeSeriesQueries(definitions: TimeSeriesQueryDefinition[], options?: UseTimeSeriesQueryOptions) {\n const { getPlugin } = usePluginRegistry();\n const context = useTimeSeriesQueryContext();\n\n const pluginLoaderResponse = usePlugins(\n TIME_SERIES_QUERY_KEY,\n definitions.map((d) => ({ kind: d.spec.plugin.kind }))\n );\n\n return useQueries({\n queries: definitions.map((definition, idx) => {\n const plugin = pluginLoaderResponse[idx]?.data;\n const { queryEnabled, queryKey } = getQueryOptions({ plugin, definition, context });\n return {\n enabled: queryEnabled,\n queryKey: queryKey,\n refetchInterval: context.refreshIntervalInMs > 0 ? context.refreshIntervalInMs : undefined,\n queryFn: async () => {\n // Keep options out of query key so we don't re-run queries because suggested step changes\n const ctx: TimeSeriesQueryContext = { ...context, suggestedStepMs: options?.suggestedStepMs };\n const plugin = await getPlugin(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);\n const data = await plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);\n return data;\n },\n };\n }),\n });\n}\n\n/**\n * Build the time series query context object from data available at runtime\n */\nfunction useTimeSeriesQueryContext(): TimeSeriesQueryContext {\n const { absoluteTimeRange, refreshKey, refreshIntervalInMs } = useTimeRange();\n const variableState = useTemplateVariableValues();\n const datasourceStore = useDatasourceStore();\n\n return {\n timeRange: absoluteTimeRange,\n variableState,\n datasourceStore,\n refreshKey,\n refreshIntervalInMs: refreshIntervalInMs,\n };\n}\n\n/**\n * Get active time series queries for query results summary\n */\nexport function useActiveTimeSeriesQueries() {\n const queryClient = useQueryClient();\n const queryCache = queryClient.getQueryCache();\n return getActiveTimeSeriesQueries(queryCache);\n}\n\n/**\n * Filter all cached queries down to only active time series queries\n */\nexport function getActiveTimeSeriesQueries(cache: QueryCache) {\n const queries: TimeSeriesDataQuery[] = [];\n\n for (const query of cache.findAll({ type: 'active' })) {\n const firstPart = query.queryKey?.[0] as UnknownSpec;\n if (firstPart?.kind && (firstPart.kind as string).startsWith(TIME_SERIES_QUERY_KEY)) {\n queries.push(query as Query<TimeSeriesData, unknown, TimeSeriesData, QueryKey>);\n }\n }\n\n return queries;\n}\n"],"names":["useQuery","useQueries","useQueryClient","useTemplateVariableValues","useTimeRange","useDatasourceStore","usePlugin","usePluginRegistry","usePlugins","TIME_SERIES_QUERY_KEY","getVariableValuesKey","v","Object","values","map","JSON","stringify","value","join","filterVariableStateMap","names","fromEntries","entries","filter","name","includes","getQueryOptions","plugin","definition","context","timeRange","datasourceStore","suggestedStepMs","variableState","refreshKey","dependencies","dependsOn","spec","variableDependencies","variables","filteredVariabledState","variablesValueKey","queryKey","waitToLoad","some","loading","queryEnabled","undefined","useTimeSeriesQuery","options","data","kind","useTimeSeriesQueryContext","enabled","refetchInterval","refreshIntervalInMs","queryFn","Error","ctx","getTimeSeriesData","useTimeSeriesQueries","definitions","getPlugin","pluginLoaderResponse","d","queries","idx","absoluteTimeRange","useActiveTimeSeriesQueries","queryClient","queryCache","getQueryCache","getActiveTimeSeriesQueries","cache","query","findAll","type","firstPart","startsWith","push"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,QAAQ,EAAEC,UAAU,EAAEC,cAAc,QAAqC,uBAAuB,CAAC;AAG1G,SAA2BC,yBAAyB,QAAQ,sBAAsB,CAAC;AACnF,SAASC,YAAY,QAAQ,qBAAqB,CAAC;AACnD,SAASC,kBAAkB,QAAQ,eAAe,CAAC;AACnD,S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fG,UAAU;QACVqB,mBAAmB,EAAEA,mBAAmB;KACzC,CAAC;AACJ,CAAC;AAED;;CAEC,GACD,OAAO,SAASa,0BAA0B,GAAG;IAC3C,MAAMC,WAAW,GAAGnE,cAAc,EAAE,AAAC;IACrC,MAAMoE,UAAU,GAAGD,WAAW,CAACE,aAAa,EAAE,AAAC;IAC/C,OAAOC,0BAA0B,CAACF,UAAU,CAAC,CAAC;AAChD,CAAC;AAED;;CAEC,GACD,OAAO,SAASE,0BAA0B,CAACC,KAAiB,EAAE;IAC5D,MAAMR,OAAO,GAA0B,EAAE,AAAC;IAE1C,KAAK,MAAMS,KAAK,IAAID,KAAK,CAACE,OAAO,CAAC;QAAEC,IAAI,EAAE,QAAQ;KAAE,CAAC,CAAE;YACnCF,GAAc;QAAhC,MAAMG,SAAS,GAAGH,CAAAA,GAAc,GAAdA,KAAK,CAAChC,QAAQ,cAAdgC,GAAc,WAAK,GAAnBA,KAAAA,CAAmB,GAAnBA,GAAc,AAAE,CAAC,CAAC,CAAC,AAAe,AAAC;QACrD,IAAIG,CAAAA,SAAS,aAATA,SAAS,WAAM,GAAfA,KAAAA,CAAe,GAAfA,SAAS,CAAE1B,IAAI,CAAA,IAAI,AAAC0B,SAAS,CAAC1B,IAAI,CAAY2B,UAAU,CAACrE,qBAAqB,CAAC,EAAE;YACnFwD,OAAO,CAACc,IAAI,CAACL,KAAK,CAA6D,CAAC;QAClF,CAAC;IACH,CAAC;IAED,OAAOT,OAAO,CAAC;AACjB,CAAC"}
1
+ {"version":3,"sources":["../../src/runtime/time-series-queries.ts"],"sourcesContent":["// Copyright 2023 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport {\n useQuery,\n useQueries,\n useQueryClient,\n Query,\n QueryCache,\n QueryKey,\n QueryObserverOptions,\n} from '@tanstack/react-query';\nimport { TimeSeriesQueryDefinition, UnknownSpec, TimeSeriesData } from '@perses-dev/core';\nimport { TimeSeriesDataQuery, TimeSeriesQueryContext, TimeSeriesQueryPlugin } from '../model';\nimport { VariableStateMap, useVariableValues } from './template-variables';\nimport { useTimeRange } from './TimeRangeProvider';\nimport { useDatasourceStore } from './datasources';\nimport { usePlugin, usePluginRegistry, usePlugins } from './plugin-registry';\n\nexport interface UseTimeSeriesQueryOptions {\n suggestedStepMs?: number;\n}\n\nexport const TIME_SERIES_QUERY_KEY = 'TimeSeriesQuery';\n\n/**\n * Returns a serialized string of the current state of variable values.\n */\nfunction getVariableValuesKey(v: VariableStateMap) {\n return Object.values(v)\n .map((v) => JSON.stringify(v.value))\n .join(',');\n}\n\nfunction filterVariableStateMap(v: VariableStateMap, names?: string[]) {\n if (!names) {\n return v;\n }\n return Object.fromEntries(Object.entries(v).filter(([name]) => names.includes(name)));\n}\n\nfunction getQueryOptions({\n plugin,\n definition,\n context,\n}: {\n plugin?: TimeSeriesQueryPlugin;\n definition: TimeSeriesQueryDefinition;\n context: TimeSeriesQueryContext;\n}) {\n const { timeRange, datasourceStore, suggestedStepMs, variableState, refreshKey } = context;\n\n const dependencies = plugin?.dependsOn ? plugin.dependsOn(definition.spec.plugin.spec, context) : {};\n const variableDependencies = dependencies?.variables;\n\n // Determine queryKey\n const filteredVariabledState = filterVariableStateMap(variableState, variableDependencies);\n const variablesValueKey = getVariableValuesKey(filteredVariabledState);\n const queryKey = [definition, timeRange, datasourceStore, suggestedStepMs, variablesValueKey, refreshKey] as const;\n\n // Determine queryEnabled\n let waitToLoad = false;\n if (variableDependencies) {\n waitToLoad = variableDependencies.some((v) => variableState[v]?.loading);\n }\n\n const queryEnabled = plugin !== undefined && !waitToLoad;\n\n return {\n queryKey,\n queryEnabled,\n };\n}\n\n/**\n * Runs a time series query using a plugin and returns the results.\n */\nexport const useTimeSeriesQuery = (\n definition: TimeSeriesQueryDefinition,\n options?: UseTimeSeriesQueryOptions,\n queryOptions?: QueryObserverOptions\n) => {\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 refetchInterval: context.refreshIntervalInMs > 0 ? context.refreshIntervalInMs : false,\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?: QueryObserverOptions\n) {\n const { getPlugin } = usePluginRegistry();\n const context = useTimeSeriesQueryContext();\n\n const pluginLoaderResponse = usePlugins(\n TIME_SERIES_QUERY_KEY,\n definitions.map((d) => ({ kind: d.spec.plugin.kind }))\n );\n\n return useQueries({\n queries: definitions.map((definition, idx) => {\n const plugin = pluginLoaderResponse[idx]?.data;\n const { queryEnabled, queryKey } = getQueryOptions({ plugin, definition, context });\n return {\n enabled: (queryOptions?.enabled ?? true) && queryEnabled,\n queryKey: queryKey,\n refetchInterval: context.refreshIntervalInMs > 0 ? context.refreshIntervalInMs : undefined,\n queryFn: async () => {\n // Keep options out of query key so we don't re-run queries because suggested step changes\n const ctx: TimeSeriesQueryContext = { ...context, suggestedStepMs: options?.suggestedStepMs };\n const plugin = await getPlugin(TIME_SERIES_QUERY_KEY, definition.spec.plugin.kind);\n const data = await plugin.getTimeSeriesData(definition.spec.plugin.spec, ctx);\n return data;\n },\n };\n }),\n });\n}\n\n/**\n * Build the time series query context object from data available at runtime\n */\nfunction useTimeSeriesQueryContext(): TimeSeriesQueryContext {\n const { absoluteTimeRange, refreshKey, refreshIntervalInMs } = useTimeRange();\n const variableState = useVariableValues();\n const datasourceStore = useDatasourceStore();\n\n return {\n timeRange: absoluteTimeRange,\n variableState,\n datasourceStore,\n refreshKey,\n refreshIntervalInMs: refreshIntervalInMs,\n };\n}\n\n/**\n * Get active time series queries for query results summary\n */\nexport function useActiveTimeSeriesQueries() {\n const queryClient = useQueryClient();\n const queryCache = queryClient.getQueryCache();\n return getActiveTimeSeriesQueries(queryCache);\n}\n\n/**\n * Filter all cached queries down to only active time series queries\n */\nexport function getActiveTimeSeriesQueries(cache: QueryCache) {\n const queries: TimeSeriesDataQuery[] = [];\n\n for (const query of cache.findAll({ type: 'active' })) {\n const firstPart = query.queryKey?.[0] as UnknownSpec;\n if (firstPart?.kind && (firstPart.kind as string).startsWith(TIME_SERIES_QUERY_KEY)) {\n queries.push(query as Query<TimeSeriesData, unknown, TimeSeriesData, QueryKey>);\n }\n }\n\n return queries;\n}\n"],"names":["useQuery","useQueries","useQueryClient","useVariableValues","useTimeRange","useDatasourceStore","usePlugin","usePluginRegistry","usePlugins","TIME_SERIES_QUERY_KEY","getVariableValuesKey","v","Object","values","map","JSON","stringify","value","join","filterVariableStateMap","names","fromEntries","entries","filter","name","includes","getQueryOptions","plugin","definition","context","timeRange","datasourceStore","suggestedStepMs","variableState","refreshKey","dependencies","dependsOn","spec","variableDependencies","variables","filteredVariabledState","variablesValueKey","queryKey","waitToLoad","some","loading","queryEnabled","undefined","useTimeSeriesQuery","options","queryOptions","data","kind","useTimeSeriesQueryContext","enabled","refetchInterval","refreshIntervalInMs","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,QAKT,wBAAwB;AAG/B,SAA2BC,iBAAiB,QAAQ,uBAAuB;AAC3E,SAASC,YAAY,QAAQ,sBAAsB;AACnD,SAASC,kBAAkB,QAAQ,gBAAgB;AACnD,SAASC,SAAS,EAAEC,iBAAiB,EAAEC,UAAU,QAAQ,oBAAoB;AAM7E,OAAO,MAAMC,wBAAwB,kBAAkB;AAEvD;;CAEC,GACD,SAASC,qBAAqBC,CAAmB;IAC/C,OAAOC,OAAOC,OAAOF,GAClBG,IAAI,CAACH,IAAMI,KAAKC,UAAUL,EAAEM,QAC5BC,KAAK;AACV;AAEA,SAASC,uBAAuBR,CAAmB,EAAES,KAAgB;IACnE,IAAI,CAACA,OAAO;QACV,OAAOT;IACT;IACA,OAAOC,OAAOS,YAAYT,OAAOU,QAAQX,GAAGY,OAAO,CAAC,CAACC,KAAK,GAAKJ,MAAMK,SAASD;AAChF;AAEA,SAASE,gBAAgB,EACvBC,OAAM,EACNC,WAAU,EACVC,QAAO,EAKR;IACC,MAAM,EAAEC,UAAS,EAAEC,gBAAe,EAAEC,gBAAe,EAAEC,cAAa,EAAEC,WAAU,EAAE,GAAGL;IAEnF,MAAMM,eAAeR,CAAAA,mBAAAA,oBAAAA,KAAAA,IAAAA,OAAQS,SAAQ,IAAIT,OAAOS,UAAUR,WAAWS,KAAKV,OAAOU,MAAMR,WAAW,CAAC;IACnG,MAAMS,uBAAuBH,yBAAAA,0BAAAA,KAAAA,IAAAA,aAAcI;IAE3C,qBAAqB;I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