"""
@app.post("/analyze", response_model=AnalysisResponse)
async def analyze_data():
"""Run full data analysis and generate reports"""
global analyzer, last_analysis_time, analysis_results
if analyzer is None:
try:
analyzer = DataQualityAnalyzer()
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to initialize analyzer: {str(e)}")
try:
# Run analysis
analyzer.analyze_all_datasets()
report = analyzer.generate_report()
analyzer.plot_metrics()
# Get list of generated plots
plots_dir = analyzer.data_dir.parent / "reports" / "plots"
plots_available = [f.name for f in plots_dir.glob("*.png")]
# Update global state
last_analysis_time = datetime.now().isoformat()
analysis_results = report
return AnalysisResponse(
summary=report["summary"],
datasets=report["datasets"],
plots_available=plots_available,
timestamp=last_analysis_time
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")
@app.get("/analysis/status", response_model=StatusResponse)
async def get_analysis_status():
"""Get the status of the last analysis run"""
if last_analysis_time is None:
return StatusResponse(
last_analysis=None,
summary=None,
is_analyzed=False
)
return StatusResponse(
last_analysis=last_analysis_time,
summary=analysis_results["summary"] if analysis_results else None,
is_analyzed=True
)
@app.get("/plots/{plot_name}")
async def get_plot(plot_name: str):
"""Get a specific plot by name"""
plots_dir = Path("data/reports/plots")
plot_path = plots_dir / plot_name
if not plot_path.exists():
raise HTTPException(status_code=404, detail=f"Plot {plot_name} not found")
return FileResponse(plot_path)
@app.get("/datasets")
async def get_datasets():
"""List all available datasets for analysis"""
if analyzer is None:
try:
analyzer = DataQualityAnalyzer()
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to initialize analyzer: {str(e)}")
try:
datasets = []
for file_path in analyzer.data_dir.glob("*.json"):
datasets.append({
"name": file_path.stem,
"path": str(file_path),
"size": file_path.stat().st_size
})
return {"datasets": datasets}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to list datasets: {str(e)}")
if __name__ == "__main__":
uvicorn.run("analysis_api:app", host="0.0.0.0", port=8001, reload=True)