mdbkit 0.5.4__tar.gz → 0.5.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. {mdbkit-0.5.4 → mdbkit-0.5.5}/PKG-INFO +1 -1
  2. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/__init__.py +1 -1
  3. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/render.py +14 -7
  4. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit.egg-info/PKG-INFO +1 -1
  5. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit.egg-info/SOURCES.txt +1 -0
  6. {mdbkit-0.5.4 → mdbkit-0.5.5}/pyproject.toml +1 -1
  7. mdbkit-0.5.5/tests/test_render_e2e.py +237 -0
  8. {mdbkit-0.5.4 → mdbkit-0.5.5}/LICENSE +0 -0
  9. {mdbkit-0.5.4 → mdbkit-0.5.5}/README.md +0 -0
  10. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/advisor.py +0 -0
  11. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/analysis.py +0 -0
  12. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/cli.py +0 -0
  13. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/compare.py +0 -0
  14. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/demo.py +0 -0
  15. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/explain.py +0 -0
  16. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/filtering.py +0 -0
  17. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/ftdc.py +0 -0
  18. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/lab.py +0 -0
  19. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/oslog.py +0 -0
  20. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/parser.py +0 -0
  21. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/rebuild.py +0 -0
  22. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/report.py +0 -0
  23. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/scripts.py +0 -0
  24. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/serverstatus.py +0 -0
  25. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit/triage.py +0 -0
  26. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit.egg-info/dependency_links.txt +0 -0
  27. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit.egg-info/entry_points.txt +0 -0
  28. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit.egg-info/requires.txt +0 -0
  29. {mdbkit-0.5.4 → mdbkit-0.5.5}/mdbkit.egg-info/top_level.txt +0 -0
  30. {mdbkit-0.5.4 → mdbkit-0.5.5}/setup.cfg +0 -0
  31. {mdbkit-0.5.4 → mdbkit-0.5.5}/tests/test_demo_lab.py +0 -0
  32. {mdbkit-0.5.4 → mdbkit-0.5.5}/tests/test_explain.py +0 -0
  33. {mdbkit-0.5.4 → mdbkit-0.5.5}/tests/test_ftdc.py +0 -0
  34. {mdbkit-0.5.4 → mdbkit-0.5.5}/tests/test_mdbkit.py +0 -0
  35. {mdbkit-0.5.4 → mdbkit-0.5.5}/tests/test_oslog_serverstatus.py +0 -0
  36. {mdbkit-0.5.4 → mdbkit-0.5.5}/tests/test_rebuild_report.py +0 -0
  37. {mdbkit-0.5.4 → mdbkit-0.5.5}/tests/test_triage.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mdbkit
3
- Version: 0.5.4
3
+ Version: 0.5.5
4
4
  Summary: Offline toolkit for MongoDB 4.4+ structured logs: log analysis, slow-query shapes, connection churn, and deterministic index advice. A spiritual successor to mtools' log tools.
5
5
  Author: Saqib Ameen Subhan
6
6
  License: MIT
@@ -1,3 +1,3 @@
1
1
  """mdbkit — an offline toolkit for MongoDB structured logs and index advice."""
2
2
 
3
- __version__ = "0.5.4"
3
+ __version__ = "0.5.5"
@@ -230,13 +230,20 @@ def render_recommendations(recs: List[Recommendation], stats: ParseStats,
230
230
 
231
231
  # ----------------------------------------------------------------- FTDC ----
232
232
 
233
- def _human(n: float, label: str = "") -> str:
233
+ def _human(n, label: str = "") -> str:
234
234
  """Format an FTDC value, honouring the unit implied by its label.
235
235
 
236
236
  Only byte-valued metrics get binary-prefix formatting; a metric already
237
237
  expressed in KB or MB must not be re-scaled as if it were bytes.
238
+
239
+ Tolerant of odd input on purpose: a display helper must never be the
240
+ thing that takes down a whole command.
238
241
  """
239
- low = label.lower()
242
+ if n is None:
243
+ return "-"
244
+ if not isinstance(n, (int, float)):
245
+ return str(n)
246
+ low = str(label).lower()
240
247
  if low.endswith("bytes"):
241
248
  if n >= 2 ** 30:
242
249
  return "%.1f GiB" % (n / 2 ** 30)
@@ -286,13 +293,13 @@ def render_ftdc_summary(reader, file_count: int) -> str:
286
293
  rate = reader.rate(label)
287
294
  counters.append((
288
295
  label,
289
- _human(label, st.get("change")) if st.get("change") is not None else "-",
296
+ _human(st.get("change"), label) if st.get("change") is not None else "-",
290
297
  ("%.1f/s" % rate) if rate is not None else "-",
291
- _human(label, st.get("last"))))
298
+ _human(st.get("last"), label)))
292
299
  else:
