From f8300ccf8e8186cf04b5d90516bbf8d5d2b0c6d9 Mon Sep 17 00:00:00 2001 From: Suyash Gautam Date: Sun, 28 May 2023 12:24:05 +0530 Subject: [PATCH] Added discord metrics and github metrics api --- .gitignore | 4 ++- db.py | 39 +++++++++++++-------- main.py | 70 +++++++++++++++++++++++++++++++------ samples/discordMetrics.json | 14 ++++++++ samples/githubMetrics.json | 23 ++++++++++++ 5 files changed, 125 insertions(+), 25 deletions(-) create mode 100644 samples/discordMetrics.json create mode 100644 samples/githubMetrics.json diff --git a/.gitignore b/.gitignore index 367c07d..19041bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ #all files relating to python vitual env -/.venv +/venv #environment variables and cache *.env */__pycache__/* + +/__pycache__ diff --git a/db.py b/db.py index 31d8fb0..5830d3f 100644 --- a/db.py +++ b/db.py @@ -14,26 +14,37 @@ def __init__(self, url, key) -> None: self.supabase_key = key self.client: Client = create_client(self.supabase_url, self.supabase_key) - def add_user(self, userdata): + def add_mentor(self, userdata): - data = self.client.table("users").insert(userdata).execute() + data = self.client.table("mentors").insert(userdata).execute() print(data.data) return data - def user_exists(self, discord_id): - data = self.client.table("users").select("*").eq("discord_id", discord_id).execute() + def add_contributor(self, userdata): + + data = self.client.table("contributors").insert(userdata).execute() + print(data.data) + return data + + def mentor_exists(self, discord_id): + data = self.client.table("mentors").select("*").eq("discord_id", discord_id).execute() if len(data.data)>0: return True else: return False + + def contributor_exists(self, discord_id): + data = self.client.table("contributors").select("*").eq("discord_id", discord_id).execute() + if len(data.data)>0: + return True + else: + return False + + def add_discord_metrics(self, discord_metrics): + data = self.client.table("discord_metrics").insert(discord_metrics).execute() + return data - - -tester = SupabaseInterface(url,key) -# tester.add_user({ -# "discord_id": 476285280811483140, -# "github_id": 74085496 - -# }) - -print(tester.user_exists(476285280811483141)) \ No newline at end of file + def add_github_metrics(self, github_metrics): + data = self.client.table("github_metrics").insert(github_metrics).execute() + return data + \ No newline at end of file diff --git a/main.py b/main.py index be8327c..110b63b 100644 --- a/main.py +++ b/main.py @@ -38,11 +38,17 @@ def register(discord_userdata): #Check if the user is registered supabase_client = SupabaseInterface(url=url, key=key) - if supabase_client.user_exists(discord_id=discord_id): - print('true') - authenticated_url = f'{os.getenv("HOST")}/already_authenticated' - return redirect(authenticated_url) - + if role == 'mentor': + if supabase_client.mentor_exists(discord_id=discord_id): + print('true') + authenticated_url = f'{os.getenv("HOST")}/already_authenticated' + return redirect(authenticated_url) + elif role == 'contributor': + if supabase_client.contributor_exists(discord_id=discord_id): + print('true') + authenticated_url = f'{os.getenv("HOST")}/already_authenticated' + return redirect(authenticated_url) + #get github ID github_url_for_access_token = 'https://github.com/login/oauth/access_token' data = { @@ -64,15 +70,59 @@ def register(discord_userdata): github_username = user.json()["login"] - #adding to the database - supabase_client.add_user({ + user_data = { "discord_id": int(discord_id), "github_id": github_id, "github_url": f"https://github.com/{github_username}", "discord_username": discord_username, - "role": role - }) + } + #adding to the database + if role == 'mentor': + supabase_client.add_mentor(user_data) + elif role == 'contributor': + supabase_client.add_contributor(user_data) - return render_template('success.html'), {"Refresh": f'1; url=https://discord.com/channels/{os.getenv("DISCORD_SERVER_ID")}'} \ No newline at end of file + return render_template('success.html'), {"Refresh": f'1; url=https://discord.com/channels/{os.getenv("DISCORD_SERVER_ID")}'} + +@app.route("/metrics/discord", methods = ['POST']) +def discord_metrics(): + url = os.getenv("SUPABASE_URL") + key = os.getenv("SUPABASE_KEY") + request_data = request.json + discord_data = [] + for product_name, value in request_data.items(): + data = { + "product_name" : product_name, + "mentor_messages" : value['mentor_messages'], + "contributor_messages": value['contributor_messages'] + } + discord_data.append(data) + + supabase_client = SupabaseInterface(url=url, key=key) + data = supabase_client.add_discord_metrics(discord_data) + return data.data + +@app.route("/metrics/github", methods = ['POST']) +def github_metrics(): + url = os.getenv("SUPABASE_URL") + key = os.getenv("SUPABASE_KEY") + request_data = request.json + github_data = [] + for product_name, value in request_data.items(): + data = { + "product_name" : product_name, + "open_prs" : value['open_prs'], + "closed_prs": value['closed_prs'], + "open_issues": value['open_issues'], + "closed_issues": value['closed_issues'], + "number_of_commits": value['number_of_commits'], + } + github_data.append(data) + + supabase_client = SupabaseInterface(url, key) + data = supabase_client.add_github_metrics(github_data) + return data.data + + \ No newline at end of file diff --git a/samples/discordMetrics.json b/samples/discordMetrics.json new file mode 100644 index 0000000..23a7272 --- /dev/null +++ b/samples/discordMetrics.json @@ -0,0 +1,14 @@ +{ + "cqube": { + "mentor_messages" : 1, + "contributor_messages": 20 + }, + "text2sql": { + "mentor_messages" : 30, + "contributor_messages": 12 + }, + "beckn": { + "mentor_messages" : 22, + "contributor_messages": 201 + } +} \ No newline at end of file diff --git a/samples/githubMetrics.json b/samples/githubMetrics.json new file mode 100644 index 0000000..933ee95 --- /dev/null +++ b/samples/githubMetrics.json @@ -0,0 +1,23 @@ +{ + "text2sql": { + "open_prs" : 1, + "closed_prs": 2, + "open_issues": 3, + "closed_issues": 0, + "number_of_commits": 64 + }, + "beckn": { + "open_prs" : 6, + "closed_prs": 2, + "open_issues": 32, + "closed_issues": 0, + "number_of_commits": 6 + }, + "cqube": { + "open_prs" : 10, + "closed_prs": 2, + "open_issues": 13, + "closed_issues": 10, + "number_of_commits": 128 + } +} \ No newline at end of file