From 8e60f1670a276cac280694ddec289e724f2afe8a Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 7 Jul 2019 16:03:41 -0400 Subject: Only fetch annotations when using cold cache --- gitsrht/annotations.py | 19 +++++++++++++------ gitsrht/blueprints/repo.py | 10 ++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/gitsrht/annotations.py b/gitsrht/annotations.py index b9508fd..aa31ff9 100644 --- a/gitsrht/annotations.py +++ b/gitsrht/annotations.py @@ -200,16 +200,23 @@ def validate_annotation(valid, anno): "f'{field}' must be a string") class AnnotatedFormatter(_BaseFormatter): - def __init__(self, annos, link_prefix): + def __init__(self, get_annos, link_prefix): super().__init__() - self.annos = dict() + self.get_annos = get_annos self.link_prefix = link_prefix - for anno in (annos or list()): + + @property + def annos(self): + if hasattr(self, "_annos"): + return self._annos + self._annos = dict() + for anno in (self.get_annos() or list()): lineno = int(anno["lineno"]) - self.annos.setdefault(lineno, list()) - self.annos[lineno].append(anno) - self.annos[lineno] = sorted(self.annos[lineno], + self._annos.setdefault(lineno, list()) + self._annos[lineno].append(anno) + self._annos[lineno] = sorted(self._annos[lineno], key=lambda anno: anno.get("from", -1)) + return self._annos def _annotate_token(self, token, colno, annos): # TODO: Extend this to support >1 anno per token diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index abd156e..9c457ee 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -48,13 +48,15 @@ def get_readme(repo, tip, link_prefix=None): link_prefix=link_prefix) def _highlight_file(repo, ref, name, data, blob_id): - annotations = redis.get(f"git.sr.ht:git:annotations:{repo.id}:{blob_id}") - if annotations: - annotations = json.loads(annotations.decode()) + def get_annos(): + annotations = redis.get(f"git.sr.ht:git:annotations:{repo.id}:{blob_id}") + if annotations: + return json.loads(annotations.decode()) + return None link_prefix = url_for( 'repo.tree', owner=repo.owner, repo=repo.name, ref=ref) return get_highlighted_file("git.sr.ht:git", name, blob_id, data, - formatter=AnnotatedFormatter(annotations, link_prefix)) + formatter=AnnotatedFormatter(get_annos, link_prefix)) def render_empty_repo(owner, repo): origin = cfg("git.sr.ht", "origin") -- cgit v1.2.3