ECM3408 Enterprise Computing Continuous Assessment Notes
Design (20%)
Effectiveness (10/10):
- Four microservices ("addTrack", "deleteTrack", "listTracks", "checkFragment") are offered and appear to implement the four user stories (S1-S4) fully.
Elegance (4/10):
- "addTrack", "deleteTrack", and "listTracks" are coupled via a shared database.
- "checkFragment" lacks cohesion as it combines fragment recognition and database lookup, which should be separated.
Implementation (60%)
Clarity (10/30):
- The implementation resembles a monolithic system, with everything running as a single Flask app on port 3000.
- The entire monolith must be understood, modified, and deployed at once; failure in any part causes total failure.
- The resulting 120-line program is hard to understand and doesn't meet the assessment's requirements.
Correctness (10/30):
- Usability issue: Cannot update and record simultaneously, hindering error correction.
- Not fully RESTful: The catalogue implements a Store instead of a Collection. Addition and update should use PUT instead of POST.
- Incorrect verb usage in URLs (e.g., "/deleteTrack"). Track resource names should appear in the URL and in a JSON object for addition.
- Incorrect response code: Successful deletion should return 204 No Content.
- API keys should not be included in source code.
Testing (20%)
- Coverage (14/20):
- The tests are too trusting; they don't verify data integrity by reading the database after additions or deletions.
- Looking at response codes and JSON messages is insufficient.
Appendix A: A Possible Design
Tracks Microservice
Allows creation and updating of music tracks.
PUT /tracks/id (Figure 1)
- Takes a JSON object with
id(string) andaudio(Base64 encoded WAV file) properties. - Used for database storage.
- Successful results: 201 Created, 204 No Content.
- Unsuccessful results: 400 Bad Request, 500 Internal Server Error.
- Takes a JSON object with
Allows reading of a music track.
GET /tracks/id (Figure 2)
- Successful result: 200 OK with a JSON object containing
id(string) andaudio(Base64 encoded WAV file). - Unsuccessful results: 404 Not Found, 500 Internal Server Error.
- Successful result: 200 OK with a JSON object containing
Allows listing all music tracks.
GET /tracks (Figure 3)
- Successful result: 200 OK with a list of strings (track IDs).
Allows deleting a music track.
DELETE /tracks/id (Figure 4)
- Successful result: 204 No Content.
- Unsuccessful results: 404 Not Found, 500 Internal Server Error.
Recognize Microservice
- Allows recognition of music fragments.
- POST /recognize (Figure 5)
- Takes a JSON object with
audio(Base64 encoded WAV file) property. - Successful result: 200 OK with a JSON object containing
id(string). - Unsuccessful results: 400 Bad Request, 404 Not Found, 500 Internal Server Error.
- Takes a JSON object with
Shamzam Microservice
- Allows retrieval of music tracks using music fragments.
- POST /shamzam (Figure 6)
- Takes a JSON object with
audio(Base64 encoded WAV file) property. - Successful result: 200 OK with a JSON object containing
audio(Base64 encoded WAV file) of the full track. - Unsuccessful results: 400 Bad Request, 404 Not Found, 500 Internal Server Error.
- Takes a JSON object with
Appendix B: A Possible Implementation
database.py
- Defines a
Repositoryclass for database operations. - Uses SQLite for storage.
import repository
db = repository.Repository("tracks")
recognize.py
- Implements the Recognize microservice using Flask.
- Uses Audd.io for audio recognition.
import base64
import os
import requests
from flask import Flask, request
AUDD_KEY = os.environ["AUDD_KEY"]
AUDD_PATH = "https://api.audd.io/recognize"
app = Flask(__name__)
@app.route("/recognize", methods=["POST"])
def endpoint():
js = request.get_json()
audio = js.get("audio")
if audio != None:
hdrs = {"api_token": AUDD_KEY}
fake = {"file": ("decoded", base64.b64decode(audio))}
rsp = requests.post(AUDD_PATH, headers=hdrs, files=fake)
if rsp.status_code == 200:
js2 = rsp.json()
return unravel(js2)
else:
return "", 500 # Internal Server Error
else:
return "", 400 # Bad Request
def unravel(js):
if js.get("status") == "success":
if js.get("result") != None:
return {"id": js["result"]["title"]}, 200 # OK
else:
return "", 404 # Not Found
else:
return "", 400 # Bad Request
if __name__ == "__main__":
app.run(host="localhost", port=3001)
repository.py
- Defines the
Repositoryclass with methods for database interactions.
import sqlite3
class Repository:
def __init__(self, table):
self.table = table
self.database = f'{self.table}.db'
self.make()
def make(self):
with sqlite3.connect(self.database) as connection:
cursor = connection.cursor()
cursor.execute(f"CREATE TABLE IF NOT EXISTS {self.table} " +
"(id TEXT PRIMARY KEY, audio TEXT)")
connection.commit()
def clear(self):
with sqlite3.connect(self.database) as connection:
cursor = connection.cursor()
cursor.execute(f"DELETE FROM {self.table}")
connection.commit()
def insert(self, js):
with sqlite3.connect(self.database) as connection:
cursor = connection.cursor()
cursor.execute(f"INSERT INTO {self.table} (id, audio) VALUES (?,?)",
(js["id"], js["audio"]))
connection.commit()
return cursor.rowcount
def update(self, js):
with sqlite3.connect(self.database) as connection:
cursor = connection.cursor()
cursor.execute(f"UPDATE {self.table} SET audio =? WHERE id =?",
(js["audio"], js["id"]))
connection.commit()
return cursor.rowcount
def lookup(self, id):
with sqlite3.connect(self.database) as connection:
cursor = connection.cursor()
cursor.execute(f"SELECT id, audio FROM {self.table} WHERE id =?", (id,))
row = cursor.fetchone()
if row:
return {"id": row[0], "audio": row[1]}
else:
return None
def delete(self, id):
with sqlite3.connect(self.database) as connection:
cursor = connection.cursor()
cursor.execute("DELETE FROM tracks WHERE id =?", (id,))
connection.commit()
return cursor.rowcount > 0
def list(self):
with sqlite3.connect(self.database) as connection:
cursor = connection.cursor()
cursor.execute("SELECT * FROM tracks")
return [row[0] for row in cursor.fetchall()]
shamzam.py
- Implements the Shamzam microservice using Flask.
- Communicates with the Tracks and Recognize microservices.
import requests
from flask import Flask, request
tracks = "http://localhost:3000/tracks"
recognize = "http://localhost:3001/recognize"
app = Flask(__name__)
@app.route("/shamzam", methods=["POST"])
def endpoint():
js = request.get_json()
hdrs = {"Content-Type": "application/json"}
rsp = requests.post(recognize, headers=hdrs, json=js)
if rsp.status_code == 200:
js2 = rsp.json()
rsp = requests.get(f'{tracks}/{js2["id"]}')
if rsp.status_code == 200:
return rsp.json(), 200 # OK
else:
return "", 404 # Not Found
else:
return "", 404 # Not Found
if __name__ == "__main__":
app.run(host="localhost", port=3002)
tracks.py
- Implements the Tracks microservice using Flask.
- Uses the
databasemodule for database operations.
import database
from flask import Flask, request
app = Flask(__name__)
@app.route("/tracks/<string:id>", methods=["PUT"])
def endpoint1(id):
js = request.get_json()
id2 = js.get("id")
audio = js.get("audio")
if id2 != None and audio != None and id == id2:
if database.db.lookup(id) != None:
if database.db.update(js):
return "", 204 # No Content
else:
return "", 500 # Internal Server Error
else:
if database.db.insert(js):
return "", 201 # Created
else:
return "", 500 # Internal Server Error
else:
return "", 400 # Bad Request
@app.route("/tracks/<string:id>", methods=["GET"])
def endpoint2(id):
js = database.db.lookup(id)
if js != None:
return js, 200 # OK
else:
return "", 404 # Not Found
@app.route("/tracks/<string:id>", methods=["DELETE"])
def endpoint3(id):
if database.db.lookup(id) != None:
if database.db.delete(id):
return "", 204 # No Content
else:
return "", 500 # Internal Server Error
else:
return "", 404 # Bad Request
@app.route("/tracks", methods=["GET"])
def endpoint4():
js = database.db.list()
return js, 200 # OK
if __name__ == "__main__":
app.run(host="localhost", port=3000)
test-shamzam.py
- Implements unit tests (end-to-end tests) for each Shamzam user story.
import base64
import database
import requests
import unittest
tracks = "http://localhost:3000/tracks"
recognize = "http://localhost:3001/recognize"
shamzam = "http://localhost:3002/shamzam"
class Testing(unittest.TestCase):
def test1(self):
database.db.clear()
id = "Blinding Lights"
f = open(f'{id}.wav', "rb")
wav = f.read()
f.close()
audio = base64.b64encode(wav).decode("ascii")
track = {"id": id, "audio": audio}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 201)
rsp = requests.get(f'{tracks}/{id}')
self.assertEqual(rsp.status_code, 200)
self.assertEqual(id, rsp.json()["id"])
self.assertEqual(audio, rsp.json()["audio"])
def test2(self):
database.db.clear()
id = "Don’t Look Back In Anger"
f = open(f'{id}.wav', "rb")
wav = f.read()
f.close()
audio = base64.b64encode(wav).decode("ascii")
track = {"id": id, "audio": audio}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 201)
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 204)
rsp = requests.get(f'{tracks}/{id}')
self.assertEqual(rsp.status_code, 200)
self.assertEqual(id, rsp.json()["id"])
self.assertEqual(audio, rsp.json()["audio"])
def test3(self):
database.db.clear()
id = "Everybody ( Backstreet’s Back ) ( Radio Edit )"
f = open(f'{id}.wav', "rb")
wav = f.read()
f.close()
audio = base64.b64encode(wav).decode("ascii")
track = {"audio": audio}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 400)
def test4(self):
database.db.clear()
id = "good 4 u"
track = {"id": id}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 400)
def test5(self):
database.db.clear()
id = "Blinding Lights"
f = open(f'{id}.wav', "rb")
wav = f.read()
f.close()
audio = base64.b64encode(wav).decode("ascii")
track = {"id": id, "audio": audio}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 201)
rsp = requests.delete(f'{tracks}/{id}')
self.assertEqual(rsp.status_code, 204)
rsp = requests.get(f'{tracks}/{id}')
self.assertEqual(rsp.status_code, 404)
def test6(self):
database.db.clear()
id = "Davos"
rsp = requests.delete(f'{tracks}/{id}')
self.assertEqual(rsp.status_code, 404)
def test7(self):
database.db.clear()
id1 = "Blinding Lights"
f = open(f'{id1}.wav', "rb")
wav = f.read()
f.close()
audio = base64.b64encode(wav).decode("ascii")
track = {"id": id1, "audio": audio}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id1}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 201)
id2 = "Don’t Look Back In Anger"
f = open(f'{id2}.wav', "rb")
wav = f.read()
f.close()
audio = base64.b64encode(wav).decode("ascii")
track = {"id": id2, "audio": audio}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id2}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 201)
id3 = "Everybody ( Backstreet’s Back ) ( Radio Edit )"
f = open(f'{id3}.wav', "rb")
wav = f.read()
f.close()
audio = base64.b64encode(wav).decode("ascii")
track = {"id": id3, "audio": audio}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id3}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 201)
id4 = "good 4 u"
f = open(f'{id4}.wav', "rb")
wav = f.read()
f.close()
audio = base64.b64encode(wav).decode("ascii")
track = {"id": id4, "audio": audio}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id4}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 201)
rsp = requests.get(tracks)
self.assertEqual(rsp.status_code, 200)
self.assertTrue(id1 in rsp.json())
self.assertTrue(id2 in rsp.json())
self.assertTrue(id3 in rsp.json())
self.assertTrue(id4 in rsp.json())
def test8(self):
database.db.clear()
id1 = "Blinding Lights"
f1 = open(f'{id1}.wav', "rb")
wav1 = f1.read()
f1.close()
audio1 = base64.b64encode(wav1).decode("ascii")
track = {"id": id1, "audio": audio1}
hdrs = {"Content-Type": "application/json"}
rsp = requests.put(f'{tracks}/{id1}', headers=hdrs, json=track)
self.assertEqual(rsp.status_code, 201)
f2 = open(f'~{id1}.wav', "rb")
wav2 = f2.read()
f2.close()
audio2 = base64.b64encode(wav2).decode("ascii")
frag = {"audio": audio2}
hdrs = {"Content-Type": "application/json"}
rsp = requests.post(shamzam, headers=hdrs, json=frag)
self.assertEqual(rsp.status_code, 200)
self.assertEqual(audio1, rsp.json()["audio"])