private getCorrectedPingData()

in glean/src/core/metrics/database.ts [314:370]


  private getCorrectedPingData(ping: string, lifetime: Lifetime): Metrics {
    const store = this.chooseStore(lifetime);
    const data = store.get([ping]);
    if (isUndefined(data)) {
      return {};
    }

    if (!isObject(data)) {
      log(
        METRICS_DATABASE_LOG_TAG,
        `Invalid value found in storage for ping "${ping}". Deleting.`,
        LoggingLevel.Debug
      );
      store.delete([ping]);
      return {};
    }

    const correctedData: Metrics = {};
    // All top keys should be metric types.
    for (const metricType in data) {
      const metrics = data[metricType];
      if (!isObject(metrics)) {
        log(
          METRICS_DATABASE_LOG_TAG,
          `Unexpected data found in storage for metrics of type "${metricType}" in ping "${ping}". Deleting.`,
          LoggingLevel.Debug
        );
        store.delete([ping, metricType]);
        continue;
      }

      for (const metricIdentifier in metrics) {
        if (!validateMetricInternalRepresentation(metricType, metrics[metricIdentifier])) {
          log(
            METRICS_DATABASE_LOG_TAG,
            `Invalid value "${JSON.stringify(
              metrics[metricIdentifier]
            )}" found in storage for metric "${metricIdentifier}". Deleting.`,
            LoggingLevel.Debug
          );

          store.delete([ping, metricType, metricIdentifier]);
          continue;
        }

        if (!correctedData[metricType]) {
          correctedData[metricType] = {};
        }

        // Coercion is fine here, `validateMetricInternalRepresentation`
        // validated that this is of the correct type.
        correctedData[metricType][metricIdentifier] = metrics[metricIdentifier] as JSONValue;
      }
    }

    return correctedData;
  }