|
| 1 | +from collections import defaultdict |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +from git_analytics.entities import AnalyticsCommit, AnalyticsResult |
| 6 | +from git_analytics.interfaces import CommitAnalyzer |
| 7 | + |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class FileExtensionChangeStats: |
| 11 | + insertions: int = 0 |
| 12 | + deletions: int = 0 |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class Result(AnalyticsResult): |
| 17 | + files_extensions_total: Dict[str, FileExtensionChangeStats] |
| 18 | + files_extensions_by_author: Dict[str, Dict[str, FileExtensionChangeStats]] |
| 19 | + |
| 20 | + |
| 21 | +def _get_file_extension(file_path: str) -> str: |
| 22 | + filename = file_path.split("/")[-1] |
| 23 | + filename_parts = filename.split(".") |
| 24 | + if len(filename_parts) == 1 or filename_parts[0] == "": |
| 25 | + return "no_extension" |
| 26 | + return filename_parts[-1].lower() |
| 27 | + |
| 28 | + |
| 29 | +class LanguageAnalyzer(CommitAnalyzer): |
| 30 | + name = "language_statistics" |
| 31 | + |
| 32 | + def __init__(self) -> None: |
| 33 | + self._total: Dict[str, FileExtensionChangeStats] = defaultdict(FileExtensionChangeStats) |
| 34 | + self._by_author: Dict[str, Dict[str, FileExtensionChangeStats]] = defaultdict( |
| 35 | + lambda: defaultdict(FileExtensionChangeStats) |
| 36 | + ) |
| 37 | + |
| 38 | + def process(self, commit: AnalyticsCommit) -> None: |
| 39 | + for changed_file in commit.files: |
| 40 | + file_extension = _get_file_extension(changed_file) |
| 41 | + self._total[file_extension].insertions += commit.files[changed_file].insertions |
| 42 | + self._total[file_extension].deletions += commit.files[changed_file].deletions |
| 43 | + self._by_author[commit.commit_author][file_extension].insertions += commit.files[changed_file].insertions |
| 44 | + self._by_author[commit.commit_author][file_extension].deletions += commit.files[changed_file].deletions |
| 45 | + |
| 46 | + def result(self) -> Result: |
| 47 | + return Result( |
| 48 | + files_extensions_total=dict(sorted(self._total.items(), key=lambda item: item[1].insertions, reverse=True)), |
| 49 | + files_extensions_by_author={ |
| 50 | + author: dict(sorted(counter.items(), key=lambda item: item[1].insertions, reverse=True)) |
| 51 | + for author, counter in self._by_author.items() |
| 52 | + }, |
| 53 | + ) |
0 commit comments