Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show live CPU and memory usage of user session backend #414

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions backend/src/backend/user_session/routers/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@
START_TIME_CONTAINER = datetime.datetime.now()


def human_readable(psutil_object: NamedTuple) -> Dict[str, Union[str, Dict[str, str]]]:
return {
key: f"{getattr(psutil_object, key):.1f} %"
if key == "percent"
else f"{getattr(psutil_object, key) / (1024**3):.2f} GiB"
for key in psutil_object._fields
}
def _convert_psutil_object_to_dict(psutil_object: NamedTuple) -> Dict[str, Union[str, Dict[str, str]]]:
return {key: getattr(psutil_object, key) for key in psutil_object._fields}


@router.get("/user_session_container")
Expand All @@ -34,8 +29,9 @@ async def user_session_container(

return {
"username": authenticated_user.get_username(),
"start_time_container": START_TIME_CONTAINER,
"root_disk_system": human_readable(psutil.disk_usage("/")),
"memory_system": human_readable(psutil.virtual_memory()),
"memory_python_process": human_readable(psutil.Process().memory_info()),
"startTimeContainer": START_TIME_CONTAINER,
"rootDiskSystem": _convert_psutil_object_to_dict(psutil.disk_usage("/")),
"memorySystem": _convert_psutil_object_to_dict(psutil.virtual_memory()),
"memoryPythonProcess": _convert_psutil_object_to_dict(psutil.Process().memory_info()),
"cpuPercent": psutil.cpu_percent(),
}
18 changes: 6 additions & 12 deletions frontend/src/framework/internal/components/NavBar/navBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { resolveClassNames } from "@lib/utils/resolveClassNames";
import {
ChevronLeft,
ChevronRight,
GitHub,
GridView,
Link,
List,
Expand All @@ -26,6 +25,8 @@ import {
} from "@mui/icons-material";
import { useQueryClient } from "@tanstack/react-query";

import { UserSessionState } from "./private-components/UserSessionState";

type NavBarProps = {
workbench: Workbench;
};
Expand Down Expand Up @@ -249,17 +250,10 @@ export const NavBar: React.FC<NavBarProps> = (props) => {
<NavBarDivider />
<LoginButton className="w-full !text-slate-800 h-10" showText={expanded} />
<div className="flex-grow h-5" />
<NavBarDivider />
<Button
title="Visit project on GitHub"
onClick={() => window.open("https://github.com/equinor/webviz", "_blank")}
className={resolveClassNames("w-full !text-slate-500 hover:!text-slate-800 h-10", {
"mb-16": isDevMode(),
})}
startIcon={<GitHub fontSize="small" />}
>
{expanded ? "Webviz on GitHub" : ""}
</Button>
<div className={isDevMode() ? "mb-16" : ""}>
<NavBarDivider />
<UserSessionState expanded={expanded} />
</div>
</div>
{ensembleDialogOpen && (
<SelectEnsemblesDialog selectedEnsembles={selectedEnsembles} onClose={handleEnsembleDialogClose} />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Memory } from "@mui/icons-material";
import { useQuery } from "@tanstack/react-query";

import { apiService } from "@framework/ApiService";

const useUserSessionState = () => useQuery({
queryKey: ["default.userSessionContainer"],
queryFn: () => apiService.default.userSessionContainer(),
refetchInterval: 2000
});

export const UserSessionState = ({expanded}: {expanded: boolean}) => {

const sessionState = useUserSessionState();

const memoryPercent = Math.round(sessionState.data?.memorySystem?.percent) || "-"
const cpuPercent = Math.round(sessionState.data?.cpuPercent) || "-"

return <div className="text-xs whitespace-nowrap text-gray-400">
<div><Memory fontSize="small" /> {expanded ? "Memory:" : "M"} {memoryPercent} %</div>
<div><Memory fontSize="small" /> {expanded ? "CPU:" : "C"} {cpuPercent} %</div>
</div>
}