Skip to content

Commit

Permalink
Merge pull request #89 from ssi-dk/feat/types
Browse files Browse the repository at this point in the history
feat: added support for multiple tree methods, added type annotations
  • Loading branch information
allanhvam committed Aug 26, 2024
2 parents aca1aa9 + 12985f2 commit 360d7a4
Show file tree
Hide file tree
Showing 60 changed files with 4,288 additions and 7,585 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ ${mkfile_dir}/web/src/services/bio_api/openapi : ${mkfile_dir}/openapi_specs/bio
rm -rf ${mkfile_dir}/web/src/services/bio_api/openapi
docker run --rm -v "${mkfile_dir}:/local" \
--user ${mkfile_user} \
"openapitools/openapi-generator:cli-v5.2.0" \
"openapitools/openapi-generator-cli:v7.7.0" \
generate \
-i /local/openapi_specs/bio_api.yaml \
-g python \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ export const NearestNeighborModal = (props: Props) => {
const { selection, onClose } = props;
const [isSearching, setIsSearching] = useState<boolean>(false);
const [cutoff, setCutoff] = React.useState(15);
const onChangeCutoff = (value: string) => setCutoff(parseInt(value));
const onChangeCutoff = (value: string) => {
const n = parseInt(value);
if (isNaN(n)) {
setCutoff(0);
return;
}
setCutoff(n);
};
const [unknownsAreDiffs, setUnknownsAreDiffs] = useState<boolean>(false);
const toast = useToast();
const dispatch = useDispatch();
Expand Down
19 changes: 13 additions & 6 deletions app/src/app/workspaces/send-to-microreact-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { sendToMicroreact as sendToMicroreactQuery } from "./microreact-query-co
import { RootState } from "app/root-reducer";
import { useSelector } from "react-redux";
import { TreeMethod, UserInfo, WorkspaceInfo } from "sap-client";
import { TreeMethodSelect } from "./tree-method-select";
import { TreeMethodCheckboxGroup } from "./tree-method-checkbox-group";

type Props = {
workspace: string;
Expand All @@ -34,7 +34,9 @@ export const SendToMicroreactModal = (props: Props) => {
localStorage.getItem(localStorageKey) ?? ""
);
const [isSending, setIsSending] = useState<boolean>(false);
const [treeMethod, setTreeMethod] = useState<string>(TreeMethod.single);
const [treeMethods, setTreeMethods] = useState<Array<string>>([
TreeMethod.single,
]);

const workspaceInfo = useSelector<RootState>(
(s) => s.entities.workspace ?? {}
Expand All @@ -45,7 +47,7 @@ export const SendToMicroreactModal = (props: Props) => {
return sendToMicroreactQuery({
workspace: workspace,
mr_access_token: token,
tree_method: TreeMethod[treeMethod],
tree_methods: treeMethods.map((tm) => TreeMethod[tm]),
});
});

Expand Down Expand Up @@ -89,8 +91,11 @@ export const SendToMicroreactModal = (props: Props) => {
/>
</div>
<div>
{t("Tree method")}:
<TreeMethodSelect value={treeMethod} onChange={setTreeMethod} />
{t("Tree methods")}:
<TreeMethodCheckboxGroup
value={treeMethods}
onChange={setTreeMethods}
/>
</div>
</div>
) : null}
Expand All @@ -104,7 +109,9 @@ export const SendToMicroreactModal = (props: Props) => {
colorScheme="blue"
mr={3}
onClick={onSend}
disabled={isSending || !token || !treeMethod}
disabled={
isSending || !token || !treeMethods || treeMethods.length === 0
}
>
{t("Send")}
</Button>
Expand Down
38 changes: 38 additions & 0 deletions app/src/app/workspaces/tree-method-checkbox-group.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Checkbox, CheckboxGroup, Stack } from "@chakra-ui/react";
import React, { useCallback, useMemo } from "react";
import { TreeMethod } from "sap-client";
import { StringOrNumber } from "utils";

type Props = {
onChange: (id: Array<string>) => void;
value: Array<string>;
};

export const TreeMethodCheckboxGroup = (props: Props) => {
const { onChange, value } = props;

const treeMethods = useMemo(() => {
const methods = new Array<string>();
for (const tm in TreeMethod) {
methods.push(tm);
}
return methods;
}, []);

const onChangeCallback = useCallback(
(checkboxValue: StringOrNumber[]) => {
onChange(checkboxValue.map((v) => String(v)));
},
[onChange]
);

return (
<CheckboxGroup defaultValue={value} onChange={onChangeCallback}>
<Stack spacing={2}>
{treeMethods?.map((treeMethod) => {
return <Checkbox value={treeMethod}>{treeMethod}</Checkbox>;
})}
</Stack>
</CheckboxGroup>
);
};
8 changes: 4 additions & 4 deletions app/src/sap-client/models/NewMicroreactProjectRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ export interface NewMicroreactProjectRequest {
mr_access_token: string;
/**
*
* @type {TreeMethod}
* @type {Array<TreeMethod>}
* @memberof NewMicroreactProjectRequest
*/
tree_method: TreeMethod;
tree_methods: Array<TreeMethod>;
}

export function NewMicroreactProjectRequestFromJSON(json: any): NewMicroreactProjectRequest {
return {
'workspace': json['workspace'],
'mr_access_token': json['mr_access_token'],
'tree_method': TreeMethodFromJSON(json['tree_method']),
'tree_methods': (json['tree_methods'] as Array<any>).map(TreeMethodFromJSON),
};
}

Expand All @@ -59,7 +59,7 @@ export function NewMicroreactProjectRequestToJSON(value?: NewMicroreactProjectRe
return {
'workspace': value.workspace,
'mr_access_token': value.mr_access_token,
'tree_method': TreeMethodToJSON(value.tree_method),
'tree_methods': (value.tree_methods as Array<any>).map(TreeMethodToJSON),
};
}

Expand Down
15 changes: 14 additions & 1 deletion bifrost/bifrost_queue_broker/common/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import binascii
import logging

from typing import Dict
from typing import Dict, Literal, Tuple, overload

from dateutil import parser
from pymongo import MongoClient, mongo_client
Expand Down Expand Up @@ -37,6 +37,15 @@
) = SOFI_BIFROST_ENCRYPTION_NAMESPACE.split(".", 1)