293
- gauges.append((label, _human(label, st["min"]),
294
- _human(label, st["avg"]), _human(label, st["max"]),
295
- _human(label, st["last"])))
300
+ gauges.append((label, _human(st["min"], label),
301
+ _human(st["avg"], label), _human(st["max"], label),
302
+ _human(st["last"], label)))
296
303
 
297
304
  if gauges:
298
305
  parts.append("current values (min / average / max over the window)")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mdbkit
3
- Version: 0.5.4
3
+ Version: 0.5.5
4
4
  Summary: Offline toolkit for MongoDB 4.4+ structured logs: log analysis, slow-query shapes, connection churn, and deterministic index advice. A spiritual successor to mtools' log tools.
5
5
  Author: Saqib Ameen Subhan
6
6
  License: MIT
@@ -31,4 +31,5 @@ tests/test_ftdc.py
31
31
  tests/test_mdbkit.py
32
32
  tests/test_oslog_serverstatus.py
33
33
  tests/test_rebuild_report.py
34
+ tests/test_render_e2e.py
34
35
  tests/test_triage.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "mdbkit"
7
- version = "0.5.4"
7
+ version = "0.5.5"
8
8
  description = "Offline toolkit for MongoDB 4.4+ structured logs: log analysis, slow-query shapes, connection churn, and deterministic index advice. A spiritual successor to mtools' log tools."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -0,0 +1,237 @@
