fleet/frontend/components/forms/ConfigurePackQueryForm/ConfigurePackQueryForm.jsx
Martavis Parker 5d647025f9
1497 improved query experience (#1998)
* Step 1 for improving query experience (#1591)

* fake change to create draft PR

* temp routes to work and not modify old query page

* created new API abstraction for query

* refactored App.jsx to prepare react-query

* fixed flow of redirects after page refresh; functional component added

* setup for getting data on edit

* implementing functions for query page

* Old form showing on new setup

* improving and breaking up query form

* no need for the helpers anymore; clean up

* added type for button component variant

* step toward new save modal; have to switch gears to #1619

* creating new query works

* clean up

* linting cleanup

* added default value for new query

* will address dynamic save disabled in edit step

* Step 2 for improving query experience (select targets) (#1732)

* fake change to create draft PR

* temp routes to work and not modify old query page

* created new API abstraction for query

* refactored App.jsx to prepare react-query

* fixed flow of redirects after page refresh; functional component added

* setup for getting data on edit

* implementing functions for query page

* Old form showing on new setup

* improving and breaking up query form

* no need for the helpers anymore; clean up

* added type for button component variant

* step toward new save modal; have to switch gears to #1619

* creating new query works

* clean up

* linting cleanup

* added default value for new query

* split steps into separate files for readability

* components laid out

* new targets picker

* function clean up

* styling tables

* fixing logic

* fixed logic to keep getting related hosts

* formatting targets for API

* fixed default query

* clean up

* styled target selectors; fixed target input styles

* began total count

* forgot to remove debugging code

* lint fixes

* added target count from API

* clean up

* able to remove selected host targets from table

* lint fixes

* Improving query experience - Step 3 (query results) (#1766)

* fake change to create draft PR

* temp routes to work and not modify old query page

* created new API abstraction for query

* refactored App.jsx to prepare react-query

* fixed flow of redirects after page refresh; functional component added

* setup for getting data on edit

* implementing functions for query page

* Old form showing on new setup

* improving and breaking up query form

* no need for the helpers anymore; clean up

* added type for button component variant

* step toward new save modal; have to switch gears to #1619

* creating new query works

* clean up

* linting cleanup

* added default value for new query

* split steps into separate files for readability

* components laid out

* new targets picker

* function clean up

* styling tables

* fixing logic

* fixed logic to keep getting related hosts

* formatting targets for API

* fixed default query

* clean up

* styled target selectors; fixed target input styles

* began total count

* forgot to remove debugging code

* lint fixes

* added target count from API

* clean up

* able to remove selected host targets from table

* lint fixes

* connected run query with modern React/JS; clean up

* linting fixes

* fixed logic to retrieve results from live query

* linting fixes

* created new, simpler query progress

* populating results and errors tables as expected

* syntax fixes

* fixing styles for query results

* more styling for query results

* manual merge from main

* Rename core->free and basic->premium

* Fix lint js

* Comment out portion of test that seems to timeout

* Rename tier  to premium if basic is still loaded

* go sum

* Query Experience Cleanup Tasks (#1807)

* fixes to get merged main branch to build and work

* moved screens for query pages; clean up

* updated and typed react ace for query form; clean up

* using console error instead

* added real types instead of `any` except for errors

* query side panel ts and functional. prep for close task.

* ability to hide, show query table sidebar

* improved live query status warning

* added loading and error state for targets search

* error screen for targets; improved loading display

* now using API-created label for all linux

* missed some files on previous commit

* able to edit query

* clean up

* lint fixes

* query results showing as they come

* remove unused code

* removed old query page. major file cleanup.

* removed selectedTargets redux implementation

* removed unused redux actions and reducers

* removed unused keys in initial state

* selectedOsqueryTable is now using context API

* removed all querypages redux code

* set up context for app and user

* fixed auth with temp fix for wrapper

* completed redux removal from query page

* fixed var names coming from main branch

* fixed var name changes coming from issue 1501

* fixed save popup bug; clean up

* added permissions

* fixed login redirect

* removed unused props

* linting fix

* clean up

* removed unused component, refactor, and clean up

* fixed styles for step 1 as admin

* fixed styles for step 1 as observer

* fixed percentage of online hosts

* added loading progress to query stop button

* reset query status on run again

* added download icon to export button text

* fixed error reset on name input; fixed styles

* fixed bug where query value wasn't saving

* fixed query value when blank

* fixed bug - default query was running every time

* auto adding host from url to targets

* fixed flows for repeating run and save steps

* fleet ace is now TS and functional

* fixed a couple of tests

* fixed issues with query value text inconsistencies

* fixed query side panel not showing

* hiding error count if not > 0

* fixed showing editor for different roles

* using integer for targets

* go sum

* fixed targets param

* catching all errors while running query

* fixed hover state for title and description

* ignore unit test for now; lint fixes

* locking react-ace version

* ignoring tests breaking in github actions

* brought tests back

* fixing file name

* fixing file name again

* fixed e2e test

* have to ignore tests for now

* ignore certain premium tests for now

* one last test to revamp

* another test

* fixed teamflow test

* fixed observer query 403

* lint fixes

* fixed maintainer test

* added changes file

Co-authored-by: Tomas Touceda <chiiph@gmail.com>
2021-09-10 12:06:37 -07:00

167 lines
4.6 KiB
JavaScript

import React, { Component } from "react";
import PropTypes from "prop-types";
import { pull } from "lodash";
import Button from "components/buttons/Button";
import Dropdown from "components/forms/fields/Dropdown";
import Form from "components/forms/Form";
import formFieldInterface from "interfaces/form_field";
import InputField from "components/forms/fields/InputField";
import validate from "components/forms/ConfigurePackQueryForm/validate";
import {
FREQUENCY_DROPDOWN_OPTIONS,
PLATFORM_DROPDOWN_OPTIONS,
LOGGING_TYPE_OPTIONS,
MIN_OSQUERY_VERSION_OPTIONS,
} from "utilities/constants";
const baseClass = "configure-pack-query-form";
const fieldNames = [
"query_id",
"interval",
"logging_type",
"platform",
"shard",
"version",
];
export class ConfigurePackQueryForm extends Component {
static propTypes = {
fields: PropTypes.shape({
interval: formFieldInterface.isRequired,
logging_type: formFieldInterface.isRequired,
platform: formFieldInterface.isRequired,
version: formFieldInterface.isRequired,
shard: formFieldInterface.isRequired,
}).isRequired,
formData: PropTypes.shape({
id: PropTypes.number,
}),
handleSubmit: PropTypes.func,
onCancel: PropTypes.func,
};
componentWillMount() {
const { fields } = this.props;
if (fields && fields.shard && !fields.shard.value) {
fields.shard.value = "";
}
}
onCancel = (evt) => {
evt.preventDefault();
const { formData, onCancel: handleCancel } = this.props;
return handleCancel(formData);
};
handlePlatformChoice = (value) => {
const {
fields: { platform },
} = this.props;
const valArray = value.split(",");
// Remove All if another OS is chosen
if (valArray.indexOf("") === 0 && valArray.length > 1) {
return platform.onChange(pull(valArray, "").join(","));
}
// Remove OS if All is chosen
if (valArray.length > 1 && valArray.indexOf("") > -1) {
return platform.onChange("");
}
return platform.onChange(value);
};
renderCancelButton = () => {
const { formData } = this.props;
const { onCancel } = this;
if (!formData.id) {
return false;
}
return (
<Button
className={`${baseClass}__cancel-btn`}
onClick={onCancel}
variant="inverse"
>
Cancel
</Button>
);
};
render() {
const { fields, handleSubmit } = this.props;
const { handlePlatformChoice, renderCancelButton } = this;
// Uncontrolled form field defaults to snapshot if !fields.logging_type
const loggingType = fields.logging_type.value || "snapshot";
return (
<form className={baseClass} onSubmit={handleSubmit}>
<h2 className={`${baseClass}__title`}>Configuration</h2>
<div className={`${baseClass}__fields`}>
<Dropdown
{...fields.logging_type}
options={LOGGING_TYPE_OPTIONS}
placeholder="- - -"
label="Logging"
value={loggingType}
wrapperClassName={`${baseClass}__form-field ${baseClass}__form-field--logging`}
/>
<InputField
{...fields.interval}
inputWrapperClass={`${baseClass}__form-field ${baseClass}__form-field--frequency`}
placeholder="- - -"
label="Frequency (seconds)"
// hint="Seconds"
type="number"
/>
<Dropdown
{...fields.platform}
options={PLATFORM_DROPDOWN_OPTIONS}
placeholder="- - -"
label="Platform"
onChange={handlePlatformChoice}
multi
wrapperClassName={`${baseClass}__form-field ${baseClass}__form-field--platform`}
/>
<Dropdown
{...fields.version}
options={MIN_OSQUERY_VERSION_OPTIONS}
placeholder="- - -"
label="Minimum osquery version"
wrapperClassName={`${baseClass}__form-field ${baseClass}__form-field--osquer-vers`}
/>
<InputField
{...fields.shard}
inputWrapperClass={`${baseClass}__form-field ${baseClass}__form-field--shard`}
placeholder="- - -"
label="Shard"
type="number"
/>
<div className={`${baseClass}__btn-wrapper`}>
{renderCancelButton()}
<Button
className={`${baseClass}__submit-btn`}
type="submit"
variant="brand"
>
Save
</Button>
</div>
</div>
</form>
);
}
}
export default Form(ConfigurePackQueryForm, {
fields: fieldNames,
validate,
});