Skip to content

Commit

Permalink
Move the rendering logic from the view to the template #1145
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Jul 29, 2024
1 parent 19deb61 commit 0d30220
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
13 changes: 8 additions & 5 deletions scanpipe/templates/scanpipe/project_dependency_tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
{{ max_depth|json_script:"max_depth" }}
<script>
const data = JSON.parse(document.getElementById("dependency_tree").textContent);
const row_count = JSON.parse(document.getElementById("row_count").textContent);
const max_depth = JSON.parse(document.getElementById("max_depth").textContent);
width = 110 * max_depth;
height = 25 * row_count;
const hierarchyData = d3.hierarchy(data);
const columnWidth = 110;
const rowWidth = 25;
const columnCount = hierarchyData.height;
const rowCount = hierarchyData.links().length;
const width = columnWidth * (columnCount + 1);
const height = rowWidth * (rowCount + 1);

function indent() {
return (root) => {
Expand All @@ -56,7 +59,7 @@
width: width,
height: height,
marks: [
Plot.tree(d3.hierarchy(data).leaves(), {
Plot.tree(hierarchyData.leaves(), {
path: (node) => node.ancestors().reverse().map(({ data: { name } }) => name).join("|"),
delimiter: "|",
treeLayout: indent,
Expand Down
14 changes: 0 additions & 14 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2297,8 +2297,6 @@ def get_context_data(self, **kwargs):
return context

context["dependency_tree"] = dependency_tree
context["max_depth"] = self.max_depth(dependency_tree)
context["row_count"] = self.count_rows(dependency_tree)
return context

def get_dependency_tree(self, project):
Expand All @@ -2321,15 +2319,3 @@ def get_node(self, package):
if children:
node["children"] = children
return node

def max_depth(self, node):
"""Recursively finds the maximum depth of the given tree."""
if children := node.get("children", []):
return 1 + max(self.max_depth(child) for child in children)
return 1

def count_rows(self, node):
count = 1 if "name" in node else 0
for child in node.get("children", []):
count += self.count_rows(child)
return count

0 comments on commit 0d30220

Please sign in to comment.