Skip to content

Commit

Permalink
[keyserver] Add num revisions reviewed to monthly Phabricator leaderb…
Browse files Browse the repository at this point in the history
…oard

Summary:
This adds a query for the number of revisions reviewed. It's there for both the monthly stats and the yearly stats.

Linear task: [ENG-6306](https://linear.app/comm/issue/ENG-6306/phabricator-leaderboard)

Depends on D10477

Test Plan:
1. I created a Phabricator leaderboard thread in my local environment
2. I changed `phabLeaderboardChannel` with the value of that thread ID
3. I patched `postLeaderboard` to test the behavior on January 2024
4. `yarn script dist/scripts/phab-leaderboard.js`

Reviewers: atul

Reviewed By: atul

Subscribers: tomek

Differential Revision: https://phab.comm.dev/D10478
  • Loading branch information
Ashoat committed Dec 29, 2023
1 parent 46fe7b4 commit 079642c
Showing 1 changed file with 101 additions and 3 deletions.
104 changes: 101 additions & 3 deletions keyserver/src/cron/phab-leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,68 @@ function messageForNumRevisionsLandedLeaderboard(
);
}

type NumRevisionsReviewedLeaderboard = Array<{
+username: string,
+num_revisions_reviewed: string,
}>;
async function getNumRevisionsReviewedLeaderboard(
conn: DBConnection,
startTime: number,
endTime: number,
): Promise<NumRevisionsReviewedLeaderboard> {
const query = SQL`
SELECT x.username, COUNT(x.id) AS num_revisions_reviewed
FROM (
SELECT r.username, r.id, r.comment_time
FROM (
SELECT r.id,
u.userName AS username,
t.dateCreated AS comment_time,
JSON_ARRAYAGG(t.transactionType) AS transactions
FROM phabricator_differential.differential_revision r
LEFT JOIN phabricator_differential.differential_transaction t
ON t.objectPHID = r.phid
LEFT JOIN phabricator_user.user u
ON u.phid = t.authorPHID
WHERE t.authorPHID != r.authorPHID
AND u.userName IS NOT NULL
GROUP BY r.id, u.userName, t.dateCreated
) r
WHERE
(
NOT JSON_CONTAINS(transactions, '"differential.revision.resign"') OR
JSON_CONTAINS(transactions, '"differential:inline"')
)
AND (
JSON_CONTAINS(transactions, '"differential:inline"') OR
JSON_CONTAINS(transactions, '"core:comment"') OR
JSON_CONTAINS(transactions, '"differential.revision.accept"') OR
JSON_CONTAINS(transactions, '"differential.revision.reject"')
)
AND r.comment_time >= ${startTime} AND r.comment_time < ${endTime}
GROUP BY r.username, r.id
) x
GROUP BY x.username
ORDER BY num_revisions_reviewed DESC;
`;
const [results] = await conn.query(query);
return results;
}

function messageForNumRevisionsReviewedLeaderboard(
leaderboard: NumRevisionsReviewedLeaderboard,
date: Date,
dateTimeFormatOptions: Intl$DateTimeFormatOptions,
) {
return (
'### Review leaderboard for ' +
`${date.toLocaleString('default', dateTimeFormatOptions)}\n` +
'```\n' +
`${JSON.stringify(leaderboard, undefined, 2)}\n` +
'```'
);
}

async function postLeaderboard() {
if (!process.env.RUN_COMM_TEAM_DEV_SCRIPTS) {
// This is a job that the Comm internal team uses
Expand Down Expand Up @@ -162,18 +224,22 @@ async function postLeaderboard() {
let yearlyLineCountResults: ?LineCountLeaderboard;
let monthlyNumRevisionsLandedResults: ?NumRevisionsLandedLeaderboard;
let yearlyNumRevisionsLandedResults: ?NumRevisionsLandedLeaderboard;
let monthlyNumRevisionsReviewedResults: ?NumRevisionsReviewedLeaderboard;
let yearlyNumRevisionsReviewedResults: ?NumRevisionsReviewedLeaderboard;
try {
const monthlyPromise = (async () => {
const startTime = startOfLastMonth.getTime() / 1000;
const endTime = startOfThisMonth.getTime() / 1000;
return Promise.all([
getLineCountLeaderboard(conn, startTime, endTime),
getNumRevisionsLandedLeaderboard(conn, startTime, endTime),
getNumRevisionsReviewedLeaderboard(conn, startTime, endTime),
]);
})();
const yearlyPromise: Promise<?[
LineCountLeaderboard,
NumRevisionsLandedLeaderboard,
NumRevisionsReviewedLeaderboard,
]> = (async () => {
if (startOfThisMonth.getMonth() !== 0) {
return undefined;
Expand All @@ -183,16 +249,24 @@ async function postLeaderboard() {
return Promise.all([
getLineCountLeaderboard(conn, startTime, endTime),
getNumRevisionsLandedLeaderboard(conn, startTime, endTime),
getNumRevisionsReviewedLeaderboard(conn, startTime, endTime),
]);
})();
const [monthlyResults, yearlyResults] = await Promise.all([
monthlyPromise,
yearlyPromise,
]);
[monthlyLineCountResults, monthlyNumRevisionsLandedResults] =
monthlyResults;
[
monthlyLineCountResults,
monthlyNumRevisionsLandedResults,
monthlyNumRevisionsReviewedResults,
] = monthlyResults;
if (yearlyResults) {
[yearlyLineCountResults, yearlyNumRevisionsLandedResults] = yearlyResults;
[
yearlyLineCountResults,
yearlyNumRevisionsLandedResults,
yearlyNumRevisionsReviewedResults,
] = yearlyResults;
}
} finally {
conn.end();
Expand Down Expand Up @@ -223,6 +297,17 @@ async function postLeaderboard() {
{ month: 'long', year: 'numeric' },
),
},
{
type: messageTypes.TEXT,
threadID: phabLeaderboardChannel,
creatorID: bots.commbot.userID,
time: Date.now(),
text: messageForNumRevisionsReviewedLeaderboard(
monthlyNumRevisionsReviewedResults,
startOfLastMonth,
{ month: 'long', year: 'numeric' },
),
},
];
if (yearlyLineCountResults) {
messageDatas.push({
Expand Down Expand Up @@ -250,6 +335,19 @@ async function postLeaderboard() {
),
});
}
if (yearlyNumRevisionsReviewedResults) {
messageDatas.push({
type: messageTypes.TEXT,
threadID: phabLeaderboardChannel,
creatorID: bots.commbot.userID,
time: Date.now(),
text: messageForNumRevisionsReviewedLeaderboard(
yearlyNumRevisionsReviewedResults,
startOfLastYear,
{ year: 'numeric' },
),
});
}

await createMessages(viewer, messageDatas);
}
Expand Down

0 comments on commit 079642c

Please sign in to comment.