|
1 | 1 | from collections import Counter, defaultdict |
2 | 2 | from dataclasses import dataclass |
3 | | -from datetime import date |
4 | | -from enum import Enum |
5 | | -from typing import Dict |
| 3 | +from typing import Dict, List |
6 | 4 |
|
7 | 5 | from git_analytics.entities import AnalyticsCommit, AnalyticsResult |
| 6 | +from git_analytics.helpers import get_number_week |
8 | 7 | from git_analytics.interfaces import CommitAnalyzer |
9 | 8 |
|
10 | 9 |
|
11 | | -class CommitType(Enum): |
12 | | - FEATURE = "feature" |
13 | | - FIX = "fix" |
14 | | - DOCS = "docs" |
15 | | - STYLE = "style" |
16 | | - REFACTOR = "refactor" |
17 | | - TEST = "test" |
18 | | - CHORE = "chore" |
19 | | - WIP = "wip" |
20 | | - MERGE = "merge" |
21 | | - UNKNOWN = "unknown" |
22 | | - |
23 | | - |
24 | 10 | @dataclass |
25 | 11 | class Result(AnalyticsResult): |
26 | | - items: Dict[date, Dict[CommitType, int]] |
| 12 | + commit_type_by_week: Dict[str, Dict[str, int]] |
| 13 | + commit_type_counter: Dict[str, int] |
| 14 | + author_commit_type_by_week: Dict[str, Dict[str, Dict[str, int]]] |
| 15 | + author_commit_type_counter: Dict[str, Dict[str, int]] |
27 | 16 |
|
28 | 17 |
|
29 | | -TYPE_COMMIT_LIST: tuple = tuple(ct.value for ct in CommitType) |
| 18 | +LIST_OF_TYPE_COMMIT: List[str] = ["feature", "fix", "docs", "style", "refactor", "test", "chore", "wip", "merge"] |
30 | 19 |
|
31 | 20 |
|
32 | | -def _get_type_list(commit_message: str): |
33 | | - result = [tag for tag in TYPE_COMMIT_LIST if tag in commit_message] |
| 21 | +def _get_type_list(commit_message: str) -> List[str]: |
| 22 | + result = [tag for tag in LIST_OF_TYPE_COMMIT if tag in commit_message.lower()] |
34 | 23 | if result: |
35 | 24 | return result |
36 | | - return [CommitType.UNKNOWN] |
| 25 | + return ["unknown"] |
37 | 26 |
|
38 | 27 |
|
39 | 28 | class CommitTypeAnalyzer(CommitAnalyzer): |
40 | 29 | name = "commit_type" |
41 | 30 |
|
42 | 31 | def __init__(self) -> None: |
43 | | - self._by_date: Dict[date, Counter] = defaultdict(Counter) |
| 32 | + self._commit_type_by_week: Dict[str, Counter] = defaultdict(Counter) |
| 33 | + self._commit_type_counter: Counter = Counter() |
| 34 | + self._author_commit_type_by_week: Dict[str, Dict[str, Counter]] = defaultdict(lambda: defaultdict(Counter)) |
| 35 | + self._author_commit_type_counter: Dict[str, Counter] = defaultdict(Counter) |
44 | 36 |
|
45 | 37 | def process(self, commit: AnalyticsCommit) -> None: |
46 | | - commit_date = commit.committed_datetime.date() |
| 38 | + week_number = get_number_week(commit.committed_datetime) |
47 | 39 | commit_types = _get_type_list(commit.message) |
48 | 40 | for commit_type in commit_types: |
49 | | - self._by_date[commit_date][commit_type] += 1 |
| 41 | + self._commit_type_by_week[week_number][commit_type] += 1 |
| 42 | + self._commit_type_counter[commit_type] += 1 |
| 43 | + self._author_commit_type_by_week[commit.commit_author][week_number][commit_type] += 1 |
| 44 | + self._author_commit_type_counter[commit.commit_author][commit_type] += 1 |
50 | 45 |
|
51 | 46 | def result(self) -> Result: |
52 | | - return Result(items={dt: dict(counter) for dt, counter in self._by_date.items()}) |
| 47 | + return Result( |
| 48 | + commit_type_by_week={wn: dict(sorted(c.items())) for wn, c in sorted(self._commit_type_by_week.items())}, |
| 49 | + commit_type_counter=dict(sorted(self._commit_type_counter.items())), |
| 50 | + author_commit_type_by_week={ |
| 51 | + a: {wn: dict(sorted(c.items())) for wn, c in sorted(weeks.items())} |
| 52 | + for a, weeks in sorted(self._author_commit_type_by_week.items()) |
| 53 | + }, |
| 54 | + author_commit_type_counter={ |
| 55 | + a: dict(sorted(c.items())) for a, c in sorted(self._author_commit_type_counter.items()) |
| 56 | + }, |
| 57 | + ) |
0 commit comments