# The MongoClient is thread safe and pooled, so no problem sharing it :)
@overload
def get_connection() -> MongoClient: ...

@overload
def get_connection(with_enc: Literal[False]) -> MongoClient: ...

@overload
def get_connection(with_enc: Literal[True]) -> Tuple[MongoClient, ClientEncryption]: ...

def get_connection(with_enc=False):
"""
Returns instance of global pooled connection.
Expand Down Expand Up @@ -119,6 +128,10 @@ def get_connection(with_enc=False):
else:
return CONNECTION

def get_collection(collection: str):
conn = get_connection()
db = conn[DB_NAME]
return db[collection]

def recursive_replace(data, replacement_fn, filter_list=None, filtered_parent=False):
# If no filter_list is provided, then assume all leaf nodes in tree must be replaced
Expand Down
8 changes: 5 additions & 3 deletions openapi_specs/SOFI/SOFI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1589,12 +1589,14 @@ components:
type: string
mr_access_token:
type: string
tree_method:
$ref: "#/components/schemas/TreeMethod"
tree_methods:
type: array
items:
$ref: "#/components/schemas/TreeMethod"
required:
- workspace
- mr_access_token
- tree_method
- tree_methods

NewMicroreactProjectResponse:
properties:
Expand Down
8 changes: 5 additions & 3 deletions web/openapi_specs/SOFI/SOFI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1589,12 +1589,14 @@ components:
type: string
mr_access_token:
type: string
tree_method:
$ref: "#/components/schemas/TreeMethod"
tree_methods:
type: array
items:
$ref: "#/components/schemas/TreeMethod"
required:
- workspace
- mr_access_token
- tree_method
- tree_methods

NewMicroreactProjectResponse:
properties:
Expand Down
3 changes: 2 additions & 1 deletion web/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ python-jose[cryptography]
six>=1.9
jwcrypto >= 0.8
Flask-JWT-Extended >= 3.25.1
openapi-spec-validator==0.2.9
openapi-spec-validator==0.2.9
pydantic==2.8.2
15 changes: 14 additions & 1 deletion web/src/SAP/common/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import binascii
import logging

from typing import Dict
from typing import Dict, Literal, Tuple, overload

from dateutil import parser
from pymongo import MongoClient, mongo_client
Expand Down Expand Up @@ -37,6 +37,15 @@
) = SOFI_BIFROST_ENCRYPTION_NAMESPACE.split(".", 1)

# The MongoClient is thread safe and pooled, so no problem sharing it :)
@overload
def get_connection() -> MongoClient: ...

@overload
def get_connection(with_enc: Literal[False]) -> MongoClient: ...

@overload
def get_connection(with_enc: Literal[True]) -> Tuple[MongoClient, ClientEncryption]: ...

def get_connection(with_enc=False):
"""
Returns instance of global pooled connection.
Expand Down Expand Up @@ -119,6 +128,10 @@ def get_connection(with_enc=False):
else:
return CONNECTION

