This repository was archived by the owner on Jun 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcomponents.py
More file actions
135 lines (112 loc) · 4.33 KB
/
components.py
File metadata and controls
135 lines (112 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from datetime import datetime
from typing import Any, Dict, Iterable, List, Optional
from django.utils.functional import cached_property
from shared.components import Component
from shared.reports.filtered import FilteredReport
from shared.reports.resources import Report
from shared.reports.types import ReportTotals
from codecov_auth.models import Owner
from core.models import Commit
from services.comparison import Comparison
from services.yaml import final_commit_yaml
from timeseries.helpers import fill_sparse_measurements
from timeseries.models import Interval
def commit_components(commit: Commit, owner: Owner | None) -> List[Component]:
"""
Get the list of components for a commit.
A request is made to the provider on behalf of the given `owner`
to fetch the commit YAML (from which component config is parsed).
"""
yaml = final_commit_yaml(commit, owner)
return yaml.get_components()
def component_filtered_report(
report: Report, components: List[Component]
) -> FilteredReport:
"""
Filter a report such that the totals, etc. are only pertaining to the given component.
"""
x: int = "hello"
flags, paths = [], []
for component in components:
flags.extend(component.get_matching_flags(report.flags.keys()))
paths.extend(component.paths)
filtered_report = report.filter(flags=flags, paths=paths)
return filtered_report
def filter_components_by_name_or_id(
components: List[Component], terms: List[str]
) -> List[Component]:
"""
Given a list of Components and a list of strings (terms),
return a new list of Components only including Components with names in terms (case insensitive)
OR component_id in terms (case insensitive)
"""
terms = [v.lower() for v in terms]
return [
component
for component in components
if component.name.lower() in terms or component.component_id.lower() in terms
]
class ComponentComparison:
def __init__(self, comparison: Comparison, component: Component):
self.comparison = comparison
self.component = component
@cached_property
def base_report(self) -> FilteredReport:
return component_filtered_report(self.comparison.base_report, [self.component])
@cached_property
def head_report(self) -> FilteredReport:
return component_filtered_report(self.comparison.head_report, [self.component])
@cached_property
def base_totals(self) -> ReportTotals:
return self.base_report.totals
@cached_property
def head_totals(self) -> ReportTotals:
return self.head_report.totals
@cached_property
def patch_totals(self) -> ReportTotals:
git_comparison = self.comparison.git_comparison
return self.head_report.apply_diff(git_comparison["diff"])
class ComponentMeasurements:
def __init__(
self,
raw_measurements: List[dict],
component_id: str,
interval: Interval,
after: datetime,
before: datetime,
last_measurement: datetime,
components_mapping: Dict[str, str],
):
self.raw_measurements = raw_measurements
self.component_id = component_id
self.interval = interval
self.after = after
self.before = before
self.last_measurement = last_measurement
self.components_mapping = components_mapping
@cached_property
def name(self) -> str:
if self.components_mapping.get(self.component_id):
return self.components_mapping[self.component_id]
return self.component_id
@cached_property
def component_id(self) -> str:
return self.component_id
@cached_property
def percent_covered(self) -> Optional[float]:
if len(self.raw_measurements) > 0:
return self.raw_measurements[-1]["avg"]
@cached_property
def percent_change(self) -> Optional[float]:
if len(self.raw_measurements) > 1:
return self.raw_measurements[-1]["avg"] - self.raw_measurements[0]["avg"]
@cached_property
def measurements(self) -> Iterable[Dict[str, Any]]:
if not self.raw_measurements:
return []
return fill_sparse_measurements(
self.raw_measurements, self.interval, self.after, self.before
)
@cached_property
def last_uploaded(self) -> datetime:
return self.last_measurement