• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

scope3data / scope3ai-py / 12817474237

16 Jan 2025 08:58PM UTC coverage: 95.822% (+15.3%) from 80.557%
12817474237

Pull #66

github

dd0110
kevdevg
fix: fixing tracers tests
Pull Request #66: fix: fix async submit impact

55 of 56 new or added lines in 10 files covered. (98.21%)

45 existing lines in 9 files now uncovered.

2110 of 2202 relevant lines covered (95.82%)

3.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
/scope3ai/api/tracer.py
1
from typing import List, Optional
4✔
2

3
from .typesgen import ImpactResponse, ModeledRow
4✔
4

5

6
class Tracer:
4✔
7
    def __init__(
4✔
8
        self,
9
        name: str = None,
10
    ) -> None:
11
        from scope3ai.lib import Scope3AI
4✔
12

13
        self.scope3ai = Scope3AI.get_instance()
4✔
14
        self.name = name
4✔
15
        self.children: List[Tracer] = []
4✔
16
        self.rows: List[ModeledRow] = []
4✔
17
        self.traces = []  # type: List[Scope3AIContext]
4✔
18

19
    def impact(self, timeout: Optional[int] = None) -> ImpactResponse:
4✔
20
        """
21
        Return an aggregated impact response for the current tracer and its children.
22

23
        As the impact is computed asynchronously, this method will wait for the
24
        impact response to be available before returning it.
25
        """
26
        for trace in self.traces:
4✔
27
            trace.wait_impact(timeout)
4✔
28
        return self._impact()
4✔
29

30
    async def aimpact(self, timeout: Optional[int] = None) -> ImpactResponse:
4✔
31
        """
32
        Async version of Tracer::impact.
33
        """
34
        for trace in self.traces:
×
35
            await trace.await_impact(timeout)
×
UNCOV
36
        return self._impact()
×
37

38
    def _impact(self) -> ImpactResponse:
4✔
39
        """
40
        Return an aggregated impact response for the current tracer and its children.
41
        """
42
        all_rows = self.get_all_rows()
4✔
43
        return ImpactResponse(
4✔
44
            rows=all_rows,
45
            total_energy_wh=sum([row.total_impact.usage_energy_wh for row in all_rows]),
46
            total_gco2e=sum(
47
                [row.total_impact.usage_emissions_gco2e for row in all_rows]
48
            ),
49
            total_mlh2o=sum([row.total_impact.usage_water_ml for row in all_rows]),
50
            has_errors=any([row.error is not None for row in all_rows]),
51
        )
52

53
    def add_impact(self, impact: ModeledRow) -> None:
4✔
54
        self.rows.append(impact)
4✔
55

56
    def get_all_rows(self) -> List[ModeledRow]:
4✔
57
        all_rows = self.rows[:]
4✔
58
        for child in self.children:
4✔
59
            all_rows.extend(child.get_all_rows())
4✔
60
        return all_rows
4✔
61

62
    def _link_parent(self, parent: Optional["Tracer"]) -> None:
4✔
63
        if parent and (self not in parent.children):
4✔
64
            parent.children.append(self)
4✔
65

66
    def _unlink_parent(self, parent: Optional["Tracer"]) -> None:
4✔
67
        pass
4✔
68

69
    def _link_trace(self, trace) -> None:
4✔
70
        if trace not in self.traces:
4✔
71
            self.traces.append(trace)
4✔
72

73
    def _unlink_trace(self, trace) -> None:
4✔
74
        if trace in self.traces:
4✔
75
            self.traces.remove(trace)
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc