fix: add analyzer modules
This commit is contained in:
@@ -1,77 +1,67 @@
|
|||||||
from collections import defaultdict
|
from dataclasses import dataclass
|
||||||
from typing import Dict
|
from typing import Dict, Optional
|
||||||
|
|
||||||
from src.analyzers.git_repository import GitRepository
|
from src.analyzers.git_repository import GitRepository
|
||||||
from src.models import VelocityAnalysis
|
from src.models.data_structures import VelocityAnalysis
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class VelocityAnalyzer:
|
class VelocityAnalyzer:
|
||||||
"""Analyze development velocity and productivity."""
|
"""Analyzes team velocity."""
|
||||||
|
|
||||||
def __init__(
|
repo: GitRepository
|
||||||
self,
|
days: int
|
||||||
repo: GitRepository,
|
|
||||||
days: int = 30,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize VelocityAnalyzer."""
|
|
||||||
self.repo = repo
|
|
||||||
self.days = days
|
|
||||||
|
|
||||||
def analyze(self) -> VelocityAnalysis:
|
def analyze(self) -> Optional[VelocityAnalysis]:
|
||||||
"""Analyze velocity metrics."""
|
"""Analyze team velocity."""
|
||||||
commits = self.repo.get_commits(since_days=self.days)
|
commits = self.repo.get_commits()
|
||||||
|
|
||||||
if not commits:
|
if not commits:
|
||||||
return VelocityAnalysis()
|
return None
|
||||||
|
|
||||||
total_commits = len(commits)
|
authors = self.repo.get_authors()
|
||||||
commits_per_day = total_commits / max(self.days, 1)
|
authors.sort(key=lambda a: a.commit_count, reverse=True)
|
||||||
commits_per_week = total_commits / max(self.days / 7, 1)
|
top_contributors = authors[:10]
|
||||||
|
|
||||||
daily_totals = defaultdict(int)
|
commits_per_day = len(commits) / max(1, self.days)
|
||||||
weekly_totals = defaultdict(int)
|
commits_per_week = commits_per_day * 7
|
||||||
|
commits_per_month = commits_per_day * 30
|
||||||
|
|
||||||
day_counts = defaultdict(int)
|
commits_by_day: Dict[str, int] = {}
|
||||||
hour_counts = defaultdict(int)
|
commits_by_hour: Dict[str, int] = {}
|
||||||
|
|
||||||
for commit in commits:
|
for commit in commits:
|
||||||
day_key = commit.author_datetime.strftime("%Y-%m-%d")
|
day_key = commit.timestamp.strftime("%A")
|
||||||
week_key = commit.author_datetime.strftime("%Y-W%W")
|
hour_key = commit.timestamp.strftime("%H:00")
|
||||||
day_name = commit.author_datetime.strftime("%A")
|
|
||||||
hour = commit.author_datetime.strftime("%H:00")
|
|
||||||
|
|
||||||
daily_totals[day_key] += 1
|
commits_by_day[day_key] = commits_by_day.get(day_key, 0) + 1
|
||||||
weekly_totals[week_key] += 1
|
commits_by_hour[hour_key] = commits_by_hour.get(hour_key, 0) + 1
|
||||||
day_counts[day_name] += 1
|
|
||||||
hour_counts[hour] += 1
|
|
||||||
|
|
||||||
most_active_day = max(day_counts, key=day_counts.get) if day_counts else ""
|
most_active_day = max(commits_by_day, key=commits_by_day.get) if commits_by_day else "N/A"
|
||||||
most_active_hour = max(hour_counts, key=hour_counts.get) if hour_counts else ""
|
most_active_hour = max(commits_by_hour, key=commits_by_hour.get) if commits_by_hour else "N/A"
|
||||||
|
|
||||||
if len(weekly_totals) >= 2:
|
if len(commits) >= 2:
|
||||||
weeks = sorted(weekly_totals.keys())
|
recent_commits = commits[:10]
|
||||||
recent_weeks = weeks[-2:]
|
older_commits = commits[10:20]
|
||||||
if len(recent_weeks) == 2:
|
|
||||||
current = weekly_totals[recent_weeks[1]]
|
recent_avg = sum(1 for _ in recent_commits) / max(1, len(recent_commits))
|
||||||
previous = weekly_totals[recent_weeks[0]]
|
older_avg = sum(1 for _ in older_commits) / max(1, len(older_commits))
|
||||||
if current > previous:
|
|
||||||
|
if recent_avg > older_avg * 1.1:
|
||||||
velocity_trend = "increasing"
|
velocity_trend = "increasing"
|
||||||
elif current < previous:
|
elif recent_avg < older_avg * 0.9:
|
||||||
velocity_trend = "decreasing"
|
velocity_trend = "decreasing"
|
||||||
else:
|
else:
|
||||||
velocity_trend = "stable"
|
velocity_trend = "stable"
|
||||||
else:
|
else:
|
||||||
velocity_trend = "stable"
|
velocity_trend = "stable"
|
||||||
else:
|
|
||||||
velocity_trend = "stable"
|
|
||||||
|
|
||||||
return VelocityAnalysis(
|
return VelocityAnalysis(
|
||||||
total_commits=total_commits,
|
commits_per_day=commits_per_day,
|
||||||
commits_per_day=round(commits_per_day, 2),
|
commits_per_week=commits_per_week,
|
||||||
commits_per_week=round(commits_per_week, 2),
|
commits_per_month=commits_per_month,
|
||||||
velocity_trend=velocity_trend,
|
velocity_trend=velocity_trend,
|
||||||
|
top_contributors=top_contributors,
|
||||||
most_active_day=most_active_day,
|
most_active_day=most_active_day,
|
||||||
most_active_hour=most_active_hour,
|
most_active_hour=most_active_hour,
|
||||||
weekly_totals=dict(weekly_totals),
|
|
||||||
daily_totals=dict(daily_totals),
|
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user