<<<<<<< HEAD
A Python tool that extracts table metadata from Metabase API and generates useful KPIs using OpenAI's LLM.
- π Metabase Authentication: Secure API access with session management
- π Table Metadata Extraction: Comprehensive table and column information
- π Related Table Discovery: Automatically finds and includes related table information
- π€ AI-Powered KPI Generation: Uses OpenAI GPT-4 to generate business-relevant KPIs
- π SQL Query Generation: Provides ready-to-use SQL queries for each KPI
- πΎ JSON Output: Structured output for easy integration with other tools
- Python 3.8+
- Metabase instance with API access
- OpenAI API key
- Network access to both Metabase and OpenAI APIs
-
Clone or download the project files
-
Install dependencies:
pip install -r config/requirements.txt
-
Set up environment variables: Create a
.envfile in the project root with:# Metabase Configuration METABASE_URL=http://your-metabase-instance.com METABASE_USERNAME=your_username METABASE_PASSWORD=your_password # OpenAI Configuration OPENAI_API_KEY=your_openai_api_key_here
Run the main script with default table names:
python src/kpi_extractor.pyModify the table_names list in the main() function:
table_names = [
"your_table_name",
"another_table",
"custom_table"
]from src.kpi_extractor import MetabaseKPIExtractor
# Initialize
extractor = MetabaseKPIExtractor()
# Process specific tables
results = extractor.process_table_list(["users", "orders", "products"])
# Save results
extractor.save_results(results, "my_kpis.json")The project includes individual test scripts for each step:
python test_step1_auth.pyTests Metabase connection and authentication.
python test_step2_auth.pyTests table discovery and metadata extraction.
python test_step3_llm.pyTests OpenAI integration and KPI generation.
python test_step4_integration.pyTests the complete end-to-end workflow.
The tool generates a structured JSON output:
{
"table_name": {
"table_name": "string",
"table_id": "number",
"metadata": {
"table_info": {...},
"fields": [...],
"related_tables": [...]
},
"kpis": [
{
"kpi_name": "string",
"description": "string",
"business_value": "string",
"sql_query": "string",
"output_format": "string"
}
]
}
}POST /api/session- AuthenticationGET /api/database- List databasesGET /api/database/{id}/tables- List tables in databaseGET /api/table/{id}- Get table metadataGET /api/table/{id}/query_metadata- Get table fields
The tool includes comprehensive error handling for:
- Authentication failures
- Network connectivity issues
- API rate limiting
- Invalid responses
- LLM generation failures
Built-in delays (2 seconds) between API calls to respect Metabase rate limits.
- Credentials are stored in environment variables
- Session tokens are managed securely
- No sensitive data is logged
-
Authentication Failed
- Verify Metabase URL, username, and password
- Check if Metabase instance is accessible
- Ensure user has API access permissions
-
No Tables Found
- Verify table names exist in your Metabase instance
- Check database permissions
- Ensure tables are not hidden or restricted
-
LLM Generation Failed
- Verify OpenAI API key is valid
- Check API quota and billing
- Ensure network access to OpenAI API
-
Rate Limiting
- Increase delays in the code if needed
- Check Metabase server logs for rate limit errors
Enable verbose logging by modifying the print statements in the code.
KPI_Creation/
βββ π docs/ # Documentation
β βββ ARCHITECTURE.md # Technical architecture
β βββ PROJECT_SUMMARY.md # Project overview
β βββ WORKING_KPIS_REPORT.md # Working KPIs analysis
β βββ INVALID_SQLS_ANALYSIS.md # Error analysis
β βββ CLEANUP_SUMMARY.md # Cleanup documentation
βββ π src/ # Source code
β βββ kpi_extractor.py # Core KPI generation
β βββ kpi_registrar.py # KPI registration
βββ π data/ # Data files
β βββ kpis_clean.json # Clean KPI dataset
βββ βοΈ config/ # Configuration
β βββ requirements.txt # Python dependencies
β βββ env_example.txt # Environment template
βββ π README.md # Main project documentation
βββ ποΈ archive/ # Archived files
This project is provided as-is for educational and business use.
For issues and questions:
- Check the troubleshooting section
- Review the test scripts
- Check Metabase and OpenAI documentation
- Create an issue with detailed error information =======
Automated KPI creating using Metabase
b1479c8048f2abce619116ba88b5d51af8469ecf