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

How to manage a large amount of data in a "message" table? #3922

Open
songjinu opened this issue Sep 26, 2024 · 1 comment
Open

How to manage a large amount of data in a "message" table? #3922

songjinu opened this issue Sep 26, 2024 · 1 comment
Labels
question Further information is requested

Comments

@songjinu
Copy link

How to manage a large amount of data in a "message" table?
The "message" table has accumulated too much data and you're wondering how to manage it.
if it's possible to set up a task to delete old data based on certain settings. 🤔

Copy link

dosubot bot commented Sep 26, 2024

To manage a large amount of data in a "message" table and set up a task to delete old data based on certain settings, you can use the provided delete_messages function. This function deletes messages based on the session ID. You can modify or extend this function to delete messages based on other criteria, such as a timestamp.

Here is an example of how you can set up a task to delete old data based on a timestamp:

from datetime import datetime, timedelta
from sqlalchemy import delete
from sqlmodel import Session, col
from langflow.services.deps import session_scope
from langflow.services.database.models.message.model import MessageTable

def delete_old_messages(days: int):
    """
    Delete messages older than a specified number of days.

    Args:
        days (int): The number of days to retain messages. Messages older than this will be deleted.
    """
    cutoff_date = datetime.utcnow() - timedelta(days=days)
    with session_scope() as session:
        session.exec(
            delete(MessageTable)
            .where(col(MessageTable.timestamp) < cutoff_date)
            .execution_options(synchronize_session="fetch")
        )
        session.commit()

You can then schedule this function to run periodically using a task scheduler like cron or a background task library like APScheduler.

Additionally, Langflow provides an API endpoint to delete messages based on session ID, which can be useful for managing data:

@router.delete("/messages/session/{session_id}", status_code=204)
async def delete_messages_session(
    session_id: str,
    session: Session = Depends(get_session),
):
    try:
        session.exec(
            delete(MessageTable)
            .where(col(MessageTable.session_id) == session_id)
            .execution_options(synchronize_session="fetch")
        )
        session.commit()
        return {"message": "Messages deleted successfully"}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

This approach allows you to manage the size of your "message" table by regularly deleting old data based on your specified settings [1][2].

To continue talking to Dosu, mention @dosu.

@dosubot dosubot bot added the question Further information is requested label Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant