Skip to content

Commit

Permalink
Added discord metrics and github metrics api
Browse files Browse the repository at this point in the history
  • Loading branch information
suyashgautam committed May 28, 2023
1 parent 1f562ef commit f8300cc
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#all files relating to python vitual env
/.venv
/venv
#environment variables and cache
*.env
*/__pycache__/*

/__pycache__
39 changes: 25 additions & 14 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
def add_github_metrics(self, github_metrics):
data = self.client.table("github_metrics").insert(github_metrics).execute()
return data

70 changes: 60 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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")}'}
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


14 changes: 14 additions & 0 deletions samples/discordMetrics.json
Original file line number Diff line number Diff line change
@@ -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
}
}
23 changes: 23 additions & 0 deletions samples/githubMetrics.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit f8300cc

Please sign in to comment.