diff --git a/app.py b/app.py new file mode 100644 index 0000000..c1d1af2 --- /dev/null +++ b/app.py @@ -0,0 +1,190 @@ +from models.sorts import database +from flask import Flask, request, render_template +import io +import csv +import time + +app = Flask(__name__) + + +@app.route("/") +def main(): + return "Tugas Seleksi IRK 2021" + +@app.route("/sort/selection", methods=["POST"]) +def selectionSort(): + file = request.files["file"] + column = request.form["column"] + orientation = request.form["orientation"] + stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None) + csv_input = csv.reader(stream) + final_table = [row for row in csv_input] + column_data = [row[int(column)] for row in final_table if len(row) > int(column)] + + # Preprocess + numCount = 0 + strCount = 0 + for data in column_data: + if data.isnumeric(): + numCount = numCount + 1 + else: + strCount = strCount + 1 + + if (numCount >= strCount): + column_data = [data for data in column_data if data.isnumeric()] + else: + column_data = [data for data in column_data if not data.isnumeric()] + + # Selection Sort + result = [] + start_time = time.time() + if (orientation == "ASC"): + while (len(column_data) != 0): + minimum = column_data[0] + for i in range(1,len(column_data)): + if (column_data[i] < minimum): + minimum = column_data[i] + result.append(minimum) + column_data.remove(minimum) + else: + while (len(column_data) != 0): + maximum = column_data[0] + for i in range(1,len(column_data)): + if (column_data[i] > maximum): + maximum = column_data[i] + result.append(maximum) + column_data.remove(maximum) + exec_time = time.time() - start_time + + print(result) + + params = { + "algorithm": "selection", + "result": ",".join(result), + "exec_time": str(exec_time) + } + + mysqldb.postSort(**params) + + for i in range(len(final_table)): + if(len(final_table[i]) > int(column) and len(result) > i): + final_table[i][int(column)] = result[i] + if(len(result) <= i and len(final_table[i]) > int(column)): + final_table[i][int(column)] = None + + htmlResult = """""" + for row in final_table: + htmlResult = htmlResult + """""" + for col in row: + if (col is not None): + htmlResult = htmlResult + """""" + htmlResult = htmlResult + """""" + htmlResult = htmlResult + """
""" + htmlResult = htmlResult + str(col) + htmlResult = htmlResult + """
""" + + return htmlResult + +@app.route("/sort/insertion", methods=["POST"]) +def insertionSort(): + file = request.files["file"] + column = request.form["column"] + orientation = request.form["orientation"] + stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None) + csv_input = csv.reader(stream) + final_table = [row for row in csv_input] + column_data = [row[int(column)] for row in final_table if len(row) > int(column)] + + # Preprocess + numCount = 0 + strCount = 0 + for data in column_data: + if data.isnumeric(): + numCount = numCount + 1 + else: + strCount = strCount + 1 + + if (numCount >= strCount): + column_data = [data for data in column_data if data.isnumeric()] + else: + column_data = [data for data in column_data if not data.isnumeric()] + + # Insertion Sort + result = column_data + start_time = time.time() + if (orientation == "ASC"): + for i in range(1, len(result)): + pivot = result[i] + j = i - 1 + while j >= 0 and pivot < result[j] : + result[j + 1] = result[j] + j -= 1 + result[j + 1] = pivot + else: + for i in range(1, len(result)): + pivot = result[i] + j = i - 1 + while j >= 0 and pivot > result[j] : + result[j + 1] = result[j] + j -= 1 + result[j + 1] = pivot + exec_time = time.time() - start_time + + params = { + "algorithm": "insertion", + "result": ",".join(result), + "exec_time": str(exec_time) + } + + print(result) + + mysqldb.postSort(**params) + + for i in range(len(final_table)): + if(len(final_table[i]) > int(column) and len(result) > i): + final_table[i][int(column)] = result[i] + if(len(result) <= i and len(final_table[i]) > int(column)): + final_table[i][int(column)] = None + + print(final_table) + + htmlResult = """""" + for row in final_table: + htmlResult = htmlResult + """""" + for col in row: + if (col is not None): + htmlResult = htmlResult + """""" + htmlResult = htmlResult + """""" + htmlResult = htmlResult + """
""" + htmlResult = htmlResult + str(col) + htmlResult = htmlResult + """
""" + + return htmlResult + + +@app.route("/sort/result", methods=["GET"]) +def getResult(): + if "id" in request.args.keys(): + sort_id = request.args["id"] + response = mysqldb.getSort(sort_id) + else: + response = mysqldb.getSort() + + response_dict = { + "id": response[0], + "date_time": response[1], + "algorithm": response[2], + "result": response[3], + "exec_time": response[4] + } + return response_dict + +if __name__ == "__main__": + mysqldb = database() + if mysqldb.db.is_connected(): + print('Connected to MySQL database') + + app.run(debug=True) + + if mysqldb.db is not None and mysqldb.db.is_connected(): + mysqldb.db.close() \ No newline at end of file diff --git a/html/options.html b/html/options.html new file mode 100644 index 0000000..ec5588e --- /dev/null +++ b/html/options.html @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
365
443
237
1
2
3
4
5
+ + + \ No newline at end of file diff --git a/models/__pycache__/sorts.cpython-39.pyc b/models/__pycache__/sorts.cpython-39.pyc new file mode 100644 index 0000000..599ed1d Binary files /dev/null and b/models/__pycache__/sorts.cpython-39.pyc differ diff --git a/models/sorts.py b/models/sorts.py new file mode 100644 index 0000000..eb4e997 --- /dev/null +++ b/models/sorts.py @@ -0,0 +1,38 @@ +from mysql.connector import connect + +class database: + def __init__(self): + try: + self.db = connect(host='localhost', + database='sortdb', + user='root', + password='taisanta') + except Exception as e: + print(e) + + def postSort(self, **params): + try: + print(params["algorithm"]) + cursor = self.db.cursor() + post_query ='''insert into sorts (algorithm, result, exec_time) values ('{0}', '{1}', {2})'''.format( + params["algorithm"], params["result"], params["exec_time"] + ) + print(post_query) + cursor.execute(post_query) + self.db.commit() + return "Insert Success" + except Exception as e: + print(e) + + def getSort(self, id=None): + try: + cursor = self.db.cursor() + if(id is None): + query ='''select * from sorts order by date_time desc limit 1;''' + else: + query ='''select * from sorts where id = {0};'''.format(id) + cursor.execute(query) + result = cursor.fetchone() + return result + except Exception as e: + print(e) \ No newline at end of file diff --git a/test/data.csv b/test/data.csv new file mode 100644 index 0000000..3bff530 --- /dev/null +++ b/test/data.csv @@ -0,0 +1,206 @@ +3,4,5 +4,b,3 +2,6,7 +1,3 +2 +3 +4 +5 +1 +2 +3 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +1 +2 +3 +4 +5 \ No newline at end of file