Skip to content

Commit

Permalink
Merge pull request #685 from getredash/fix/alert_sub_migration
Browse files Browse the repository at this point in the history
Feature: add settings to query results cleanup
  • Loading branch information
arikfr committed Dec 3, 2015
2 parents 51deb8f + ab72531 commit 378459d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions redash/models.py
Expand Up @@ -369,10 +369,10 @@ def to_dict(self):
}

@classmethod
def unused(cls):
week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
def unused(cls, days=7):
age_threshold = datetime.datetime.now() - datetime.timedelta(days=days)

unused_results = cls.select().where(Query.id == None, cls.retrieved_at < week_ago)\
unused_results = cls.select().where(Query.id == None, cls.retrieved_at < age_threshold)\
.join(Query, join_type=peewee.JOIN_LEFT_OUTER)

return unused_results
Expand Down
2 changes: 2 additions & 0 deletions redash/settings.py
Expand Up @@ -69,6 +69,8 @@ def all_settings():

# The following enables periodic job (every 5 minutes) of removing unused query results.
QUERY_RESULTS_CLEANUP_ENABLED = parse_boolean(os.environ.get("REDASH_QUERY_RESULTS_CLEANUP_ENABLED", "true"))
QUERY_RESULTS_CLEANUP_COUNT = int(os.environ.get("REDASH_QUERY_RESULTS_CLEANUP_COUNT", "100"))
QUERY_RESULTS_CLEANUP_MAX_AGE = int(os.environ.get("REDASH_QUERY_RESULTS_CLEANUP_MAX_AGE", "7"))

AUTH_TYPE = os.environ.get("REDASH_AUTH_TYPE", "api_key")
PASSWORD_LOGIN_ENABLED = parse_boolean(os.environ.get("REDASH_PASSWORD_LOGIN_ENABLED", "true"))
Expand Down
5 changes: 4 additions & 1 deletion redash/tasks.py
Expand Up @@ -221,7 +221,10 @@ def cleanup_query_results():
Each time the job deletes only 100 query results so it won't choke the database in case of many such results.
"""

unused_query_results = models.QueryResult.unused().limit(100)
logging.info("Running query results clean up (removing maximum of %d unused results, that are %d days old or more)",
settings.QUERY_RESULTS_CLEANUP_COUNT, settings.QUERY_RESULTS_CLEANUP_MAX_AGE)

unused_query_results = models.QueryResult.unused(settings.QUERY_RESULTS_CLEANUP_MAX_AGE).limit(settings.QUERY_RESULTS_CLEANUP_COUNT)
total_unused_query_results = models.QueryResult.unused().count()
deleted_count = models.QueryResult.delete().where(models.QueryResult.id << unused_query_results).execute()

Expand Down

0 comments on commit 378459d

Please sign in to comment.