function getRuleConditionMetrics()

in src/scaler/scaler-core/scaling-methods/base.js [76:112]


function getRuleConditionMetrics(ruleResult) {
  let /** @type {NestedCondition[]} */ ruleConditions;
  if (ruleResult?.conditions && 'all' in ruleResult?.conditions) {
    ruleConditions = ruleResult.conditions.all;
  } else if (ruleResult?.conditions && 'any' in ruleResult?.conditions) {
    ruleConditions = ruleResult?.conditions?.any;
  } else {
    ruleConditions = [];
  }

  const /** @type {!Condition[]} */ ruleConditionsList = [];
  for (const ruleCondition of ruleConditions) {
    /*
     * Narrow down typing and skip NestedConditions.
     * Only Condition (currently ConditionProperties) are to be considered.
     * TODO: add support for nested conditions.
     */
    if (!('result' in ruleCondition)) continue;
    if (!('fact' in ruleCondition)) continue;
    if (!('factResult' in ruleCondition)) continue;
    if (!('value' in ruleCondition)) continue;

    // Only consider rules if they triggered the scale (i.e. result=true).
    if (!ruleCondition.result) continue;

    /*
     * Redefining this type as workaround because json-rules-engine typing does
     * not match the actual signature nor exports the Condition class directly.
     * See: https://github.com/CacheControl/json-rules-engine/issues/253
     */
    // @ts-ignore
    const /** @type {Condition} */ condition = ruleCondition;
    ruleConditionsList.push(condition);
  }

  return ruleConditionsList;
}