Fix export hosts filtering (#5905)

This commit is contained in:
Luke Heath 2022-05-25 18:48:48 -05:00 committed by GitHub
parent 5322508a10
commit 49b409904b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 42 deletions

View file

@ -1352,15 +1352,19 @@ const ManageHostsPage = ({
}`}
>
<span>{`${count} host${count === 1 ? "" : "s"}`}</span>
<Button
className={`${baseClass}__export-btn`}
onClick={onExportHostsResults}
variant="text-link"
>
<>
Export hosts <img alt="" src={DownloadIcon} />
</>
</Button>
{count ? (
<Button
className={`${baseClass}__export-btn`}
onClick={onExportHostsResults}
variant="text-link"
>
<>
Export hosts <img alt="" src={DownloadIcon} />
</>
</Button>
) : (
<></>
)}
</div>
);
}, [isHostCountLoading, filteredHostCount]);

View file

@ -23,6 +23,21 @@ export interface ILoadHostsOptions {
visibleColumns?: string;
}
export interface IExportHostsOptions {
sortBy: ISortOption[];
page?: number;
perPage?: number;
selectedLabels?: string[];
globalFilter?: string;
teamId?: number;
policyId?: number;
policyResponse?: string;
softwareId?: number;
device_mapping?: boolean;
columns?: string;
visibleColumns?: string;
}
export type ILoadHostDetailsExtension = "device_mapping" | "macadmins";
export default {
@ -53,63 +68,56 @@ export default {
},
});
},
exportHosts: (options: ILoadHostsOptions | undefined) => {
const { HOSTS_REPORT, LABEL_HOSTS } = endpoints;
exportHosts: (options: IExportHostsOptions) => {
const { HOSTS_REPORT } = endpoints;
const sortBy = options.sortBy;
const selectedLabels = options?.selectedLabels || [];
const globalFilter = options?.globalFilter || "";
const sortBy = options?.sortBy || [];
const teamId = options?.teamId || null;
const policyId = options?.policyId || null;
const policyResponse = options?.policyResponse || null;
const policyResponse = options?.policyResponse || "passing";
const softwareId = options?.softwareId || null;
const visibleColumns = options?.visibleColumns || null;
let orderKeyParam = "";
let orderDirection = "";
if (sortBy.length !== 0) {
const sortItem = sortBy[0];
orderKeyParam += `&order_key=${sortItem.key}`;
orderDirection = `&order_direction=${sortItem.direction}`;
if (!sortBy.length) {
throw Error("sortBy is a required field.");
}
let searchQuery = "";
const orderKeyParam = `?order_key=${sortBy[0].key}`;
const orderDirection = `&order_direction=${sortBy[0].direction}`;
let path = `${HOSTS_REPORT}${orderKeyParam}${orderDirection}`;
if (globalFilter !== "") {
searchQuery = `&query=${globalFilter}`;
path += `&query=${globalFilter}`;
}
let path = "";
const labelPrefix = "labels/";
// Handle multiple filters
const label = selectedLabels.find((f) => f.includes(labelPrefix));
const status = selectedLabels.find((f) => !f.includes(labelPrefix));
const isValidStatus =
status === "new" || status === "online" || status === "offline";
if (label) {
const lid = label.substr(labelPrefix.length);
path = `${LABEL_HOSTS(
parseInt(lid, 10)
)}?${searchQuery}${orderKeyParam}${orderDirection}`;
const label = selectedLabels.find((f) => f.includes(labelPrefix)) || "";
const status = selectedLabels.find((f) => !f.includes(labelPrefix)) || "";
const statusFilterList = ["new", "online", "offline"];
const isStatusFilter = statusFilterList.includes(status);
// connect status if applicable
if (status && isValidStatus) {
path += `&status=${status}`;
}
} else if (status && isValidStatus) {
path = `${HOSTS_REPORT}?&status=${status}${searchQuery}${orderKeyParam}${orderDirection}`;
} else {
path = `${HOSTS_REPORT}?${searchQuery}${orderKeyParam}${orderDirection}`;
if (isStatusFilter) {
path += `&status=${status}`;
}
if (teamId) {
path += `&team_id=${teamId}`;
}
// Label OR policy_id OR software_id are valid filters.
if (label) {
const lid = label.substr(labelPrefix.length);
path += `&label_id=${parseInt(lid, 10)}`;
}
if (!label && policyId) {
path += `&policy_id=${policyId}`;
path += `&policy_response=${policyResponse || "passing"}`; // TODO: confirm whether there should be a default if there is an id but no response sepcified
path += `&policy_response=${policyResponse}`;
}
// TODO: consider how to check for mutually exclusive scenarios with label, policy and software
if (!label && !policyId && softwareId) {
path += `&software_id=${softwareId}`;
}