|
| 1 | +"""DOCSIS channel health analysis with configurable thresholds.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +log = logging.getLogger("docsis.analyzer") |
| 6 | + |
| 7 | +# --- Reference thresholds --- |
| 8 | +# Downstream Power (dBmV): ideal 0, good -7..+7, marginal -10..+10 |
| 9 | +DS_POWER_WARN = 7.0 |
| 10 | +DS_POWER_CRIT = 10.0 |
| 11 | + |
| 12 | +# Upstream Power (dBmV): good 35-49, marginal 50-54, bad >54 |
| 13 | +US_POWER_WARN = 50.0 |
| 14 | +US_POWER_CRIT = 54.0 |
| 15 | + |
| 16 | +# SNR / MER (dB): good >30, marginal 25-30, bad <25 |
| 17 | +SNR_WARN = 30.0 |
| 18 | +SNR_CRIT = 25.0 |
| 19 | + |
| 20 | +# Uncorrectable errors threshold |
| 21 | +UNCORR_ERRORS_CRIT = 10000 |
| 22 | + |
| 23 | + |
| 24 | +def _parse_float(val, default=0.0): |
| 25 | + try: |
| 26 | + return float(val) |
| 27 | + except (TypeError, ValueError): |
| 28 | + return default |
| 29 | + |
| 30 | + |
| 31 | +def _channel_health(issues): |
| 32 | + """Return health string from issue list.""" |
| 33 | + if not issues: |
| 34 | + return "good" |
| 35 | + if any("critical" in i for i in issues): |
| 36 | + return "critical" |
| 37 | + return "warning" |
| 38 | + |
| 39 | + |
| 40 | +def _health_detail(issues): |
| 41 | + """Build a human-readable detail string from issue list.""" |
| 42 | + if not issues: |
| 43 | + return "" |
| 44 | + labels = [] |
| 45 | + for i in issues: |
| 46 | + if "power" in i and "critical" in i: |
| 47 | + labels.append("Power kritisch") |
| 48 | + elif "power" in i: |
| 49 | + labels.append("Power erhoeht") |
| 50 | + if "snr" in i and "critical" in i: |
| 51 | + labels.append("SNR kritisch") |
| 52 | + elif "snr" in i: |
| 53 | + labels.append("SNR niedrig") |
| 54 | + return " + ".join(labels) if labels else "" |
| 55 | + |
| 56 | + |
| 57 | +def _assess_ds_channel(ch, docsis_ver): |
| 58 | + """Assess a single downstream channel. Returns (health, health_detail).""" |
| 59 | + issues = [] |
| 60 | + power = _parse_float(ch.get("powerLevel")) |
| 61 | + |
| 62 | + if abs(power) > DS_POWER_CRIT: |
| 63 | + issues.append("power critical") |
| 64 | + elif abs(power) > DS_POWER_WARN: |
| 65 | + issues.append("power warning") |
| 66 | + |
| 67 | + if docsis_ver == "3.0" and ch.get("mse"): |
| 68 | + snr = abs(_parse_float(ch["mse"])) |
| 69 | + if snr < SNR_CRIT: |
| 70 | + issues.append("snr critical") |
| 71 | + elif snr < SNR_WARN: |
| 72 | + issues.append("snr warning") |
| 73 | + elif docsis_ver == "3.1" and ch.get("mer"): |
| 74 | + snr = _parse_float(ch["mer"]) |
| 75 | + if snr < SNR_CRIT: |
| 76 | + issues.append("snr critical") |
| 77 | + elif snr < SNR_WARN: |
| 78 | + issues.append("snr warning") |
| 79 | + |
| 80 | + return _channel_health(issues), _health_detail(issues) |
| 81 | + |
| 82 | + |
| 83 | +def _assess_us_channel(ch): |
| 84 | + """Assess a single upstream channel. Returns (health, health_detail).""" |
| 85 | + issues = [] |
| 86 | + power = _parse_float(ch.get("powerLevel")) |
| 87 | + |
| 88 | + if power > US_POWER_CRIT: |
| 89 | + issues.append("power critical") |
| 90 | + elif power > US_POWER_WARN: |
| 91 | + issues.append("power warning") |
| 92 | + |
| 93 | + return _channel_health(issues), _health_detail(issues) |
| 94 | + |
| 95 | + |
| 96 | +def analyze(data: dict) -> dict: |
| 97 | + """Analyze DOCSIS data and return structured result. |
| 98 | +
|
| 99 | + Returns dict with keys: |
| 100 | + summary: dict of summary metrics |
| 101 | + ds_channels: list of downstream channel dicts |
| 102 | + us_channels: list of upstream channel dicts |
| 103 | + """ |
| 104 | + ds = data.get("channelDs", {}) |
| 105 | + ds31 = ds.get("docsis31", []) |
| 106 | + ds30 = ds.get("docsis30", []) |
| 107 | + |
| 108 | + us = data.get("channelUs", {}) |
| 109 | + us31 = us.get("docsis31", []) |
| 110 | + us30 = us.get("docsis30", []) |
| 111 | + |
| 112 | + # --- Parse downstream channels --- |
| 113 | + ds_channels = [] |
| 114 | + for ch in ds30: |
| 115 | + power = _parse_float(ch.get("powerLevel")) |
| 116 | + snr = abs(_parse_float(ch.get("mse"))) if ch.get("mse") else None |
| 117 | + health, health_detail = _assess_ds_channel(ch, "3.0") |
| 118 | + ds_channels.append({ |
| 119 | + "channel_id": ch.get("channelID", 0), |
| 120 | + "frequency": ch.get("frequency", ""), |
| 121 | + "power": power, |
| 122 | + "modulation": ch.get("modulation") or ch.get("type", ""), |
| 123 | + "snr": snr, |
| 124 | + "correctable_errors": ch.get("corrErrors", 0), |
| 125 | + "uncorrectable_errors": ch.get("nonCorrErrors", 0), |
| 126 | + "docsis_version": "3.0", |
| 127 | + "health": health, |
| 128 | + "health_detail": health_detail, |
| 129 | + }) |
| 130 | + for ch in ds31: |
| 131 | + power = _parse_float(ch.get("powerLevel")) |
| 132 | + snr = _parse_float(ch.get("mer")) if ch.get("mer") else None |
| 133 | + health, health_detail = _assess_ds_channel(ch, "3.1") |
| 134 | + ds_channels.append({ |
| 135 | + "channel_id": ch.get("channelID", 0), |
| 136 | + "frequency": ch.get("frequency", ""), |
| 137 | + "power": power, |
| 138 | + "modulation": ch.get("modulation") or ch.get("type", ""), |
| 139 | + "snr": snr, |
| 140 | + "correctable_errors": ch.get("corrErrors", 0), |
| 141 | + "uncorrectable_errors": ch.get("nonCorrErrors", 0), |
| 142 | + "docsis_version": "3.1", |
| 143 | + "health": health, |
| 144 | + "health_detail": health_detail, |
| 145 | + }) |
| 146 | + |
| 147 | + ds_channels.sort(key=lambda c: c["channel_id"]) |
| 148 | + |
| 149 | + # --- Parse upstream channels --- |
| 150 | + us_channels = [] |
| 151 | + for ch in us30: |
| 152 | + health, health_detail = _assess_us_channel(ch) |
| 153 | + us_channels.append({ |
| 154 | + "channel_id": ch.get("channelID", 0), |
| 155 | + "frequency": ch.get("frequency", ""), |
| 156 | + "power": _parse_float(ch.get("powerLevel")), |
| 157 | + "modulation": ch.get("modulation") or ch.get("type", ""), |
| 158 | + "multiplex": ch.get("multiplex", ""), |
| 159 | + "docsis_version": "3.0", |
| 160 | + "health": health, |
| 161 | + "health_detail": health_detail, |
| 162 | + }) |
| 163 | + for ch in us31: |
| 164 | + health, health_detail = _assess_us_channel(ch) |
| 165 | + us_channels.append({ |
| 166 | + "channel_id": ch.get("channelID", 0), |
| 167 | + "frequency": ch.get("frequency", ""), |
| 168 | + "power": _parse_float(ch.get("powerLevel")), |
| 169 | + "modulation": ch.get("modulation") or ch.get("type", ""), |
| 170 | + "multiplex": ch.get("multiplex", ""), |
| 171 | + "docsis_version": "3.1", |
| 172 | + "health": health, |
| 173 | + "health_detail": health_detail, |
| 174 | + }) |
| 175 | + |
| 176 | + us_channels.sort(key=lambda c: c["channel_id"]) |
| 177 | + |
| 178 | + # --- Summary metrics --- |
| 179 | + ds_powers = [c["power"] for c in ds_channels] |
| 180 | + us_powers = [c["power"] for c in us_channels] |
| 181 | + ds_snrs = [c["snr"] for c in ds_channels if c["snr"] is not None] |
| 182 | + |
| 183 | + total_corr = sum(c["correctable_errors"] for c in ds_channels) |
| 184 | + total_uncorr = sum(c["uncorrectable_errors"] for c in ds_channels) |
| 185 | + |
| 186 | + summary = { |
| 187 | + "ds_total": len(ds_channels), |
| 188 | + "us_total": len(us_channels), |
| 189 | + "ds_power_min": round(min(ds_powers), 1) if ds_powers else 0, |
| 190 | + "ds_power_max": round(max(ds_powers), 1) if ds_powers else 0, |
| 191 | + "ds_power_avg": round(sum(ds_powers) / len(ds_powers), 1) if ds_powers else 0, |
| 192 | + "us_power_min": round(min(us_powers), 1) if us_powers else 0, |
| 193 | + "us_power_max": round(max(us_powers), 1) if us_powers else 0, |
| 194 | + "us_power_avg": round(sum(us_powers) / len(us_powers), 1) if us_powers else 0, |
| 195 | + "ds_snr_min": round(min(ds_snrs), 1) if ds_snrs else 0, |
| 196 | + "ds_snr_avg": round(sum(ds_snrs) / len(ds_snrs), 1) if ds_snrs else 0, |
| 197 | + "ds_correctable_errors": total_corr, |
| 198 | + "ds_uncorrectable_errors": total_uncorr, |
| 199 | + } |
| 200 | + |
| 201 | + # --- Overall health --- |
| 202 | + issues = [] |
| 203 | + if ds_powers and (min(ds_powers) < -DS_POWER_CRIT or max(ds_powers) > DS_POWER_CRIT): |
| 204 | + issues.append("DS Power ausserhalb Norm") |
| 205 | + if us_powers and max(us_powers) > US_POWER_CRIT: |
| 206 | + issues.append("US Power kritisch hoch") |
| 207 | + elif us_powers and max(us_powers) > US_POWER_WARN: |
| 208 | + issues.append("US Power erhoeht") |
| 209 | + if ds_snrs and min(ds_snrs) < SNR_CRIT: |
| 210 | + issues.append("SNR zu niedrig") |
| 211 | + elif ds_snrs and min(ds_snrs) < SNR_WARN: |
| 212 | + issues.append("SNR grenzwertig") |
| 213 | + if total_uncorr > UNCORR_ERRORS_CRIT: |
| 214 | + issues.append("Viele uncorrectable Errors") |
| 215 | + |
| 216 | + if not issues: |
| 217 | + summary["health"] = "Gut" |
| 218 | + elif any("kritisch" in i for i in issues): |
| 219 | + summary["health"] = "Schlecht" |
| 220 | + else: |
| 221 | + summary["health"] = "Grenzwertig" |
| 222 | + summary["health_details"] = "; ".join(issues) if issues else "Alles OK" |
| 223 | + |
| 224 | + log.info( |
| 225 | + "Analysis: DS=%d US=%d Health=%s", |
| 226 | + len(ds_channels), len(us_channels), summary["health"], |
| 227 | + ) |
| 228 | + |
| 229 | + return { |
| 230 | + "summary": summary, |
| 231 | + "ds_channels": ds_channels, |
| 232 | + "us_channels": us_channels, |
| 233 | + } |
0 commit comments