@grafana/scenes 6.29.0--canary.1197.16645626691.0 → 6.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -8508,6 +8508,253 @@ const createProperty = (color) => {
8508
8508
  };
8509
8509
  };
8510
8510
 
8511
+ class SceneDataLayerSetBase extends SceneObjectBase {
8512
+ constructor() {
8513
+ super(...arguments);
8514
+ /** Mark it as a data layer */
8515
+ this.isDataLayer = true;
8516
+ /**
8517
+ * Subject to emit results to.
8518
+ */
8519
+ this._results = new rxjs.ReplaySubject(1);
8520
+ this._dataLayersMerger = new DataLayersMerger();
8521
+ }
8522
+ subscribeToAllLayers(layers) {
8523
+ if (layers.length > 0) {
8524
+ this.querySub = this._dataLayersMerger.getMergedStream(layers).subscribe(this._onLayerUpdateReceived.bind(this));
8525
+ } else {
8526
+ this._results.next({ origin: this, data: emptyPanelData });
8527
+ this.setStateHelper({ data: emptyPanelData });
8528
+ }
8529
+ }
8530
+ _onLayerUpdateReceived(results) {
8531
+ var _a;
8532
+ let series = [];
8533
+ for (const result of results) {
8534
+ if ((_a = result.data) == null ? void 0 : _a.series) {
8535
+ series = series.concat(result.data.series);
8536
+ }
8537
+ }
8538
+ const combinedData = { ...emptyPanelData, series };
8539
+ this._results.next({ origin: this, data: combinedData });
8540
+ this.setStateHelper({ data: combinedData });
8541
+ }
8542
+ getResultsStream() {
8543
+ return this._results;
8544
+ }
8545
+ cancelQuery() {
8546
+ var _a;
8547
+ (_a = this.querySub) == null ? void 0 : _a.unsubscribe();
8548
+ }
8549
+ /**
8550
+ * This helper function is to counter the contravariance of setState
8551
+ */
8552
+ setStateHelper(state) {
8553
+ setBaseClassState(this, state);
8554
+ }
8555
+ }
8556
+ class SceneDataLayerSet extends SceneDataLayerSetBase {
8557
+ constructor(state) {
8558
+ var _a, _b;
8559
+ super({
8560
+ name: (_a = state.name) != null ? _a : "Data layers",
8561
+ layers: (_b = state.layers) != null ? _b : []
8562
+ });
8563
+ this.addActivationHandler(() => this._onActivate());
8564
+ }
8565
+ _onActivate() {
8566
+ this._subs.add(
8567
+ this.subscribeToState((newState, oldState) => {
8568
+ var _a;
8569
+ if (newState.layers !== oldState.layers) {
8570
+ (_a = this.querySub) == null ? void 0 : _a.unsubscribe();
8571
+ this.subscribeToAllLayers(newState.layers);
8572
+ }
8573
+ })
8574
+ );
8575
+ this.subscribeToAllLayers(this.state.layers);
8576
+ return () => {
8577
+ var _a;
8578
+ (_a = this.querySub) == null ? void 0 : _a.unsubscribe();
8579
+ };
8580
+ }
8581
+ }
8582
+ SceneDataLayerSet.Component = ({ model }) => {
8583
+ const { layers } = model.useState();
8584
+ return /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, layers.map((layer) => /* @__PURE__ */ React__default.default.createElement(layer.Component, { model: layer, key: layer.state.key })));
8585
+ };
8586
+
8587
+ class SceneDataTransformer extends SceneObjectBase {
8588
+ constructor(state) {
8589
+ super(state);
8590
+ this._results = new rxjs.ReplaySubject(1);
8591
+ /**
8592
+ * Scan transformations for variable usage and re-process transforms when a variable values change
8593
+ */
8594
+ this._variableDependency = new VariableDependencyConfig(
8595
+ this,
8596
+ {
8597
+ statePaths: ["transformations"],
8598
+ onReferencedVariableValueChanged: () => this.reprocessTransformations()
8599
+ }
8600
+ );
8601
+ this.addActivationHandler(() => this.activationHandler());
8602
+ }
8603
+ activationHandler() {
8604
+ const sourceData = this.getSourceData();
8605
+ this._subs.add(sourceData.subscribeToState((state) => this.transform(state.data)));
8606
+ if (sourceData.state.data) {
8607
+ this.transform(sourceData.state.data);
8608
+ }
8609
+ return () => {
8610
+ if (this._transformSub) {
8611
+ this._transformSub.unsubscribe();
8612
+ }
8613
+ };
8614
+ }
8615
+ getSourceData() {
8616
+ if (this.state.$data) {
8617
+ if (this.state.$data instanceof SceneDataLayerSet) {
8618
+ throw new Error("SceneDataLayerSet can not be used as data provider for SceneDataTransformer.");
8619
+ }
8620
+ return this.state.$data;
8621
+ }
8622
+ if (!this.parent || !this.parent.parent) {
8623
+ throw new Error("SceneDataTransformer must either have $data set on it or have a parent.parent with $data");
8624
+ }
8625
+ return sceneGraph.getData(this.parent.parent);
8626
+ }
8627
+ setContainerWidth(width) {
8628
+ if (this.state.$data && this.state.$data.setContainerWidth) {
8629
+ this.state.$data.setContainerWidth(width);
8630
+ }
8631
+ }
8632
+ isDataReadyToDisplay() {
8633
+ const dataObject = this.getSourceData();
8634
+ if (dataObject.isDataReadyToDisplay) {
8635
+ return dataObject.isDataReadyToDisplay();
8636
+ }
8637
+ return true;
8638
+ }
8639
+ reprocessTransformations() {
8640
+ this.transform(this.getSourceData().state.data, true);
8641
+ }
8642
+ cancelQuery() {
8643
+ var _a, _b;
8644
+ (_b = (_a = this.getSourceData()).cancelQuery) == null ? void 0 : _b.call(_a);
8645
+ }
8646
+ getResultsStream() {
8647
+ return this._results;
8648
+ }
8649
+ clone(withState) {
8650
+ const clone = super.clone(withState);
8651
+ if (this._prevDataFromSource) {
8652
+ clone["_prevDataFromSource"] = this._prevDataFromSource;
8653
+ }
8654
+ return clone;
8655
+ }
8656
+ haveAlreadyTransformedData(data) {
8657
+ if (!this._prevDataFromSource) {
8658
+ return false;
8659
+ }
8660
+ if (data === this._prevDataFromSource) {
8661
+ return true;
8662
+ }
8663
+ const { series, annotations } = this._prevDataFromSource;
8664
+ if (data.series === series && data.annotations === annotations) {
8665
+ if (this.state.data && data.state !== this.state.data.state) {
8666
+ this.setState({ data: { ...this.state.data, state: data.state } });
8667
+ }
8668
+ return true;
8669
+ }
8670
+ return false;
8671
+ }
8672
+ transform(data$1, force = false) {
8673
+ var _a;
8674
+ if (this.state.transformations.length === 0 || !data$1) {
8675
+ this._prevDataFromSource = data$1;
8676
+ this.setState({ data: data$1 });
8677
+ if (data$1) {
8678
+ this._results.next({ origin: this, data: data$1 });
8679
+ }
8680
+ return;
8681
+ }
8682
+ if (!force && this.haveAlreadyTransformedData(data$1)) {
8683
+ return;
8684
+ }
8685
+ let interpolatedTransformations = this._interpolateVariablesInTransformationConfigs(data$1);
8686
+ const seriesTransformations = interpolatedTransformations.filter((transformation) => {
8687
+ if ("options" in transformation || "topic" in transformation) {
8688
+ return transformation.topic == null || transformation.topic === data.DataTopic.Series;
8689
+ }
8690
+ return true;
8691
+ }).map((transformation) => "operator" in transformation ? transformation.operator : transformation);
8692
+ const annotationsTransformations = interpolatedTransformations.filter((transformation) => {
8693
+ if ("options" in transformation || "topic" in transformation) {
8694
+ return transformation.topic === data.DataTopic.Annotations;
8695
+ }
8696
+ return false;
8697
+ }).map((transformation) => "operator" in transformation ? transformation.operator : transformation);
8698
+ if (this._transformSub) {
8699
+ this._transformSub.unsubscribe();
8700
+ }
8701
+ const ctx = {
8702
+ interpolate: (value) => {
8703
+ var _a2;
8704
+ return sceneGraph.interpolate(this, value, (_a2 = data$1.request) == null ? void 0 : _a2.scopedVars);
8705
+ }
8706
+ };
8707
+ let streams = [data.transformDataFrame(seriesTransformations, data$1.series, ctx)];
8708
+ if (data$1.annotations && data$1.annotations.length > 0 && annotationsTransformations.length > 0) {
8709
+ streams.push(data.transformDataFrame(annotationsTransformations, (_a = data$1.annotations) != null ? _a : []));
8710
+ }
8711
+ this._transformSub = rxjs.forkJoin(streams).pipe(
8712
+ rxjs.map((values) => {
8713
+ const transformedSeries = values[0];
8714
+ const transformedAnnotations = values[1];
8715
+ return {
8716
+ ...data$1,
8717
+ series: transformedSeries,
8718
+ annotations: transformedAnnotations != null ? transformedAnnotations : data$1.annotations
8719
+ };
8720
+ }),
8721
+ rxjs.catchError((err) => {
8722
+ var _a2;
8723
+ console.error("Error transforming data: ", err);
8724
+ const sourceErr = ((_a2 = this.getSourceData().state.data) == null ? void 0 : _a2.errors) || [];
8725
+ const transformationError = runtime.toDataQueryError(err);
8726
+ transformationError.message = `Error transforming data: ${transformationError.message}`;
8727
+ const result = {
8728
+ ...data$1,
8729
+ state: data.LoadingState.Error,
8730
+ // Combine transformation error with upstream errors
8731
+ errors: [...sourceErr, transformationError]
8732
+ };
8733
+ return rxjs.of(result);
8734
+ })
8735
+ ).subscribe((transformedData) => {
8736
+ this.setState({ data: transformedData });
8737
+ this._results.next({ origin: this, data: transformedData });
8738
+ this._prevDataFromSource = data$1;
8739
+ });
8740
+ }
8741
+ _interpolateVariablesInTransformationConfigs(data) {
8742
+ var _a;
8743
+ const transformations = this.state.transformations;
8744
+ if (this._variableDependency.getNames().size === 0) {
8745
+ return transformations;
8746
+ }
8747
+ const onlyObjects = transformations.every((t) => typeof t === "object");
8748
+ if (onlyObjects) {
8749
+ return JSON.parse(sceneGraph.interpolate(this, JSON.stringify(transformations), (_a = data.request) == null ? void 0 : _a.scopedVars));
8750
+ }
8751
+ return transformations.map((t) => {
8752
+ var _a2;
8753
+ return typeof t === "object" ? JSON.parse(sceneGraph.interpolate(this, JSON.stringify(t), (_a2 = data.request) == null ? void 0 : _a2.scopedVars)) : t;
8754
+ });
8755
+ }
8756
+ }
8757
+
8511
8758
  class VizPanel extends SceneObjectBase {
8512
8759
  constructor(state) {
8513
8760
  var _a;
@@ -8746,6 +8993,18 @@ class VizPanel extends SceneObjectBase {
8746
8993
  if (plugin.onPanelMigration && currentVersion !== pluginVersion && !isAfterPluginChange) {
8747
8994
  panel.options = await plugin.onPanelMigration(panel);
8748
8995
  }
8996
+ let $data = this.state.$data;
8997
+ if (panel.transformations && $data) {
8998
+ if ($data instanceof SceneDataTransformer) {
8999
+ $data.setState({ transformations: panel.transformations });
9000
+ } else if ($data instanceof SceneQueryRunner) {
9001
+ $data.clearParent();
9002
+ $data = new SceneDataTransformer({
9003
+ transformations: panel.transformations,
9004
+ $data
9005
+ });
9006
+ }
9007
+ }
8749
9008
  const withDefaults = data.getPanelOptionsWithDefaults({
8750
9009
  plugin,
8751
9010
  currentOptions: panel.options,
@@ -8754,6 +9013,7 @@ class VizPanel extends SceneObjectBase {
8754
9013
  });
8755
9014
  this._plugin = plugin;
8756
9015
  this.setState({
9016
+ $data,
8757
9017
  options: withDefaults.options,
8758
9018
  fieldConfig: withDefaults.fieldConfig,
8759
9019
  pluginVersion: currentVersion,
@@ -9653,253 +9913,6 @@ class DataProviderProxy extends SceneObjectBase {
9653
9913
  }
9654
9914
  }
9655
9915
 
9656
- class SceneDataLayerSetBase extends SceneObjectBase {
9657
- constructor() {
9658
- super(...arguments);
9659
- /** Mark it as a data layer */
9660
- this.isDataLayer = true;
9661
- /**
9662
- * Subject to emit results to.
9663
- */
9664
- this._results = new rxjs.ReplaySubject(1);
9665
- this._dataLayersMerger = new DataLayersMerger();
9666
- }
9667
- subscribeToAllLayers(layers) {
9668
- if (layers.length > 0) {
9669
- this.querySub = this._dataLayersMerger.getMergedStream(layers).subscribe(this._onLayerUpdateReceived.bind(this));
9670
- } else {
9671
- this._results.next({ origin: this, data: emptyPanelData });
9672
- this.setStateHelper({ data: emptyPanelData });
9673
- }
9674
- }
9675
- _onLayerUpdateReceived(results) {
9676
- var _a;
9677
- let series = [];
9678
- for (const result of results) {
9679
- if ((_a = result.data) == null ? void 0 : _a.series) {
9680
- series = series.concat(result.data.series);
9681
- }
9682
- }
9683
- const combinedData = { ...emptyPanelData, series };
9684
- this._results.next({ origin: this, data: combinedData });
9685
- this.setStateHelper({ data: combinedData });
9686
- }
9687
- getResultsStream() {
9688
- return this._results;
9689
- }
9690
- cancelQuery() {
9691
- var _a;
9692
- (_a = this.querySub) == null ? void 0 : _a.unsubscribe();
9693
- }
9694
- /**
9695
- * This helper function is to counter the contravariance of setState
9696
- */
9697
- setStateHelper(state) {
9698
- setBaseClassState(this, state);
9699
- }
9700
- }
9701
- class SceneDataLayerSet extends SceneDataLayerSetBase {
9702
- constructor(state) {
9703
- var _a, _b;
9704
- super({
9705
- name: (_a = state.name) != null ? _a : "Data layers",
9706
- layers: (_b = state.layers) != null ? _b : []
9707
- });
9708
- this.addActivationHandler(() => this._onActivate());
9709
- }
9710
- _onActivate() {
9711
- this._subs.add(
9712
- this.subscribeToState((newState, oldState) => {
9713
- var _a;
9714
- if (newState.layers !== oldState.layers) {
9715
- (_a = this.querySub) == null ? void 0 : _a.unsubscribe();
9716
- this.subscribeToAllLayers(newState.layers);
9717
- }
9718
- })
9719
- );
9720
- this.subscribeToAllLayers(this.state.layers);
9721
- return () => {
9722
- var _a;
9723
- (_a = this.querySub) == null ? void 0 : _a.unsubscribe();
9724
- };
9725
- }
9726
- }
9727
- SceneDataLayerSet.Component = ({ model }) => {
9728
- const { layers } = model.useState();
9729
- return /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, layers.map((layer) => /* @__PURE__ */ React__default.default.createElement(layer.Component, { model: layer, key: layer.state.key })));
9730
- };
9731
-
9732
- class SceneDataTransformer extends SceneObjectBase {
9733
- constructor(state) {
9734
- super(state);
9735
- this._results = new rxjs.ReplaySubject(1);
9736
- /**
9737
- * Scan transformations for variable usage and re-process transforms when a variable values change
9738
- */
9739
- this._variableDependency = new VariableDependencyConfig(
9740
- this,
9741
- {
9742
- statePaths: ["transformations"],
9743
- onReferencedVariableValueChanged: () => this.reprocessTransformations()
9744
- }
9745
- );
9746
- this.addActivationHandler(() => this.activationHandler());
9747
- }
9748
- activationHandler() {
9749
- const sourceData = this.getSourceData();
9750
- this._subs.add(sourceData.subscribeToState((state) => this.transform(state.data)));
9751
- if (sourceData.state.data) {
9752
- this.transform(sourceData.state.data);
9753
- }
9754
- return () => {
9755
- if (this._transformSub) {
9756
- this._transformSub.unsubscribe();
9757
- }
9758
- };
9759
- }
9760
- getSourceData() {
9761
- if (this.state.$data) {
9762
- if (this.state.$data instanceof SceneDataLayerSet) {
9763
- throw new Error("SceneDataLayerSet can not be used as data provider for SceneDataTransformer.");
9764
- }
9765
- return this.state.$data;
9766
- }
9767
- if (!this.parent || !this.parent.parent) {
9768
- throw new Error("SceneDataTransformer must either have $data set on it or have a parent.parent with $data");
9769
- }
9770
- return sceneGraph.getData(this.parent.parent);
9771
- }
9772
- setContainerWidth(width) {
9773
- if (this.state.$data && this.state.$data.setContainerWidth) {
9774
- this.state.$data.setContainerWidth(width);
9775
- }
9776
- }
9777
- isDataReadyToDisplay() {
9778
- const dataObject = this.getSourceData();
9779
- if (dataObject.isDataReadyToDisplay) {
9780
- return dataObject.isDataReadyToDisplay();
9781
- }
9782
- return true;
9783
- }
9784
- reprocessTransformations() {
9785
- this.transform(this.getSourceData().state.data, true);
9786
- }
9787
- cancelQuery() {
9788
- var _a, _b;
9789
- (_b = (_a = this.getSourceData()).cancelQuery) == null ? void 0 : _b.call(_a);
9790
- }
9791
- getResultsStream() {
9792
- return this._results;
9793
- }
9794
- clone(withState) {
9795
- const clone = super.clone(withState);
9796
- if (this._prevDataFromSource) {
9797
- clone["_prevDataFromSource"] = this._prevDataFromSource;
9798
- }
9799
- return clone;
9800
- }
9801
- haveAlreadyTransformedData(data) {
9802
- if (!this._prevDataFromSource) {
9803
- return false;
9804
- }
9805
- if (data === this._prevDataFromSource) {
9806
- return true;
9807
- }
9808
- const { series, annotations } = this._prevDataFromSource;
9809
- if (data.series === series && data.annotations === annotations) {
9810
- if (this.state.data && data.state !== this.state.data.state) {
9811
- this.setState({ data: { ...this.state.data, state: data.state } });
9812
- }
9813
- return true;
9814
- }
9815
- return false;
9816
- }
9817
- transform(data$1, force = false) {
9818
- var _a;
9819
- if (this.state.transformations.length === 0 || !data$1) {
9820
- this._prevDataFromSource = data$1;
9821
- this.setState({ data: data$1 });
9822
- if (data$1) {
9823
- this._results.next({ origin: this, data: data$1 });
9824
- }
9825
- return;
9826
- }
9827
- if (!force && this.haveAlreadyTransformedData(data$1)) {
9828
- return;
9829
- }
9830
- let interpolatedTransformations = this._interpolateVariablesInTransformationConfigs(data$1);
9831
- const seriesTransformations = interpolatedTransformations.filter((transformation) => {
9832
- if ("options" in transformation || "topic" in transformation) {
9833
- return transformation.topic == null || transformation.topic === data.DataTopic.Series;
9834
- }
9835
- return true;
9836
- }).map((transformation) => "operator" in transformation ? transformation.operator : transformation);
9837
- const annotationsTransformations = interpolatedTransformations.filter((transformation) => {
9838
- if ("options" in transformation || "topic" in transformation) {
9839
- return transformation.topic === data.DataTopic.Annotations;
9840
- }
9841
- return false;
9842
- }).map((transformation) => "operator" in transformation ? transformation.operator : transformation);
9843
- if (this._transformSub) {
9844
- this._transformSub.unsubscribe();
9845
- }
9846
- const ctx = {
9847
- interpolate: (value) => {
9848
- var _a2;
9849
- return sceneGraph.interpolate(this, value, (_a2 = data$1.request) == null ? void 0 : _a2.scopedVars);
9850
- }
9851
- };
9852
- let streams = [data.transformDataFrame(seriesTransformations, data$1.series, ctx)];
9853
- if (data$1.annotations && data$1.annotations.length > 0 && annotationsTransformations.length > 0) {
9854
- streams.push(data.transformDataFrame(annotationsTransformations, (_a = data$1.annotations) != null ? _a : []));
9855
- }
9856
- this._transformSub = rxjs.forkJoin(streams).pipe(
9857
- rxjs.map((values) => {
9858
- const transformedSeries = values[0];
9859
- const transformedAnnotations = values[1];
9860
- return {
9861
- ...data$1,
9862
- series: transformedSeries,
9863
- annotations: transformedAnnotations != null ? transformedAnnotations : data$1.annotations
9864
- };
9865
- }),
9866
- rxjs.catchError((err) => {
9867
- var _a2;
9868
- console.error("Error transforming data: ", err);
9869
- const sourceErr = ((_a2 = this.getSourceData().state.data) == null ? void 0 : _a2.errors) || [];
9870
- const transformationError = runtime.toDataQueryError(err);
9871
- transformationError.message = `Error transforming data: ${transformationError.message}`;
9872
- const result = {
9873
- ...data$1,
9874
- state: data.LoadingState.Error,
9875
- // Combine transformation error with upstream errors
9876
- errors: [...sourceErr, transformationError]
9877
- };
9878
- return rxjs.of(result);
9879
- })
9880
- ).subscribe((transformedData) => {
9881
- this.setState({ data: transformedData });
9882
- this._results.next({ origin: this, data: transformedData });
9883
- this._prevDataFromSource = data$1;
9884
- });
9885
- }
9886
- _interpolateVariablesInTransformationConfigs(data) {
9887
- var _a;
9888
- const transformations = this.state.transformations;
9889
- if (this._variableDependency.getNames().size === 0) {
9890
- return transformations;
9891
- }
9892
- const onlyObjects = transformations.every((t) => typeof t === "object");
9893
- if (onlyObjects) {
9894
- return JSON.parse(sceneGraph.interpolate(this, JSON.stringify(transformations), (_a = data.request) == null ? void 0 : _a.scopedVars));
9895
- }
9896
- return transformations.map((t) => {
9897
- var _a2;
9898
- return typeof t === "object" ? JSON.parse(sceneGraph.interpolate(this, JSON.stringify(t), (_a2 = data.request) == null ? void 0 : _a2.scopedVars)) : t;
9899
- });
9900
- }
9901
- }
9902
-
9903
9916
  class VariableValueSelectors extends SceneObjectBase {
9904
9917
  }
9905
9918
  VariableValueSelectors.Component = VariableValueSelectorsRenderer;