Method / Script to Find if a Metric or Expression is used in the UI / Analytics


#1

Is there a method or script to hit an api available to see whether a metric or expression is used in a live environment, either in the UI or an analytic?

I’m looking to compile a list of unused metrics and am currently manually reviewing UI and analytics code in the customer’s repo.


#2

For finding if a metric is used in an analytic there is an api called

  /*
   * Get the configured metrics of interest for the specified source types.  If the list of types is empty,
   * interesting metrics are returned for all interesting source types (see sourceTypes above).
   * The optional argument specfies whether to include unused DFEs as well (default false).
   */
  sourceMetrics: function(sourceTypes: [TypeRef], unused: boolean): [SourceMetrics]

There are also apis available on MetricUtils to look for metrics for various type and usages

type MetricUtils {

  /**
   * Given a string expression, identify variables in it.
   * Variable names are returned in alphabetically sorted order.
   * eg: pow(a,2) - 2 * a * b + 3 * pow(b,2) ==> [a,b]
   * @param expression
   */
  identifyVariables: function(expression: !string): [string]

  /**
   * API to return the list of compound metric objects where the given metric is directly used
   * @param metric
   *            The metric for which usages need to be found
   * @return the list of compound metrics where the metric is used directly.
   */
  findDirectUsages: function(metric: Metric): [CompoundMetric]

  /**
   * API to return the list of compound metric objects where the given metric is used, directly or indirectly
   * @param metric
   *            The metric for which usages need to be found
   * @return the list of compound metrics where the metric is used. This includes all metrics till the root of the tree
   */
  findUsages: function(metric: Metric): [CompoundMetric]

  /**
   * API to return the list of compound metric objects for all the given metrics
   * @param metrics
   *            List of metrics for which usages need to be found
   * @return the list of compound metrics per metric. This includes all metrics till the root of the tree
   */
  findUsagesForMultipleMetrics: function(metrics: [Metric]): map<string, [CompoundMetric]>

  /**
   * API to return the list of compound metric objects for all the given metrics
   * @param metrics
   *            List of metrics for which usages need to be found
   * @return the list of compound metrics where the metrics is used. This includes all metrics till the root of the tree
   */
  findCompoundMetrics: function(metrics: [Metric]): [CompoundMetric]

  /**
   * API to return list of all compound and simple metric objects that use TSDecl and satisfies the provided filter
   * @param filter
   *            Additional filter to be applied on the SimpleMetric type
   * @return the list of simple and compound metric objects that contain tsDecl based metrics and satisfies the filter
   */
  listTSDeclMetrics: function(filter: string): [Obj]

  /**
   * API to return list of all compound and simple metric objects that use ActionDecl and satisfies the provided filter
   * @param filter
   *            Additional filter to be applied on the SimpleMetric type
   * @return the list of simple and compound metric objects that contain actionDecl based metrics and satisfies the filter
   */
  listActionDeclMetrics: function(filter: string): [Obj]

  /**
   * Gets a list of all metrics affected by a field. A metric is affected by a field if it is referenced anywhere in
   * the metric path or expression.
   * Note that only metrics that participate in automatic invalidation will be returned. This means that only metrics
   * such as the following:
   * 1. Metrics whose path leads to a timeseries header type and expression is the expression on that header type that
   * gets the data point values.
   * 2. AsTimeseries metrics.
   * Also note that metrics such as the following will not be returned:
   * 1. Metrics that don't participate in any analytics (as determined by ACE).
   * 2. Metrics that specify an action
   * @param typeRef
   *            Type of which the field lives
   * @param field
   *            Name of the field
   * @return list of simple and compound metrics that uses the field on the type
   */
  listMetricsForField: function(typeRef: TypeRef, field: string): [Obj]

  /**
   * Gets a list of all metrics affected by a type. A metric is affected by a type if it appears as either the source
   * type, or is referenced anywhere in the metric path or expression.
   * Note that only metrics that participate in automatic invalidation will be returned. This means that only metrics
   * such as the following:
   * 1. Metrics whose path leads to a timeseries header type and expression is the expression on that header type that
   * gets the data point values.
   * 2. AsTimeseries metrics.
   * Also note that metrics such as the following will not be returned:
   * 1. Metrics that don't participate in any analytics (as determined by ACE).
   * 2. Metrics that specify an action
   * @param typeRef
   *            Type which is used in the path
   * @return list of simple and compound metrics that uses the type
   */
  listMetricsForType: function(typeRef: TypeRef): [Obj]

  /**
   * Check if the expression is a valid metric expression
   * @param expression
   *                Expression that needs to be validated
   * @param sourceType (Optional)
   *                Source type for which this expression needs to check validity for
   * @return a boolean true if the expression is valid else throws an error
   */
  isExpressionValid: function(expression: !string, sourceType: TypeRef): boolean

  /**
   * Deep merge Objs Arry
   * @param objs
   *            An array of objs to merge
   * @return Deep merged objs
   */
  deepmergeObjs : function(objs: [Obj]) : [Obj] js server
}

#3

Thanks @rohit.sureka! MetricUtils.findUsages() is exactly what I was looking for.