1
+ """End-to-end rendering tests.
2
+
3
+ A user hit `AttributeError: 'int' object has no attribute 'lower'` running
4
+ `mdbkit ftdc summary` on a real diagnostic.data. The cause was swapped
5
+ arguments in `render_ftdc_summary`, and 139 tests missed it because every
6
+ FTDC test stopped at the decoder and never rendered anything.
7
+
8
+ So these tests deliberately run each command the way a user does — through
9
+ the CLI, all the way to printed output — rather than testing internals. The
10
+ decoder was the hard part and it was thoroughly covered; the bug was in the
11
+ formatting nobody thought worth testing.
12
+ """
13
+
14
+ import json
15
+ import struct
16
+ import zlib
17
+
18
+ import pytest
19
+
20
+ from mdbkit.cli import main
21
+ from mdbkit.demo import DemoLog, write_extras
22
+ from mdbkit.render import _human
23
+
24
+ from test_ftdc import enc_doc, rle_varint
25
+
26
+
27
+ # --------------------------------------------------------------- fixtures ---
28
+
29
+ def make_ftdc(tmp_path, samples=4):
30
+ """A small but structurally realistic diagnostic.data directory."""
31
+ from mdbkit.ftdc import numeric_metrics, parse_document
32
+ ref = enc_doc([
33
+ ("start", 0),
34
+ ("serverStatus", {
35
+ "connections": {"current": 42, "available": 900,
36
+ "totalCreated": 5000},
37
+ "opcounters": {"query": 1000, "insert": 50, "update": 7,
38
+ "delete": 2, "getmore": 9, "command": 300},
39
+ "globalLock": {"currentQueue": {"readers": 0, "writers": 0}},
40
+ "wiredTiger": {
41
+ "cache": {"bytes currently in the cache": 2000000000,
42
+ "maximum bytes configured": 8589934592,
43
+ "tracked dirty bytes in the cache": 100000000},
44
+ "transaction": {
45
+ "transaction checkpoint most recent time (msecs)": 900},
46
+ },
47
+ "mem": {"resident": 4096, "virtual": 9000},
48
+ }),
49
+ ("systemMetrics", {
50
+ "cpu": {"user_ms": 1000, "system_ms": 500, "iowait_ms": 20},
51
+ "disks": {"sda": {"io_time_ms": 100, "reads": 40, "writes": 12,
52
+ "read_time_ms": 80, "write_time_ms": 30}},
53
+ }),
54
+ ])
55
+ parsed, _ = parse_document(ref)
56
+ leaves = numeric_metrics(parsed)
57
+ deltas = [1] * (len(leaves) * (samples - 1))
58
+ payload = ref + struct.pack("<II", len(leaves), samples - 1) \
59
+ + rle_varint(deltas)
60
+ blob = struct.pack("<I", len(payload)) + zlib.compress(payload)
61
+ d = tmp_path / "diagnostic.data"
62
+ d.mkdir()
63
+ (d / "metrics.2026-08-29T00-00-00Z-00000").write_bytes(
64
+ enc_doc([("_id", 1788000000000), ("type", 1), ("data", blob)]))
65
+ return str(d)
66
+
67
+
68
+ @pytest.fixture
69
+ def demo_log(tmp_path):
70
+ p = tmp_path / "demo.log"
71
+ p.write_text("\n".join(DemoLog(minutes=30).build()) + "\n")
72
+ return str(p)
73
+
74
+
75
+ # ------------------------------------------------------- the reported bug ---
76
+
77
+ def _row(out, label):
78
+ """The cells of one metric row, label stripped off the front."""
79
+ for line in out.splitlines():
80
+ if line.startswith(label + " ") or line.strip() == label:
81
+ return line[len(label):].split()
82
+ raise AssertionError("no row for %s in:\n%s" % (label, out))
83
+
84
+
85
+ def test_ftdc_summary_renders(tmp_path, capsys):
86
+ """The exact command that crashed for a user."""
87
+ path = make_ftdc(tmp_path)
88
+ assert main(["ftdc", "summary", path, "--all"]) == 0
89
+ out = capsys.readouterr().out
90
+ assert "mdbkit ftdc summary" in out
91
+ assert "cache.usedBytes" in out
92
+ assert "conns.current" in out
93
+
94
+
95
+ def test_ftdc_summary_every_cell_is_a_value_not_a_label(tmp_path, capsys):
96
+ """Swapping _human's arguments does not crash now that it is hardened,
97
+ it silently prints the metric name where a number belongs. So assert on
98
+ the cells, not just on the absence of an exception."""
99
+ path = make_ftdc(tmp_path)
100
+ main(["ftdc", "summary", path, "--all"])
101
+ out = capsys.readouterr().out
102
+
103
+ cache = _row(out, "cache.usedBytes")
104
+ assert cache[:2] == ["1.9", "GiB"], cache # min column, formatted
105
+ assert cache.count("GiB") == 4 # min, avg, max, last
106
+
107
+ conns = _row(out, "conns.current")
108
+ assert conns[0] == "42", conns # a number, not a label
109
+ for cell in conns:
110
+ assert "conns" not in cell
111
+ assert "GiB" not in cell
112
+
113
+
114
+ def test_ftdc_summary_separates_gauges_from_counters(tmp_path, capsys):
115
+ path = make_ftdc(tmp_path)
116
+ main(["ftdc", "summary", path, "--all"])
117
+ out = capsys.readouterr().out
118
+ assert "current values" in out
119
+ assert "cumulative since server start" in out
120
+ assert "change in window" in out
121
+
122
+
123
+ def test_ftdc_summary_does_not_rescale_non_byte_metrics(tmp_path, capsys):
124
+ """conns.current is a count, not bytes; it must never read as GiB."""
125
+ path = make_ftdc(tmp_path)
126
+ main(["ftdc", "summary", path, "--all"])
127
+ for line in capsys.readouterr().out.splitlines():
128
+ if line.startswith("conns.current"):
129
+ assert "GiB" not in line and "MiB" not in line
130
+ break
131
+ else:
132
+ pytest.fail("conns.current row missing")
133
+
134
+
135
+ def test_ftdc_timeline_and_export_render(tmp_path, capsys):
136
+ path = make_ftdc(tmp_path)
137
+ assert main(["ftdc", "timeline", path, "--all",
138
+ "--metric", "conns.current"]) == 0
139
+ assert "conns.current" in capsys.readouterr().out
140
+ assert main(["ftdc", "export", path, "--all",
141
+ "--metric", "conns.current"]) == 0
142
+ assert capsys.readouterr().out.strip()
143
+
144
+
145
+ def test_ftdc_json_is_valid(tmp_path, capsys):
146
+ path = make_ftdc(tmp_path)
147
+ assert main(["ftdc", "summary", path, "--all", "--json"]) == 0
148
+ data = json.loads(capsys.readouterr().out)
149
+ assert data["chunks"] >= 1
150
+ assert "series" in data
151
+
152
+
153
+ # ------------------------------------------- _human, the function that broke ---
154
+
155
+ def test_human_argument_order_and_tolerance():
156
+ # value first, label second — the order that was swapped
157
+ assert "GiB" in _human(2 * 1024 ** 3, "cache.usedBytes")
158
+ assert "GiB" not in _human(42, "conns.current")
159
+ # a display helper must never raise, whatever it is handed
160
+ assert _human(None) == "-"
161
+ assert _human("already a string") == "already a string"
162
+ assert _human(0, "") == "0"
163
+ assert _human(1234.5, None)
164
+
165
+
166
+ # ------------------------------------ every other command renders end to end ---
167
+
168
+ def test_all_commands_render(tmp_path, demo_log, capsys):
169
+ """Run each command through the CLI to printed output. This is the test
170
+ that would have caught the ftdc crash."""
171
+ write_extras(str(tmp_path))
172
+ idx = str(tmp_path / "indexes.json")
173
+ sch = str(tmp_path / "schema.json")
174
+ exp = str(tmp_path / "explain.json")
175
+
176
+ commands = [
177
+ ["loginfo", demo_log],
178
+ ["queries", demo_log],
179
+ ["queries", demo_log, "--shape", "1"],
180
+ ["connections", demo_log],
181
+ ["advise", demo_log, "--indexes", idx, "--schema", sch],
182
+ ["explain", exp],
183
+ ["triage", demo_log, "--window", "0", "--no-sysprobe"],
184
+ ["filter", demo_log, "--severity", "E"],
185
+ ["compare", demo_log, "--after", demo_log],
186
+ ["export-script", "indexes"],
187
+ ["export-script", "schema"],
188
+ ["export-script", "serverstatus"],
189
+ ]
190
+ for argv in commands:
191
+ assert main(argv) == 0, argv
192
+ out = capsys.readouterr().out
193
+ assert out.strip(), "%s printed nothing" % argv
194
+
195
+
196
+ def test_all_json_modes_are_valid_json(tmp_path, demo_log, capsys):
197
+ write_extras(str(tmp_path))
198
+ for argv in (
199
+ ["loginfo", demo_log, "--json"],
200
+ ["queries", demo_log, "--json"],
201
+ ["connections", demo_log, "--json"],
202
+ ["advise", demo_log, "--indexes", str(tmp_path / "indexes.json"),
203
+ "--json"],
204
+ ["explain", str(tmp_path / "explain.json"), "--json"],
205
+ ["triage", demo_log, "--window", "0", "--no-sysprobe", "--json"],
206
+ ["compare", demo_log, "--after", demo_log, "--json"],
207
+ ):
208
+ assert main(argv) == 0, argv
209
+ json.loads(capsys.readouterr().out)
210
+
211
+
212
+ def test_reports_render(tmp_path, demo_log, capsys):
213
+ for ext in ("md", "html"):
214
+ out = tmp_path / ("r." + ext)
215
+ assert main(["triage", demo_log, "--window", "0", "--no-sysprobe",
216
+ "--report", str(out)]) == 0
217
+ assert out.exists() and out.stat().st_size > 200
218
+ capsys.readouterr()
219
+
220
+
221
+ def test_serverstatus_and_oslog_render(tmp_path, capsys):
222
+ ss = tmp_path / "s.json"
223
+ ss.write_text(json.dumps({
224
+ "host": "h", "version": "7.0", "uptime": 3600,
225
+ "connections": {"current": 5, "available": 100, "totalCreated": 9},
226
+ "opcounters": {"query": 10, "insert": 2},
227
+ "wiredTiger": {"cache": {"bytes currently in the cache": 1 << 20,
228
+ "maximum bytes configured": 1 << 30}},
229
+ "mem": {"resident": 100}}))
230
+ assert main(["serverstatus", str(ss)]) == 0
231
+ assert "mdbkit serverstatus" in capsys.readouterr().out
232
+
233
+ sl = tmp_path / "syslog"
234
+ sl.write_text("Aug 29 09:14:03 h kernel: Out of memory: "
235
+ "Killed process 1 (mongod)\n")
236
+ assert main(["oslog", str(sl)]) == 0
237
+ assert "oom-kill" in capsys.readouterr().out
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes