aibi-toolkit 1.2.0__py3-none-any.whl
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.
- aibi_toolkit/__init__.py +44 -0
- aibi_toolkit/__main__.py +12 -0
- aibi_toolkit/audit.py +46 -0
- aibi_toolkit/autofix.py +47 -0
- aibi_toolkit/cli.py +79 -0
- aibi_toolkit/cli_internal.py +97 -0
- aibi_toolkit/config.py +73 -0
- aibi_toolkit/csvio.py +54 -0
- aibi_toolkit/dax.py +88 -0
- aibi_toolkit/diagram.py +28 -0
- aibi_toolkit/filters.py +28 -0
- aibi_toolkit/integrity.py +47 -0
- aibi_toolkit/lint.py +34 -0
- aibi_toolkit/measure.py +131 -0
- aibi_toolkit/metadata.py +92 -0
- aibi_toolkit/naming.py +136 -0
- aibi_toolkit/profile.py +61 -0
- aibi_toolkit/py.typed +0 -0
- aibi_toolkit/report.py +79 -0
- aibi_toolkit/sample.py +54 -0
- aibi_toolkit/tables.py +43 -0
- aibi_toolkit-1.2.0.dist-info/METADATA +218 -0
- aibi_toolkit-1.2.0.dist-info/RECORD +27 -0
- aibi_toolkit-1.2.0.dist-info/WHEEL +5 -0
- aibi_toolkit-1.2.0.dist-info/entry_points.txt +2 -0
- aibi_toolkit-1.2.0.dist-info/licenses/LICENSE +21 -0
- aibi_toolkit-1.2.0.dist-info/top_level.txt +1 -0
aibi_toolkit/__init__.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""aibi-toolkit: helpers for BI semantic-model metadata cleanup.
|
|
2
|
+
|
|
3
|
+
API is stable as of 1.0 — new functionality lands after this point will
|
|
4
|
+
be additive.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from . import (
|
|
8
|
+
audit,
|
|
9
|
+
autofix,
|
|
10
|
+
config,
|
|
11
|
+
csvio,
|
|
12
|
+
dax,
|
|
13
|
+
diagram,
|
|
14
|
+
filters,
|
|
15
|
+
integrity,
|
|
16
|
+
lint,
|
|
17
|
+
measure,
|
|
18
|
+
metadata,
|
|
19
|
+
naming,
|
|
20
|
+
profile,
|
|
21
|
+
report,
|
|
22
|
+
sample,
|
|
23
|
+
tables,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
__version__ = "1.2.0"
|
|
27
|
+
__all__ = [
|
|
28
|
+
"audit",
|
|
29
|
+
"autofix",
|
|
30
|
+
"config",
|
|
31
|
+
"csvio",
|
|
32
|
+
"dax",
|
|
33
|
+
"diagram",
|
|
34
|
+
"filters",
|
|
35
|
+
"integrity",
|
|
36
|
+
"lint",
|
|
37
|
+
"measure",
|
|
38
|
+
"metadata",
|
|
39
|
+
"naming",
|
|
40
|
+
"profile",
|
|
41
|
+
"report",
|
|
42
|
+
"sample",
|
|
43
|
+
"tables",
|
|
44
|
+
]
|
aibi_toolkit/__main__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Allow `python -m aibi_toolkit` to behave like the `aibi` console script.
|
|
2
|
+
|
|
3
|
+
The console script is the documented entry point (`aibi ...`); this module
|
|
4
|
+
exists so that people who reach for `python -m` get the same CLI.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import sys
|
|
8
|
+
|
|
9
|
+
from aibi_toolkit.cli import main
|
|
10
|
+
|
|
11
|
+
if __name__ == "__main__":
|
|
12
|
+
sys.exit(main())
|
aibi_toolkit/audit.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from .metadata import Model
|
|
4
|
+
|
|
5
|
+
"""Model hygiene audits for semantic models."""
|
|
6
|
+
|
|
7
|
+
from .metadata import list_measures
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def audit_measures(model: Model) -> dict[str, list[str]]:
|
|
11
|
+
"""Run a measure hygiene audit and return findings by category."""
|
|
12
|
+
measures = list_measures(model)
|
|
13
|
+
findings: dict[str, list[str]] = {
|
|
14
|
+
"missing_format": [],
|
|
15
|
+
"duplicate_names": [],
|
|
16
|
+
"long_names": [],
|
|
17
|
+
}
|
|
18
|
+
seen: set[str] = set()
|
|
19
|
+
for m in measures:
|
|
20
|
+
if not m.get("formatString"):
|
|
21
|
+
findings["missing_format"].append(m["name"])
|
|
22
|
+
if m["name"].lower() in seen:
|
|
23
|
+
findings["duplicate_names"].append(m["name"])
|
|
24
|
+
seen.add(m["name"].lower())
|
|
25
|
+
if len(m["name"]) > 64:
|
|
26
|
+
findings["long_names"].append(m["name"])
|
|
27
|
+
return findings
|
|
28
|
+
|
|
29
|
+
def audit_score(model: Model) -> int:
|
|
30
|
+
"""Score 0-100 based on measure hygiene findings."""
|
|
31
|
+
measures = list_measures(model)
|
|
32
|
+
if not measures:
|
|
33
|
+
return 100
|
|
34
|
+
findings = audit_measures(model)
|
|
35
|
+
problems = sum(len(v) for v in findings.values())
|
|
36
|
+
return max(0, 100 - round(100 * problems / (2 * len(measures))))
|
|
37
|
+
|
|
38
|
+
def audit_tables(model: Model) -> dict[str, list[str]]:
|
|
39
|
+
"""Table-level findings: name issues and empty tables."""
|
|
40
|
+
out: dict[str, list[str]] = {"empty": [], "bad_names": []}
|
|
41
|
+
for t in model.get("tables", []):
|
|
42
|
+
if not t.get("columns") and not t.get("measures"):
|
|
43
|
+
out["empty"].append(t["name"])
|
|
44
|
+
if t["name"] != t["name"].strip() or " " in t["name"]:
|
|
45
|
+
out["bad_names"].append(t["name"])
|
|
46
|
+
return out
|
aibi_toolkit/autofix.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from .metadata import Model
|
|
6
|
+
|
|
7
|
+
"""Automatic naming fixes for semantic-model dumps."""
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def fix_measure_name(name: str) -> str:
|
|
11
|
+
"""Turn a raw measure name into a display-safe one."""
|
|
12
|
+
from .naming import strip_numbers, to_display_name
|
|
13
|
+
return truncate(to_display_name(strip_numbers(name)), 64)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def truncate(name: str, limit: int = 64) -> str:
|
|
17
|
+
"""Truncate without trailing separators."""
|
|
18
|
+
out = name[:limit].rstrip()
|
|
19
|
+
while out and out[-1] in "-_ ":
|
|
20
|
+
out = out[:-1].rstrip()
|
|
21
|
+
return out
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def suggest_format(expression: str) -> str:
|
|
25
|
+
"""Suggest a format string from a measure expression."""
|
|
26
|
+
from .naming import expand_abbreviations
|
|
27
|
+
lowered = expression.lower()
|
|
28
|
+
if any(k in lowered for k in ("divide", "distinctcount", "count(")):
|
|
29
|
+
return "#,0"
|
|
30
|
+
if "%" in expand_abbreviations(lowered)[:40]:
|
|
31
|
+
return "0.0%"
|
|
32
|
+
return "$#,0.00"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def autofix_model(model: Model) -> dict[str, Any]:
|
|
36
|
+
"""Apply non-destructive fixes; returns a change log."""
|
|
37
|
+
changes: list[str] = []
|
|
38
|
+
for t in model.get("tables", []):
|
|
39
|
+
for m in t.get("measures", []):
|
|
40
|
+
fixed = fix_measure_name(m["name"])
|
|
41
|
+
if fixed != m["name"]:
|
|
42
|
+
changes.append(f"measure '{m['name']}' -> '{fixed}'")
|
|
43
|
+
m["name"] = fixed
|
|
44
|
+
if not m.get("formatString"):
|
|
45
|
+
m["formatString"] = suggest_format(m.get("expression", ""))
|
|
46
|
+
changes.append(f"format for '{m['name']}' -> {m['formatString']}")
|
|
47
|
+
return {"changes": changes, "count": len(changes)}
|
aibi_toolkit/cli.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Command-line interface for aibi-toolkit."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
|
|
7
|
+
COMMANDS = (
|
|
8
|
+
("summary", "Print model summary counts"),
|
|
9
|
+
("tables", "List tables in a model"),
|
|
10
|
+
("audit", "Run measure hygiene audit"),
|
|
11
|
+
("lint", "Lint DAX expressions"),
|
|
12
|
+
("diagram", "Print mermaid ER diagram"),
|
|
13
|
+
("fix", "Propose/apply naming and format fixes"),
|
|
14
|
+
("profile", "Expression complexity profile"),
|
|
15
|
+
("export", "Export measures to CSV"),
|
|
16
|
+
("report", "Write a markdown audit report"),
|
|
17
|
+
("synth", "Generate a sample model JSON"),
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
# Commands where args.model is an input and args.out is a write target.
|
|
21
|
+
WRITE_TARGETS = ("export", "report")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def main(argv: list[str] | None = None) -> int:
|
|
25
|
+
parser = argparse.ArgumentParser(prog="aibi", description=__doc__)
|
|
26
|
+
parser.add_argument("--version", action="version", version=f"aibi-toolkit {__import__('aibi_toolkit').__version__}")
|
|
27
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
28
|
+
for cmd, help_text in COMMANDS:
|
|
29
|
+
s = sub.add_parser(cmd, help=help_text)
|
|
30
|
+
if cmd == "synth":
|
|
31
|
+
s.add_argument("out")
|
|
32
|
+
s.add_argument("--tables", type=int, default=3, help="number of tables in the sample model")
|
|
33
|
+
continue
|
|
34
|
+
s.add_argument("model")
|
|
35
|
+
if cmd in WRITE_TARGETS:
|
|
36
|
+
s.add_argument("out")
|
|
37
|
+
if cmd == "fix":
|
|
38
|
+
s.add_argument("--dry-run", action="store_true", help="print findings without applying")
|
|
39
|
+
if cmd in ("audit", "lint", "profile"):
|
|
40
|
+
s.add_argument("--json", action="store_true", help="emit machine-readable JSON instead of text")
|
|
41
|
+
args = parser.parse_args(argv)
|
|
42
|
+
return _dispatch(args)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _dispatch(args: argparse.Namespace) -> int:
|
|
46
|
+
from . import cli_internal
|
|
47
|
+
json_flag = getattr(args, "json", False)
|
|
48
|
+
if args.command == "summary":
|
|
49
|
+
from aibi_toolkit.metadata import load_model, model_summary
|
|
50
|
+
s = model_summary(load_model(args.model))
|
|
51
|
+
print(f"tables={s['tables']} measures={s['measures']} columns={s['columns']}")
|
|
52
|
+
return 0
|
|
53
|
+
if args.command == "tables":
|
|
54
|
+
from aibi_toolkit.metadata import list_tables, load_model
|
|
55
|
+
for t in list_tables(load_model(args.model)):
|
|
56
|
+
print(t)
|
|
57
|
+
return 0
|
|
58
|
+
if args.command == "audit":
|
|
59
|
+
return cli_internal.cmd_audit(args.model, json_out=json_flag)
|
|
60
|
+
if args.command == "lint":
|
|
61
|
+
return cli_internal.cmd_lint(args.model, json_out=json_flag)
|
|
62
|
+
if args.command == "export":
|
|
63
|
+
return cli_internal.cmd_export(args.model, args.out)
|
|
64
|
+
if args.command == "diagram":
|
|
65
|
+
return cli_internal.cmd_diagram(args.model)
|
|
66
|
+
if args.command == "fix":
|
|
67
|
+
return cli_internal.cmd_fix(args.model, args.dry_run)
|
|
68
|
+
if args.command == "report":
|
|
69
|
+
return cli_internal.cmd_report(args.model, args.out)
|
|
70
|
+
if args.command == "synth":
|
|
71
|
+
return cli_internal.cmd_synth(args.out, args.tables)
|
|
72
|
+
if args.command == "profile":
|
|
73
|
+
return cli_internal.cmd_profile(args.model, json_out=json_flag)
|
|
74
|
+
return 0
|
|
75
|
+
|
|
76
|
+
if __name__ == "__main__":
|
|
77
|
+
import sys
|
|
78
|
+
|
|
79
|
+
sys.exit(main())
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Internal CLI command implementations, split out for readability."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def cmd_audit(model_path: str, json_out: bool = False) -> int:
|
|
5
|
+
import json
|
|
6
|
+
|
|
7
|
+
from aibi_toolkit.audit import audit_measures, audit_score
|
|
8
|
+
from aibi_toolkit.metadata import load_model
|
|
9
|
+
model = load_model(model_path)
|
|
10
|
+
findings = audit_measures(model)
|
|
11
|
+
if json_out:
|
|
12
|
+
print(json.dumps({"score": audit_score(model), "findings": findings}, indent=2))
|
|
13
|
+
return 1 if any(findings.values()) else 0
|
|
14
|
+
for category, names in findings.items():
|
|
15
|
+
for n in names:
|
|
16
|
+
print(f"[{category}] {n}")
|
|
17
|
+
print(f"score={audit_score(model)}")
|
|
18
|
+
return 1 if any(findings.values()) else 0
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def cmd_lint(model_path: str, json_out: bool = False) -> int:
|
|
22
|
+
import json
|
|
23
|
+
|
|
24
|
+
from aibi_toolkit.lint import lint_model
|
|
25
|
+
from aibi_toolkit.metadata import load_model
|
|
26
|
+
findings = lint_model(load_model(model_path))
|
|
27
|
+
if json_out:
|
|
28
|
+
print(json.dumps({"lint": findings}, indent=2))
|
|
29
|
+
return 1 if findings else 0
|
|
30
|
+
for name, issues in findings.items():
|
|
31
|
+
for issue in issues:
|
|
32
|
+
print(f"[lint] {name}: {issue}")
|
|
33
|
+
return 1 if findings else 0
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def cmd_export(model_path: str, out_path: str) -> int:
|
|
37
|
+
from aibi_toolkit.csvio import export_measures
|
|
38
|
+
from aibi_toolkit.metadata import list_measures, load_model
|
|
39
|
+
n = export_measures(list_measures(load_model(model_path)), out_path)
|
|
40
|
+
print(f"exported {n} measures to {out_path}")
|
|
41
|
+
return 0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def cmd_diagram(model_path: str) -> int:
|
|
45
|
+
from aibi_toolkit.diagram import mermaid_er
|
|
46
|
+
from aibi_toolkit.metadata import load_model
|
|
47
|
+
print(mermaid_er(load_model(model_path)))
|
|
48
|
+
return 0
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def cmd_fix(model_path: str, dry_run: bool = False) -> int:
|
|
52
|
+
import json
|
|
53
|
+
|
|
54
|
+
from aibi_toolkit.autofix import autofix_model
|
|
55
|
+
|
|
56
|
+
from .metadata import load_model
|
|
57
|
+
model = load_model(model_path)
|
|
58
|
+
result = autofix_model(model)
|
|
59
|
+
for c in result["changes"]:
|
|
60
|
+
print(f"fixed: {c}")
|
|
61
|
+
if not dry_run and result["count"]:
|
|
62
|
+
print(json.dumps(model, indent=2))
|
|
63
|
+
return 0 if result["count"] else 1
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def cmd_report(model_path: str, out_path: str) -> int:
|
|
67
|
+
from aibi_toolkit.metadata import load_model
|
|
68
|
+
from aibi_toolkit.report import write_report
|
|
69
|
+
write_report(load_model(model_path), out_path)
|
|
70
|
+
print(f"report written to {out_path}")
|
|
71
|
+
return 0
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def cmd_synth(out_path: str, tables: int = 3) -> int:
|
|
75
|
+
import json
|
|
76
|
+
|
|
77
|
+
from aibi_toolkit.sample import sample_model
|
|
78
|
+
model = sample_model(tables=tables)
|
|
79
|
+
from pathlib import Path
|
|
80
|
+
Path(out_path).write_text(json.dumps(model, indent=2), encoding="utf-8")
|
|
81
|
+
print(f"sample model written to {out_path}")
|
|
82
|
+
return 0
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def cmd_profile(model_path: str, json_out: bool = False) -> int:
|
|
86
|
+
import json
|
|
87
|
+
|
|
88
|
+
from aibi_toolkit.metadata import load_model
|
|
89
|
+
from aibi_toolkit.profile import profile_model
|
|
90
|
+
p = profile_model(load_model(model_path))
|
|
91
|
+
if json_out:
|
|
92
|
+
print(json.dumps(p, indent=2))
|
|
93
|
+
return 0
|
|
94
|
+
print(f"count={p['count']} avg={p['avg']} max={p['max']}")
|
|
95
|
+
for name in p["heavy"]:
|
|
96
|
+
print(f"[heavy] {name}")
|
|
97
|
+
return 0
|
aibi_toolkit/config.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Per-project config for aibi-toolkit (TOML)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
import tomllib
|
|
10
|
+
except ModuleNotFoundError: # Python 3.10: stdlib tomllib landed in 3.11
|
|
11
|
+
tomllib = None # type: ignore[assignment]
|
|
12
|
+
|
|
13
|
+
DEFAULTS: dict[str, str | int] = {
|
|
14
|
+
"max_name_length": 64,
|
|
15
|
+
"default_currency": "USD",
|
|
16
|
+
"percent_decimals": 1,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _parse_simple_toml(text: str) -> dict[str, Any]:
|
|
21
|
+
"""Minimal [section] key = value parser for when tomllib is unavailable."""
|
|
22
|
+
data: dict[str, dict[str, Any]] = {}
|
|
23
|
+
section: str | None = None
|
|
24
|
+
for raw in text.splitlines():
|
|
25
|
+
line = raw.split("#", 1)[0].strip()
|
|
26
|
+
if not line:
|
|
27
|
+
continue
|
|
28
|
+
if line.startswith("[") and line.endswith("]"):
|
|
29
|
+
section = line[1:-1].strip()
|
|
30
|
+
data.setdefault(section, {})
|
|
31
|
+
elif "=" in line and section is not None:
|
|
32
|
+
key, value = (part.strip() for part in line.split("=", 1))
|
|
33
|
+
parsed: Any
|
|
34
|
+
if value.startswith(("'", '"')):
|
|
35
|
+
parsed = value[1:-1]
|
|
36
|
+
elif value.lower() in ("true", "false"):
|
|
37
|
+
parsed = value.lower() == "true"
|
|
38
|
+
else:
|
|
39
|
+
try:
|
|
40
|
+
parsed = int(value)
|
|
41
|
+
except ValueError:
|
|
42
|
+
parsed = value
|
|
43
|
+
data[section][key] = parsed
|
|
44
|
+
return data
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def load_config(path: str | Path) -> dict[str, str | int]:
|
|
48
|
+
"""Load aibi config from TOML, falling back to defaults."""
|
|
49
|
+
p = Path(path)
|
|
50
|
+
if not p.exists():
|
|
51
|
+
return dict(DEFAULTS)
|
|
52
|
+
text = p.read_text(encoding="utf-8")
|
|
53
|
+
data = tomllib.loads(text) if tomllib is not None else _parse_simple_toml(text)
|
|
54
|
+
return {**DEFAULTS, **data.get("aibi", {})}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def validate_config(cfg: dict[str, Any]) -> list[str]:
|
|
58
|
+
"""Return human-readable config errors (empty = valid)."""
|
|
59
|
+
errors: list[str] = []
|
|
60
|
+
if not isinstance(cfg.get("max_name_length"), int) or cfg["max_name_length"] < 1:
|
|
61
|
+
errors.append("max_name_length must be a positive integer")
|
|
62
|
+
if cfg.get("default_currency") not in ("USD", "EUR", "GBP", "INR"):
|
|
63
|
+
errors.append("default_currency must be one of USD/EUR/GBP/INR")
|
|
64
|
+
if not isinstance(cfg.get("percent_decimals"), int) or not 0 <= cfg["percent_decimals"] <= 4:
|
|
65
|
+
errors.append("percent_decimals must be an integer 0-4")
|
|
66
|
+
return errors
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def default_config_toml() -> str:
|
|
70
|
+
"""Render a starter aibi.toml for users."""
|
|
71
|
+
lines = ["[aibi]"]
|
|
72
|
+
lines += [f"{k} = {v!r}" for k, v in DEFAULTS.items()]
|
|
73
|
+
return "\n".join(lines) + "\n"
|
aibi_toolkit/csvio.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""CSV export/import of measure inventories."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import csv
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
Measure = dict[str, Any]
|
|
10
|
+
MeasureKey = dict[tuple[str, str], Measure]
|
|
11
|
+
Row = dict[str, str]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def export_measures(measures: list[Measure], path: str | Path) -> int:
|
|
15
|
+
"""Write measures to CSV; returns the row count."""
|
|
16
|
+
fieldnames = ["table", "name", "expression", "formatString"]
|
|
17
|
+
with Path(path).open("w", newline="", encoding="utf-8") as f:
|
|
18
|
+
w = csv.DictWriter(f, fieldnames=fieldnames, extrasaction="ignore")
|
|
19
|
+
w.writeheader()
|
|
20
|
+
for m in measures:
|
|
21
|
+
w.writerow({k: m.get(k, "") for k in fieldnames})
|
|
22
|
+
return len(measures)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
REQUIRED_FIELDS = ("table", "name", "expression")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def import_measures(path: str | Path) -> list[Measure]:
|
|
29
|
+
"""Read measures from CSV, raising on rows missing required fields."""
|
|
30
|
+
out: list[Measure] = []
|
|
31
|
+
with Path(path).open("r", newline="", encoding="utf-8") as f:
|
|
32
|
+
for i, row in enumerate(csv.DictReader(f), start=2):
|
|
33
|
+
missing = [k for k in REQUIRED_FIELDS if not row.get(k)]
|
|
34
|
+
if missing:
|
|
35
|
+
raise ValueError(f"Row {i} missing required fields: {missing}")
|
|
36
|
+
out.append(dict(row))
|
|
37
|
+
return out
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def summarize_csv(path: str | Path) -> dict[str, int]:
|
|
41
|
+
"""Return basic stats about a measures CSV: rows and tables."""
|
|
42
|
+
rows = import_measures(path)
|
|
43
|
+
return {"rows": len(rows), "tables": len({r["table"] for r in rows})}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def diff_inventories(old_path: str | Path, new_path: str | Path) -> dict[str, Any]:
|
|
47
|
+
"""Compare two measure CSVs: added, removed, changed."""
|
|
48
|
+
old: MeasureKey = {(r["table"], r["name"]): r for r in import_measures(old_path)}
|
|
49
|
+
new: MeasureKey = {(r["table"], r["name"]): r for r in import_measures(new_path)}
|
|
50
|
+
return {
|
|
51
|
+
"added": sorted(set(new) - set(old)),
|
|
52
|
+
"removed": sorted(set(old) - set(new)),
|
|
53
|
+
"changed": sorted(k for k in set(old) & set(new) if old[k] != new[k]),
|
|
54
|
+
}
|
aibi_toolkit/dax.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
"""DAX snippet builders."""
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def build_sum_measure(table: str, column: str, name: str) -> str:
|
|
7
|
+
"""SUM measure using the safe, readable pattern."""
|
|
8
|
+
return (
|
|
9
|
+
f"{name} =\n"
|
|
10
|
+
f"VAR _rows = '{table}'\n"
|
|
11
|
+
f"RETURN SUM(_rows[{column}])"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def build_distinctcount_measure(table: str, column: str, name: str) -> str:
|
|
16
|
+
"""DISTINCTCOUNT measure snippet."""
|
|
17
|
+
return f"{name} =\nDISTINCTCOUNT('{table}'[{column}])"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def build_safe_ratio(numerator: str, denominator: str, name: str, alt: str = "0") -> str:
|
|
21
|
+
"""Ratio wrapped in DIVIDE so divide-by-zero never errors."""
|
|
22
|
+
return f"{name} =\nDIVIDE([{numerator}], [{denominator}], {alt})"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def build_ytd_measure(base: str, date_column: str, name: str) -> str:
|
|
26
|
+
"""Year-to-date measure over a base measure."""
|
|
27
|
+
return (
|
|
28
|
+
f"{name} =\n"
|
|
29
|
+
f"TOTALYTD([{base}], '{date_column.split('.')[0] if '.' in date_column else date_column}')"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def build_mom_measure(base: str, name: str) -> str:
|
|
34
|
+
"""Month-over-month measure over a base measure."""
|
|
35
|
+
return (
|
|
36
|
+
f"{name} =\n"
|
|
37
|
+
f"VAR _prev = CALCULATE([{base}], PREVIOUSMONTH())\n"
|
|
38
|
+
f"RETURN DIVIDE([{base}] - _prev, _prev)"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def build_rank_measure(column: str, name: str) -> str:
|
|
43
|
+
"""Rank measure snippet."""
|
|
44
|
+
return (
|
|
45
|
+
f"{name} =\n"
|
|
46
|
+
f"RANKX(ALLSELECTED(), [{column}], , DESC, DENSE)"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def build_soy_measure(base: str, date_column: str, name: str) -> str:
|
|
51
|
+
"""Start-of-year-to-date variant alias for TOTALYTD."""
|
|
52
|
+
return build_ytd_measure(base, date_column, name)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def build_pyo_measure(base: str, date_column: str, name: str) -> str:
|
|
56
|
+
"""Prior-year-over-year measure."""
|
|
57
|
+
return (
|
|
58
|
+
f"{name} =\n"
|
|
59
|
+
f"VAR _prev = CALCULATE([{base}], SAMEPERIODLASTYEAR('{date_column}'))\n"
|
|
60
|
+
f"RETURN DIVIDE([{base}] - _prev, _prev)"
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def build_cumulative_measure(base: str, date_column: str, name: str) -> str:
|
|
65
|
+
"""Running-total measure."""
|
|
66
|
+
return (
|
|
67
|
+
f"{name} =\n"
|
|
68
|
+
f"VAR _max = MAX('{date_column}')\n"
|
|
69
|
+
f"RETURN CALCULATE([{base}], FILTER(ALL('{date_column}'), '{date_column}' <= _max))"
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def build_sliceresponsive_measure(base: str, name: str = "Default") -> str:
|
|
74
|
+
"""Baseline measure that ignores slicers for comparison views."""
|
|
75
|
+
return f"{name} =\nCALCULATE([{base}], ALLSELECTED())"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def build_quarter_measure(base: str, date_column: str, name: str) -> str:
|
|
79
|
+
"""Quarter-to-date measure."""
|
|
80
|
+
return (
|
|
81
|
+
f"{name} =\n"
|
|
82
|
+
f"TOTALQTD([{base}], '{date_column}')"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def build_previous_period(base: str, shift: str, name: str) -> str:
|
|
87
|
+
"""Shifted-period measure (e.g. previous month)."""
|
|
88
|
+
return f"{name} =\nCALCULATE([{base}], DATEADD('{shift}', -1, MONTH))"
|
aibi_toolkit/diagram.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from .metadata import Model
|
|
4
|
+
|
|
5
|
+
"""Mermaid ER-diagram export of model relationships."""
|
|
6
|
+
|
|
7
|
+
def mermaid_er(model: Model) -> str:
|
|
8
|
+
"""Render relationships as a mermaid erDiagram block."""
|
|
9
|
+
lines = ["erDiagram"]
|
|
10
|
+
for r in model.get("relationships", []):
|
|
11
|
+
f = _safe(r.get("fromTable", "?"))
|
|
12
|
+
t = _safe(r.get("toTable", "?"))
|
|
13
|
+
lines.append(f" {f} ||--o{{ {t} : has")
|
|
14
|
+
return "\n".join(lines)
|
|
15
|
+
|
|
16
|
+
def _safe(name: str) -> str:
|
|
17
|
+
return name.replace(" ", "_") or "Unknown"
|
|
18
|
+
|
|
19
|
+
def mermaid_tables(model: Model, limit: int = 20) -> str:
|
|
20
|
+
"""Render the first N tables as a mermaid graph for top-level shape."""
|
|
21
|
+
lines = ["graph TD"]
|
|
22
|
+
for i, t in enumerate(model.get("tables", [])[:limit]):
|
|
23
|
+
lines.append(f" T{i}[{_safe(t['name'])}]")
|
|
24
|
+
for r in model.get("relationships", []):
|
|
25
|
+
names = [x["name"] for x in model.get("tables", [])]
|
|
26
|
+
if r.get("fromTable") in names and r.get("toTable") in names:
|
|
27
|
+
lines.append(f" T{names.index(r['fromTable'])} --> T{names.index(r['toTable'])}")
|
|
28
|
+
return "\n".join(lines)
|
aibi_toolkit/filters.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
"""DAX filter-context snippets."""
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def build_topn_measure(measure: str, table: str, column: str, top: int = 10, name: str = "Top N") -> str:
|
|
7
|
+
"""Show a measure filtered to the top N items of a column."""
|
|
8
|
+
return (
|
|
9
|
+
f"{name} {measure} =\n"
|
|
10
|
+
f"VAR _top = TOPN({top}, ALLSELECTED('{table}'), [{measure}])\n"
|
|
11
|
+
f"RETURN CALCULATE([{measure}], KEEPFILTERS(_top))"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def build_contains_filter(table: str, column: str, values: list[str], name: str = "Filtered") -> str:
|
|
16
|
+
"""Snippet filtering a table by a list of values."""
|
|
17
|
+
joined = ", ".join(f'"{v}"' for v in values)
|
|
18
|
+
return f"{name} =\nCALCULATE([Selected Measure], FILTER('{table}', '{table}'[{column}] IN {{{joined}}}))"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def build_isfiltered_check(name: str = "Has Filter") -> str:
|
|
22
|
+
"""Guard measure that reports whether a visual has cross-filtering."""
|
|
23
|
+
return f"{name} =\nIF(ISFILTERED(), 1, 0)"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def build_remove_all(table: str, name: str = "Unfiltered") -> str:
|
|
27
|
+
"""Measure with all filters removed from a table."""
|
|
28
|
+
return f"{name} =\nCALCULATE([Selected Measure], REMOVEFILTERS('{table}'))"
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Model relationship integrity checks."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .metadata import Model
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def check_relationships(model: Model) -> dict[str, list[str]]:
|
|
9
|
+
"""Find inactive and many-to-many unsafe relationships."""
|
|
10
|
+
out: dict[str, list[str]] = {"inactive": [], "bidirectional": []}
|
|
11
|
+
for r in model.get("relationships", []):
|
|
12
|
+
label = f"{r.get('fromTable')} -> {r.get('toTable')}"
|
|
13
|
+
if not r.get("isActive", True):
|
|
14
|
+
out["inactive"].append(label)
|
|
15
|
+
if r.get("crossFilterDirection") == "both":
|
|
16
|
+
out["bidirectional"].append(label)
|
|
17
|
+
return out
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def check_cardinality(model: Model) -> list[str]:
|
|
21
|
+
"""Warn on relationships without declared cardinality."""
|
|
22
|
+
return [
|
|
23
|
+
f"{r.get('fromTable')} -> {r.get('toTable')}"
|
|
24
|
+
for r in model.get("relationships", [])
|
|
25
|
+
if not r.get("cardinality")
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def orphan_tables(model: Model) -> list[str]:
|
|
30
|
+
"""Tables not connected to any relationship."""
|
|
31
|
+
rel_tables: set[str] = set()
|
|
32
|
+
for r in model.get("relationships", []):
|
|
33
|
+
rel_tables.add(r.get("fromTable"))
|
|
34
|
+
rel_tables.add(r.get("toTable"))
|
|
35
|
+
return [
|
|
36
|
+
t["name"] for t in model.get("tables", [])
|
|
37
|
+
if t["name"] not in rel_tables and (t.get("columns") or t.get("measures"))
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def role_playing_dimensions(model: Model) -> dict[str, list[str]]:
|
|
42
|
+
"""Detect a date/lookup table joined multiple times."""
|
|
43
|
+
counts: dict[str, list[str]] = {}
|
|
44
|
+
for r in model.get("relationships", []):
|
|
45
|
+
to_table = r.get("toTable")
|
|
46
|
+
counts.setdefault(to_table, []).append(r.get("fromTable", "?"))
|
|
47
|
+
return {t: srcs for t, srcs in counts.items() if len(srcs) > 1}
|