def get_collection(collection: str):
conn = get_connection()
db = conn[DB_NAME]
return db[collection]

def recursive_replace(data, replacement_fn, filter_list=None, filtered_parent=False):
# If no filter_list is provided, then assume all leaf nodes in tree must be replaced
Expand Down
38 changes: 19 additions & 19 deletions web/src/SAP/generated/models/new_microreact_project_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ class NewMicroreactProjectRequest(Model):
Do not edit the class manually.
"""

def __init__(self, workspace=None, mr_access_token=None, tree_method=None): # noqa: E501
def __init__(self, workspace=None, mr_access_token=None, tree_methods=None): # noqa: E501
"""NewMicroreactProjectRequest - a model defined in OpenAPI
:param workspace: The workspace of this NewMicroreactProjectRequest. # noqa: E501
:type workspace: str
:param mr_access_token: The mr_access_token of this NewMicroreactProjectRequest. # noqa: E501
:type mr_access_token: str
:param tree_method: The tree_method of this NewMicroreactProjectRequest. # noqa: E501
:type tree_method: TreeMethod
:param tree_methods: The tree_methods of this NewMicroreactProjectRequest. # noqa: E501
:type tree_methods: List[TreeMethod]
"""
self.openapi_types = {
'workspace': str,
'mr_access_token': str,
'tree_method': TreeMethod,
'tree_methods': List[TreeMethod],
}

self.attribute_map = {
'workspace': 'workspace',
'mr_access_token': 'mr_access_token',
'tree_method': 'tree_method',
'tree_methods': 'tree_methods',
}

self._workspace = workspace
self._mr_access_token = mr_access_token
self._tree_method = tree_method
self._tree_methods = tree_methods

@classmethod
def from_dict(cls, dikt):
Expand Down Expand Up @@ -103,24 +103,24 @@ def mr_access_token(self, mr_access_token):
self._mr_access_token = mr_access_token

@property
def tree_method(self):
"""Gets the tree_method of this NewMicroreactProjectRequest.
def tree_methods(self):
"""Gets the tree_methods of this NewMicroreactProjectRequest.
:return: The tree_method of this NewMicroreactProjectRequest.
:rtype: TreeMethod
:return: The tree_methods of this NewMicroreactProjectRequest.
:rtype: List[TreeMethod]
"""
return self._tree_method
return self._tree_methods

@tree_method.setter
def tree_method(self, tree_method):
"""Sets the tree_method of this NewMicroreactProjectRequest.
@tree_methods.setter
def tree_methods(self, tree_methods):
"""Sets the tree_methods of this NewMicroreactProjectRequest.
:param tree_method: The tree_method of this NewMicroreactProjectRequest.
:type tree_method: TreeMethod
:param tree_methods: The tree_methods of this NewMicroreactProjectRequest.
:type tree_methods: List[TreeMethod]
"""
if tree_method is None:
raise ValueError("Invalid value for `tree_method`, must not be `None`") # noqa: E501
if tree_methods is None:
raise ValueError("Invalid value for `tree_methods`, must not be `None`") # noqa: E501

self._tree_method = tree_method
self._tree_methods = tree_methods
11 changes: 8 additions & 3 deletions web/src/SAP/generated/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1600,17 +1600,22 @@ components:
NewMicroreactProjectRequest:
example:
workspace: workspace
tree_methods:
- null
- null
mr_access_token: mr_access_token
properties:
workspace:
type: string
mr_access_token:
type: string
tree_method:
$ref: '#/components/schemas/TreeMethod'
tree_methods:
items:
$ref: '#/components/schemas/TreeMethod'
type: array
required:
- mr_access_token
- tree_method
- tree_methods
- workspace
type: object
NewMicroreactProjectResponse:
Expand Down
1 change: 1 addition & 0 deletions web/src/SAP/generated/test/test_microreact_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_send_to_microreact(self):
"""
body = {
"workspace" : "workspace",
"tree_methods" : [ null, null ],
"mr_access_token" : "mr_access_token"
}
headers = {
Expand Down
Loading

0 comments on commit 360d7a4

Please sign in to comment.