Add gzip support for data in static page exports.

This commit is contained in:
Tim Buckley 2015-07-29 15:01:29 -06:00
parent b3bdeabaf3
commit 79959e76dc
4 changed files with 45 additions and 18 deletions

View File

@ -1,4 +1,5 @@
import os import os
import gzip
import shutil import shutil
import django import django
@ -8,6 +9,7 @@ from django.test import RequestFactory
from django.core.urlresolvers import resolve from django.core.urlresolvers import resolve
from stackviz.parser import tempest_subunit from stackviz.parser import tempest_subunit
from stackviz import settings
EXPORT_PATHS = [ EXPORT_PATHS = [
@ -30,17 +32,31 @@ def fake_render_view(path):
return response return response
def export_single_page(path, dest_dir): def export_single_page(path, dest_dir, use_gzip=False):
dest_file = path dest_file = path
if dest_file.startswith('/'): if dest_file.startswith('/'):
dest_file = dest_file[1:] dest_file = dest_file[1:]
with open(os.path.join(dest_dir, dest_file), 'w') as f: open_func = open
if use_gzip:
open_func = gzip.open
dest_file += ".gz"
with open_func(os.path.join(dest_dir, dest_file), 'wb') as f:
content = fake_render_view(path).content content = fake_render_view(path).content
f.write(content) f.write(content)
def init_django(args):
# remove leading / from static URL to give them correct filesystem paths
settings.STATIC_URL = settings.STATIC_URL[1:]
settings.USE_GZIP = args.gzip
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "stackviz.settings")
django.setup()
def main(): def main():
parser = ArgumentParser(description="Generates a self-contained, static " parser = ArgumentParser(description="Generates a self-contained, static "
"StackViz site at the given path.") "StackViz site at the given path.")
@ -49,6 +65,9 @@ def main():
"doesn't already exist.") "doesn't already exist.")
parser.add_argument("--ignore-bower", parser.add_argument("--ignore-bower",
help="Ignore missing Bower components.") help="Ignore missing Bower components.")
parser.add_argument("--gzip",
help="Enable gzip compression for data files.",
action="store_true")
args = parser.parse_args() args = parser.parse_args()
@ -65,6 +84,8 @@ def main():
os.mkdir(args.path) os.mkdir(args.path)
init_django(args)
print "Copying static files ..." print "Copying static files ..."
shutil.copytree(os.path.join('stackviz', 'static'), shutil.copytree(os.path.join('stackviz', 'static'),
os.path.join(args.path, 'static')) os.path.join(args.path, 'static'))
@ -79,20 +100,13 @@ def main():
export_single_page('/tempest_results_%d.html' % run_id, args.path) export_single_page('/tempest_results_%d.html' % run_id, args.path)
print "Exporting data for tempest run #%d" % run_id print "Exporting data for tempest run #%d" % run_id
export_single_page('/tempest_api_tree_%d.json' % run_id, args.path) export_single_page('/tempest_api_tree_%d.json' % run_id,
export_single_page('/tempest_api_raw_%d.json' % run_id, args.path) args.path, args.gzip)
export_single_page('/tempest_api_raw_%d.json' % run_id,
# TODO args.path, args.gzip)
# export_single_page('tempest_api_details_%d.json' % run_id, args.path) export_single_page('/tempest_api_details_%d.json' % run_id,
args.path, args.gzip)
if __name__ == '__main__': if __name__ == '__main__':
# remove leading / from static URL to give them correct filesystem paths
import stackviz.settings as settings
settings.STATIC_URL = settings.STATIC_URL[1:]
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "stackviz.settings")
django.setup()
main() main()

View File

@ -1,7 +1,8 @@
from stackviz.parser.tempest_subunit import get_repositories from stackviz.parser.tempest_subunit import get_repositories
from stackviz.settings import USE_GZIP
def inject_extra_context(request): def inject_extra_context(request):
return { return {
'tempest_latest_run': get_repositories()[0].latest_id() 'tempest_latest_run': get_repositories()[0].latest_id(),
'use_gzip': USE_GZIP
} }

View File

@ -91,3 +91,8 @@ TEST_REPOSITORIES = [
'/opt/stack/tempest/', '/opt/stack/tempest/',
BASE_DIR BASE_DIR
] ]
# If true, AJAX calls should attempt to load `*.json.gz` files rather than plain
# `*.json` files. This should only ever be toggled `True` for static site
# exports and is not currently supported on live servers.
USE_GZIP = False

View File

@ -149,7 +149,14 @@ window.addEventListener('load', function() {
var selectedItem = null; var selectedItem = null;
var selectedValue = null; var selectedValue = null;
loadTimeline("tempest_api_raw_{{run_id}}.json", { var url = "tempest_api_raw_{{run_id}}.json";
if ("{{use_gzip}}" === "True") {
url += ".gz";
}
console.log("url is", url);
loadTimeline(url, {
container: $("#timeline-container")[0], container: $("#timeline-container")[0],
onClick: function(d) { onClick: function(d) {
var self = d3.select(this); var self = d3.select(this);