Repository navigation
Expand file tree
/
Copy pathanalyzer.py
More file actions
170 lines (144 loc) · 6.15 KB
/
Copy pathanalyzer.py
File metadata and controls
170 lines (144 loc) · 6.15 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""Aggregation layer: combines decoder + dictionary + validator (+ optional
cross_check) output into one ReadingAssessment per key.
validator.py stays a narrow rule-checker and dictionary.py stays a narrow
name lookup; this is the only place that produces a unified, still-mechanical
"suspicious but not certain" picture per reading. Keeping this layer separate
is also what makes a future diff engine cheap to add: it would just diff two
lists of ReadingAssessment, which already match report.py's JSON schema.
"""
from dataclasses import dataclass
from typing import Dict, Iterable, List, Optional
import cross_check as cross_check_module
import decoder
import dictionary
import validator
STATUS_OK = "OK"
STATUS_INVALID = "INVALID"
STATUS_OUT_OF_RANGE = "OUT_OF_RANGE"
@dataclass(frozen=True)
class ReadingAssessment:
key: str
type_code: str
raw_hex: str
raw_int: Optional[int]
decoded: object
decode_ok: bool
unit: Optional[str]
category: str
name: str
source: str
confidence: str
status: str
reason: Optional[str]
severity: int
cross_check: Optional[cross_check_module.CrossCheckResult] = None
@dataclass(frozen=True)
class Stats:
total: int
known: int
unknown: int
invalid: int
out_of_range: int
# Severity is a prioritization aid for a human investigator, not a diagnosis:
# higher means "look at this first", nothing more.
_SEVERITY_TABLE = {
("CPU", "sentinel_value"): 100,
("CPU", "no_data"): 100,
("CPU", "out_of_range"): 80,
("Fan", "sentinel_value"): 95,
("Fan", "no_data"): 95,
("Fan", "out_of_range"): 70,
("GPU", "sentinel_value"): 90,
("GPU", "no_data"): 90,
("GPU", "out_of_range"): 75,
}
_DEFAULT_KNOWN_SEVERITY = {"sentinel_value": 40, "no_data": 40, "out_of_range": 30}
_UNKNOWN_CATEGORY_SEVERITY = {"sentinel_value": 5, "no_data": 5, "out_of_range": 5}
_CONFIDENCE_SEVERITY_WEIGHT = {"HIGH": 1.0, "MEDIUM": 0.8, "LOW": 0.6, "UNKNOWN": 0.0}
def severity_score(category: str, reason: Optional[str], confidence: str = "UNKNOWN") -> int:
if reason is None:
return 0
# A reading whose *name* we don't actually know (confidence UNKNOWN)
# shouldn't outrank a curated one just because the key-namespace
# convention happened to place it in a real category -- e.g. an
# uncurated "T"+"C" key scored identically to curated CPU sensors
# before this weighting, misdirecting the "look at this first" order.
if category == "Unknown" or confidence == "UNKNOWN":
return _UNKNOWN_CATEGORY_SEVERITY.get(reason, 5)
base = _SEVERITY_TABLE.get((category, reason), _DEFAULT_KNOWN_SEVERITY.get(reason, 20))
return round(base * _CONFIDENCE_SEVERITY_WEIGHT[confidence])
def assess(
raw_reading,
reference_temps: Optional[Dict[str, float]] = None,
present_keys: Optional[Iterable[str]] = None,
) -> ReadingAssessment:
key_info = dictionary.lookup(raw_reading.key, present_keys=present_keys)
decode_result = decoder.decode(raw_reading.type_code, raw_reading.raw_bytes)
validation = validator.validate(
key=raw_reading.key,
type_code=raw_reading.type_code,
raw=raw_reading.raw_bytes,
decoded_value=decode_result.value,
category=key_info.category,
name=key_info.name,
)
if validation.is_invalid:
status = STATUS_INVALID
elif validation.is_out_of_range:
status = STATUS_OUT_OF_RANGE
else:
status = STATUS_OK
numeric = isinstance(decode_result.value, (int, float)) and not isinstance(decode_result.value, bool)
raw_int = int.from_bytes(raw_reading.raw_bytes, byteorder="big", signed=False) if numeric else None
# Same fixed-point type families encode temperature, fan RPM, current, and
# power alike (e.g. a real iMac16,1 dump: F0Ac is fpe2, IC0C is sp78) --
# the type code alone never tells you the physical quantity, only the
# "T*" key-prefix convention does. See validator.is_temperature_key.
is_temperature = decoder.is_temperature_shaped_type(raw_reading.type_code) and validator.is_temperature_key(raw_reading.key)
is_fan_speed = key_info.category == "Fan" and "speed" in key_info.name.lower()
cross = None
if reference_temps and key_info.category == "CPU" and is_temperature and numeric and status == STATUS_OK:
cross = cross_check_module.cross_validate(decode_result.value, reference_temps)
if is_temperature:
unit = "°C"
elif is_fan_speed:
unit = "RPM"
else:
unit = None
return ReadingAssessment(
key=raw_reading.key,
type_code=raw_reading.type_code,
raw_hex=raw_reading.raw_bytes.hex(),
raw_int=raw_int,
decoded=decode_result.value,
decode_ok=decode_result.ok,
unit=unit,
category=key_info.category,
name=key_info.name,
source=key_info.source,
confidence=key_info.confidence,
status=status,
reason=validation.reason,
severity=severity_score(key_info.category, validation.reason, key_info.confidence),
cross_check=cross,
)
def assess_all(raw_readings, reference_temps: Optional[Dict[str, float]] = None) -> List[ReadingAssessment]:
# Computed once and threaded through so dictionary.lookup can tell
# whether a case-variant "sibling" key is genuinely alone in this dump
# or whether multiple real variants coexist (see dictionary.py) --
# confirmed on the real dump: TM0P/TM0p/Tm0P/Tm0p are all present with
# two different value groups, which falsifies "same sensor" for that
# family and must suppress the sibling-name inference.
present_keys = {r.key for r in raw_readings}
return [assess(r, reference_temps=reference_temps, present_keys=present_keys) for r in raw_readings]
def summarize(assessments: List[ReadingAssessment]) -> Stats:
known = sum(1 for a in assessments if a.confidence != "UNKNOWN")
invalid = sum(1 for a in assessments if a.status == STATUS_INVALID)
out_of_range = sum(1 for a in assessments if a.status == STATUS_OUT_OF_RANGE)
return Stats(
total=len(assessments),
known=known,
unknown=len(assessments) - known,
invalid=invalid,
out_of_range=out_of_range,
)