-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
261 lines (233 loc) · 8.24 KB
/
Copy pathapp.py
File metadata and controls
261 lines (233 loc) · 8.24 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
"""kmap web app — explorable map of the enriched kernel corpus."""
from __future__ import annotations
import os
import subprocess
from collections import Counter
from pathlib import Path
from flask import Flask, abort, jsonify, render_template, request
from .db import connect
from .index import _default_files_dir
from .search import get_index, tokens_from_program_text
# FILES_DIR is the corpus location (for reading program bodies on the detail
# page and for the git-diff view). CORPUS_ROOT is the repo that owns files/.
FILES_DIR = _default_files_dir()
CORPUS_ROOT = FILES_DIR.parent
app = Flask(__name__, template_folder="templates", static_folder="static")
def _db():
return connect()
@app.route("/")
def index():
conn = _db()
try:
cur = conn.cursor()
counts = {}
for table in ("programs", "calls", "bugs"):
cur.execute(f"SELECT COUNT(*) FROM {table}")
counts[table] = cur.fetchone()[0]
cur.execute("SELECT COUNT(DISTINCT bug_id) FROM programs WHERE bug_id IS NOT NULL")
counts["distinct_bugs"] = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM bugs WHERE crash_type IS NOT NULL")
counts["bugs_with_meta"] = cur.fetchone()[0]
finally:
conn.close()
return render_template("index.html", counts=counts)
@app.route("/subsystems")
def subsystems():
conn = _db()
try:
# programs-per-subsystem (one bug may have many programs)
rows = conn.execute(
"""
SELECT COALESCE(b.subsystem,'?') AS sub,
COUNT(*) AS n_programs,
COUNT(DISTINCT p.bug_id) AS n_bugs
FROM programs p
LEFT JOIN bugs b ON b.bug_id = p.bug_id
GROUP BY sub
ORDER BY n_programs DESC
"""
).fetchall()
finally:
conn.close()
data = [{"subsystem": r[0], "programs": r[1], "bugs": r[2]} for r in rows]
if request.args.get("format") == "json":
return jsonify(data)
return render_template("subsystems.html", rows=data)
@app.route("/bug-classes")
def bug_classes():
conn = _db()
try:
rows = conn.execute(
"""
SELECT COALESCE(b.crash_type,'?'),
COUNT(*) AS n_programs,
COUNT(DISTINCT p.bug_id) AS n_bugs
FROM programs p
LEFT JOIN bugs b ON b.bug_id = p.bug_id
GROUP BY b.crash_type
ORDER BY n_programs DESC
"""
).fetchall()
finally:
conn.close()
data = [{"crash_type": r[0], "programs": r[1], "bugs": r[2]} for r in rows]
if request.args.get("format") == "json":
return jsonify(data)
return render_template("bug_classes.html", rows=data)
@app.route("/motifs")
def motifs():
n = int(request.args.get("n", 3))
limit = int(request.args.get("limit", 100))
if n not in (2, 3, 4):
abort(400)
conn = _db()
try:
rows = conn.execute(
"SELECT gram, count FROM ngrams WHERE n = ? ORDER BY count DESC LIMIT ?",
(n, limit),
).fetchall()
finally:
conn.close()
data = [{"gram": r[0], "count": r[1]} for r in rows]
if request.args.get("format") == "json":
return jsonify(data)
return render_template("motifs.html", rows=data, n=n, limit=limit)
@app.route("/nn", methods=["GET", "POST"])
def nn():
result = None
program_text = ""
if request.method == "POST":
program_text = request.form.get("program", "")
tokens = tokens_from_program_text(program_text)
if tokens:
conn = _db()
try:
idx = get_index(conn)
hits = idx.query(tokens, top_k=30, conn=conn)
result = [{
"program_id": h.program_id,
"path": os.path.relpath(h.path, CORPUS_ROOT),
"bug_id": h.bug_id,
"score": round(h.score, 4),
"shared": h.shared_tokens,
"title": h.title,
"crash_type": h.crash_type,
"subsystem": h.subsystem,
} for h in hits]
finally:
conn.close()
return render_template("nn.html", program_text=program_text, result=result)
@app.route("/seeds")
def seeds():
"""Recommend N programs for a given subsystem (or crash class)."""
subsystem = request.args.get("subsystem")
crash = request.args.get("crash")
limit = int(request.args.get("limit", 50))
where = []
params: list = []
if subsystem:
where.append("b.subsystem = ?")
params.append(subsystem)
if crash:
where.append("b.crash_type = ?")
params.append(crash)
where_sql = ("WHERE " + " AND ".join(where)) if where else ""
conn = _db()
try:
# pick at most 3 programs per bug (to diversify) then cut to limit
rows = conn.execute(
f"""
SELECT p.id, p.path, p.bug_id, p.n_calls,
b.title, b.crash_type, b.subsystem,
ROW_NUMBER() OVER (PARTITION BY p.bug_id ORDER BY p.n_calls DESC) AS rn
FROM programs p
LEFT JOIN bugs b ON b.bug_id = p.bug_id
{where_sql}
""",
params,
).fetchall()
finally:
conn.close()
picked = [r for r in rows if r[7] <= 3][:limit]
data = [{
"program_id": r[0],
"path": os.path.relpath(r[1], CORPUS_ROOT),
"bug_id": r[2],
"n_calls": r[3],
"title": r[4],
"crash_type": r[5],
"subsystem": r[6],
} for r in picked]
if request.args.get("format") == "json":
return jsonify(data)
return render_template("seeds.html", rows=data, subsystem=subsystem, crash=crash, limit=limit)
@app.route("/diff")
def diff():
"""Corpus diff from git log on files/."""
n = int(request.args.get("n", 20))
try:
raw = subprocess.check_output(
["git", "log", "--no-merges", "-n", str(n),
"--name-status",
"--pretty=format:__COMMIT__%x09%H%x09%ct%x09%s",
"--", "files/"],
cwd=str(CORPUS_ROOT), text=True,
)
except subprocess.CalledProcessError as e:
return f"git log failed: {e}", 500
commits = []
cur = None
for line in raw.splitlines():
if not line.strip():
continue
if line.startswith("__COMMIT__\t"):
if cur:
commits.append(cur)
parts = line.split("\t", 3)
cur = {"hash": parts[1], "ts": int(parts[2]),
"subject": parts[3] if len(parts) > 3 else "",
"added": [], "removed": [], "modified": []}
elif cur is not None and "\t" in line:
tag, _, name = line.partition("\t")
bucket = {"A": "added", "D": "removed", "M": "modified"}.get(tag[:1])
if bucket:
cur[bucket].append(name)
if cur:
commits.append(cur)
if request.args.get("format") == "json":
return jsonify(commits)
return render_template("diff.html", commits=commits)
@app.route("/program/<int:pid>")
def program(pid):
conn = _db()
try:
row = conn.execute(
"""
SELECT p.id, p.path, p.bug_id, p.n_calls, p.options,
b.title, b.crash_type, b.subsystem, b.status
FROM programs p
LEFT JOIN bugs b ON b.bug_id = p.bug_id
WHERE p.id = ?
""", (pid,),
).fetchone()
if not row:
abort(404)
calls = conn.execute(
"SELECT idx, name, variant, raw, defines FROM calls WHERE program_id = ? ORDER BY idx",
(pid,),
).fetchall()
finally:
conn.close()
try:
body = Path(row[1]).read_text(errors="replace")
except OSError:
body = "(file missing)"
return render_template("program.html", meta={
"id": row[0], "path": os.path.relpath(row[1], CORPUS_ROOT),
"bug_id": row[2], "n_calls": row[3], "options": row[4],
"title": row[5], "crash_type": row[6],
"subsystem": row[7], "status": row[8],
}, calls=calls, body=body)
if __name__ == "__main__":
port = int(os.environ.get("PORT", "5057"))
app.run(host="127.0.0.1", port=port, debug=False)