export async function getSummaryData()

in ui/changes/src/common.js [296:375]


export async function getSummaryData(
  bugSummaries,
  grouping = "daily",
  startDate,
  counter,
  filter,
  dateGetter = (summary) => summary.date,
  initialValue = () => 0
) {
  let dates = [...new Set(bugSummaries.map((summary) => dateGetter(summary)))];
  dates.sort((a, b) =>
    Temporal.PlainDate.compare(getPlainDate(a), getPlainDate(b))
  );

  let dailyData = {};
  for (let date of dates) {
    if (Temporal.PlainDate.compare(getPlainDate(date), startDate) == -1) {
      continue;
    }

    dailyData[date] = new Counter(initialValue);
  }

  for (let summary of bugSummaries) {
    let counterObj = dailyData[dateGetter(summary)];
    if (!counterObj) {
      continue;
    }

    if (filter && !filter(summary)) {
      continue;
    }

    counter(counterObj, summary);
  }

  if (grouping == "daily") {
    return dailyData;
  }

  const labels = [
    ...new Set(Object.values(dailyData).flatMap((data) => Object.keys(data))),
  ];

  function _merge(val, cur) {
    if (Array.isArray(val)) {
      return val.concat(cur);
    } else {
      return val + cur;
    }
  }

  let groupDate;
  if (grouping == "weekly") {
    groupDate = dateToWeek;
  } else if (grouping == "monthly") {
    groupDate = dateToMonth;
  } else if (grouping == "by_release") {
    groupDate = await dateToVersion;
  } else {
    throw new Error(`Unexpected grouping: ${grouping}`);
  }

  let groupedData = {};
  for (const daily in dailyData) {
    const group = groupDate(daily);

    if (!groupedData[group]) {
      groupedData[group] = new Counter(initialValue);
    }

    for (const label of labels) {
      groupedData[group][label] = _merge(
        groupedData[group][label],
        dailyData[daily][label]
      );
    }
  }
  return groupedData;
}