export async function MakeRepoReport()

in functions/src/report.ts [333:435]


export async function MakeRepoReport(
  org: string,
  repo: string
): Promise<report.Repo> {
  // Get two snapshots
  const now = new Date();

  // Use yesterday and 7 days ago in case today's snapshot job
  // has not run yet.
  const startDate = new Date(now.getTime() - DAY_MS);
  const endDate = new Date(startDate.getTime() - 7 * DAY_MS);

  const after = await FetchClosestSnapshot(org, repo, startDate);
  const afterDate = after.date;
  const afterSnap = after.snapshot;

  const before = await FetchClosestSnapshot(org, repo, endDate);
  const beforeDate = before.date;
  const beforeSnap = before.snapshot;

  if (!afterSnap) {
    throw `Couldn't get 'after' snapshot for ${startDate}`;
  }

  if (!beforeSnap) {
    throw `Couldn't get 'before' snapshot for ${endDate}`;
  }

  // Simple counting stats
  const open_issues = new report.Diff(
    beforeSnap.open_issues_count,
    afterSnap.open_issues_count
  );
  const stars = new report.Diff(
    beforeSnap.stargazers_count,
    afterSnap.stargazers_count
  );
  const forks = new report.Diff(beforeSnap.forks_count, afterSnap.forks_count);

  // SAM Score
  const sam = new report.Diff(
    ComputeSAMScore(beforeSnap),
    ComputeSAMScore(afterSnap)
  );

  // Check for difference in issues
  const before_ids = Object.keys(beforeSnap.issues);
  const after_ids = Object.keys(afterSnap.issues);

  // Issues present in only the "before" snap are newly closed, issues present in only
  // the "after" snap are newly opened.
  const closed_issues: report.ChangedIssue[] = util
    .setDiff(before_ids, after_ids)
    .map(id => {
      const issue = beforeSnap.issues[id];
      return toChangedIssue(org, repo, issue);
    });
  const opened_issues: report.ChangedIssue[] = util
    .setDiff(after_ids, before_ids)
    .map(id => {
      const issue = afterSnap.issues[id];
      return toChangedIssue(org, repo, issue);
    });

  // Get the repo report for more detail stats
  const repoStats = await stats.getRepoIssueStats(org, repo);

  // Get the labels with the most open issues
  const labelStats = repoStats.labelStats;
  const sortedLabelKeys = Object.keys(labelStats).sort((a, b) => {
    const aStats = labelStats[a];
    const bStats = labelStats[b];

    return bStats.open - aStats.open;
  });

  const worst_labels: report.LabelReport[] = [];
  for (let i = 0; i < sortedLabelKeys.length && i < 5; i++) {
    const key = sortedLabelKeys[i];
    const stats = labelStats[key];
    worst_labels.push({
      name: key,
      ...stats
    });
  }

  return {
    name: repo,

    start: util.DateSlug(beforeDate),
    end: util.DateSlug(afterDate),

    sam,
    open_issues,
    stars,
    forks,

    opened_issues,
    closed_issues,

    worst_labels
  };
}