Skip to content

Commit

Permalink
fix(api): empty metrics digest
Browse files Browse the repository at this point in the history
  • Loading branch information
waltergalvao committed Jan 30, 2025
1 parent ad084c2 commit 2bd655d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
37 changes: 23 additions & 14 deletions apps/api/src/app/digests/services/digest-team-metrics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const getTeamMetrics = async (digest: DigestWithRelations) => {
const { latest, previous } = getChartFilters(digest);

const latestResults: Record<DigestMetricType, number> = await all({
prCount: averageMetricsService.getPullRequestCount(latest),
cycleTime: averageMetricsService.getAverageCycleTime(latest),
timeForFirstReview:
averageMetricsService.getAverageTimeForFirstReview(latest),
Expand All @@ -75,6 +76,7 @@ const getTeamMetrics = async (digest: DigestWithRelations) => {
});

const previousResults: Record<DigestMetricType, number> = await all({
prCount: averageMetricsService.getPullRequestCount(previous),
cycleTime: averageMetricsService.getAverageCycleTime(previous),
timeForFirstReview:
averageMetricsService.getAverageTimeForFirstReview(previous),
Expand Down Expand Up @@ -114,6 +116,7 @@ const getTeamMetrics = async (digest: DigestWithRelations) => {
};

return {
...buildMetric("prCount"),
...buildMetric("cycleTime"),
...buildMetric("timeForFirstReview"),
...buildMetric("timeForApproval"),
Expand Down Expand Up @@ -153,7 +156,7 @@ const getDigestMessageBlocks = async (
elements: [
{
type: "text",
text: `Avg of ${format(new UTCDate(latest.startDate), "MMM dd")}${format(new UTCDate(latest.endDate), "MMM dd")} (vs ${format(new UTCDate(previous.startDate), "MMM dd")}${format(new UTCDate(previous.endDate), "MMM dd")})`,
text: `Avg of ${format(new UTCDate(latest.startDate), "MMM dd")}${format(new UTCDate(latest.endDate), "MMM dd")} (vs ${format(new UTCDate(previous.startDate), "MMM dd")}${format(new UTCDate(previous.endDate), "MMM dd")})${metrics.prCount.latest.value} PRs from current period analyzed`,
},
],
},
Expand Down Expand Up @@ -193,7 +196,7 @@ const getDigestMessageBlocks = async (
type: "rich_text_section",
elements: getMetricLineElements({
label: "⏱️ PR Cycle Time",
value: `${formatMsDuration(Number(metrics.cycleTime.latest.value), dateFormatter)}`,
value: `${formatMsDuration(Number(metrics.cycleTime.latest.value), dateFormatter) || "N/A"}`,
change: metrics.cycleTime.change,
}),
},
Expand All @@ -204,32 +207,38 @@ const getDigestMessageBlocks = async (
type: "rich_text_section",
elements: getMetricLineElements({
label: "Time to First Review",
value: `${formatMsDuration(
Number(metrics.timeForFirstReview.latest.value),
dateFormatter
)}`,
value: `${
formatMsDuration(
Number(metrics.timeForFirstReview.latest.value),
dateFormatter
) || "N/A"
}`,
change: metrics.timeForFirstReview.change,
}),
},
{
type: "rich_text_section",
elements: getMetricLineElements({
label: "Time to Approve",
value: `${formatMsDuration(
Number(metrics.timeForApproval.latest.value),
dateFormatter
)}`,
value: `${
formatMsDuration(
Number(metrics.timeForApproval.latest.value),
dateFormatter
) || "N/A"
}`,
change: metrics.timeForApproval.change,
}),
},
{
type: "rich_text_section",
elements: getMetricLineElements({
label: "Time to Merge",
value: `${formatMsDuration(
Number(metrics.timeToMerge.latest.value),
dateFormatter
)}`,
value: `${
formatMsDuration(
Number(metrics.timeToMerge.latest.value),
dateFormatter
) || "N/A"
}`,
change: metrics.timeToMerge.change,
}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface MetricLineElements {
}

export type DigestMetricType =
| "prCount"
| "timeToMerge"
| "timeForFirstReview"
| "timeForApproval"
Expand Down
19 changes: 19 additions & 0 deletions apps/api/src/app/metrics/services/average-metrics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,22 @@ export const getAveragePullRequestSize = async (

return results.at(0)?.value || 0;
};

export const getPullRequestCount = async (filters: AverageMetricFilters) => {
const query = Prisma.sql`
SELECT COUNT(*) AS value
FROM "PullRequestTracking" pt
${innerJoinClause}
WHERE p."mergedAt" >= ${new Date(filters.startDate)}
AND p."mergedAt" <= ${new Date(filters.endDate)}
AND p."mergedAt" IS NOT NULL
AND tm."teamId" = ${filters.teamId}
AND wm."workspaceId" = ${filters.workspaceId}
`;

const results = await getPrisma(filters.workspaceId).$queryRaw<
{ value: number }[]
>(query);

return results.at(0)?.value || 0;
};

0 comments on commit 2bd655d

Please sign in to comment.