@grafana/scenes 8.10.2 → 8.11.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
@@ -3363,24 +3363,53 @@ function isExtraQueryProvider(obj) {
3363
3363
  }
3364
3364
 
3365
3365
  const passthroughProcessor = (_, secondary) => rxjs.of(secondary);
3366
- const extraQueryProcessingOperator = (processors) => (data) => {
3366
+ function combineLoadingStates(responseState) {
3367
+ var _a;
3368
+ if (responseState.some((s) => s === data.LoadingState.Error)) {
3369
+ return data.LoadingState.Error;
3370
+ }
3371
+ if (responseState.some((s) => s === data.LoadingState.Loading)) {
3372
+ return data.LoadingState.Loading;
3373
+ }
3374
+ if (responseState.some((s) => s === data.LoadingState.Streaming)) {
3375
+ return data.LoadingState.Streaming;
3376
+ }
3377
+ return (_a = responseState[0]) != null ? _a : data.LoadingState.Done;
3378
+ }
3379
+ const extraQueryProcessingOperator = (panelDataProcessors) => (data) => {
3380
+ const processedCache = /* @__PURE__ */ new WeakMap();
3367
3381
  return data.pipe(
3368
- rxjs.mergeMap(([primary, ...secondaries]) => {
3369
- const processedSecondaries = secondaries.flatMap((s) => {
3382
+ rxjs.mergeMap(([primaryResponse, ...secondaryResponses]) => {
3383
+ const processedSecondaries = secondaryResponses.map((s) => {
3370
3384
  var _a, _b;
3371
- return (_b = (_a = processors.get(s.request.requestId)) == null ? void 0 : _a(primary, s)) != null ? _b : rxjs.of(s);
3385
+ const cached = processedCache.get(s);
3386
+ if (cached) {
3387
+ return rxjs.of(cached);
3388
+ }
3389
+ const processedPanelData = (_b = (_a = panelDataProcessors.get(s.request.requestId)) == null ? void 0 : _a(primaryResponse, s)) != null ? _b : rxjs.of(s);
3390
+ return processedPanelData.pipe(rxjs.tap((out) => processedCache.set(s, out)));
3372
3391
  });
3373
- return rxjs.forkJoin([rxjs.of(primary), ...processedSecondaries]);
3392
+ return rxjs.combineLatest([rxjs.of(primaryResponse), ...processedSecondaries]);
3374
3393
  }),
3375
- rxjs.map(([primary, ...processedSecondaries]) => {
3394
+ rxjs.map(([processedPrimary, ...processedSecondaries]) => {
3376
3395
  var _a;
3396
+ const allResponses = [processedPrimary, ...processedSecondaries];
3397
+ const allResponseErrors = allResponses.flatMap((d) => {
3398
+ var _a2;
3399
+ return (_a2 = d.errors) != null ? _a2 : [];
3400
+ });
3377
3401
  return {
3378
- ...primary,
3379
- series: [...primary.series, ...processedSecondaries.flatMap((s) => s.series)],
3380
- annotations: [...(_a = primary.annotations) != null ? _a : [], ...processedSecondaries.flatMap((s) => {
3381
- var _a2;
3382
- return (_a2 = s.annotations) != null ? _a2 : [];
3383
- })]
3402
+ ...processedPrimary,
3403
+ state: combineLoadingStates(allResponses.map((d) => d.state)),
3404
+ series: [...processedPrimary.series, ...processedSecondaries.flatMap((s) => s.series)],
3405
+ annotations: [
3406
+ ...(_a = processedPrimary.annotations) != null ? _a : [],
3407
+ ...processedSecondaries.flatMap((s) => {
3408
+ var _a2;
3409
+ return (_a2 = s.annotations) != null ? _a2 : [];
3410
+ })
3411
+ ],
3412
+ ...allResponseErrors.length > 0 ? { errors: allResponseErrors, error: allResponseErrors[0] } : {}
3384
3413
  };
3385
3414
  })
3386
3415
  );
@@ -11102,6 +11131,123 @@ function shouldUseMixedRuntimeDatasource(sceneObject, datasource, queries) {
11102
11131
  }
11103
11132
  return false;
11104
11133
  }
11134
+ function parseRefsFromMathExpression(input) {
11135
+ const bracket = /\$\{([A-Za-z0-9_ ]+?)\}/gm;
11136
+ const simple = /\$([A-Za-z0-9_]+)/gm;
11137
+ const found = /* @__PURE__ */ new Set();
11138
+ let match;
11139
+ while ((match = bracket.exec(input)) !== null) {
11140
+ found.add(match[1]);
11141
+ }
11142
+ while ((match = simple.exec(input)) !== null) {
11143
+ found.add(match[1]);
11144
+ }
11145
+ return Array.from(found);
11146
+ }
11147
+ function getExpressionReferences(query) {
11148
+ const { type, expression, conditions } = query;
11149
+ if (type === "math" || type === "sql") {
11150
+ return parseRefsFromMathExpression(expression != null ? expression : "");
11151
+ }
11152
+ if (type === "classic_conditions") {
11153
+ return (conditions != null ? conditions : []).map((condition) => {
11154
+ var _a, _b;
11155
+ return (_b = (_a = condition.query) == null ? void 0 : _a.params) == null ? void 0 : _b[0];
11156
+ }).filter((refId) => Boolean(refId));
11157
+ }
11158
+ return expression ? [expression] : [];
11159
+ }
11160
+ function rewriteExpressionReferences(query, renameMap) {
11161
+ var _a, _b;
11162
+ const { type } = query;
11163
+ if (type === "math" || type === "sql") {
11164
+ let expression = (_a = query.expression) != null ? _a : "";
11165
+ for (const oldRef of Object.keys(renameMap)) {
11166
+ const pattern = new RegExp("(\\$" + oldRef + "\\b)|(\\$\\{" + oldRef + "\\})", "gm");
11167
+ expression = expression.replace(pattern, "${" + renameMap[oldRef] + "}");
11168
+ }
11169
+ query.expression = expression;
11170
+ } else if (type === "classic_conditions") {
11171
+ ((_b = query.conditions) != null ? _b : []).forEach((condition) => {
11172
+ var _a2;
11173
+ const params = (_a2 = condition.query) == null ? void 0 : _a2.params;
11174
+ if (params && params[0] && renameMap[params[0]]) {
11175
+ params[0] = renameMap[params[0]];
11176
+ }
11177
+ });
11178
+ } else if (query.expression && renameMap[query.expression]) {
11179
+ query.expression = renameMap[query.expression];
11180
+ }
11181
+ }
11182
+ function expandExpressionFanOut(sceneObject, targets) {
11183
+ var _a, _b, _c;
11184
+ const isExpression = (query) => Boolean(runtime.isExpressionReference && runtime.isExpressionReference(query.datasource));
11185
+ if (!targets.some(isExpression)) {
11186
+ return targets;
11187
+ }
11188
+ let variableName;
11189
+ let uids;
11190
+ for (const target of targets) {
11191
+ if (isExpression(target)) {
11192
+ continue;
11193
+ }
11194
+ const candidate = getSimpleVariableNameFromDatasourceUid((_a = target.datasource) == null ? void 0 : _a.uid);
11195
+ if (!candidate) {
11196
+ continue;
11197
+ }
11198
+ const value = (_b = sceneGraph.lookupVariable(candidate, sceneObject)) == null ? void 0 : _b.getValue();
11199
+ if (Array.isArray(value)) {
11200
+ const selected = value.filter((item) => item !== "default").map(String);
11201
+ if (selected.length > 1) {
11202
+ variableName = candidate;
11203
+ uids = selected;
11204
+ break;
11205
+ }
11206
+ }
11207
+ }
11208
+ if (!variableName || !uids) {
11209
+ return targets;
11210
+ }
11211
+ const fannedRootRefIds = /* @__PURE__ */ new Set();
11212
+ for (const target of targets) {
11213
+ if (!isExpression(target) && getSimpleVariableNameFromDatasourceUid((_c = target.datasource) == null ? void 0 : _c.uid) === variableName) {
11214
+ fannedRootRefIds.add(target.refId);
11215
+ }
11216
+ }
11217
+ const dependentRefIds = new Set(fannedRootRefIds);
11218
+ let changed = true;
11219
+ while (changed) {
11220
+ changed = false;
11221
+ for (const target of targets) {
11222
+ if (!isExpression(target) || dependentRefIds.has(target.refId)) {
11223
+ continue;
11224
+ }
11225
+ if (getExpressionReferences(target).some((refId) => dependentRefIds.has(refId))) {
11226
+ dependentRefIds.add(target.refId);
11227
+ changed = true;
11228
+ }
11229
+ }
11230
+ }
11231
+ const expanded = targets.filter((target) => !dependentRefIds.has(target.refId));
11232
+ const toFanOut = targets.filter((target) => dependentRefIds.has(target.refId));
11233
+ uids.forEach((uid, index) => {
11234
+ const renameMap = {};
11235
+ for (const target of toFanOut) {
11236
+ renameMap[target.refId] = `${target.refId}_${index}`;
11237
+ }
11238
+ for (const target of toFanOut) {
11239
+ const clone = lodash.cloneDeep(target);
11240
+ clone.refId = renameMap[target.refId];
11241
+ if (fannedRootRefIds.has(target.refId)) {
11242
+ clone.datasource = { ...clone.datasource, uid };
11243
+ } else {
11244
+ rewriteExpressionReferences(clone, renameMap);
11245
+ }
11246
+ expanded.push(clone);
11247
+ }
11248
+ });
11249
+ return expanded;
11250
+ }
11105
11251
  class SceneQueryRunner extends SceneObjectBase {
11106
11252
  constructor(initialState) {
11107
11253
  super(initialState);
@@ -11413,7 +11559,7 @@ class SceneQueryRunner extends SceneObjectBase {
11413
11559
  if (secondaries.length > 0) {
11414
11560
  const secondaryStreams = secondaries.map((r) => runRequest(ds, r));
11415
11561
  const op = extraQueryProcessingOperator(processors);
11416
- stream = rxjs.forkJoin([stream, ...secondaryStreams]).pipe(op);
11562
+ stream = rxjs.combineLatest([stream, ...secondaryStreams]).pipe(op);
11417
11563
  }
11418
11564
  const panelProfiler = findPanelProfiler(this);
11419
11565
  stream = stream.pipe(
@@ -11496,6 +11642,7 @@ class SceneQueryRunner extends SceneObjectBase {
11496
11642
  }
11497
11643
  return query;
11498
11644
  });
11645
+ request.targets = expandExpressionFanOut(this, request.targets);
11499
11646
  const lowerIntervalLimit = minInterval ? interpolate(this, minInterval) : ds.interval;
11500
11647
  const norm = data.rangeUtil.calculateInterval(timeRange.state.value, request.maxDataPoints, lowerIntervalLimit);
11501
11648
  request.scopedVars = Object.assign({}, request.scopedVars, {