Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
STACK_DIR = 'examples'
HOST = '0.0.0.0'
PORT = 5000
JSONIFY_PRETTYPRINT_REGULAR = False
USE_HEATMAP_FILECACHE = True
JSONIFY_PRETTYPRINT_REGULAR = False
33 changes: 32 additions & 1 deletion app/util/heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import os
import gzip
import collections
from os.path import abspath, join
import json
from os.path import abspath, join, isfile, dirname, basename
from math import ceil, floor
from flask import abort

Expand Down Expand Up @@ -59,6 +60,11 @@ def read_offsets(filename):
# use cached heatmap
return heatmap_cache[path]

if config.USE_HEATMAP_FILECACHE and avail_heatmap_filecache(path):
fc = read_heatmap_filecache(path)
if fc.mtime == mtime:
return fc.heatmap

# read .gz files via a "gunzip -c" pipe
if filename.endswith(".gz"):
try:
Expand Down Expand Up @@ -109,8 +115,33 @@ def read_offsets(filename):
heatmap = collections.namedtuple('offsets', ['start', 'end', 'offsets'])(start, end, offsets)
heatmap_cache[path] = heatmap
heatmap_mtimes[path] = mtime
if config.USE_HEATMAP_FILECACHE:
write_heatmap_filecache(path, heatmap, mtime)
return heatmap

def convert_path_to_filecache(path):
return dirname(path) + '/.' + basename(path) + '-heatmap-cache'


def write_heatmap_filecache(path, heatmap, mtime):
f = open(convert_path_to_filecache(path), 'wt')
f.write(json.dumps({'start': heatmap.start, 'end': heatmap.end, 'offsets': heatmap.offsets, 'mtime': mtime}))
f.close()


def read_heatmap_filecache(path):
f = open(convert_path_to_filecache(path), 'rt')
h = json.loads(f.read())
f.close()
return collections.namedtuple('cache', ['heatmap', 'mtime'])(
collections.namedtuple('offsets', ['start', 'end', 'offsets'])(h['start'], h['end'], h['offsets']),
h['mtime']
)

def avail_heatmap_filecache(path):
filename = convert_path_to_filecache(path)
return isfile(filename)

# return a heatmap from the cached offsets
def generate_heatmap(filename, rows=None):
o = read_offsets(filename)
Expand Down