adds maintenance function to clear all users' token records

This commit is contained in:
nai-degen
2023-08-30 22:38:33 -05:00
parent ffc0c6472e
commit 4b32130eaa
4 changed files with 60 additions and 20 deletions
+13
View File
@@ -0,0 +1,13 @@
{
"overrides": [
{
"files": [
"*.ejs"
],
"options": {
"printWidth": 160,
"bracketSameLine": true
}
}
]
}
+6
View File
@@ -165,6 +165,12 @@ router.post("/maintenance", (req, res) => {
message = `success: All users' token quotas reset to ${turbo} (Turbo), ${gpt4} (GPT-4), ${claude} (Claude).`;
break;
}
case "resetCounts": {
const users = userStore.getUsers();
users.forEach((user) => userStore.resetUsage(user.token));
message = `success: All users' token usage records reset.`;
break;
}
default: {
throw new HttpError(400, "Invalid action");
}
+9
View File
@@ -228,6 +228,15 @@ export function refreshQuota(token: string) {
usersToFlush.add(token);
}
export function resetUsage(token: string) {
const user = users.get(token);
if (!user) return;
const { tokenCounts } = user;
const counts = Object.entries(tokenCounts) as [ModelFamily, number][];
counts.forEach(([model]) => (tokenCounts[model] = 0));
usersToFlush.add(token);
}
/** Disables the given user, optionally providing a reason. */
export function disableUser(token: string, reason?: string) {
const user = users.get(token);
+32 -20
View File
@@ -1,20 +1,12 @@
<%- include("../_partials/admin-header", { title: "OAI Reverse Proxy Admin" })
%>
<%- include("../_partials/admin-header", { title: "OAI Reverse Proxy Admin" }) %>
<h1>OAI Reverse Proxy Admin</h1>
<% if (!persistenceEnabled) { %>
<p style="color: red; background-color: #eedddd; padding: 1em">
<strong
>⚠️ Users will be lost when the server restarts because persistence is not
configured.</strong
><br />
<br />Be sure to export your users and import them again after restarting the
server if you want to keep them.<br />
<strong>⚠️ Users will be lost when the server restarts because persistence is not configured.</strong><br />
<br />Be sure to export your users and import them again after restarting the server if you want to keep them.<br />
<br />
See the
<a
target="_blank"
href="https://gitgud.io/khanon/oai-reverse-proxy/-/blob/main/docs/user-management.md#firebase-realtime-database"
>
<a target="_blank" href="https://gitgud.io/khanon/oai-reverse-proxy/-/blob/main/docs/user-management.md#firebase-realtime-database">
user management documentation</a
>
to learn how to set up persistence.
@@ -31,18 +23,38 @@
<form id="maintenanceForm" action="/admin/manage/maintenance" method="post">
<input id="_csrf" type="hidden" name="_csrf" value="<%= csrfToken %>" />
<input id="hiddenAction" type="hidden" name="action" value="" />
<button type="button" onclick="submitForm('recheck')">
Force OpenAI Key Recheck
</button>
<% if (quotasEnabled) { %>
<button type="button" onclick="submitForm('resetQuotas')">
Reset All Users' Quotas
</button>
<% } %>
<div display="flex" flex-direction="column">
<fieldset>
<legend>Key Recheck</legend>
<button id="recheck-keys" type="button" onclick="submitForm('recheck')">Force OpenAI Key Recheck</button>
<label for="recheck-keys">Triggers a recheck of all OpenAI keys without restarting the server.</label>
</fieldset>
<% if (quotasEnabled) { %>
<fieldset>
<legend>Bulk Quota Management</legend>
<p>
<button id="refresh-quotas" type="button" onclick="submitForm('resetQuotas')">Refresh All Quotas</button>
Resets all users' quotas to the values set in the <code>TOKEN_QUOTA_*</code> environment variables.
</p>
<p>
<button id="clear-token-counts" type="button" onclick="submitForm('resetCounts')">Clear All Token Counts</button>
Resets all users' token records to zero.
</p>
</fieldset>
<% } %>
</div>
</form>
<script>
let confirmed = false;
function submitForm(action) {
if (action === "resetCounts" && !confirmed) {
document.getElementById("clear-token-counts").innerText = "💣 Confirm Clear All Token Counts";
alert("⚠️ This will permanently clear token records for all users. If you only want to refresh quotas, use the other button.");
confirmed = true;
return;
}
document.getElementById("hiddenAction").value = action;
document.getElementById("maintenanceForm").submit();
}