Fixed wrong ordering of eventmessages
All checks were successful
checks-impure / test (pull_request) Successful in 26s
checks / test (pull_request) Successful in 1m14s

This commit is contained in:
2024-01-24 18:41:12 +01:00
parent 8a5232255f
commit bc4761e7a2
2 changed files with 18 additions and 10 deletions

View File

@@ -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)
##############################

View File

@@ -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()
)