Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DASHBOARD_COLUMNS to config, with defaults to match current COSRI #156

Merged
merged 10 commits into from
Nov 15, 2022
29 changes: 29 additions & 0 deletions patientsearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ def load_json_config(potential_json_string):
"Your account is not authorized for access, please contact an administrator",
)

DASHBOARD_COLUMNS = json.loads(
os.getenv(
"DASHBOARD_COLUMNS",
json.dumps(
[
{
"label": "First Name",
"expr": "$.name[0].given[0]",
},
{
"label": "Last Name",
"expr": "$.name[0].family",
},
{
"label": "Birth Date",
"expr": "$.birthDate",
},
{
"label": "Last Accessed",
"defaultSort": "desc",
"expr": "$.meta.lastUpdated",
"dataType": "date",
},
],
),
)
)

FHIR_REST_EXTRA_PARAMS_LIST = json.loads(os.getenv("FHIR_REST_EXTRA_PARAMS_LIST", "[]"))
LANDING_INTRO = os.getenv("LANDING_INTRO", "")
LANDING_BUTTON_TEXT = os.getenv("LANDING_BUTTON_TEXT", "")
LANDING_BODY = os.getenv("LANDING_BODY", "")
Expand Down
96 changes: 91 additions & 5 deletions patientsearch/src/js/components/PatientListTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getUrlParameter,
getRolesFromToken,
getClientsByRequiredRoles,
isEmptyArray,
isString,
validateToken,
} from "../helpers/utility";
Expand Down Expand Up @@ -218,6 +219,7 @@ export default function PatientListTable() {
last_name: "family",
birth_date: "birthdate",
last_accessed: "_lastUpdated",
mrn: "identifier"
};
const default_columns = [
{
Expand All @@ -232,6 +234,12 @@ export default function PatientListTable() {
label: "Birth Date",
expr: "$.birthDate",
},
{
label: "Last Accessed",
defaultSort: "desc",
expr: "$.meta.lastUpdated",
dataType: "date",
},
];
const errorStyle = { display: errorMessage ? "block" : "none" };
const toTop = () => {
Expand All @@ -245,7 +253,11 @@ export default function PatientListTable() {
};
const getColumns = () => {
const configColumns = getAppSettingByKey("DASHBOARD_COLUMNS");
let cols = configColumns ? configColumns : default_columns;
const isValidConfig = configColumns && Array.isArray(configColumns);
let cols = isValidConfig ? configColumns : default_columns;
if (!isValidConfig) {
console.log("invalid columns via config. Null or not an array.");
}
const hasIdField = cols.filter((col) => col.field === "id").length > 0;
//columns must include an id field, add if not present
if (!hasIdField)
Expand Down Expand Up @@ -634,6 +646,7 @@ export default function PatientListTable() {
totalCount: 0,
};
let apiURL = `/fhir/Patient?_include=Patient:link&_total=accurate&_count=${pagination.pageSize}`;

if (
pagination.pageNumber > pagination.prevPageNumber &&
pagination.nextPageURL
Expand Down Expand Up @@ -665,8 +678,7 @@ export default function PatientListTable() {
resolve(defaults);
return;
}
let responseData = formatData(response.entry);
setData(responseData || []);

if (needExternalAPILookup()) setNoPMPFlag(responseData);
let responsePageoffset = 0;
let responseSelfLink = response.link
Expand Down Expand Up @@ -711,11 +723,85 @@ export default function PatientListTable() {
totalCount: response.total,
},
});
resolve({

let patientResources = response.entry.filter(
(item) => item.resource && item.resource.resourceType === "Patient"
);

let responseData = formatData(patientResources) || [];

const additionalParams = getAppSettingByKey(
"FHIR_REST_EXTRA_PARAMS_LIST"
);
const resolvedData = {
data: responseData,
page: currentPage,
totalCount: response.total,
});
};
if (isEmptyArray(additionalParams)) {
setData(responseData);
resolve(resolvedData);
return;
}
// query for additional resources if specified via config
// gather patient id(s) from API returned result
const ids = patientResources
.map((item) => item.resource.id)
.join(",");
// FHIR resources request(s)
const requests = additionalParams.map((queryString) =>
fetchData(
`/fhir/${queryString}` +
(queryString.indexOf("?") !== -1 ? "&" : "?") +
`patient=${ids}&_count=1000`,
noCacheParam
)
);
const queryResults = (async () => {
const results = await Promise.all(requests).catch((e) => {
throw new Error(e);
});
if (isEmptyArray(results)) return patientResources;
return patientResources.map((item) => {
let subjectId = item.resource.id;
if (!item.resource["resources"]) item.resource["resources"] = [];
results.forEach((result) => {
if (isEmptyArray(result.entry)) return true;
item.resource["resources"] = [
...item.resource["resources"],
...result.entry
.filter(
(o) =>
o.resource &&
o.resource.subject &&
o.resource.subject.reference &&
o.resource.subject.reference.split("/")[1] === subjectId
)
.map((resourceItem) => resourceItem.resource),
];
});
return item;
});
})();
queryResults
.then((data) => {
console.log("query result data ", data);
const resultData = formatData(data);
setData(resultData || []);
resolve({
data: resultData,
page: currentPage,
totalCount: response.total,
});
})
.catch((e) => {
setErrorMessage(
"Error retrieving additional FHIR resources. See console for detail."
);
console.log(e);
setData(responseData);
resolve(resolvedData);
});
})
.catch((error) => {
console.log("Failed to retrieve data", error);
Expand Down
4 changes: 4 additions & 0 deletions patientsearch/src/js/helpers/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,7 @@ export function setFavicon(href) {
if (!faviconEl) return;
faviconEl.href = href;
}

export function isEmptyArray(arrObj) {
return !arrObj || !Array.isArray(arrObj) || arrObj.length === 0;
}
55 changes: 55 additions & 0 deletions tests/test_columnconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from datetime import datetime
import jmespath
import json
import os

from pytest import fixture


def load_json(datadir, filename):
with open(os.path.join(datadir, filename), "r") as json_file:
data = json.load(json_file)
return data


@fixture
def patient_bundle(datadir):
return load_json(datadir, "patient_bundle.json")


def test_id_count(patient_bundle):
assert patient_bundle["total"] == 7
expr = jmespath.compile("entry[?resource.resourceType=='Patient'].resource.id")
results = expr.search(patient_bundle)
assert len(results) == 7


def test_max_authored_by_patient(patient_bundle):
results = {}
date_format = "%Y-%m-%d"

expr = jmespath.compile("entry[?resource.resourceType=='Patient'].resource.id")
patient_ids = expr.search(patient_bundle)

for id in patient_ids:
# from each patient ID, obtain the list of QuestionnaireResponse.authored values
expr = jmespath.compile(
f"entry[?resource.subject.reference=='Patient/{id}'].resource.authored"
)

# create a list of sortable datetime objects from authored date strings
auth_dates = [
datetime.strptime(a, date_format) for a in expr.search(patient_bundle)
]
auth_dates.sort()

# retain string form for max found for patient, or empty string
results[id] = (
datetime.strftime(auth_dates[-1], date_format) if auth_dates else ""
)

# Confirm a few known
assert len(results) == 7
assert results["53"] == "2022-11-02"
assert results["test-patient-1"] == ""
assert results["test-patient-2"] == "2022-11-02"
Loading