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

scope3data / scope3ai-py / 12953930249

24 Jan 2025 04:50PM UTC coverage: 96.405% (+15.8%) from 80.557%
12953930249

Pull #78

github

89ada1
web-flow
Merge 1e5564797 into 0cfbba85d
Pull Request #78: feat(api): synchronize api, fixes pyright issues and api example

69 of 71 new or added lines in 2 files covered. (97.18%)

48 existing lines in 10 files now uncovered.

2440 of 2531 relevant lines covered (96.4%)

3.85 hits per line

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

93.75
/scope3ai/api/tracer.py
1
from typing import List, Optional
4✔
2
from uuid import uuid4
4✔
3

4
from .typesgen import ImpactResponse, ModeledRow
4✔
5

6

7
class Tracer:
4✔
8
    def __init__(
4✔
9
        self,
10
        name: str = None,
11
        keep_traces: bool = False,
12
        client_id: Optional[str] = None,
13
        project_id: Optional[str] = None,
14
        application_id: Optional[str] = None,
15
        session_id: Optional[str] = None,
16
        trace_id: Optional[str] = None,
17
    ) -> None:
18
        from scope3ai.lib import Scope3AI
4✔
19

20
        self.trace_id = trace_id or uuid4().hex
4✔
21
        self.scope3ai = Scope3AI.get_instance()
4✔
22
        self.name = name
4✔
23
        self.keep_traces = keep_traces
4✔
24
        self.children: List[Tracer] = []
4✔
25
        self.rows: List[ModeledRow] = []
4✔
26
        self.traces = []  # type: List[Scope3AIContext]
4✔
27

28
        self.client_id = client_id
4✔
29
        self.project_id = project_id
4✔
30
        self.application_id = application_id
4✔
31
        self.session_id = session_id
4✔
32

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

37
        As the impact is computed asynchronously, this method will wait for the
38
        impact response to be available before returning it.
39
        """
40
        for trace in self.traces:
4✔
41
            trace.wait_impact(timeout)
4✔
42
        return self._impact()
4✔
43

44
    async def aimpact(self, timeout: Optional[int] = None) -> ImpactResponse:
4✔
45
        """
46
        Async version of Tracer::impact.
47
        """
UNCOV
48
        for trace in self.traces:
×
UNCOV
49
            await trace.await_impact(timeout)
×
UNCOV
50
        return self._impact()
×
51

52
    def _impact(self) -> ImpactResponse:
4✔
53
        """
54
        Return an aggregated impact response for the current tracer and its children.
55
        """
56
        all_rows = self.get_all_rows()
4✔
57
        return ImpactResponse(
4✔
58
            rows=all_rows,
59
            total_energy_wh=sum([row.total_impact.usage_energy_wh for row in all_rows]),
60
            total_gco2e=sum(
61
                [row.total_impact.usage_emissions_gco2e for row in all_rows]
62
            ),
63
            total_mlh2o=sum([row.total_impact.usage_water_ml for row in all_rows]),
64
            has_errors=any([row.error is not None for row in all_rows]),
65
        )
66

67
    def add_impact(self, impact: ModeledRow) -> None:
4✔
68
        self.rows.append(impact)
4✔
69

70
    def get_all_rows(self) -> List[ModeledRow]:
4✔
71
        all_rows = self.rows[:]
4✔
72
        for child in self.children:
4✔
73
            all_rows.extend(child.get_all_rows())
4✔
74
        return all_rows
4✔
75

76
    def _link_parent(self, parent: Optional["Tracer"]) -> None:
4✔
77
        if parent and (self not in parent.children):
4✔
78
            parent.children.append(self)
4✔
79

80
    def _unlink_parent(self, parent: Optional["Tracer"]) -> None:
4✔
81
        pass
4✔
82

83
    def _link_trace(self, trace) -> None:
4✔
84
        if trace not in self.traces:
4✔
85
            self.traces.append(trace)
4✔
86

87
    def _unlink_trace(self, trace) -> None:
4✔
88
        if self.keep_traces:
4✔
89
            return
4✔
90
        if trace in self.traces:
4✔
91
            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