fix: add analyzer modules
Some checks failed
CI / build (push) Has been cancelled
CI / test (push) Has been cancelled

This commit is contained in:
2026-02-01 08:19:56 +00:00
parent 9e67d25517
commit 0ceab37f63

View File

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