Coverage for little_loops / issue_history / hotspots.py: 98%

58 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-03-04 01:17 -0600

1"""Issue history hotspot detection analysis.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6from typing import Any 

7 

8from little_loops.issue_history.models import ( 

9 CompletedIssue, 

10 Hotspot, 

11 HotspotAnalysis, 

12) 

13from little_loops.issue_history.parsing import _extract_paths_from_issue 

14 

15 

16def analyze_hotspots( 

17 issues: list[CompletedIssue], 

18 contents: dict[Path, str] | None = None, 

19) -> HotspotAnalysis: 

20 """Identify files and directories that appear repeatedly in issues. 

21 

22 Args: 

23 issues: List of completed issues 

24 contents: Pre-loaded issue file contents (path -> content) 

25 

26 Returns: 

27 HotspotAnalysis with file and directory hotspots 

28 """ 

29 file_data: dict[str, dict[str, Any]] = {} # path -> {count, ids, types} 

30 dir_data: dict[str, dict[str, Any]] = {} # dir -> {count, ids, types} 

31 

32 for issue in issues: 

33 if contents is not None and issue.path in contents: 

34 content = contents[issue.path] 

35 else: 

36 try: 

37 content = issue.path.read_text(encoding="utf-8") 

38 except Exception: 

39 continue 

40 

41 paths = _extract_paths_from_issue(content) 

42 

43 for path in paths: 

44 # Track file hotspot 

45 if path not in file_data: 

46 file_data[path] = {"count": 0, "ids": [], "types": {}} 

47 file_data[path]["count"] += 1 

48 file_data[path]["ids"].append(issue.issue_id) 

49 file_data[path]["types"][issue.issue_type] = ( 

50 file_data[path]["types"].get(issue.issue_type, 0) + 1 

51 ) 

52 

53 # Track directory hotspot 

54 if "/" in path: 

55 dir_path = "/".join(path.split("/")[:-1]) + "/" 

56 else: 

57 dir_path = "./" 

58 

59 if dir_path not in dir_data: 

60 dir_data[dir_path] = {"count": 0, "ids": [], "types": {}} 

61 if issue.issue_id not in dir_data[dir_path]["ids"]: 

62 dir_data[dir_path]["count"] += 1 

63 dir_data[dir_path]["ids"].append(issue.issue_id) 

64 dir_data[dir_path]["types"][issue.issue_type] = ( 

65 dir_data[dir_path]["types"].get(issue.issue_type, 0) + 1 

66 ) 

67 

68 # Convert to Hotspot objects 

69 file_hotspots: list[Hotspot] = [] 

70 for path, data in file_data.items(): 

71 bug_count = data["types"].get("BUG", 0) 

72 total = data["count"] 

73 bug_ratio = bug_count / total if total > 0 else 0.0 

74 

75 # Determine churn indicator 

76 if total >= 5: 

77 churn = "high" 

78 elif total >= 3: 

79 churn = "medium" 

80 else: 

81 churn = "low" 

82 

83 file_hotspots.append( 

84 Hotspot( 

85 path=path, 

86 issue_count=total, 

87 issue_ids=data["ids"], 

88 issue_types=data["types"], 

89 bug_ratio=bug_ratio, 

90 churn_indicator=churn, 

91 ) 

92 ) 

93 

94 # Convert directory data to Hotspot objects 

95 dir_hotspots: list[Hotspot] = [] 

96 for path, data in dir_data.items(): 

97 bug_count = data["types"].get("BUG", 0) 

98 total = data["count"] 

99 bug_ratio = bug_count / total if total > 0 else 0.0 

100 

101 if total >= 5: 

102 churn = "high" 

103 elif total >= 3: 

104 churn = "medium" 

105 else: 

106 churn = "low" 

107 

108 dir_hotspots.append( 

109 Hotspot( 

110 path=path, 

111 issue_count=total, 

112 issue_ids=data["ids"], 

113 issue_types=data["types"], 

114 bug_ratio=bug_ratio, 

115 churn_indicator=churn, 

116 ) 

117 ) 

118 

119 # Sort by issue count descending 

120 file_hotspots.sort(key=lambda h: -h.issue_count) 

121 dir_hotspots.sort(key=lambda h: -h.issue_count) 

122 

123 # Identify bug magnets (>60% bug ratio, at least 3 issues) 

124 bug_magnets = [h for h in file_hotspots if h.bug_ratio > 0.6 and h.issue_count >= 3] 

125 bug_magnets.sort(key=lambda h: (-h.bug_ratio, -h.issue_count)) 

126 

127 return HotspotAnalysis( 

128 file_hotspots=file_hotspots[:10], # Top 10 

129 directory_hotspots=dir_hotspots[:10], # Top 10 

130 bug_magnets=bug_magnets[:5], # Top 5 

131 )