Skip to content

Commit

Permalink
Fix open server URL
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed May 6, 2024
1 parent 31a8afc commit 1e79321
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/servers/OpenServerButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button } from '@mui/material';
import { Fragment, memo, useCallback } from 'react';
import { Fragment, memo, useCallback, useMemo } from 'react';

import { useAxios } from '../common/AxiosContext';
import { useJupyterhub } from '../common/JupyterhubContext';
Expand Down Expand Up @@ -36,10 +36,14 @@ function _OpenServerButton(props: IOpenServerButton) {
}
}, [props, axios, jhData]);

const pendingUrl = useMemo(() => {
const url = props.url.replace('user', 'hub/spawn-pending');
return url.endsWith('/') ? url.slice(0, -1) : url;
}, [props.url]);
return (
<Fragment>
{props.active && (
<Button href={props.url} target="_blank">
<Button href={pendingUrl} target="_blank">
Open Server
</Button>
)}
Expand Down
6 changes: 4 additions & 2 deletions src/servers/ServersList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const columns: GridColDef[] = [
<OpenServerButton
url={params.row.url}
serverName={params.row.name}
imageName={params.row.image}
imageName={params.row.uid ?? params.row.image}
active={params.row.active}
/>
);
Expand All @@ -86,7 +86,9 @@ function _ServerList(props: IServerListProps) {
}
const allServers = servers.map((it, id) => {
const newItem: any = { ...it, id };
newItem.image = it.user_options.image ?? '';
newItem.image =
it.user_options?.display_name ?? it.user_options.image ?? '';
newItem.uid = it.user_options?.uid ?? null;
newItem.last_activity = formatTime(newItem.last_activity);
return newItem;
});
Expand Down
2 changes: 1 addition & 1 deletion src/servers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export interface IServerData {
name: string;
url: string;
last_activity: string;
user_options: { image?: string };
user_options: { image?: string; display_name?: string; uid?: string };
active: boolean;
}
10 changes: 5 additions & 5 deletions tljh_repo2docker/binderhub_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from urllib.parse import quote
from uuid import UUID, uuid4

from aiodocker import Docker, DockerError
from aiodocker import Docker
from jupyterhub.utils import url_path_join
from tornado import web

Expand Down Expand Up @@ -44,11 +44,11 @@ async def delete(self):
async with db_context() as db:
image = await image_db_manager.read(db, uid)
if image:
async with Docker() as docker:
try:
try:
async with Docker() as docker:
await docker.images.delete(image.name)
except DockerError:
pass
except Exception:
pass
deleted = await image_db_manager.delete(db, uid)

self.set_header("content-type", "application/json")
Expand Down
21 changes: 20 additions & 1 deletion tljh_repo2docker/database/manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import List, Type, Union
from typing import List, Optional, Type, Union

import sqlalchemy as sa
from pydantic import UUID4
Expand Down Expand Up @@ -105,6 +105,25 @@ async def read_all(self, db: AsyncSession) -> List[DockerImageOutSchema]:
resources = (await db.execute(sa.select(self._table))).scalars().all()
return [self._schema_out.model_validate(r) for r in resources]

async def read_by_image_name(
self, db: AsyncSession, image: str
) -> Optional[DockerImageOutSchema]:
"""
Get image by its name.
Args:
db: An asyncio version of SQLAlchemy session.
Returns:
The list of resources retrieved.
"""
statement = sa.select(self._table).where(self._table.name == image)
try:
result = await db.execute(statement)
return self._schema_out.model_validate(result.scalars().first())
except Exception:
return None

async def update(
self, db: AsyncSession, obj_in: DockerImageUpdateSchema, optimistic: bool = True
) -> Union[DockerImageOutSchema, None]:
Expand Down
18 changes: 17 additions & 1 deletion tljh_repo2docker/servers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from inspect import isawaitable
from typing import Dict, List

from tornado import web

Expand All @@ -24,7 +25,22 @@ async def get(self):

user_data = await self.fetch_user()

server_data = user_data.all_spawners()
server_data: List[Dict] = user_data.all_spawners()

db_context, image_db_manager = self.get_db_handlers()
if db_context and image_db_manager:
async with db_context() as db:
for data in server_data:
image_name = data.get("user_options", {}).get("image", None)
if image_name:
db_data = await image_db_manager.read_by_image_name(
db, image_name
)
if db_data:
data["user_options"]["uid"] = str(db_data.uid)
data["user_options"][
"display_name"
] = db_data.image_meta.display_name
named_server_limit = 0
result = self.render_template(
"servers.html",
Expand Down
2 changes: 0 additions & 2 deletions tljh_repo2docker/tests/binderhub_build/test_logs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import pytest
from jupyterhub.tests.utils import async_requests

Expand Down
2 changes: 0 additions & 2 deletions tljh_repo2docker/tests/local_build/test_logs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import pytest
from jupyterhub.tests.utils import async_requests

Expand Down
2 changes: 1 addition & 1 deletion ui-tests/jupyterhub_config_binderhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"-m",
"tljh_repo2docker",
"--ip",
"127.0.0.1",
"0.0.0.0",
"--port",
"6789",
"--config",
Expand Down

0 comments on commit 1e79321

Please sign in to comment.