Skip to content

Commit 1c19262

Browse files
committed
2 parents 3bfde45 + 17ae76a commit 1c19262

File tree

5 files changed

+260
-0
lines changed

5 files changed

+260
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dmp_2/__pycache__/*
2+
.env
3+
env/*
4+
5+
__pycache__/*

app.py

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
from flask import Flask, jsonify
2+
from db import SupabaseInterface
3+
from collections import defaultdict
4+
from flasgger import Swagger
5+
6+
7+
app = Flask(__name__)
8+
9+
Swagger(app)
10+
11+
12+
@app.route('/api/greeting', methods=['GET'])
13+
def greeting():
14+
"""
15+
A simple greeting endpoint.
16+
---
17+
responses:
18+
200:
19+
description: A greeting message
20+
schema:
21+
type: object
22+
properties:
23+
message:
24+
type: string
25+
example: Hello, welcome to my API!
26+
"""
27+
response = {
28+
'message': 'Hello, welcome to my API!'
29+
}
30+
return jsonify(response)
31+
32+
@app.route('/api/get-data', methods=['GET'])
33+
def get_data():
34+
"""
35+
Fetch data from Supabase.
36+
---
37+
responses:
38+
200:
39+
description: Data fetched successfully
40+
schema:
41+
type: array
42+
items:
43+
type: object
44+
500:
45+
description: Error fetching data
46+
schema:
47+
type: object
48+
properties:
49+
error:
50+
type: string
51+
"""
52+
try:
53+
response = SupabaseInterface().get_instance().client.table('dmp_pr_updates').select('*').execute()
54+
data = response.data
55+
return jsonify(data)
56+
except Exception as e:
57+
return jsonify({'error': str(e)}), 500
58+
59+
def group_by_owner(data):
60+
grouped_data = defaultdict(list)
61+
for record in data:
62+
owner = record['owner']
63+
grouped_data[owner].append(record)
64+
65+
66+
#Arrange data as reponse format
67+
res = []
68+
for val in grouped_data:
69+
dict_ = {}
70+
dict_['org_name'] = val
71+
dict_['issues'] = grouped_data[val]
72+
73+
res.append(dict_)
74+
75+
return {"issues":res}
76+
77+
@app.route('/api/issues', methods=['GET'])
78+
def get_issues():
79+
"""
80+
Fetch all issues and group by owner.
81+
---
82+
responses:
83+
200:
84+
description: Issues grouped by owner
85+
schema:
86+
type: object
87+
additionalProperties:
88+
type: array
89+
items:
90+
type: object
91+
500:
92+
description: Error fetching issues
93+
schema:
94+
type: object
95+
properties:
96+
error:
97+
type: string
98+
"""
99+
try:
100+
response = SupabaseInterface().get_instance().client.table('dmp_issue_updates').select('*').execute()
101+
data = response.data
102+
grouped_data = group_by_owner(data)
103+
return jsonify(grouped_data)
104+
except Exception as e:
105+
return jsonify({'error': str(e)}), 500
106+
107+
@app.route('/api/issues/<owner>', methods=['GET'])
108+
def get_issues_by_owner(owner):
109+
"""
110+
Fetch issues by owner.
111+
---
112+
parameters:
113+
- name: owner
114+
in: path
115+
type: string
116+
required: true
117+
description: The owner of the issues
118+
responses:
119+
200:
120+
description: Issues fetched successfully
121+
schema:
122+
type: array
123+
items:
124+
type: object
125+
500:
126+
description: Error fetching issues
127+
schema:
128+
type: object
129+
properties:
130+
error:
131+
type: string
132+
"""
133+
try:
134+
response = SupabaseInterface().get_instance().client.table('dmp_issue_updates').select('*').eq('owner', owner).execute()
135+
if not response.data:
136+
return jsonify({'error': "No data found"}), 500
137+
data = response.data
138+
data = [{**item, "name": item["owner"]} for item in data]
139+
return jsonify(data)
140+
except Exception as e:
141+
return jsonify({'error': str(e)}), 500
142+
143+
@app.route('/api/issues/<owner>/<issue>', methods=['GET'])
144+
def get_issues_by_owner_id(owner, issue):
145+
"""
146+
Fetch issues by owner and issue number.
147+
---
148+
parameters:
149+
- name: owner
150+
in: path
151+
type: string
152+
required: true
153+
description: The owner of the issues
154+
- name: issue
155+
in: path
156+
type: string
157+
required: true
158+
description: The issue number
159+
responses:
160+
200:
161+
description: Issues fetched successfully
162+
schema:
163+
type: array
164+
items:
165+
type: object
166+
500:
167+
description: Error fetching issues
168+
schema:
169+
type: object
170+
properties:
171+
error:
172+
type: string
173+
"""
174+
try:
175+
response = SupabaseInterface().get_instance().client.table('dmp_issue_updates').select('*').eq('owner', owner).eq('issue_number', issue).execute()
176+
if not response.data:
177+
return jsonify({'error': "No data found"}), 500
178+
data = response.data
179+
return jsonify(data)
180+
except Exception as e:
181+
return jsonify({'error': str(e)}), 500
182+
183+
if __name__ == '__main__':
184+
app.run(debug=True)

db.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os, sys
2+
from typing import Any
3+
from supabase import create_client, Client
4+
from supabase.lib.client_options import ClientOptions
5+
from abc import ABC, abstractmethod
6+
7+
client_options = ClientOptions(postgrest_client_timeout=None)
8+
9+
10+
11+
class SupabaseInterface():
12+
13+
_instance = None
14+
15+
def __init__(self):
16+
if not SupabaseInterface._instance:
17+
18+
# Load environment variables
19+
from dotenv import load_dotenv
20+
load_dotenv()
21+
22+
SUPABASE_URL = os.getenv('SUPABASE_URL')
23+
SUPABASE_KEY = os.getenv('SUPABASE_KEY')
24+
self.client: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
25+
SupabaseInterface._instance = self
26+
else:
27+
SupabaseInterface._instance = self._instance
28+
29+
30+
31+
@staticmethod
32+
def get_instance():
33+
# Static method to retrieve the singleton instance
34+
if not SupabaseInterface._instance:
35+
# If no instance exists, create a new one
36+
SupabaseInterface._instance = SupabaseInterface()
37+
return SupabaseInterface._instance
38+
39+
40+
def readAll(self, table):
41+
data = self.client.table(f"{table}").select("*").execute()
42+
return data.data
43+
44+
def add_data(self, data,table_name):
45+
data = self.client.table(table_name).insert(data).execute()
46+
return data.data
47+
48+
def add_data_filter(self, data, table_name):
49+
# Construct the filter based on the provided column names and values
50+
filter_data = {column: data[column] for column in ['dmp_id','issue_number','owner']}
51+
52+
# Check if the data already exists in the table based on the filter
53+
existing_data = self.client.table(table_name).select("*").eq('dmp_id',data['dmp_id']).execute()
54+
55+
# If the data already exists, return without creating a new record
56+
if existing_data.data:
57+
return "Data already exists"
58+
59+
# If the data doesn't exist, insert it into the table
60+
new_data = self.client.table(table_name).insert(data).execute()
61+
return new_data.data

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Flask==3.0.3
2+
httpx==0.27.0
3+
python-dotenv==1.0.1
4+
supabase==2.4.5
5+
gunicorn==22.0.0
6+
flasgger==0.9.7.1

wsgi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from app import app
2+
3+
if __name__ == "__main__":
4+
app.run()

0 commit comments

Comments
 (0)