replaced old api calls with new version (#5254)

This commit is contained in:
Martavis Parker 2022-04-20 10:45:32 -07:00 committed by GitHub
parent 9f981f9e49
commit 0cc02b5fe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 111 additions and 9 deletions

View File

@ -3,7 +3,7 @@ import PropTypes from "prop-types";
import classnames from "classnames";
import { isEqual, noop } from "lodash";
import Fleet from "fleet";
import targetsAPI from "services/entities/targets";
import targetInterface from "interfaces/target";
import { formatSelectedTargetsForApi } from "fleet/helpers";
import Input from "./SelectTargetsInput";
@ -134,8 +134,12 @@ class SelectTargetsDropdown extends Component {
this.setState({ isLoadingTargets: true, query });
return Fleet.targets
.loadAll(query, queryId, formatSelectedTargetsForApi(selectedTargets))
return targetsAPI
.DEPRECATED_loadAll(
query,
queryId,
formatSelectedTargetsForApi(selectedTargets)
)
.then((response) => {
const { targets } = response;
const isEmpty = targets.length === 0;

View File

@ -3,14 +3,13 @@ import { useQuery, useMutation } from "react-query";
import { InjectedRouter, Params } from "react-router/lib/Router";
import { useErrorHandler } from "react-error-boundary";
// @ts-ignore
import Fleet from "fleet";
import { AppContext } from "context/app";
import { PolicyContext } from "context/policy";
import { QUERIES_PAGE_STEPS, DEFAULT_POLICY } from "utilities/constants";
import globalPoliciesAPI from "services/entities/global_policies";
import teamPoliciesAPI from "services/entities/team_policies";
import hostAPI from "services/entities/hosts";
import statusAPI from "services/entities/status";
import { IPolicyFormData, IPolicy } from "interfaces/policy";
import { ITarget } from "interfaces/target";
import { IHost } from "interfaces/host";
@ -154,7 +153,7 @@ const PolicyPage = ({
);
const detectIsFleetQueryRunnable = () => {
Fleet.status.live_query().catch(() => {
statusAPI.live_query().catch(() => {
setIsLiveQueryRunnable(false);
});
};

View File

@ -3,13 +3,12 @@ import { useQuery, useMutation } from "react-query";
import { useErrorHandler } from "react-error-boundary";
import { InjectedRouter, Params } from "react-router/lib/Router";
// @ts-ignore
import Fleet from "fleet";
import { AppContext } from "context/app";
import { QueryContext } from "context/query";
import { QUERIES_PAGE_STEPS, DEFAULT_QUERY } from "utilities/constants";
import queryAPI from "services/entities/queries";
import hostAPI from "services/entities/hosts";
import statusAPI from "services/entities/status";
import { IHost } from "interfaces/host";
import { IQueryFormData, IQuery } from "interfaces/query";
import { ITarget } from "interfaces/target";
@ -122,7 +121,7 @@ const QueryPage = ({
);
const detectIsFleetQueryRunnable = () => {
Fleet.status.live_query().catch(() => {
statusAPI.live_query().catch(() => {
setIsLiveQueryRunnable(false);
});
};

View File

@ -0,0 +1,15 @@
import sendRequest from "services";
import endpoints from "fleet/endpoints";
export default {
result_store: () => {
const { STATUS_RESULT_STORE } = endpoints;
return sendRequest("GET", STATUS_RESULT_STORE);
},
live_query: () => {
const { STATUS_LIVE_QUERY } = endpoints;
return sendRequest("GET", STATUS_LIVE_QUERY);
},
};

View File

@ -2,6 +2,7 @@
import sendRequest from "services";
import endpoints from "fleet/endpoints";
import { ITargetsAPIResponse, ISelectedTargets } from "interfaces/target";
import appendTargetTypeToTargets from "utilities/append_target_type_to_targets";
interface ITargetsProps {
query?: string;
@ -15,6 +16,13 @@ const defaultSelected = {
teams: [],
};
// TODO: deprecated until frontend\components\forms\fields\SelectTargetsDropdown
// is fully replaced with frontend\components\TargetsInput
const DEPRECATED_defaultSelected = {
hosts: [],
labels: [],
};
export default {
loadAll: ({
query = "",
@ -29,4 +37,30 @@ export default {
selected,
});
},
// TODO: deprecated until frontend\components\forms\fields\SelectTargetsDropdown
// is fully replaced with frontend\components\TargetsInput
DEPRECATED_loadAll: (
query = "",
queryId = null,
selected = DEPRECATED_defaultSelected
) => {
const { TARGETS } = endpoints;
return sendRequest("POST", TARGETS, {
query,
query_id: queryId,
selected,
}).then((response) => {
const { targets } = response;
return {
...response,
targets: [
...appendTargetTypeToTargets(targets.hosts, "hosts"),
...appendTargetTypeToTargets(targets.labels, "labels"),
...appendTargetTypeToTargets(targets.teams, "teams"),
],
};
});
},
};

View File

@ -0,0 +1,51 @@
import { IHost } from "interfaces/host";
import { map } from "lodash";
export const parseEntityFunc = (host: IHost) => {
let hostCpuOutput = null;
if (host) {
let clockSpeedOutput = null;
try {
const clockSpeed =
host.cpu_brand.split("@ ")[1] || host.cpu_brand.split("@")[1];
const clockSpeedFlt = parseFloat(clockSpeed.split("GHz")[0].trim());
clockSpeedOutput = Math.floor(clockSpeedFlt * 10) / 10;
} catch (e) {
// Some CPU brand strings do not fit this format and we can't parse the
// clock speed. Leave it set to 'Unknown'.
console.log(
`Unable to parse clock speed from cpu_brand: ${host.cpu_brand}`
);
}
if (host.cpu_physical_cores || clockSpeedOutput) {
hostCpuOutput = `${host.cpu_physical_cores || "Unknown"} x ${
clockSpeedOutput || "Unknown"
} GHz`;
}
}
const additionalAttrs = {
cpu_type: hostCpuOutput,
target_type: "hosts",
};
return {
...host,
...additionalAttrs,
};
};
const appendTargetTypeToTargets = (targets: any, targetType: string) => {
return map(targets, (target) => {
if (targetType === "hosts") {
return parseEntityFunc(target);
}
return {
...target,
target_type: targetType,
};
});
};
export default appendTargetTypeToTargets;