Skip to content

Commit

Permalink
feat: metric digest granular date formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
waltergalvao committed Dec 6, 2024
1 parent c8dda68 commit 5ed4530
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
43 changes: 25 additions & 18 deletions apps/api/src/app/digests/services/digest-team-metrics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@ import {
DigestMetricType,
MetricLineElements,
} from "./digest-team-metrics.types";
import {
endOfDay,
format,
millisecondsToHours,
startOfDay,
sub,
} from "date-fns";
import { DurationUnit, endOfDay, format, startOfDay, sub } from "date-fns";
import { Frequency } from "@prisma/client";
import * as averageMetricsService from "../../metrics/services/average-metrics.service";
import { AverageMetricFilters } from "../../metrics/services/average-metrics.service";
import { all, capitalize } from "radash";
import { getPullRequestSize } from "../../github/services/github-pull-request-tracking.service";
import { UTCDate } from "@date-fns/utc";
import { formatMsDuration } from "../../../lib/date";
import { AverageMetricFilters } from "../../metrics/services/average-metrics.types";

export const sendTeamMetricsDigest = async (digest: DigestWithRelations) => {
const { slackClient } = await getWorkspaceSlackClient(digest.workspaceId);
Expand Down Expand Up @@ -133,6 +128,15 @@ const getDigestMessageBlocks = async (
): Promise<AnyBlock[]> => {
const { latest, previous } = getChartFilters(digest);

const dateFormatter: DurationUnit[] = [
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
];

return [
{
type: "header",
Expand Down Expand Up @@ -189,7 +193,7 @@ const getDigestMessageBlocks = async (
type: "rich_text_section",
elements: getMetricLineElements({
label: "⏱️ PR Cycle Time",
value: `${millisecondsToHours(Number(metrics.cycleTime.latest.value))} hours`,
value: `${formatMsDuration(Number(metrics.cycleTime.latest.value), dateFormatter)}`,
change: metrics.cycleTime.change,
}),
},
Expand All @@ -200,29 +204,32 @@ const getDigestMessageBlocks = async (
type: "rich_text_section",
elements: getMetricLineElements({
label: "Time to First Review",
value: `${millisecondsToHours(
Number(metrics.timeForFirstReview.latest.value)
)} hours`,
value: `${formatMsDuration(
Number(metrics.timeForFirstReview.latest.value),
dateFormatter
)}`,
change: metrics.timeForFirstReview.change,
}),
},
{
type: "rich_text_section",
elements: getMetricLineElements({
label: "Time to Approve",
value: `${millisecondsToHours(
Number(metrics.timeForApproval.latest.value)
)} hours`,
value: `${formatMsDuration(
Number(metrics.timeForApproval.latest.value),
dateFormatter
)}`,
change: metrics.timeForApproval.change,
}),
},
{
type: "rich_text_section",
elements: getMetricLineElements({
label: "Time to Merge",
value: `${millisecondsToHours(
Number(metrics.timeToMerge.latest.value)
)} hours`,
value: `${formatMsDuration(
Number(metrics.timeToMerge.latest.value),
dateFormatter
)}`,
change: metrics.timeToMerge.change,
}),
},
Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/lib/date.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
differenceInBusinessDays,
differenceInMilliseconds,
DurationUnit,
endOfDay,
formatDuration,
intervalToDuration,
isSameDay,
isWeekend,
parseISO,
Expand All @@ -14,6 +17,18 @@ export const parseNullableISO = (
date: string | null | undefined
): Date | null => (date ? parseISO(date) : null);

export const formatMsDuration = (
durationInMs: number,
format?: DurationUnit[]
) => {
const duration = intervalToDuration({
start: new Date(Date.now() - durationInMs),
end: new Date(),
});

return formatDuration(duration, { format, delimiter: ", " });
};

export const differenceInBusinessMilliseconds = (
startDate: Date,
endDate: Date
Expand Down

0 comments on commit 5ed4530

Please sign in to comment.