mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-07-04 17:20:19 +08:00
unify 3 status endpoints and move status fetches to oasst_api_client
This commit is contained in:
@@ -148,6 +148,27 @@ export class OasstApiClient {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tasks availability information for given `user`.
|
||||
*/
|
||||
async fetch_tasks_availability(user: object): Promise<any> {
|
||||
return this.post("/api/v1/tasks/availability", user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message stats from the backend.
|
||||
*/
|
||||
async fetch_stats(): Promise<any> {
|
||||
return this.get("/api/v1/stats/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tree manager stats from the backend.
|
||||
*/
|
||||
async fetch_tree_manager(): Promise<any> {
|
||||
return this.get("/api/v1/stats/tree_manager");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the `BackendUser` associated with `user_id`
|
||||
*/
|
||||
|
||||
@@ -56,46 +56,13 @@ const StatusIndex = ({ user }) => {
|
||||
router.push("/");
|
||||
}, [router, session, status]);
|
||||
|
||||
const [availability, setAvailability] = useState(0);
|
||||
const [stats, setStats] = useState(0);
|
||||
const [treeManager, setTreeManager] = useState(0);
|
||||
const {
|
||||
data: dataStatus,
|
||||
error: errorStatus,
|
||||
isLoading: isLoadingStatus,
|
||||
} = useSWRImmutable("/api/admin/status", get);
|
||||
|
||||
const {
|
||||
data: dataAvailability,
|
||||
error: errorAvailability,
|
||||
isLoading: isLoadingAvailability,
|
||||
} = useSWRImmutable("/api/admin/tasks_availability", get, {
|
||||
onSuccess: (data) => {
|
||||
setAvailability(data);
|
||||
},
|
||||
onError: (error) => {
|
||||
setAvailability(error);
|
||||
},
|
||||
});
|
||||
const {
|
||||
data: dataStats,
|
||||
error: errorStats,
|
||||
isLoading: isLoadingStats,
|
||||
} = useSWRImmutable("/api/admin/stats", get, {
|
||||
onSuccess: (data) => {
|
||||
setStats(data);
|
||||
},
|
||||
onError: (error) => {
|
||||
setStats(error);
|
||||
},
|
||||
});
|
||||
const {
|
||||
data: dataTreeManager,
|
||||
error: errorTreeManager,
|
||||
isLoading: isLoadingTreeManager,
|
||||
} = useSWRImmutable("/api/admin/tree_manager", get, {
|
||||
onSuccess: (data) => {
|
||||
setTreeManager(data);
|
||||
},
|
||||
onError: (error) => {
|
||||
setTreeManager(error);
|
||||
},
|
||||
});
|
||||
const { tasksAvailability, stats, treeManager } = dataStatus || {};
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -114,7 +81,7 @@ const StatusIndex = ({ user }) => {
|
||||
</Text>
|
||||
<Box bg={dataBackgroundColor} borderRadius="xl" p="6" pt="4" pr="12">
|
||||
<pre id="json">
|
||||
{availability ? JSON.stringify(availability, null, 2) : <CircularProgress isIndeterminate />}
|
||||
{tasksAvailability ? JSON.stringify(tasksAvailability, null, 2) : <CircularProgress isIndeterminate />}
|
||||
</pre>
|
||||
</Box>
|
||||
</Card>
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import { withRole } from "src/lib/auth";
|
||||
|
||||
/**
|
||||
* Returns the message stats.
|
||||
*/
|
||||
const handler = withRole("admin", async (req, res) => {
|
||||
const statsRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/stats/`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"X-API-Key": process.env.FASTAPI_KEY,
|
||||
},
|
||||
});
|
||||
|
||||
const stats = await statsRes.json();
|
||||
|
||||
res.status(statsRes.status).json(stats);
|
||||
});
|
||||
|
||||
export default handler;
|
||||
@@ -0,0 +1,26 @@
|
||||
import { withRole } from "src/lib/auth";
|
||||
import { oasstApiClient } from "src/lib/oasst_api_client";
|
||||
|
||||
/**
|
||||
* Returns tasks availability, stats, and tree manager stats.
|
||||
*/
|
||||
const handler = withRole("admin", async (req, res) => {
|
||||
const dummy_user = {
|
||||
id: "__dummy_user__",
|
||||
display_name: "Dummy User",
|
||||
auth_method: "local",
|
||||
};
|
||||
const tasksAvailabilityData = await oasstApiClient.fetch_tasks_availability(dummy_user);
|
||||
const statsData = await oasstApiClient.fetch_stats();
|
||||
const treeManagerData = await oasstApiClient.fetch_tree_manager();
|
||||
|
||||
const status = {
|
||||
tasksAvailability: tasksAvailabilityData,
|
||||
stats: statsData,
|
||||
treeManager: treeManagerData,
|
||||
};
|
||||
|
||||
res.status(200).json(status);
|
||||
});
|
||||
|
||||
export default handler;
|
||||
@@ -1,24 +0,0 @@
|
||||
import { withRole } from "src/lib/auth";
|
||||
|
||||
/**
|
||||
* Returns result of tasks availability query using a dummy user.
|
||||
*/
|
||||
const handler = withRole("admin", async (req, res) => {
|
||||
const tasksAvailabilityRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/tasks/availability`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-API-Key": process.env.FASTAPI_KEY,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: "__dummy_user__",
|
||||
display_name: "Dummy User",
|
||||
auth_method: "local",
|
||||
}),
|
||||
});
|
||||
const tasksAvailability = await tasksAvailabilityRes.json();
|
||||
|
||||
res.status(tasksAvailabilityRes.status).json(tasksAvailability);
|
||||
});
|
||||
|
||||
export default handler;
|
||||
@@ -1,18 +0,0 @@
|
||||
import { withRole } from "src/lib/auth";
|
||||
|
||||
/**
|
||||
* Returns tree manager stats.
|
||||
*/
|
||||
const handler = withRole("admin", async (req, res) => {
|
||||
const treeManagerRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/stats/tree_manager`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"X-API-Key": process.env.FASTAPI_KEY,
|
||||
},
|
||||
});
|
||||
const treeManager = await treeManagerRes.json();
|
||||
|
||||
res.status(treeManagerRes.status).json(treeManager);
|
||||
});
|
||||
|
||||
export default handler;
|
||||
Reference in New Issue
Block a user