Add models, analyzers, and formatters
Some checks failed
CI / test (push) Has been cancelled

This commit is contained in:
2026-02-01 07:57:59 +00:00
parent 568f75ff29
commit 6e5424b2de

View File

@@ -1,63 +1,79 @@
from collections import defaultdict from collections import defaultdict
from dataclasses import dataclass from datetime import datetime
from typing import Dict, List, Optional from typing import Dict, List
from src.analyzers.git_repository import GitRepository from src.analyzers.git_repository import GitRepository
from src.models.data_structures import CommitAnalysis from src.models import Author, CommitAnalysis
@dataclass
class CommitPatternAnalyzer: class CommitPatternAnalyzer:
"""Analyzes commit patterns.""" """Analyze commit patterns and statistics."""
repo: GitRepository def __init__(
days: int self,
repo: GitRepository,
days: int = 30,
) -> None:
"""Initialize CommitPatternAnalyzer."""
self.repo = repo
self.days = days
def analyze(self) -> Optional[CommitAnalysis]: def analyze(self) -> CommitAnalysis:
"""Analyze commit patterns.""" """Analyze commit patterns."""
commits = self.repo.get_commits() commits = self.repo.get_commits(since_days=self.days)
if not commits: if not commits:
return None return CommitAnalysis()
commits_by_hour: Dict[str, int] = defaultdict(int) authors = defaultdict(lambda: {"count": 0, "lines_added": 0, "lines_deleted": 0})
commits_by_day: Dict[str, int] = defaultdict(int)
commits_by_week: Dict[str, int] = defaultdict(int)
author_commits: Dict[str, List[str]] = defaultdict(list) commits_by_hour = defaultdict(int)
commits_by_day = defaultdict(int)
commits_by_week = defaultdict(int)
commits_by_month = defaultdict(int)
for commit in commits: for commit in commits:
hour_key = commit.timestamp.strftime("%H:00") hour = commit.author_datetime.strftime("%H:00")
day_key = commit.timestamp.strftime("%A") day = commit.author_datetime.strftime("%A")
week_key = commit.timestamp.strftime("%Y-W%U") week = commit.author_datetime.strftime("%Y-W%W")
month = commit.author_datetime.strftime("%Y-%m")
commits_by_hour[hour_key] += 1 commits_by_hour[hour] += 1
commits_by_day[day_key] += 1 commits_by_day[day] += 1
commits_by_week[week_key] += 1 commits_by_week[week] += 1
commits_by_month[month] += 1
author_commits[commit.author_email].append(commit.sha) author_key = f"{commit.author_name} <{commit.author_email}>"
authors[author_key]["count"] += 1
authors[author_key]["lines_added"] += commit.additions
authors[author_key]["lines_deleted"] += commit.deletions
authors = [] top_authors = [
for email, commit_shas in author_commits.items(): Author(
author = self.repo.get_authors() name=name.split(" <")[0],
for a in author: email=name.split("<")[1].rstrip(">") if "<" in name else "",
if a.email == email: commit_count=data["count"],
a.commit_count = len(commit_shas) lines_added=data["lines_added"],
authors.append(a) lines_deleted=data["lines_deleted"],
break )
for name, data in sorted(
authors.items(),
key=lambda x: x[1]["count"],
reverse=True,
)[:10]
]
authors.sort(key=lambda a: a.commit_count, reverse=True) unique_authors = len(authors)
top_authors = authors[:10] total_commits = len(commits)
average_commits_per_day = total_commits / max(self.days, 1)
total_days = max(1, self.days)
avg_commits_per_day = len(commits) / total_days
return CommitAnalysis( return CommitAnalysis(
total_commits=len(commits), total_commits=total_commits,
unique_authors=len(authors), unique_authors=unique_authors,
commits_by_hour=dict(commits_by_hour), commits_by_hour=dict(commits_by_hour),
commits_by_day=dict(commits_by_day), commits_by_day=dict(commits_by_day),
commits_by_week=dict(commits_by_week), commits_by_week=dict(commits_by_week),
commits_by_month=dict(commits_by_month),
top_authors=top_authors, top_authors=top_authors,
average_commits_per_day=avg_commits_per_day, average_commits_per_day=average_commits_per_day,
) )