Add support for KKV data stores to the data stores tab (#576)

This commit is contained in:
jbirddog 2023-10-25 12:59:09 -04:00 committed by GitHub
parent ffaaf03f97
commit 16ae1d9ccd
2 changed files with 44 additions and 6 deletions

View File

@ -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)

View File

@ -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' ? (
<pre>
<code>{JSON.stringify(value, null, 4)}</code>
</pre>
) : (
value
);
return <TableCell>{valueToUse}</TableCell>;
};
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(<TableHeader>{key}</TableHeader>));
@ -80,7 +91,7 @@ export default function DataStoreList() {
return (
<TableRow>
{keys.map((key) => {
return <TableCell>{object[key]}</TableCell>;
return getCell(object[key]);
})}
</TableRow>
);