-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HUD] Button for updating Dr. CI (#5844)
Add button on top right where logged in users can update Dr. CI https://torchci-git-csl-drcibutton-fbopensource.vercel.app/pr/138513 I am failing to log in on the preview, but it works locally, probably due to prod vs preview env vars Rate limit users to 10 calls / hr Is this a good idea? No clue Should this be limited to people with write access?
- Loading branch information
Showing
5 changed files
with
198 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Button, CircularProgress, Tooltip } from "@mui/material"; | ||
import { useSession } from "next-auth/react"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export default function DrCIButton({ | ||
owner, | ||
repo, | ||
prNumber, | ||
}: { | ||
owner: string; | ||
repo: string; | ||
prNumber: number; | ||
}) { | ||
const session = useSession(); | ||
const loggedIn = session.status === "authenticated" && session.data !== null; | ||
// loading, clickable, failed, rateLimited | ||
const [buttonState, setButtonState] = useState("clickable"); | ||
|
||
const url = `/api/drci/drci?prNumber=${prNumber}`; | ||
if (buttonState == "loading" && loggedIn) { | ||
fetch(url, { | ||
method: "POST", | ||
body: JSON.stringify({ repo }), | ||
headers: { | ||
Authorization: session.data!["accessToken"], | ||
"Cache-Control": "no-cache", | ||
"Content-Type": "application/json", | ||
}, | ||
}).then((res) => { | ||
if (res.status == 429) { | ||
setButtonState("rateLimited"); | ||
return; | ||
} | ||
if (!res.ok) { | ||
setButtonState("failed"); | ||
return; | ||
} | ||
setButtonState("clickable"); | ||
return res.json(); | ||
}); | ||
} | ||
|
||
useEffect(() => { | ||
if (buttonState == "failed" || buttonState == "rateLimited") { | ||
setTimeout(() => { | ||
setButtonState("clickable"); | ||
}, 5000); | ||
} | ||
}, [buttonState]); | ||
|
||
return ( | ||
<Tooltip | ||
title={ | ||
owner == "pytorch" | ||
? loggedIn | ||
? "Click to update Dr. CI. This might take a while." | ||
: "You must be logged in to update Dr. CI" | ||
: "Dr. CI is only available for pytorch org PRs" | ||
} | ||
> | ||
<span> | ||
<Button | ||
variant="contained" | ||
disableElevation | ||
disabled={ | ||
!loggedIn || buttonState != "clickable" || owner != "pytorch" | ||
} | ||
onClick={() => { | ||
setButtonState("loading"); | ||
}} | ||
> | ||
{buttonState == "loading" && ( | ||
<CircularProgress | ||
size={20} | ||
sx={{ | ||
color: "primary", | ||
position: "absolute", | ||
top: "50%", | ||
left: "50%", | ||
marginTop: "-10px", | ||
marginLeft: "-10px", | ||
}} | ||
/> | ||
)} | ||
{buttonState == "rateLimited" | ||
? "Exceeded Rate Limit" | ||
: buttonState == "failed" | ||
? "Failed to Update" | ||
: "Update Dr. CI"} | ||
</Button> | ||
</span> | ||
</Tooltip> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Rate limit users | ||
import dayjs from "dayjs"; | ||
import { getClickhouseClientWritable, queryClickhouse } from "./clickhouse"; | ||
|
||
async function checkRateLimit(user: string, key: string) { | ||
const res = await queryClickhouse( | ||
` | ||
select count() as count from misc.rate_limit | ||
where user = {user: String} and key = {key: String} and time_inserted > {timestamp: DateTime}`, | ||
{ | ||
user, | ||
key, | ||
timestamp: dayjs() | ||
.utc() | ||
.subtract(1, "hour") | ||
.format("YYYY-MM-DD HH:mm:ss"), | ||
} | ||
); | ||
if (res.length == 0) { | ||
return 0; | ||
} | ||
return res[0].count; | ||
} | ||
|
||
async function incrementRateLimit(user: string, key: string) { | ||
await getClickhouseClientWritable().insert({ | ||
table: "misc.rate_limit", | ||
values: [[user, key, dayjs().utc().format("YYYY-MM-DD HH:mm:ss")]], | ||
}); | ||
} | ||
|
||
export async function drCIRateLimitExceeded(user: string) { | ||
const rateLimit = 10; | ||
return (await checkRateLimit(user, "DrCI")) >= rateLimit; | ||
} | ||
|
||
export async function incrementDrCIRateLimit(user: string) { | ||
return await incrementRateLimit(user, "DrCI"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters