Skip to content

Commit

Permalink
Add support for KKV data stores to the data stores tab (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbirddog authored Oct 25, 2023
1 parent 209c8d6 commit 7642012
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@

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


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)


Expand All @@ -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)
17 changes: 14 additions & 3 deletions spiffworkflow-frontend/src/components/DataStoreList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default function DataStoreList() {
1,
'datastore'
);
console.log();
const dataStoreType = searchParams.get('type') || '';
const dataStoreName = searchParams.get('name') || '';

Expand All @@ -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>));
Expand All @@ -80,7 +91,7 @@ export default function DataStoreList() {
return (
<TableRow>
{keys.map((key) => {
return <TableCell>{object[key]}</TableCell>;
return getCell(object[key]);
})}
</TableRow>
);
Expand Down

0 comments on commit 7642012

Please sign in to comment.