All files / if-run/lib explain.ts

100% Statements 45/45
90.9% Branches 20/22
100% Functions 9/9
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134  2x   2x   2x       2x           2x         4x         2x 2x   2x   14x     2x       2x           2x 8x 8x             8x 8x     8x 4x   4x     8x 4x   4x     8x   6x 2x                   2x       8x 8x     8x 5x   5x 15x 13x                   8x 8x 8x   8x         1x           7x   7x           1x            
import {ParameterMetadata} from '@grnsft/if-core/types';
import {ERRORS} from '@grnsft/if-core/utils';
 
import {STRINGS} from '../../common/config';
 
import {logger} from '../../common/util/logger';
 
import {ExplainParams, ExplainStorageType} from '../types/explain';
 
const {ManifestValidationError} = ERRORS;
const {
  AGGREGATION_UNITS_NOT_MATCH,
  AGGREGATION_METHODS_NOT_MATCH,
  MISSING_INPUTS_PARAMETER,
  MISSING_OUTPUTS_PARAMETER,
} = STRINGS;
 
/**
 * Retrieves stored explain data.
 */
export const explain = () => storeExplainData.plugins;
 
/**
 * Manages the storage of explain data.
 */
const storeExplainData = (() => {
  let plugins: ExplainStorageType = {};
 
  const pluginManager = {
    get plugins() {
      return plugins;
    },
    set plugins(value: ExplainStorageType) {
      plugins = value;
    },
  };
 
  return pluginManager;
})();
 
/**
 * Adds new explain data to the stored explain data.
 */
export const addExplainData = (params: ExplainParams) => {
  const {pluginName, metadata} = params;
  const plugin: ExplainStorageType = {
    [pluginName]: {
      inputs: metadata?.inputs ?? {},
      outputs: metadata?.outputs ?? {},
    },
  };
 
  const isInputsMissing = !Object.keys(plugin[pluginName].inputs || {}).length;
  const isOutputsMissing = !Object.keys(plugin[pluginName].outputs || {})
    .length;
 
  if (isInputsMissing) {
    delete plugin[pluginName].inputs;
 
    logger.warn(MISSING_INPUTS_PARAMETER(pluginName));
  }
 
  if (isOutputsMissing) {
    delete plugin[pluginName].outputs;
 
    logger.warn(MISSING_OUTPUTS_PARAMETER(pluginName));
  }
 
  checkMetadatas(metadata);
 
  if (!isInputsMissing || !isOutputsMissing) {
    storeExplainData.plugins = {
      ...storeExplainData.plugins,
      ...plugin,
    };
  }
};
 
/**
 * Checks if the 'unit' and 'aggregation-method' of the parameter are the same throughout the manifest
 */
const checkMetadatas = (metadata: {
  inputs?: ParameterMetadata;
  outputs?: ParameterMetadata;
}) => {
  const inputsOutputsMetadata = {...metadata?.inputs, ...metadata?.outputs};
  const storedParameters: any = {};
 
  // Populate stored parameters with metadata from each plugin
  Object.values(storeExplainData.plugins).forEach(plugin => {
    const storedInputOutputMetadata = {...plugin.inputs, ...plugin.outputs};
 
    Object.keys(storedInputOutputMetadata).forEach(parameter => {
      if (!storedParameters[parameter]) {
        storedParameters[parameter] = {
          unit: storedInputOutputMetadata[parameter].unit,
          'aggregation-method':
            storedInputOutputMetadata[parameter]['aggregation-method'],
        };
      }
    });
  });
 
  // Validate input-output metadata against stored parameters
  Object.keys(inputsOutputsMetadata).forEach(parameterName => {
    const parameter = inputsOutputsMetadata[parameterName];
    const storedParameter = storedParameters[parameterName];
 
    if (
      parameter &&
      Object.keys(storedParameters).includes(parameterName) &&
      storedParameter.unit !== parameter.unit
    ) {
      throw new ManifestValidationError(
        AGGREGATION_UNITS_NOT_MATCH(parameterName)
      );
    }
 
    // Check for aggregation-method mismatch
    const inputAggregation = parameter['aggregation-method'];
 
    if (
      storedParameter &&
      (storedParameter['aggregation-method']?.component !==
        inputAggregation?.component ||
        storedParameter['aggregation-method']?.time !== inputAggregation?.time)
    ) {
      throw new ManifestValidationError(
        AGGREGATION_METHODS_NOT_MATCH(parameterName)
      );
    }
  });
};