From bc4761e7a27f8ec29b98839b466589774f28b0a8 Mon Sep 17 00:00:00 2001 From: Luis-Hebendanz Date: Wed, 24 Jan 2024 18:41:12 +0100 Subject: [PATCH] Fixed wrong ordering of eventmessages --- .../clan_cli/webui/routers/endpoints.py | 17 +++++++++-------- pkgs/clan-cli/clan_cli/webui/sql_crud.py | 11 +++++++++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/webui/routers/endpoints.py b/pkgs/clan-cli/clan_cli/webui/routers/endpoints.py index 4417c78..3c4f25c 100644 --- a/pkgs/clan-cli/clan_cli/webui/routers/endpoints.py +++ b/pkgs/clan-cli/clan_cli/webui/routers/endpoints.py @@ -1,11 +1,13 @@ +import json import logging import time import typing +from collections import OrderedDict from typing import Any, List, Optional import httpx from fastapi import APIRouter, BackgroundTasks, Depends, Query -from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.responses import HTMLResponse, PlainTextResponse from sqlalchemy.orm import Session from clan_cli.config import ap_url, c1_url, c2_url, dlg_url, group_type_to_label @@ -360,14 +362,15 @@ def create_eventmessage( @typing.no_type_check @router.get( "/api/v1/event_messages", - response_class=JSONResponse, + response_class=PlainTextResponse, tags=[Tags.eventmessages], ) def get_all_eventmessages( skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db) -) -> JSONResponse: +) -> PlainTextResponse: + # SQL sorts eventmessages by timestamp, so we don't need to sort them here eventmessages = sql_crud.get_eventmessages(db, skip=skip, limit=limit) - result: dict[int, dict[int, List[Eventmessage]]] = {} + result: dict[int, dict[int, List[Eventmessage]]] = OrderedDict() for msg in eventmessages: # Use the group_type_to_label from config.py to get the group name and msg_type name @@ -385,7 +388,7 @@ def get_all_eventmessages( # Initialize the result array and dictionary if result.get(group_name) is None: - result[group_name] = {} + result[group_name] = OrderedDict() if result[group_name].get(msg.group_id) is None: result[group_name][msg.group_id] = [] @@ -408,9 +411,7 @@ def get_all_eventmessages( ).dict() ) - # sort by timestamp - result_arr.sort(key=lambda x: x["timestamp"]) - return JSONResponse(content=result, status_code=200) + return PlainTextResponse(content=json.dumps(result, indent=4), status_code=200) ############################## diff --git a/pkgs/clan-cli/clan_cli/webui/sql_crud.py b/pkgs/clan-cli/clan_cli/webui/sql_crud.py index aab8ed8..2cf3633 100644 --- a/pkgs/clan-cli/clan_cli/webui/sql_crud.py +++ b/pkgs/clan-cli/clan_cli/webui/sql_crud.py @@ -1,7 +1,7 @@ # Imports from typing import List, Optional -from sqlalchemy import func +from sqlalchemy import asc, func from sqlalchemy.orm import Session from sqlalchemy.sql.expression import true @@ -319,4 +319,11 @@ def create_eventmessage( def get_eventmessages( db: Session, skip: int = 0, limit: int = 100 ) -> List[sql_models.Eventmessage]: - return db.query(sql_models.Eventmessage).offset(skip).limit(limit).all() + # Use order_by and desc to sort by timestamp + return ( + db.query(sql_models.Eventmessage) + .order_by(asc(sql_models.Eventmessage.timestamp)) + .offset(skip) + .limit(limit) + .all() + )