emoss08 / Trenova

An Open Source AI-driven asset based transportation management system
http://trenova.app
Other
25 stars 7 forks source link

chore: cleanup tasks in one function #234

Closed wolfredstep closed 7 months ago

wolfredstep commented 8 months ago
          **issue (llm):** While the addition of the `clear_expired_reports` task extends the functionality of our system to handle more types of data cleanup, it also introduces some redundancy and increases the overall complexity of our task management. Both this task and the existing `clear_expired_audit_logs` share a conceptual similarity in their purpose and execution logic. To streamline our codebase and reduce maintenance overhead, I recommend considering a more generic approach to handling these types of data cleanup tasks.

One potential solution could be to implement a single, parameterized task that can handle different types of data cleanup based on input parameters. This would not only reduce the number of individual tasks we need to manage but also centralize the cleanup logic, making it easier to maintain and extend in the future. Here's a brief example of how we might refactor this:

@app.task(bind=True, max_retries=1, default_retry_delay=60)
def clear_expired_data(self: "Task", data_type: str) -> str:
    """
    Delete expired data for a given data type in Trenova.

    This task uses the Django management command specific to the data type
    to delete records that are older than 30 days.

    Args:
        data_type (str): The type of data to clean up ('audit_logs' or 'reports').

    Returns:
        str: Success message.
    """
    command_map = {
        'audit_logs': 'auditlogflush',
        'reports': 'clear_reports',
    }
    if data_type not in command_map:
        raise ValueError(f"Unsupported data type: {data_type}")

    try:
        call_command(command_map[data_type])
    except CommandCallException as exc:
        raise self.retry(exc=exc) from exc

    return f"Successfully deleted expired {data_type}."

This approach not only simplifies the task management but also makes it easier to introduce new types of data cleanup in the future by simply extending the command_map.

_Originally posted by @sourcery-ai[bot] in https://github.com/emoss08/Trenova/pull/233#discussion_r1510211873_