fillMissingDaysTrendHolder function

TimeSeries<TrendHolder> fillMissingDaysTrendHolder(
  1. TimeSeries<TrendHolder> timeSeries,
  2. int rewindDays
)

Use this function to fill missing days (the function adds NaNs) both in the future and in the past (up to the specified number of days in the past). Since the SDK does not return a value for the score in a day where the confidence it too low, this function can be useful to fill in blanks for UI/UX purposes. timeSeries the current TimeSeries rewindDays how many days to fill in in the past

Implementation

TimeSeries<TrendHolder> fillMissingDaysTrendHolder(
    TimeSeries<TrendHolder> timeSeries, int rewindDays) {
  final List<ZonedDateTime> newTimestamps = <ZonedDateTime>[];
  final List<TrendHolder> newValues = <TrendHolder>[];
  final List<TrendHolder> newConfidenceIntervalLow = <TrendHolder>[];
  final List<TrendHolder> newConfidenceIntervalHigh = <TrendHolder>[];
  final List<double> newConfidence = <double>[];
  final ZonedDateTime currentDay = ZonedDateTime.now();
  ZonedDateTime prevDate = timeSeries.timestamps.isNotEmpty
      ? timeSeries.timestamps[0].subtract(Duration(days: rewindDays))
      : currentDay.subtract(Duration(days: rewindDays));

  if (timeSeries.timestamps.isEmpty) {
    final int nMissingDays = currentDay.difference(prevDate).inDays - 1;
    for (int j = 0; j < nMissingDays; j++) {
      newTimestamps.add(prevDate.add(Duration(days: j + 1)));
      newValues.add(TrendHolder.empty());
      newConfidenceIntervalLow.add(TrendHolder.empty());
      newConfidenceIntervalHigh.add(TrendHolder.empty());
      newConfidence.add(double.nan);
    }
  }

  for (int i = 0; i < timeSeries.values.length; i++) {
    final int nMissingDays =
        timeSeries.timestamps[i].difference(prevDate).inDays - 1;
    for (int j = 0; j < nMissingDays; j++) {
      newTimestamps.add(prevDate.add(Duration(days: j + 1)));
      newValues.add(TrendHolder.empty());
      newConfidenceIntervalLow.add(TrendHolder.empty());
      newConfidenceIntervalHigh.add(TrendHolder.empty());
      newConfidence.add(double.nan);
    }
    newTimestamps.add(timeSeries.timestamps[i]);
    newValues.add(timeSeries.values[i]);
    newConfidenceIntervalLow.add(timeSeries.confidenceIntervalLow[i]);
    newConfidenceIntervalHigh.add(timeSeries.confidenceIntervalHigh[i]);
    newConfidence.add(timeSeries.confidence[i]);
    prevDate = timeSeries.timestamps[i];
  }

  // Adding missing days up to today
  final int nMissingFutureDays = newTimestamps.isNotEmpty
      ? newTimestamps.last.difference(currentDay).inDays
      : 0;
  final ZonedDateTime lastValue = newTimestamps.last;
  for (int j = 0; j < nMissingFutureDays; j++) {
    newTimestamps.add(lastValue.add(Duration(days: j + 1)));
    newValues.add(TrendHolder.empty());
    newConfidenceIntervalLow.add(TrendHolder.empty());
    newConfidenceIntervalHigh.add(TrendHolder.empty());
    newConfidence.add(double.nan);
  }

  return TimeSeries<TrendHolder>(
    values: newValues,
    timestamps: newTimestamps,
    confidenceIntervalLow: newConfidenceIntervalLow,
    confidenceIntervalHigh: newConfidenceIntervalHigh,
    confidence: newConfidence,
  );
}