-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
321 lines (263 loc) · 9.86 KB
/
app.py
File metadata and controls
321 lines (263 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from flask import Flask, render_template, request, jsonify, redirect, url_for
from db import analysis_collection, mongo_available, mongo_error
import hashlib
import requests
import os
from datetime import datetime, timezone
from dotenv import load_dotenv
from services.analyzer import run_analysis, normalize_repo_url
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import threading
load_dotenv()
PORT = os.getenv("PORT") or 5000
DISABLE_ML_TAGGER = (os.getenv("DISABLE_ML_TAGGER") or "").strip().lower()
USE_LOCAL_MODEL = os.getenv("USE_LOCAL_MODEL", "1").strip().lower() in {"1", "true", "yes", "on"}
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") or os.getenv("GITHUB_API_TOKEN")
LOCAL_MODEL_DISABLED = DISABLE_ML_TAGGER in {"1", "true", "yes", "on", "all", "local_only"}
if USE_LOCAL_MODEL and not LOCAL_MODEL_DISABLED:
try:
from ml_tagger.predict_render import load_model
except Exception as exc:
load_model = None
print(f"ML model import failed, continuing without ML tagging: {exc}")
else:
try:
load_model()
except Exception as exc:
print(f"ML model load failed, continuing without ML tagging: {exc}")
app = Flask(__name__)
limiter = Limiter(
get_remote_address,
app=app,
default_limits=["100 per hour"]
)
def run_analysis_background(cache_key, repo_url, commit1, commit2):
result = run_analysis(repo_url, commit1, commit2)
store_analysis_result(
cache_key,
repo_url,
commit1,
commit2,
result
)
def extract_repo_info(url):
# Example: https://github.com/user/repo
parts = url.rstrip("/").split("/")
return parts[-2], parts[-1]
def github_api_headers():
headers = {
"Accept": "application/vnd.github+json",
"User-Agent": "code-ripple-app",
}
if GITHUB_TOKEN:
headers["Authorization"] = f"Bearer {GITHUB_TOKEN}"
return headers
def fetch_recent_commits(repo_url, limit=20, page=1):
# Shared helper so both the HTML page and JSON API use the same commit-fetching logic.
owner, repo = extract_repo_info(repo_url)
api_url = f"https://api.github.com/repos/{owner}/{repo}/commits"
# Ask GitHub for exactly the number of commits we want to show on the results page.
response = requests.get(
api_url,
params={"per_page": limit, "page": page},
headers=github_api_headers(),
timeout=10
)
if response.status_code == 403:
remaining = response.headers.get("X-RateLimit-Remaining")
reset_at = response.headers.get("X-RateLimit-Reset")
if remaining == "0":
detail = "GitHub API rate limit exceeded"
if not GITHUB_TOKEN:
detail += ". Add GITHUB_TOKEN on Render to use the authenticated rate limit."
if reset_at:
detail += f" Reset epoch: {reset_at}."
raise RuntimeError(detail)
response.raise_for_status()
commits = response.json()
result = []
for c in commits[:limit]:
commit_data = c["commit"]
author = commit_data.get("author") or {}
result.append({
"sha": c["sha"],
"message": commit_data.get("message"),
"author": author.get("name", "Unknown"),
"date": author.get("date")
})
return {
"commits": result,
"page": page,
"per_page": limit,
"has_more": len(commits) == limit
}
def make_cache_key(repo_url, c1, c2):
# Sort the commits inside the cache key so the same comparison reuses one DB entry.
ordered_commits = sorted([c1, c2])
raw = f"{repo_url}|{ordered_commits[0]}|{ordered_commits[1]}"
return hashlib.sha1(raw.encode()).hexdigest()
def get_cached_analysis(cache_key):
# Reuse stored analysis results so repeated comparisons do not rerun the analyzer.
if not mongo_available or analysis_collection is None:
return None
return analysis_collection.find_one({"cache_key": cache_key})
def store_analysis_result(cache_key, repo_url, commit1, commit2, result):
# Upsert keeps the cache fresh without creating duplicate analysis documents.
if not mongo_available or analysis_collection is None:
return False
result = analysis_collection.update_one(
{"cache_key": cache_key},
{
"$set": {
"cache_key": cache_key,
"repo_url": repo_url,
"commit1": commit1,
"commit2": commit2,
"result": result,
"updated_at": datetime.now(timezone.utc)
},
"$setOnInsert": {
"created_at": datetime.now(timezone.utc)
}
},
upsert=True
)
return result.acknowledged
@app.route("/")
def index():
return render_template("index.html")
@app.route("/commits", methods=["GET"])
def commits_page():
repo_url = request.args.get("repo_url", "")
commits = []
error = None
if repo_url:
try:
# Render the latest 20 commits on the server so the results page is fully Jinja-driven.
commit_data = fetch_recent_commits(repo_url, limit=20, page=1)
commits = commit_data["commits"]
except Exception as e:
error = str(e)
commit_data = {"has_more": False}
else:
commit_data = {"has_more": False}
return render_template(
"results.html",
repo_url=repo_url,
commits=commits,
error=error,
has_more_commits=commit_data["has_more"]
)
@app.route("/commits", methods=["POST"])
@limiter.limit("20 per minute")
def get_commits():
data = request.json
repo_url = data.get("repo_url")
page = data.get("page", 1)
per_page = data.get("per_page", 20)
try:
# Keep the JSON endpoint available for any future async workflow.
page = max(1, int(page))
per_page = max(1, min(int(per_page), 100))
return jsonify(fetch_recent_commits(repo_url, limit=per_page, page=page))
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/compare", methods=["POST"])
@limiter.limit("10 per minute")
def compare():
data = request.json
repo_url = data.get("repo_url")
commit1 = data.get("commit1")
commit2 = data.get("commit2")
if not repo_url or not commit1 or not commit2:
return jsonify({"error": "Missing required fields"}), 400
if len(repo_url) > 200:
return jsonify({"error": "Invalid repo URL"}), 400
normalized_repo_url = normalize_repo_url(repo_url)
cache_key = make_cache_key(normalized_repo_url, commit1, commit2)
cached_analysis = get_cached_analysis(cache_key)
# 🔥 IF NOT CACHED → RUN IN BACKGROUND
if not cached_analysis:
thread = threading.Thread(
target=run_analysis_background,
args=(cache_key, normalized_repo_url, commit1, commit2),
daemon=True
)
thread.start()
return jsonify({
"status": "ok",
"redirect_url": url_for("analysis_loading_page", cache_key=cache_key)
})
@app.route("/analysis")
def analysis_root():
# Keep this route friendly if someone navigates to /analysis directly.
return redirect(url_for("index"))
@app.route("/analysis/<cache_key>")
def analysis_page(cache_key):
cached_analysis = get_cached_analysis(cache_key)
cache_status = request.args.get("cache_status", "unknown")
if not cached_analysis:
return render_template(
"analysis.html",
error="No cached analysis found for this comparison. MongoDB may be unavailable." if not mongo_available else "No cached analysis found for this comparison.",
analysis_data=None,
summary=[],
chunks=[],
ai_summary={},
meta={},
repo_url="",
commit1="",
commit2="",
cache_key=cache_key,
cache_source="Unavailable",
mongo_error=mongo_error,
mongo_available=mongo_available,
cache_status=cache_status
), 404
result = cached_analysis.get("result", {})
# -------- SAFE EXTRACTION --------
meta = result.get("meta", {})
summary = result.get("summary", [])
chunks = result.get("chunks", [])
ai_summary = result.get("ai_summary", {})
return render_template(
"analysis.html",
error=None,
# 🔥 CORE DATA
analysis_data=result,
meta=meta,
summary=summary,
chunks=chunks,
ai_summary=ai_summary,
# 🔥 COMPAT (remove later if needed)
details=[], # legacy, avoid template crash
# 🔥 METADATA
repo_url=cached_analysis.get("repo_url", ""),
commit1=cached_analysis.get("commit1", ""),
commit2=cached_analysis.get("commit2", ""),
cached_at=cached_analysis.get("updated_at"),
cache_key=cached_analysis.get("cache_key", ""),
cache_source="MongoDB" if mongo_available else "Unavailable",
mongo_error=mongo_error,
mongo_available=mongo_available,
cache_status=cache_status
)
@app.route("/api/analysis/<cache_key>", methods=["GET"])
@limiter.limit("360 per minute")
def get_analysis_api(cache_key):
cached_analysis = get_cached_analysis(cache_key)
if not cached_analysis:
return jsonify({"status": "processing"}), 202 # 🔥 important
return jsonify({
"status": "ready",
"meta": cached_analysis["result"].get("meta", {}),
"summary": cached_analysis["result"].get("summary", []),
"chunks": cached_analysis["result"].get("chunks", []),
"ai_summary": cached_analysis["result"].get("ai_summary", {})
})
@app.route("/analysis_loading/<cache_key>")
def analysis_loading_page(cache_key):
return render_template("analysis_loading.html", cache_key=cache_key)
if __name__ == "__main__":
app.run("0.0.0.0", PORT)