Skip to content

Commit

Permalink
Tested and fixed issues
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
  • Loading branch information
pierDipi authored and aliok committed Oct 3, 2023
1 parent 3553ca0 commit 21d98a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
12 changes: 9 additions & 3 deletions services/analytics-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ This image is published at `quay.io/kevent-mesh/ai-demo-analytics-service:main`.
# Sending feedbacks

```shell
kubectl get routes -n knative-serving-ingress # Get analytics service route URL
analytics_service_route_host="analytics-service-ai-demo.apps.<omitted>"
curl -X POST -H "Content-Type: application/json" http://${analytics_service_route_host}/feedbacks -d '{"feedback": 1, "uploadId": "xyz"}'
analytics_service_url=$(k get ksvc -n ai-demo analytics-service -o=jsonpath='{.status.url}')
curl -v -X POST -H "Content-Type: application/json" ${analytics_service_url}/feedbacks -d '{"uploadId": "xyz", "feedback": 1}'
```

# Sending predictions

```shell
analytics_service_url=$(k get ksvc -n ai-demo analytics-service -o=jsonpath='{.status.url}')
curl -v -X POST -H "Content-Type: application/json" ${analytics_service_url}/predictions -d '{"uploadId": "xyz", "probability": 0.999, "x0": 0.24543, "x1": 0.24543, "y0": 0.24543, "y1": 0.24543}'
```


40 changes: 21 additions & 19 deletions services/analytics-service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def init_feedbacks_table():
cursor.execute('CREATE TABLE IF NOT EXISTS feedbacks ('
'id serial PRIMARY KEY,'
'feedback INT NOT NULL,'
'upload_id VARCHAR(200) NOT NULL,'
'upload_id VARCHAR(200) UNIQUE NOT NULL,'
'created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP);')
conn.commit()
cursor.close()
Expand All @@ -21,12 +21,12 @@ def init_predictions_table():
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS predictions ('
'id serial PRIMARY KEY,'
'probability DECIMAL(1, 12) NOT NULL,'
'upload_id VARCHAR(200) NOT NULL,'
'x0 DECIMAL(1, 12) NOT NULL,'
'x1 DECIMAL(1, 12) NOT NULL,'
'y0 DECIMAL(1, 12) NOT NULL,'
'y1 DECIMAL(1, 12) NOT NULL,'
'probability DECIMAL(13, 12) NOT NULL,'
'upload_id VARCHAR(200) UNIQUE NOT NULL,'
'x0 DECIMAL(13, 12) NOT NULL,'
'x1 DECIMAL(13, 12) NOT NULL,'
'y0 DECIMAL(13, 12) NOT NULL,'
'y1 DECIMAL(13, 12) NOT NULL,'
'created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP);')
conn.commit()
cursor.close()
Expand Down Expand Up @@ -66,15 +66,16 @@ def receive_feedbacks():

body = request.json

if 'feedback' not in body:
return f"'feedback' not in request", 400

if 'uploadId' not in body:
return f"'uploadId' not in request", 400
required_fields = ['feedback', 'uploadId']
for x in required_fields:
if x not in body:
return f"'{x}' not in request", 400

cur = conn.cursor()

cur.execute('INSERT INTO feedbacks (feedback, upload_id) VALUES (%s, %s)',
cur.execute('INSERT INTO feedbacks (feedback, upload_id) '
'VALUES (%s, %s) '
'ON CONFLICTS (upload_id) DO NOTHING',
(body['feedback'], body['uploadId']))

conn.commit()
Expand All @@ -99,16 +100,17 @@ def receive_predictions():

body = request.json

if 'probability' not in body:
return f"'probability' not in request", 400

if 'uploadId' not in body:
return f"'uploadId' not in request", 400
required_fields = ['probability', 'uploadId', 'x0', 'x1', 'y0', 'y1']
for x in required_fields:
if x not in body:
return f"'{x}' not in request", 400

cur = conn.cursor()

cur.execute(
'INSERT INTO predictions (probability, upload_id, x0, x1, y0, y1) VALUES (%s, %s, %s, %s, %s, %s)',
'INSERT INTO predictions (probability, upload_id, x0, x1, y0, y1) '
'VALUES (%s, %s, %s, %s, %s, %s) '
'ON CONFLICTS (upload_id) DO NOTHING',
(body['probability'], body['uploadId'], body['x0'], body['x1'], body['y0'], body['y1']))

conn.commit()
Expand Down

0 comments on commit 21d98a7

Please sign in to comment.