@grafana/scenes 6.47.1 → 6.48.0--canary.1236.19576619727.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
@@ -1774,7 +1774,7 @@ function isRepeatCloneOrChildOf(scene) {
1774
1774
  return false;
1775
1775
  }
1776
1776
 
1777
- class MultiValueVariable extends SceneObjectBase {
1777
+ const _MultiValueVariable = class _MultiValueVariable extends SceneObjectBase {
1778
1778
  constructor() {
1779
1779
  super(...arguments);
1780
1780
  this._urlSync = new MultiValueUrlSyncHandler(this);
@@ -1865,7 +1865,7 @@ class MultiValueVariable extends SceneObjectBase {
1865
1865
  } else {
1866
1866
  const defaultState = this.getDefaultSingleState(options);
1867
1867
  stateUpdate.value = defaultState.value;
1868
- stateUpdate.text = defaultState.text;
1868
+ stateUpdate.text = defaultState.label;
1869
1869
  }
1870
1870
  return stateUpdate;
1871
1871
  }
@@ -1887,16 +1887,35 @@ class MultiValueVariable extends SceneObjectBase {
1887
1887
  if (this.state.allValue) {
1888
1888
  return new CustomAllValue(this.state.allValue, this);
1889
1889
  }
1890
- value = this.state.options.map((x) => x.value);
1890
+ value = this.state.options.map((o) => o.value);
1891
1891
  }
1892
- if (fieldPath != null && Array.isArray(value)) {
1893
- const index = parseInt(fieldPath, 10);
1894
- if (!isNaN(index) && index >= 0 && index < value.length) {
1895
- return value[index];
1892
+ if (fieldPath != null) {
1893
+ if (Array.isArray(value)) {
1894
+ const index = parseInt(fieldPath, 10);
1895
+ if (!isNaN(index) && index >= 0 && index < value.length) {
1896
+ return value[index];
1897
+ }
1898
+ const accesor2 = this.getFieldAccessor(fieldPath);
1899
+ return value.map((v) => {
1900
+ const o2 = this.state.options.find((o3) => o3.value === v);
1901
+ return o2 ? accesor2(o2.properties) : v;
1902
+ });
1903
+ }
1904
+ const accesor = this.getFieldAccessor(fieldPath);
1905
+ const o = this.state.options.find((o2) => o2.value === value);
1906
+ if (o) {
1907
+ return accesor(o.properties);
1896
1908
  }
1897
1909
  }
1898
1910
  return value;
1899
1911
  }
1912
+ getFieldAccessor(fieldPath) {
1913
+ const accessor = _MultiValueVariable.fieldAccessorCache[fieldPath];
1914
+ if (accessor) {
1915
+ return accessor;
1916
+ }
1917
+ return _MultiValueVariable.fieldAccessorCache[fieldPath] = lodash.property(fieldPath);
1918
+ }
1900
1919
  getValueText() {
1901
1920
  if (this.hasAllValue()) {
1902
1921
  return ALL_VARIABLE_TEXT;
@@ -1914,18 +1933,18 @@ class MultiValueVariable extends SceneObjectBase {
1914
1933
  if (this.state.defaultToAll) {
1915
1934
  return { value: [ALL_VARIABLE_VALUE], text: [ALL_VARIABLE_TEXT] };
1916
1935
  } else if (options.length > 0) {
1917
- return { value: [options[0].value], text: [options[0].label] };
1936
+ return { value: [options[0].value], text: [options[0].label], properties: [options[0].properties] };
1918
1937
  } else {
1919
1938
  return { value: [], text: [] };
1920
1939
  }
1921
1940
  }
1922
1941
  getDefaultSingleState(options) {
1923
1942
  if (this.state.defaultToAll) {
1924
- return { value: ALL_VARIABLE_VALUE, text: ALL_VARIABLE_TEXT };
1943
+ return { value: ALL_VARIABLE_VALUE, label: ALL_VARIABLE_TEXT };
1925
1944
  } else if (options.length > 0) {
1926
- return { value: options[0].value, text: options[0].label };
1945
+ return { value: options[0].value, label: options[0].label, properties: options[0].properties };
1927
1946
  } else {
1928
- return { value: "", text: "" };
1947
+ return { value: "", label: "" };
1929
1948
  }
1930
1949
  }
1931
1950
  /**
@@ -2010,15 +2029,17 @@ class MultiValueVariable extends SceneObjectBase {
2010
2029
  this.updateValueGivenNewOptions(options);
2011
2030
  });
2012
2031
  }
2013
- }
2032
+ };
2033
+ _MultiValueVariable.fieldAccessorCache = {};
2034
+ let MultiValueVariable = _MultiValueVariable;
2014
2035
  function findOptionMatchingCurrent(currentValue, currentText, options) {
2015
2036
  let textMatch;
2016
- for (const item of options) {
2017
- if (item.value === currentValue) {
2018
- return item;
2037
+ for (const o of options) {
2038
+ if (o.value === currentValue) {
2039
+ return o;
2019
2040
  }
2020
- if (item.label === currentText) {
2021
- textMatch = item;
2041
+ if (o.label === currentText) {
2042
+ textMatch = o;
2022
2043
  }
2023
2044
  }
2024
2045
  return textMatch;
@@ -9439,7 +9460,7 @@ function metricNamesToVariableValues(variableRegEx, sort, metricNames) {
9439
9460
  value = firstMatch[1];
9440
9461
  }
9441
9462
  }
9442
- options.push({ label: text, value });
9463
+ options.push({ label: text, value, properties: item.properties });
9443
9464
  }
9444
9465
  options = lodash.uniqBy(options, "value");
9445
9466
  return sortVariableValues(options, sort);
@@ -9513,7 +9534,7 @@ function sortByNaturalSort(options) {
9513
9534
  });
9514
9535
  }
9515
9536
 
9516
- function toMetricFindValues() {
9537
+ function toMetricFindValues(valueProp, textProp) {
9517
9538
  return (source) => source.pipe(
9518
9539
  rxjs.map((panelData) => {
9519
9540
  const frames = panelData.series;
@@ -9526,58 +9547,86 @@ function toMetricFindValues() {
9526
9547
  if (frames[0].fields.length === 0) {
9527
9548
  return [];
9528
9549
  }
9529
- const processedDataFrames = data.getProcessedDataFrames(frames);
9550
+ const indices = validateIndices(findFieldsIndices(frames));
9530
9551
  const metrics = [];
9531
- let valueIndex = -1;
9532
- let textIndex = -1;
9533
- let stringIndex = -1;
9534
- let expandableIndex = -1;
9535
- for (const frame of processedDataFrames) {
9536
- for (let index = 0; index < frame.fields.length; index++) {
9537
- const field = frame.fields[index];
9538
- const fieldName = data.getFieldDisplayName(field, frame, frames).toLowerCase();
9539
- if (field.type === data.FieldType.string && stringIndex === -1) {
9540
- stringIndex = index;
9541
- }
9542
- if (fieldName === "text" && field.type === data.FieldType.string && textIndex === -1) {
9543
- textIndex = index;
9544
- }
9545
- if (fieldName === "value" && field.type === data.FieldType.string && valueIndex === -1) {
9546
- valueIndex = index;
9547
- }
9548
- if (fieldName === "expandable" && (field.type === data.FieldType.boolean || field.type === data.FieldType.number) && expandableIndex === -1) {
9549
- expandableIndex = index;
9550
- }
9551
- }
9552
- }
9553
- if (stringIndex === -1) {
9554
- throw new Error("Couldn't find any field of type string in the results.");
9555
- }
9556
9552
  for (const frame of frames) {
9557
9553
  for (let index = 0; index < frame.length; index++) {
9558
- const expandable = expandableIndex !== -1 ? frame.fields[expandableIndex].values.get(index) : void 0;
9559
- const string = frame.fields[stringIndex].values.get(index);
9560
- const text = textIndex !== -1 ? frame.fields[textIndex].values.get(index) : "";
9561
- const value = valueIndex !== -1 ? frame.fields[valueIndex].values.get(index) : "";
9562
- if (valueIndex === -1 && textIndex === -1) {
9563
- metrics.push({ text: string, value: string, expandable });
9554
+ const fieldValue = (fieldIndex) => fieldIndex !== -1 ? frame.fields[fieldIndex].values.get(index) : void 0;
9555
+ const value = fieldValue(indices.value);
9556
+ const text = fieldValue(indices.text);
9557
+ const expandable = fieldValue(indices.expandable);
9558
+ if (!indices.properties.length) {
9559
+ metrics.push({
9560
+ value: value || text,
9561
+ text: text || value,
9562
+ expandable
9563
+ });
9564
9564
  continue;
9565
9565
  }
9566
- if (valueIndex === -1 && textIndex !== -1) {
9567
- metrics.push({ text, value: text, expandable });
9568
- continue;
9566
+ const properties = {};
9567
+ for (const p of indices.properties) {
9568
+ properties[p.name] = fieldValue(p.index);
9569
9569
  }
9570
- if (valueIndex !== -1 && textIndex === -1) {
9571
- metrics.push({ text: value, value, expandable });
9572
- continue;
9573
- }
9574
- metrics.push({ text, value, expandable });
9570
+ metrics.push({
9571
+ value: value || valueProp || text || textProp,
9572
+ text: text || textProp || value || valueProp,
9573
+ properties,
9574
+ expandable
9575
+ });
9575
9576
  }
9576
9577
  }
9577
9578
  return metrics;
9578
9579
  })
9579
9580
  );
9580
9581
  }
9582
+ function findFieldsIndices(frames) {
9583
+ const indices = {
9584
+ value: -1,
9585
+ text: -1,
9586
+ expandable: -1,
9587
+ properties: []
9588
+ };
9589
+ for (const frame of data.getProcessedDataFrames(frames)) {
9590
+ for (let index = 0; index < frame.fields.length; index++) {
9591
+ const field = frame.fields[index];
9592
+ const fieldName = data.getFieldDisplayName(field, frame, frames).toLowerCase();
9593
+ if (field.type === data.FieldType.string) {
9594
+ if (fieldName === "value") {
9595
+ if (indices.value === -1) {
9596
+ indices.value = index;
9597
+ }
9598
+ continue;
9599
+ }
9600
+ if (fieldName === "text") {
9601
+ if (indices.text === -1) {
9602
+ indices.text = index;
9603
+ }
9604
+ continue;
9605
+ }
9606
+ indices.properties.push({ name: fieldName, index });
9607
+ continue;
9608
+ }
9609
+ if (fieldName === "expandable" && (field.type === data.FieldType.boolean || field.type === data.FieldType.number) && indices.expandable === -1) {
9610
+ indices.expandable = index;
9611
+ }
9612
+ }
9613
+ }
9614
+ return indices;
9615
+ }
9616
+ function validateIndices(indices, valueProp, textProp) {
9617
+ const hasNoValueOrText = indices.value === -1 && indices.text === -1;
9618
+ if (hasNoValueOrText && !indices.properties.length) {
9619
+ throw new Error("Couldn't find any field of type string in the results");
9620
+ }
9621
+ if (hasNoValueOrText && indices.properties.length === 1) {
9622
+ indices.value = indices.properties[0].index;
9623
+ indices.properties = [];
9624
+ }
9625
+ if (hasNoValueOrText && indices.properties.length && true && true) {
9626
+ throw new Error("Properties found in series but missing valueProp and textProp");
9627
+ }
9628
+ return indices;
9629
+ }
9581
9630
  function areMetricFindValues(data$1) {
9582
9631
  if (!data$1) {
9583
9632
  return false;
@@ -9981,7 +10030,7 @@ const sceneGraph = {
9981
10030
  getScopes
9982
10031
  };
9983
10032
 
9984
- class LocalValueVariable extends SceneObjectBase {
10033
+ const _LocalValueVariable = class _LocalValueVariable extends SceneObjectBase {
9985
10034
  constructor(initialState) {
9986
10035
  super({
9987
10036
  type: "system",
@@ -9992,9 +10041,19 @@ class LocalValueVariable extends SceneObjectBase {
9992
10041
  skipUrlSync: true
9993
10042
  });
9994
10043
  }
9995
- getValue() {
10044
+ getValue(fieldPath) {
10045
+ if (fieldPath != null && this.state.properties) {
10046
+ return this.getFieldAccessor(fieldPath)(this.state.properties);
10047
+ }
9996
10048
  return this.state.value;
9997
10049
  }
10050
+ getFieldAccessor(fieldPath) {
10051
+ const accessor = _LocalValueVariable.fieldAccessorCache[fieldPath];
10052
+ if (accessor) {
10053
+ return accessor;
10054
+ }
10055
+ return _LocalValueVariable.fieldAccessorCache[fieldPath] = lodash.property(fieldPath);
10056
+ }
9998
10057
  getValueText() {
9999
10058
  return this.state.text.toString();
10000
10059
  }
@@ -10015,7 +10074,9 @@ class LocalValueVariable extends SceneObjectBase {
10015
10074
  }
10016
10075
  return false;
10017
10076
  }
10018
- }
10077
+ };
10078
+ _LocalValueVariable.fieldAccessorCache = {};
10079
+ let LocalValueVariable = _LocalValueVariable;
10019
10080
 
10020
10081
  const PATH_ID_SEPARATOR = "$";
10021
10082
  function buildPathIdFor(panel) {
@@ -11812,6 +11873,7 @@ class CustomVariable extends MultiValueVariable {
11812
11873
  super({
11813
11874
  type: "custom",
11814
11875
  query: "",
11876
+ valuesFormat: "csv",
11815
11877
  value: "",
11816
11878
  text: "",
11817
11879
  options: [],
@@ -11840,8 +11902,27 @@ class CustomVariable extends MultiValueVariable {
11840
11902
  }
11841
11903
  });
11842
11904
  }
11905
+ transformJsonToOptions(json) {
11906
+ if (!json) {
11907
+ return [];
11908
+ }
11909
+ const parsedOptions = JSON.parse(json);
11910
+ if (!Array.isArray(parsedOptions) || parsedOptions.some((o) => typeof o !== "object" || o === null)) {
11911
+ throw new Error("Query must be a JSON array of objects");
11912
+ }
11913
+ const textProp = "text";
11914
+ const valueProp = "value";
11915
+ return parsedOptions.map((o) => {
11916
+ var _a;
11917
+ return {
11918
+ label: (_a = String(o[textProp] || o[valueProp])) == null ? void 0 : _a.trim(),
11919
+ value: String(o[valueProp]).trim(),
11920
+ properties: lodash.omit(o, [textProp, valueProp])
11921
+ };
11922
+ });
11923
+ }
11843
11924
  getValueOptions(args) {
11844
- const options = this.transformCsvStringToOptions(this.state.query);
11925
+ const options = this.state.valuesFormat === "json" ? this.transformJsonToOptions(this.state.query) : this.transformCsvStringToOptions(this.state.query);
11845
11926
  if (!options.length) {
11846
11927
  this.skipNextValidation = true;
11847
11928
  }