Skip to content

Commit f67b694

Browse files
authored
chore(ci-visibility): improve segments algorithm (#5999)
CI Visibility: Improve segments algorithm so all segments are returned instead of a single segment covering them. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). ## Reviewer Checklist - [ ] Title is accurate. - [ ] No unnecessary changes are introduced. - [ ] Description motivates each change. - [ ] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Testing strategy adequately addresses listed risk(s). - [ ] Change is maintainable (easy to change, telemetry, documentation). - [ ] Release note makes sense to a user of the library. - [ ] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment.
1 parent 3cbe31f commit f67b694

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

ddtrace/internal/ci_visibility/coverage.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
from itertools import groupby
23
import json
34
import os
45
from typing import Iterable
@@ -60,15 +61,16 @@ def cover(span, root=None, **kwargs):
6061

6162

6263
def segments(lines):
63-
# type: (Iterable[int]) -> Iterable[Tuple[int, int, int, int, int]]
64+
# type: (Iterable[int]) -> List[Tuple[int, int, int, int, int]]
6465
"""Extract the relevant report data for a single file."""
65-
66-
def as_segments(it):
67-
# type: (Iterable[int]) -> Tuple[int, int, int, int, int]
68-
sequence = list(it) # type: List[int]
69-
return (sequence[0], 0, sequence[-1], 0, -1)
70-
71-
return [as_segments(sorted(lines))]
66+
_segments = []
67+
for key, g in groupby(enumerate(sorted(lines)), lambda x: x[1] - x[0]):
68+
group = list(g)
69+
start = group[0][1]
70+
end = group[-1][1]
71+
_segments.append((start, 0, end, 0, -1))
72+
73+
return _segments
7274

7375

7476
def _lines(coverage, context):
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
import pytest
3+
4+
from ddtrace.internal.ci_visibility.coverage import segments
5+
6+
7+
@pytest.mark.parametrize(
8+
"lines,expected_segments",
9+
[
10+
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [(1, 0, 10, 0, -1)]),
11+
([1, 2, 4, 5, 6, 7, 8, 9, 10], [(1, 0, 2, 0, -1), (4, 0, 10, 0, -1)]),
12+
([1, 3, 4, 5, 6, 7, 8, 9, 10], [(1, 0, 1, 0, -1), (3, 0, 10, 0, -1)]),
13+
([1, 2, 3, 4, 5, 6, 7, 8, 10], [(1, 0, 8, 0, -1), (10, 0, 10, 0, -1)]),
14+
([1, 2, 3, 4, 10, 5, 6, 7, 8], [(1, 0, 8, 0, -1), (10, 0, 10, 0, -1)]),
15+
],
16+
)
17+
def test_segments(lines, expected_segments):
18+
assert segments(lines) == expected_segments

0 commit comments

Comments
 (0)