|
| 1 | +from flask import Flask, request, render_template, send_file, redirect, url_for |
| 2 | +import sqlite3, json, os |
| 3 | +from werkzeug.utils import secure_filename |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +app = Flask(__name__) |
| 7 | +UPLOAD_FOLDER = Path("uploads") |
| 8 | +DB_PATH = Path("copilot_metrics.db") |
| 9 | +UPLOAD_FOLDER.mkdir(exist_ok=True) |
| 10 | + |
| 11 | +def create_db(json_data): |
| 12 | + conn = sqlite3.connect(DB_PATH) |
| 13 | + cur = conn.cursor() |
| 14 | + cur.execute("DROP TABLE IF EXISTS completions") |
| 15 | + cur.execute("DROP TABLE IF EXISTS summary") |
| 16 | + |
| 17 | + cur.execute(""" |
| 18 | + CREATE TABLE completions ( |
| 19 | + date TEXT, |
| 20 | + editor TEXT, |
| 21 | + language TEXT, |
| 22 | + code_suggestions INTEGER, |
| 23 | + code_acceptances INTEGER, |
| 24 | + code_lines_suggested INTEGER, |
| 25 | + code_lines_accepted INTEGER |
| 26 | + ) |
| 27 | + """) |
| 28 | + cur.execute(""" |
| 29 | + CREATE TABLE summary ( |
| 30 | + date TEXT PRIMARY KEY, |
| 31 | + total_active_users INTEGER, |
| 32 | + total_engaged_users INTEGER |
| 33 | + ) |
| 34 | + """) |
| 35 | + |
| 36 | + for entry in json_data: |
| 37 | + date = entry["date"] |
| 38 | + cur.execute("INSERT INTO summary VALUES (?, ?, ?)", ( |
| 39 | + date, |
| 40 | + entry.get("total_active_users", 0), |
| 41 | + entry.get("total_engaged_users", 0) |
| 42 | + )) |
| 43 | + |
| 44 | + completions = entry.get("copilot_ide_code_completions", {}) |
| 45 | + for editor in completions.get("editors", []): |
| 46 | + editor_name = editor["name"] |
| 47 | + for model in editor.get("models", []): |
| 48 | + for lang in model.get("languages", []): |
| 49 | + cur.execute(""" |
| 50 | + INSERT INTO completions VALUES (?, ?, ?, ?, ?, ?, ?) |
| 51 | + """, ( |
| 52 | + date, |
| 53 | + editor_name, |
| 54 | + lang.get("name", "unknown"), |
| 55 | + lang.get("total_code_suggestions", 0), |
| 56 | + lang.get("total_code_acceptances", 0), |
| 57 | + lang.get("total_code_lines_suggested", 0), |
| 58 | + lang.get("total_code_lines_accepted", 0) |
| 59 | + )) |
| 60 | + conn.commit() |
| 61 | + conn.close() |
| 62 | + |
| 63 | +@app.route("/", methods=["GET", "POST"]) |
| 64 | +def upload(): |
| 65 | + if request.method == "POST": |
| 66 | + file = request.files["file"] |
| 67 | + if file and file.filename.endswith(".json"): |
| 68 | + filepath = UPLOAD_FOLDER / secure_filename(file.filename) |
| 69 | + file.save(filepath) |
| 70 | + with open(filepath) as f: |
| 71 | + data = json.load(f) |
| 72 | + create_db(data) |
| 73 | + return redirect(url_for("download")) |
| 74 | + else: |
| 75 | + return "Invalid file type. Please upload a .json file." |
| 76 | + return render_template("upload.html") |
| 77 | + |
| 78 | +@app.route("/download") |
| 79 | +def download(): |
| 80 | + return send_file(DB_PATH, as_attachment=True) |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + app.run(debug=True) |
0 commit comments