Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Merged
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
72 changes: 72 additions & 0 deletions graphs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from rest_framework.negotiation import DefaultContentNegotiation
from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from shared.metrics import Counter, inc_counter

from api.shared.mixins import RepoPropertyMixin
from core.models import Branch, Pull
Expand All @@ -19,6 +20,21 @@

log = logging.getLogger(__name__)

FLARE_USE_COUNTER = Counter(
"graph_activity",
"How are graphs and flare being used?",
[
"position",
],
)
FLARE_SUCCESS_COUNTER = Counter(
"graph_success",
"How often are graphs successfully generated?",
[
"graph_type",
],
)


class IgnoreClientContentNegotiation(DefaultContentNegotiation):
def select_parser(self, request, parsers):
Expand Down Expand Up @@ -151,7 +167,16 @@ def get_object(self, request, *args, **kwargs):
options = dict()
graph = self.kwargs.get("graph")

# a flare graph has been requested
inc_counter(FLARE_USE_COUNTER, labels=dict(position=0))
log.info(
msg="flare graph activity",
extra=dict(position="start", graph_type=graph, kwargs=self.kwargs),
)

flare = self.get_flare()
# flare success, will generate and return graph
inc_counter(FLARE_USE_COUNTER, labels=dict(position=20))

if graph == "tree":
options["width"] = int(
Expand All @@ -164,6 +189,11 @@ def get_object(self, request, *args, **kwargs):
"height", settings["sunburst"]["options"]["height"] or 100
)
)
inc_counter(FLARE_SUCCESS_COUNTER, labels=dict(graph_type=graph))
log.info(
msg="flare graph activity",
extra=dict(position="success", graph_type=graph, kwargs=self.kwargs),
)
return tree(flare, None, None, **options)
elif graph == "icicle":
options["width"] = int(
Expand All @@ -176,6 +206,11 @@ def get_object(self, request, *args, **kwargs):
"height", settings["icicle"]["options"]["height"] or 100
)
)
inc_counter(FLARE_SUCCESS_COUNTER, labels=dict(graph_type=graph))
log.info(
msg="flare graph activity",
extra=dict(position="success", graph_type=graph, kwargs=self.kwargs),
)
return icicle(flare, **options)
elif graph == "sunburst":
options["width"] = int(
Expand All @@ -188,16 +223,27 @@ def get_object(self, request, *args, **kwargs):
"height", settings["sunburst"]["options"]["height"] or 100
)
)
inc_counter(FLARE_SUCCESS_COUNTER, labels=dict(graph_type=graph))
log.info(
msg="flare graph activity",
extra=dict(position="success", graph_type=graph, kwargs=self.kwargs),
)
return sunburst(flare, **options)

def get_flare(self):
pullid = self.kwargs.get("pullid")

if not pullid:
# pullid not in kwargs, try to generate flare from commit
inc_counter(FLARE_USE_COUNTER, labels=dict(position=12))
return self.get_commit_flare()
else:
# pullid was included in the request
inc_counter(FLARE_USE_COUNTER, labels=dict(position=1))
pull_flare = self.get_pull_flare(pullid)
if pull_flare is None:
# failed to get flare from pull OR commit - graph request failed
inc_counter(FLARE_USE_COUNTER, labels=dict(position=15))
raise NotFound(
"Not found. Note: private repositories require ?token arguments"
)
Expand All @@ -207,47 +253,73 @@ def get_commit_flare(self):
commit = self.get_commit()

if commit is None:
# could not find a commit - graph request failed
inc_counter(FLARE_USE_COUNTER, labels=dict(position=13))
raise NotFound(
"Not found. Note: private repositories require ?token arguments"
)

# will attempt to build a report from a commit
inc_counter(FLARE_USE_COUNTER, labels=dict(position=10))
report = report_service.build_report_from_commit(commit)

if report is None:
# report generation failed
inc_counter(FLARE_USE_COUNTER, labels=dict(position=14))
raise NotFound("Not found. Note: file for chunks not found in storage")

# report successfully generated
inc_counter(FLARE_USE_COUNTER, labels=dict(position=11))
return report.flare(None, [70, 100])

def get_pull_flare(self, pullid):
try:
repo = self.repo
# repo was included
inc_counter(FLARE_USE_COUNTER, labels=dict(position=2))
except Http404:
return None
pull = Pull.objects.filter(pullid=pullid, repository_id=repo.repoid).first()
if pull is not None:
# pull found
inc_counter(FLARE_USE_COUNTER, labels=dict(position=3))
if pull._flare is not None or pull._flare_storage_path is not None:
# pull has flare
inc_counter(FLARE_USE_COUNTER, labels=dict(position=4))
return pull.flare
# pull not found or pull does not have flare, try to generate flare
inc_counter(FLARE_USE_COUNTER, labels=dict(position=5))
return self.get_commit_flare()

def get_commit(self):
try:
repo = self.repo
# repo included in request
inc_counter(FLARE_USE_COUNTER, labels=dict(position=6))
except Http404:
return None
if repo.private and repo.image_token != self.request.query_params.get("token"):
# failed auth
inc_counter(FLARE_USE_COUNTER, labels=dict(position=7))
return None

commitid = self.kwargs.get("commit")
if commitid:
# commitid included on request
inc_counter(FLARE_USE_COUNTER, labels=dict(position=8))
commit = repo.commits.filter(commitid=commitid).first()
else:
branch_name = self.kwargs.get("branch") or repo.branch
branch = Branch.objects.filter(
name=branch_name, repository_id=repo.repoid
).first()
if branch is None:
# failed to get a commit
inc_counter(FLARE_USE_COUNTER, labels=dict(position=16))
return None

# found a commit by finding a branch
inc_counter(FLARE_USE_COUNTER, labels=dict(position=9))
commit = repo.commits.filter(commitid=branch.head).first()

return commit
Loading