Skip to content

Commit

Permalink
chore: prepares moving presentation interests into interactive docume…
Browse files Browse the repository at this point in the history
…ntation
  • Loading branch information
arnoldknott committed Dec 9, 2024
1 parent 218a16d commit 96a7bc6
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 10 deletions.
4 changes: 2 additions & 2 deletions backendAPI/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RUN pip install --upgrade pip && \
# --no-create-home \
# backend-user
# USER backend-user
CMD ["uvicorn", "main:app", "--reload", "--log-level", "'warning'" ,"--host", "0.0.0.0", "--port", "80"]
CMD ["uvicorn", "main:app", "--reload", "--log-level", "warning" ,"--host", "0.0.0.0", "--port", "80"]

FROM base AS prod
LABEL org.opencontainers.image.source https://github.com/arnoldknott/fullstacksandbox23
Expand All @@ -33,4 +33,4 @@ RUN pip install --no-cache-dir --upgrade pip && \
# USER backend-user
ARG COMMIT_SHA="noGitBuild"
ENV COMMIT_SHA=$COMMIT_SHA
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
CMD ["uvicorn", "main:app", "--log-level", "warning", "--host", "0.0.0.0", "--port", "80"]
4 changes: 4 additions & 0 deletions backendAPI/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from routers.socketio.v1.base import presentation_interests_router, socketio_server
from routers.socketio.v1.demo_namespace import demo_namespace_router
from routers.socketio.v1.public_namespace import public_namespace_router
from routers.socketio.v1.interactive_documentation import (
interactive_documentation_router,
)
from routers.ws.v1.websockets import router as websocket_router

# print("Current directory:", os.getcwd())
Expand Down Expand Up @@ -233,6 +236,7 @@ async def lifespan(app: FastAPI):
socketio_server.register_namespace(public_namespace_router)
socketio_server.register_namespace(demo_namespace_router)
socketio_server.register_namespace(presentation_interests_router)
socketio_server.register_namespace(interactive_documentation_router)
socketio_app = ASGIApp(socketio_server, socketio_path="socketio/v1")
app.mount("/socketio/v1", app=socketio_app)

Expand Down
16 changes: 10 additions & 6 deletions backendAPI/src/routers/socketio/v1/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,21 @@
@socketio_server.event
async def connect(sid):
"""Connect event for socket.io."""
logger.info(f"Client connected with session id: {sid} outside namespaces.")
logger.warning(f"Client connected with session id: {sid} outside namespaces.")
print(f"=== routers - socketio - v1 - connect - sid {sid} / outside namespaces ===")


@socketio_server.event
async def disconnect(sid):
"""Disconnect event for socket.io."""
logger.info(f"Client with session id {sid} disconnected / outside namespaces.")
logger.warning(f"Client with session id {sid} disconnected / outside namespaces.")


# TBD: don't log the data, as it may contain sensitive information!
@socketio_server.on("*")
async def catch_all(event, sid, data):
"""Catch all events for socket.io, that don't have an event handler defined."""
logger.info(
f"Caught an event from client {sid} to unassigned event outside namespaces."
logger.warning(
f"Caught an event from client {sid} to event {event} in unassigned namespace."
)
print("=== routers - socketio - v1 - catch_all - event ===")
print(event)
Expand Down Expand Up @@ -139,6 +138,8 @@ def __init__(
self.server = socketio_server
self.namespace = namespace
self.room = room
print("=== base - __init__ - callback_on_connect ===")
print(callback_on_connect)
self.callback_on_connect = callback_on_connect
self.callback_on_disconnect = callback_on_disconnect

Expand All @@ -159,6 +160,8 @@ async def on_connect(
"""Connect event for socket.io namespaces."""
logger.info(f"Client connected with session id: {sid}.")
guards = self.guards
# print("=== base - on_connect - self.namespace ===")
# print(self.namespace, flush=True)
# print("=== base - on_connect - guards ===")
# print(guards, flush=True)
# print("=== base - on_connect - auth ===")
Expand Down Expand Up @@ -190,7 +193,7 @@ async def on_connect(
logger.info(
f"Client authenticated to access protected namespace {self.namespace}."
)
except Exception as err:
except Exception:
logger.error(f"Client with session id {sid} failed to authenticate.")
# print("=== base - on_connect - Exception ===")
# print(err, flush=True)
Expand All @@ -199,6 +202,7 @@ async def on_connect(
current_user = None
logger.info(f"Client authenticated to public namespace {self.namespace}.")
if self.callback_on_connect is not None:
print("=== base - on_connect - callback_on_connect ===")
await self.callback_on_connect(sid)

# current_user = await check_token_against_guards(token_payload, self.guards)
Expand Down
79 changes: 78 additions & 1 deletion backendAPI/src/routers/socketio/v1/interactive_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,81 @@

class InteractiveDocumentation(BaseNamespace):
def __init__(self):
super().__init__(namespace="/interactive-documentation")
super().__init__(
namespace="/interactive-documentation",
callback_on_connect=self.initial_data_transfer,
)
self.average = {
"Repository": 0.0,
"Infrastructure": 0.0,
"Architecture": 0.0,
"Security": 0.0,
"Backend": 0.0,
"Frontend": 0.0,
}
self.count = {
"Repository": 0,
"Infrastructure": 0,
"Architecture": 0,
"Security": 0,
"Backend": 0,
"Frontend": 0,
}
self.comments = []

# async def on_connect(self, sid, environ, auth=None):
# """Executes on connect for the interactive documentation namespace."""
# print("=== interactive_documentation - on_connect ===")
# print(sid, flush=True)
# # return await super().on_connect(sid, environ, auth)

async def initial_data_transfer(self, sid):
"""Executes transferring the initial data on connect."""
print("=== interactive_documentation - initial_data_transfer ===")
print("=== self.average ===")
print(self.average)
print("=== self.count ===")
print(self.count)
for topic in self.average:
await self.server.emit(
"averages",
{
"topic": topic,
"average": self.average[topic],
"count": self.count[topic],
},
to=sid,
)
for comment in self.comments:
await self.server.emit(
"server_comments",
{"topic": comment["topic"], "comment": comment["comment"]},
to=sid,
namespace=self.namespace,
)
return "initial_data_transfer"

async def on_comments(self, sid, data):
"""Presentation interests for socket.io."""
logger.info(f"Received comment in interactive documentation from client {sid}.")

self.comments.append({"topic": data["topic"], "comment": data["comment"]})
await self.emit(
"server_comments", {"topic": data["topic"], "comment": data["comment"]}
)

old_average = self.average[data["topic"]]
old_count = self.count[data["topic"]]

new_average = (old_average * old_count + data["value"]) / (old_count + 1)
self.count[data["topic"]] += 1
new_count = self.count[data["topic"]]
self.average[data["topic"]] = new_average

await self.emit(
"averages",
{"topic": data["topic"], "average": new_average, "count": new_count},
)


interactive_documentation_router = InteractiveDocumentation()
2 changes: 1 addition & 1 deletion frontend_svelte/src/components/Chat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@

<ul>
{#each old_messages as old_message}
<li>{old_message}</li>
<li>➡️ {old_message}</li>
{/each}
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
const connection: SocketioConnection = {
event: 'does_not_matter_here',
namespace: '/presentation_interests',
// namespace: '/interactive-documentation',
room: 'does_not_matter_here_either'
};
Expand Down

0 comments on commit 96a7bc6

Please sign in to comment.