|
| 1 | +from ._metrics import Metrics, SubModule |
| 2 | +from ._report import ReportingHelpers |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import List |
| 5 | +from ._graphs_presenter import GraphPresenter |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class GraphsRender: |
| 10 | + "Component responsible to generate the needed graphs for the given report." |
| 11 | + artifacts_path: str |
| 12 | + test_frameworks: List['Framework'] |
| 13 | + non_test_frameworks: List['Framework'] |
| 14 | + report: 'Report' |
| 15 | + |
| 16 | + def render_graphs(self): |
| 17 | + graph_presenter = GraphPresenter(self.artifacts_path) |
| 18 | + |
| 19 | + # Project graphs |
| 20 | + self.__project_graphs(graph_presenter=graph_presenter) |
| 21 | + |
| 22 | + # Submodules graphs |
| 23 | + self.__submodules_graphs(graph_presenter=graph_presenter) |
| 24 | + |
| 25 | + def __project_graphs(self, graph_presenter: 'GraphPresenter'): |
| 26 | + # Sorted data plots |
| 27 | + non_test_reports_sorted_data = { |
| 28 | + 'N. of classes and structs': lambda fr: fr.data.number_of_concrete_data_structures, |
| 29 | + 'Lines Of Code - LOC': lambda fr: fr.data.loc, |
| 30 | + 'Number Of Comments - NOC': lambda fr: fr.data.noc, |
| 31 | + 'N. of imports - NOI': lambda fr: fr.number_of_imports |
| 32 | + } |
| 33 | + |
| 34 | + tests_reports_sorted_data = { |
| 35 | + 'Number of tests - NOT': lambda fr: fr.data.number_of_tests |
| 36 | + } |
| 37 | + |
| 38 | + # Non-test graphs |
| 39 | + for title, framework_function in non_test_reports_sorted_data.items(): |
| 40 | + graph_presenter.sorted_data_plot(title, self.non_test_frameworks, framework_function) |
| 41 | + |
| 42 | + # Distance from the main sequence |
| 43 | + all_frameworks = self.test_frameworks + self.non_test_frameworks |
| 44 | + graph_presenter.distance_from_main_sequence_plot(self.non_test_frameworks, |
| 45 | + lambda fr: Metrics.instability(fr, all_frameworks), |
| 46 | + lambda fr: Metrics.abstractness(fr)) |
| 47 | + |
| 48 | + # Dependency graph |
| 49 | + graph_presenter.dependency_graph(self.non_test_frameworks, |
| 50 | + self.report.non_test_framework_aggregate.loc, |
| 51 | + self.report.non_test_framework_aggregate.n_o_i) |
| 52 | + |
| 53 | + # Code distribution |
| 54 | + graph_presenter.frameworks_pie_plot('Code distribution', self.non_test_frameworks, |
| 55 | + lambda fr: |
| 56 | + ReportingHelpers.decimal_format(fr.data.loc |
| 57 | + / self.report.non_test_framework_aggregate.loc)) |
| 58 | + |
| 59 | + # Test graphs |
| 60 | + for title, framework_function in tests_reports_sorted_data.items(): |
| 61 | + graph_presenter.sorted_data_plot(title, self.test_frameworks, framework_function) |
| 62 | + |
| 63 | + def __submodules_graphs(self, graph_presenter: 'GraphPresenter'): |
| 64 | + for framework in self.non_test_frameworks: |
| 65 | + GraphsRender.__render_submodules(parent='Code distribution', |
| 66 | + root_submodule=framework.submodule, |
| 67 | + graph_presenter=graph_presenter) |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def __render_submodules(parent: str, root_submodule: 'SubModule', graph_presenter: 'GraphPresenter'): |
| 71 | + current_submodule = root_submodule.next |
| 72 | + while current_submodule != root_submodule: |
| 73 | + GraphsRender.__render_submodule_loc(parent=parent, |
| 74 | + submodule=current_submodule, |
| 75 | + graph_presenter=graph_presenter) |
| 76 | + current_submodule = current_submodule.next |
| 77 | + |
| 78 | + @staticmethod |
| 79 | + def __render_submodule_loc(parent: str, submodule: 'SubModule', graph_presenter: 'GraphPresenter'): |
| 80 | + submodules = submodule.submodules |
| 81 | + if len(submodules) == 0: |
| 82 | + return |
| 83 | + total_loc = submodule.data.loc |
| 84 | + if total_loc == submodules[0].data.loc: |
| 85 | + # Single submodule folder - not useful |
| 86 | + return |
| 87 | + if len(submodule.files) > 0: |
| 88 | + # Add a submodule to represent the root slice |
| 89 | + submodules = submodules + [SubModule(name='(root)', |
| 90 | + files=submodule.files, |
| 91 | + submodules=[], |
| 92 | + parent=submodule)] |
| 93 | + |
| 94 | + chart_name = f'{parent} {submodule.path}' |
| 95 | + graph_presenter.submodules_pie_plot(chart_name, submodules, |
| 96 | + lambda s: ReportingHelpers.decimal_format(s.data.loc / total_loc)) |
0 commit comments