extractWeeklyAverages method

TimeSeries<ScreenTimeAggregate> extractWeeklyAverages()

Extracts the weekly averages from the time series.

Implementation

TimeSeries<ScreenTimeAggregate> extractWeeklyAverages() {
  final List<List<Object>> zippedValues =
      IterableZip<Object>(<List<Object>>[timestamps, values]).toList();

  final Map<ZonedDateTime, List<List<Object>>> newZippedValues =
      zippedValues.groupListsBy((List<Object> element) =>
          (element[0] as ZonedDateTime).copyWith(timezone: Timezone.now()).firstDayOfWeek);
  final List<ScreenTimeAggregate> newValues = newZippedValues.entries
      .map((MapEntry<ZonedDateTime, List<List<Object>>> element) =>
          ScreenTimeAggregate(
              totalScreenTime: element.value
                  .map((List<Object> element) =>
                      (element[1] as ScreenTimeAggregate).totalScreenTime)
                  .toList()
                  .safeAverage(),
              socialScreenTime: element.value
                  .map((List<Object> element) =>
                      (element[1] as ScreenTimeAggregate).socialScreenTime)
                  .toList()
                  .safeAverage()))
      .toList();

  return TimeSeries<ScreenTimeAggregate>(
    values: newValues,

    // fill a list of length n with 0 values
    timestamps: newZippedValues.keys.toList(),
    confidenceIntervalLow: newValues,
    confidenceIntervalHigh: newValues,
    confidence: List<double>.filled(newZippedValues.keys.length, 0.0),
  );
}