From 16ae1d9ccd0753141a887ccb2044d7943d2f5bc9 Mon Sep 17 00:00:00 2001 From: jbirddog <100367399+jbirddog@users.noreply.github.com> Date: Wed, 25 Oct 2023 12:59:09 -0400 Subject: [PATCH] Add support for KKV data stores to the data stores tab (#576) --- .../routes/data_store_controller.py | 33 +++++++++++++++++-- .../src/components/DataStoreList.tsx | 17 ++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/routes/data_store_controller.py b/spiffworkflow-backend/src/spiffworkflow_backend/routes/data_store_controller.py index d81f5225..a2a3f2ab 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/routes/data_store_controller.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/routes/data_store_controller.py @@ -6,6 +6,7 @@ from flask import make_response from spiffworkflow_backend import db from spiffworkflow_backend.exceptions.api_error import ApiError +from spiffworkflow_backend.models.kkv_data_store import KKVDataStoreModel from spiffworkflow_backend.models.typeahead import TypeaheadModel @@ -13,11 +14,15 @@ def data_store_list() -> flask.wrappers.Response: """Returns a list of the names of all the data stores.""" data_stores = [] - # Right now the only data store we support is type ahead + # Right now the only data stores we support are type ahead and kkv for cat in db.session.query(TypeaheadModel.category).distinct().order_by(TypeaheadModel.category): # type: ignore data_stores.append({"name": cat[0], "type": "typeahead"}) + keys = db.session.query(KKVDataStoreModel.top_level_key).distinct().order_by(KKVDataStoreModel.top_level_key) # type: ignore + for key in keys: + data_stores.append({"name": key[0], "type": "kkv"}) + return make_response(jsonify(data_stores), 200) @@ -44,5 +49,27 @@ def data_store_item_list( }, } return make_response(jsonify(response_json), 200) - else: - raise ApiError("unknown_data_store", f"Unknown data store type: {data_store_type}", status_code=400) + + if data_store_type == "kkv": + data_store_query = KKVDataStoreModel.query.filter_by(top_level_key=name).order_by( + KKVDataStoreModel.top_level_key, KKVDataStoreModel.secondary_key + ) + data = data_store_query.paginate(page=page, per_page=per_page, error_out=False) + results = [] + for kkv in data.items: + result = { + "secondary_key": kkv.secondary_key, + "value": kkv.value, + } + results.append(result) + response_json = { + "results": results, + "pagination": { + "count": len(data.items), + "total": data.total, + "pages": data.pages, + }, + } + return make_response(jsonify(response_json), 200) + + raise ApiError("unknown_data_store", f"Unknown data store type: {data_store_type}", status_code=400) diff --git a/spiffworkflow-frontend/src/components/DataStoreList.tsx b/spiffworkflow-frontend/src/components/DataStoreList.tsx index 990a9021..f6858c87 100644 --- a/spiffworkflow-frontend/src/components/DataStoreList.tsx +++ b/spiffworkflow-frontend/src/components/DataStoreList.tsx @@ -36,7 +36,6 @@ export default function DataStoreList() { 1, 'datastore' ); - console.log(); const dataStoreType = searchParams.get('type') || ''; const dataStoreName = searchParams.get('name') || ''; @@ -60,12 +59,24 @@ export default function DataStoreList() { }); }, [dataStores, searchParams]); + const getCell = (value: any) => { + const valueToUse = + typeof value === 'object' ? ( +
+          {JSON.stringify(value, null, 4)}
+        
+ ) : ( + value + ); + + return {valueToUse}; + }; + const getTable = () => { if (results.length === 0) { return null; } const firstResult = results[0]; - console.log('Results', results); const tableHeaders: any[] = []; const keys = Object.keys(firstResult); keys.forEach((key) => tableHeaders.push({key})); @@ -80,7 +91,7 @@ export default function DataStoreList() { return ( {keys.map((key) => { - return {object[key]}; + return getCell(object[key]); })} );