diff --git a/CHANGELOG.md b/CHANGELOG.md index 31b3a7c..9f5a32e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - [ ] Support multiple models for each target - [ ] Play button to start time dimension (not very useful?) +- [ ] Optimize CSV generation (with some vectorization using numpy) ## 1.0.0 @@ -13,11 +14,12 @@ - [x] Start/Stop datetime fields - [x] Retry CSV generation when it fails due to a bug in AMDA's API - [x] Remove duplicate NetCDFs from AMDA's API response -- [ ] Optimize CSV generation (with some vectorization using numpy) +- [ ] Add a version number somewhere +- [ ] Move the visitors counter to the footer - [ ] Cache cleanup - - [ ] API at /cache/cleanup + - [x] API at /cache/clear - [ ] CRON statement to call it -- [ ] Download raw data (as CSV) for current time interval and targets +- [ ] Download raw data (tarball of CSV) for current time interval and targets - [ ] Same via SAMP - [ ] Credit the author of the pixel art planets - [ ] Set the log level to _error_ in production (see `web/run.py`) diff --git a/web/run.py b/web/run.py index 99386f2..b57466f 100755 --- a/web/run.py +++ b/web/run.py @@ -4,7 +4,7 @@ import StringIO from math import sqrt from os import listdir, environ, remove as removefile -from os.path import isfile, join, abspath, dirname +from os.path import isfile, join, abspath, dirname, isdir import csv import json @@ -368,6 +368,34 @@ def generate_csv_file_if_needed(target_config, started_at, stopped_at): abort(500, "Failed creating CSV '%s' : %s" % (filename, e)) +def remove_files_created_before(date, in_directory): + """ + Will throw if something horrible happens, like invalid parameters. + :param date: datetime object + :param in_directory: + :return: + """ + import os + import time + + secs = time.mktime(date.timetuple()) + + if not isdir(in_directory): + raise ValueError("Directory to clean '%s' does not exist.") + + removed_files = [] + for file_name in os.listdir(in_directory): + file_path = os.path.join(in_directory, file_name) + if not os.path.isfile(file_path): + continue + t = os.stat(file_path) + if t.st_ctime < secs: + removed_files.append(file_path) + os.remove(file_path) + + return removed_files + + def increment_hit_counter(): hit_count_path = get_path("../VISITS") @@ -494,6 +522,20 @@ def download_targets_tarball(targets, started_at, stopped_at): return send_from_directory(get_path("../cache/"), gzip_filename) + +# API ######################################################################### + +@app.route("/cache/clear") +def cache_clear(): + """ + Removes all files from the cache that are older than roughly one month. + """ + a_month_ago = datetime.datetime.now() - datetime.timedelta(days=32) + cache_dir = get_path('../cache') + removed_files = remove_files_created_before(a_month_ago, cache_dir) + return "Cache cleared! Removed %d old file(s)." % len(removed_files) + + # DEV TOOLS ################################################################### # @app.route("/inspect") -- libgit2 0.21.2