Skip to content

Commit 278ba85

Browse files
Merge pull request #1 from cfabriziohivemq/feat/copilot-metrics
Feat/copilot metrics
2 parents c3c7476 + 376dae1 commit 278ba85

File tree

8 files changed

+14188
-1
lines changed

8 files changed

+14188
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Copilot Metrics Retention
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
first-job:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v4
13+
14+
- name: Copilot Metrics Retention
15+
uses: ambilykk/copilot-metrics-retention@main
16+
with:
17+
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
18+
org_name: 'hivemq'
19+
json_path: 'metrics.json'
20+
21+
- name: Commit and push if it changed
22+
run: |
23+
git config --global user.name 'Copilot Metrics Retention'
24+
git config --global user.email '[email protected]'
25+
git add -A
26+
git diff --quiet && git diff --staged --quiet || git commit -m "Copilot Metrics data update"
27+
git push
28+

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
# copilot-dashboard
1+
# Copilot-dashboard
2+
3+
## Intro
4+
Unfortunately Github only offers 28-day retention for the copilot metrics, this solution gives us the ability to retrieve data and visulasie them via metabase.
5+
6+
## Gh action
7+
there is a gh action that retrieves the json file and render it into a unique json. [Ref](*https://github.com/marketplace/actions/copilot-metrics-retention)

app.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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)
4.36 MB
Binary file not shown.

copilot_metrics.db/copilot_metrics.db.trace.db

Lines changed: 3783 additions & 0 deletions
Large diffs are not rendered by default.

data/copilot_metrics.db

40 KB
Binary file not shown.

templates/upload.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Copilot JSON to SQLite</title>
5+
</head>
6+
<body>
7+
<h2>Upload your Copilot JSON</h2>
8+
<form method="POST" enctype="multipart/form-data">
9+
<input type="file" name="file" accept=".json" required>
10+
<input type="submit" value="Convert & Download SQLite DB">
11+
</form>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)