Coverage for src/qdrant_loader/core/chunking/strategy/code/metadata/maintainability.py: 83%

18 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-08 06:05 +0000

1from __future__ import annotations 

2 

3 

4def calculate_maintainability_metrics(content: str) -> dict[str, float | int]: 

5 lines = content.split("\n") 

6 non_empty_lines = [line for line in lines if line.strip()] 

7 

8 avg_line_length = sum(len(line) for line in lines) / len(lines) if lines else 0 

9 max_line_length = max(len(line) for line in lines) if lines else 0 

10 long_lines = len([line for line in lines if len(line) > 120]) 

11 code_density = len(non_empty_lines) / len(lines) if lines else 0 

12 

13 readability_score = 100 

14 if avg_line_length > 100: 

15 readability_score -= 20 

16 if max_line_length > 200: 

17 readability_score -= 15 

18 if long_lines > len(lines) * 0.3: 

19 readability_score -= 25 

20 if code_density < 0.5: 

21 readability_score -= 10 

22 

23 return { 

24 "avg_line_length": avg_line_length, 

25 "max_line_length": max_line_length, 

26 "long_lines_count": long_lines, 

27 "code_density": code_density, 

28 "readability_score": max(0, readability_score), 

29 "estimated_read_time_minutes": ( 

30 len(non_empty_lines) / 50 if non_empty_lines else 0 

31 ), 

32 }