CodeCommunity commited on
Commit
f5f0249
·
verified ·
1 Parent(s): bde35c7

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +11 -13
app/main.py CHANGED
@@ -77,42 +77,40 @@ async def review_code(request: ReviewRequest):
77
 
78
  @app.post("/repo-dashboard-stats")
79
  async def get_dashboard_stats(request: ReviewRequest):
80
- """
81
- Specific endpoint for the Jetpack Compose Dashboard.
82
- Combines CodeBERT classification and Gemini review stats.
83
- """
84
  try:
85
- # 1. Run the existing Review Logic
86
  raw_reviews = reviewer_service.review_batch_code(request.files)
87
 
88
- # 2. Extract Security Count
89
  total_vulns = sum(len(r.get("vulnerabilities", [])) for r in raw_reviews)
90
 
91
- # 3. Calculate Performance (Maintainability)
 
92
  scores = [r.get("metrics", {}).get("maintainability", 8) for r in raw_reviews]
93
  avg_maintainability = (sum(scores) / len(scores)) * 10 if scores else 0
94
 
95
- # 4. Extract Exposed APIs (Sniffing from content)
96
  found_apis = []
97
  for f in request.files:
98
  if f.content:
99
- # Simple regex to find common route patterns
100
  matches = re.findall(r'(?:get|post|put|delete|patch)\([\'"]\/(.*?)[\'"]', f.content.lower())
101
  for match in matches:
102
  found_apis.append(f"/{match}")
103
 
104
- # 5. Repo Health Logic
 
105
  health_score = max(10, 100 - (total_vulns * 10))
106
 
107
  return {
108
- "repo_health": f"{health_score}%",
 
109
  "security_issues": total_vulns,
110
  "performance_ratio": f"{int(avg_maintainability)}%",
111
- "exposed_apis": list(set(found_apis))[:10] # Top 10 unique routes
112
  }
113
  except Exception as e:
114
  logger.error(f"Dashboard stats failed: {e}")
115
- raise HTTPException(status_code=500, detail=str(e))
116
 
117
  # 5. Application Entry Point
118
  if __name__ == "__main__":
 
77
 
78
  @app.post("/repo-dashboard-stats")
79
  async def get_dashboard_stats(request: ReviewRequest):
 
 
 
 
80
  try:
 
81
  raw_reviews = reviewer_service.review_batch_code(request.files)
82
 
83
+ # 1. Security Count
84
  total_vulns = sum(len(r.get("vulnerabilities", [])) for r in raw_reviews)
85
 
86
+ # 2. Performance Ratio (Maintainability)
87
+ # We use a default of 8 if the AI misses a file to avoid 0% scores
88
  scores = [r.get("metrics", {}).get("maintainability", 8) for r in raw_reviews]
89
  avg_maintainability = (sum(scores) / len(scores)) * 10 if scores else 0
90
 
91
+ # 3. API Sniffing
92
  found_apis = []
93
  for f in request.files:
94
  if f.content:
95
+ # Regex looks for common route decorators or methods
96
  matches = re.findall(r'(?:get|post|put|delete|patch)\([\'"]\/(.*?)[\'"]', f.content.lower())
97
  for match in matches:
98
  found_apis.append(f"/{match}")
99
 
100
+ # 4. Repo Health Calculation
101
+ # Every security issue drops health by 10 points
102
  health_score = max(10, 100 - (total_vulns * 10))
103
 
104
  return {
105
+ "repo_health": health_score,
106
+ "health_label": "Excellent Health" if health_score > 80 else "Needs Review",
107
  "security_issues": total_vulns,
108
  "performance_ratio": f"{int(avg_maintainability)}%",
109
+ "exposed_apis": list(set(found_apis))[:10]
110
  }
111
  except Exception as e:
112
  logger.error(f"Dashboard stats failed: {e}")
113
+ raise HTTPException(status_code=500, detail="Failed to aggregate repository stats")
114
 
115
  # 5. Application Entry Point
116
  if __name__ == "__main__":