From 88e6adc5da4685c05de500bb7c0860cb3fb65d55 Mon Sep 17 00:00:00 2001 From: becr3102 Date: Tue, 27 Sep 2022 15:17:40 -0600 Subject: [PATCH 1/5] #DS-491 #Updated Add two new fields from Prefect flow to API interface --- app/graphql/graphql.py | 2 ++ app/utility/utility.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/app/graphql/graphql.py b/app/graphql/graphql.py index 7397de6..3189b9e 100644 --- a/app/graphql/graphql.py +++ b/app/graphql/graphql.py @@ -66,6 +66,8 @@ def make_request( low_cost_flag no_cost_flag oer_flag + cc_id + item_id ClassBookRequirement { code } diff --git a/app/utility/utility.py b/app/utility/utility.py index 1e808a6..8c63066 100644 --- a/app/utility/utility.py +++ b/app/utility/utility.py @@ -34,6 +34,8 @@ def flatten_book_record(book): result.session = book.Session.code result.term = book.Session.Term.code result.requirement = book.ClassBookRequirement.code + result.cc_id = book.cc_id + result.item_id = book.item_id return result.to_dict() From 5631bd9fc0e79c738b4173a02a80398ca6408f1d Mon Sep 17 00:00:00 2001 From: becr3102 Date: Tue, 27 Sep 2022 15:35:58 -0600 Subject: [PATCH 2/5] #DS-491 #Updated improved code formatting --- app/graphql/graphql.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/graphql/graphql.py b/app/graphql/graphql.py index 3189b9e..fcc7272 100644 --- a/app/graphql/graphql.py +++ b/app/graphql/graphql.py @@ -90,8 +90,9 @@ def make_request( if r.status_code == 200: if r.json().get("data"): - return r.status_code, flatten_book_records( - r.json()["data"]["books_v0_2_Book"] + return ( + r.status_code, + flatten_book_records(r.json()["data"]["books_v0_2_Book"]), ) else: return 422, {"Unable to parse results."} From 6f9c6a0cd96752ae86c8d9498014eb8a9566f6e2 Mon Sep 17 00:00:00 2001 From: becr3102 Date: Tue, 27 Sep 2022 15:54:04 -0600 Subject: [PATCH 3/5] #DS-491 #Update Replace string format with f-string per pylint --- Untitled.ipynb | 200 +++++++++++++++++++++++++++++++++++++++++++++++++ app/main.py | 2 +- 2 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 Untitled.ipynb diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..acbbb5c --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,200 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "3b18ce39-cdf3-4a33-a031-1844c5fd0f8e", + "metadata": {}, + "outputs": [], + "source": [ + "import time" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "0e40f96a-c747-471e-bc67-9369434c1e80", + "metadata": {}, + "outputs": [], + "source": [ + "start_time = time.time()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "7cd58981-279f-4017-adbe-5190f9ef7899", + "metadata": {}, + "outputs": [], + "source": [ + "process_time = (time.time() - start_time) * 1000" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "75256c5e-c9d8-42f9-89ab-6f41e342a0f0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "965.6069278717041" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "process_time" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "8e489e7d-cbd9-4f87-9601-2dabe8ada6ef", + "metadata": {}, + "outputs": [], + "source": [ + "formatted_process_time = \"{0:.2f}\".format(process_time)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "fc600f2b-521f-4e82-9817-19bb371e5ded", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'965.61'" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "formatted_process_time" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "3b738aa3-3252-465f-b419-57c2a82f1ba6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'965.6069278717041'" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f'{process_time}'" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "4a6b7086-ac9a-4132-a0de-536022d12c60", + "metadata": {}, + "outputs": [], + "source": [ + "x = 3.14159265" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "19cbc134-d42e-4f9a-af82-4287bf748f8f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pi = 3.14\n" + ] + } + ], + "source": [ + "print('pi = %0.2f' %x)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "238523af-58ab-4203-994a-39f81feacd31", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pi = %0.2f\n" + ] + } + ], + "source": [ + "print(('pi = %0.2f').format(x))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "db9040d3-ec24-4099-820f-eff3cdfae602", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pi = 3.14\n" + ] + } + ], + "source": [ + "print(f'pi = {x:.2f}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7b2c9fb7-14f2-4d22-afca-98c3ea2b48dd", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/app/main.py b/app/main.py index 6dbee9c..787bdbc 100644 --- a/app/main.py +++ b/app/main.py @@ -45,7 +45,7 @@ async def log_requests(request: Request, call_next): response = await call_next(request) process_time = (time.time() - start_time) * 1000 - formatted_process_time = "{0:.2f}".format(process_time) + formatted_process_time = f'{process_time:.2f}' logger.info( "rid=%s completed_in=%sms status_code=%s", idem, From c0e735ae3e8914335a5bc5482acfebdf1d4503d0 Mon Sep 17 00:00:00 2001 From: becr3102 Date: Tue, 27 Sep 2022 16:24:36 -0600 Subject: [PATCH 4/5] #DS-491 #Updated Fixed linting and changed pseudo-random generator to secrets gnerator for security --- Untitled.ipynb | 200 ------------------------------------------------- app/main.py | 18 ++++- 2 files changed, 15 insertions(+), 203 deletions(-) delete mode 100644 Untitled.ipynb diff --git a/Untitled.ipynb b/Untitled.ipynb deleted file mode 100644 index acbbb5c..0000000 --- a/Untitled.ipynb +++ /dev/null @@ -1,200 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "3b18ce39-cdf3-4a33-a031-1844c5fd0f8e", - "metadata": {}, - "outputs": [], - "source": [ - "import time" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "0e40f96a-c747-471e-bc67-9369434c1e80", - "metadata": {}, - "outputs": [], - "source": [ - "start_time = time.time()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "7cd58981-279f-4017-adbe-5190f9ef7899", - "metadata": {}, - "outputs": [], - "source": [ - "process_time = (time.time() - start_time) * 1000" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "id": "75256c5e-c9d8-42f9-89ab-6f41e342a0f0", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "965.6069278717041" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "process_time" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "id": "8e489e7d-cbd9-4f87-9601-2dabe8ada6ef", - "metadata": {}, - "outputs": [], - "source": [ - "formatted_process_time = \"{0:.2f}\".format(process_time)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "id": "fc600f2b-521f-4e82-9817-19bb371e5ded", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'965.61'" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "formatted_process_time" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "id": "3b738aa3-3252-465f-b419-57c2a82f1ba6", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'965.6069278717041'" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "f'{process_time}'" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "4a6b7086-ac9a-4132-a0de-536022d12c60", - "metadata": {}, - "outputs": [], - "source": [ - "x = 3.14159265" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "19cbc134-d42e-4f9a-af82-4287bf748f8f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "pi = 3.14\n" - ] - } - ], - "source": [ - "print('pi = %0.2f' %x)" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "id": "238523af-58ab-4203-994a-39f81feacd31", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "pi = %0.2f\n" - ] - } - ], - "source": [ - "print(('pi = %0.2f').format(x))" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "db9040d3-ec24-4099-820f-eff3cdfae602", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "pi = 3.14\n" - ] - } - ], - "source": [ - "print(f'pi = {x:.2f}')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7b2c9fb7-14f2-4d22-afca-98c3ea2b48dd", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/app/main.py b/app/main.py index 787bdbc..7f8bc8b 100644 --- a/app/main.py +++ b/app/main.py @@ -4,7 +4,6 @@ import datetime import logging import logging.config -import random import secrets import string import time @@ -38,14 +37,27 @@ async def log_requests(request: Request, call_next): Tidy bit of logging assist to show how long each call takes. Found here: https://philstories.medium.com/fastapi-logging-f6237b84ea64 """ - idem = "".join(random.choices(string.ascii_uppercase + string.digits, k=6)) + + # Create idem with secrets module instead of random module + # which is unsuitable for security/cryptographic purposes + string_source = string.ascii_uppercase + string.digits + idem = secrets.choice(string.ascii_uppercase) + idem += secrets.choice(string.digits) + + for _ in range(4): + idem += secrets.choice(string_source) + + char_list = list(idem) + secrets.SystemRandom().shuffle(char_list) + idem = "".join(char_list) + logger.info("rid=%s start request path=%s", idem, request.url.path) start_time = time.time() response = await call_next(request) process_time = (time.time() - start_time) * 1000 - formatted_process_time = f'{process_time:.2f}' + formatted_process_time = f"{process_time:.2f}" logger.info( "rid=%s completed_in=%sms status_code=%s", idem, From 740e20677017f5fdf5d7f5c3c26e3556c13d29a4 Mon Sep 17 00:00:00 2001 From: becr3102 Date: Tue, 27 Sep 2022 16:48:47 -0600 Subject: [PATCH 5/5] #DS-491 #Update Update tests for graphql results with new fields --- test/flattened_output1.json | 4 +++- test/graphql_output1.json | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/test/flattened_output1.json b/test/flattened_output1.json index 59e3455..aad3e43 100644 --- a/test/flattened_output1.json +++ b/test/flattened_output1.json @@ -17,7 +17,9 @@ "low_cost_flag": "N", "no_cost_flag": "N", "oer_flag": "N", - "requirement": "R" + "requirement": "R", + "cc_id": "null", + "item_id": "null" } ] } \ No newline at end of file diff --git a/test/graphql_output1.json b/test/graphql_output1.json index 70b6fd6..2c4f7e2 100644 --- a/test/graphql_output1.json +++ b/test/graphql_output1.json @@ -33,6 +33,8 @@ "low_cost_flag": "N", "no_cost_flag": "N", "oer_flag": "N", + "cc_id": "null", + "item_id": "null", "ClassBookRequirement": { "code": "R" }