|
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 | | - timeseries: Dict[date, Dict[CommitType, int]] |
27 | | - total_counter: Dict[CommitType, int] |
28 | | - author_total_counter: Dict[str, 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]] |
29 | 16 |
|
30 | 17 |
|
31 | | -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"] |
32 | 19 |
|
33 | 20 |
|
34 | | -def _get_type_list(commit_message: str): |
35 | | - result = [tag for tag in TYPE_COMMIT_LIST if tag in commit_message.lower()] |
| 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()] |
36 | 23 | if result: |
37 | 24 | return result |
38 | | - return [CommitType.UNKNOWN.value] |
| 25 | + return ["unknown"] |
39 | 26 |
|
40 | 27 |
|
41 | 28 | class CommitTypeAnalyzer(CommitAnalyzer): |
42 | 29 | name = "commit_type" |
43 | 30 |
|
44 | 31 | def __init__(self) -> None: |
45 | | - self._by_date: Dict[date, Counter] = defaultdict(Counter) |
46 | | - self._total_counter: Counter = Counter() |
47 | | - self._author_total_counter: Dict[str, 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) |
48 | 36 |
|
49 | 37 | def process(self, commit: AnalyticsCommit) -> None: |
50 | | - commit_date = commit.committed_datetime.date() |
| 38 | + week_number = get_number_week(commit.committed_datetime) |
51 | 39 | commit_types = _get_type_list(commit.message) |
52 | 40 | for commit_type in commit_types: |
53 | | - self._by_date[commit_date][commit_type] += 1 |
54 | | - self._total_counter[commit_type] += 1 |
55 | | - self._author_total_counter[commit.commit_author][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 |
56 | 45 |
|
57 | 46 | def result(self) -> Result: |
58 | 47 | return Result( |
59 | | - timeseries={dt: dict(counter) for dt, counter in self._by_date.items()}, |
60 | | - author_total_counter={author: dict(counter) for author, counter in self._author_total_counter.items()}, |
61 | | - total_counter=dict(self._total_counter), |
| 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 | + }, |
62 | 57 | ) |
0 commit comments