1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Module defining TrimmedStat class."""
16 from __future__ import annotations
18 from dataclasses import dataclass
20 from .load_stats import LoadStats
21 from .target_spec import TargetSpec
25 class TrimmedStat(LoadStats):
26 """Load stats trimmed to a single target.
28 Useful mainly for reporting the overall results.
31 def __post_init__(self) -> None:
32 """Initialize load value and check there is one target to track."""
33 super().__post_init__()
34 if len(self.target_to_stat) != 1:
35 raise ValueError(f"No single target: {self.target_to_stat!r}")
38 def for_target(stats: LoadStats, target: TargetSpec) -> TrimmedStat:
39 """Return new instance with only one target in the mapping.
41 :param stats: The load stats instance to trim.
42 :param target: The one target which should remain in the mapping.
43 :type stats: LoadStats
44 :type target: TargetSpec
45 :return: Newly created instance.
49 rounding=stats.rounding,
50 int_load=stats.int_load,
51 target_to_stat={target: stats.target_to_stat[target]},