Skip to content

Commit

Permalink
client: add ignore domains for stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Blakhard committed Mar 21, 2023
1 parent eefc389 commit 24d75c4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 31 deletions.
1 change: 1 addition & 0 deletions client/src/__locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@
"statistics_retention_confirm": "Are you sure you want to change statistics retention? If you decrease the interval value, some data will be lost",
"statistics_cleared": "Statistics successfully cleared",
"statistics_enable": "Enable statistics",
"statistics_ignore_domains": "Ignore domains (separated by newline)",
"interval_hours": "{{count}} hour",
"interval_hours_plural": "{{count}} hours",
"filters_configuration": "Filters configuration",
Expand Down
2 changes: 1 addition & 1 deletion client/src/actions/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const getStatsConfigSuccess = createAction('GET_STATS_CONFIG_SUCCESS');
export const getStatsConfig = () => async (dispatch) => {
dispatch(getStatsConfigRequest());
try {
const data = await apiClient.getStatsInfo();
const data = await apiClient.getStatsConfig();
dispatch(getStatsConfigSuccess(data));
} catch (error) {
dispatch(addErrorToast({ error }));
Expand Down
10 changes: 5 additions & 5 deletions client/src/api/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,9 @@ class Api {
// Settings for statistics
GET_STATS = { path: 'stats', method: 'GET' };

STATS_INFO = { path: 'stats_info', method: 'GET' };
GET_STATS_CONFIG = { path: 'stats/config', method: 'GET' };

STATS_CONFIG = { path: 'stats_config', method: 'POST' };
UPDATE_STATS_CONFIG = { path: 'stats/config/update', method: 'PUT' };

STATS_RESET = { path: 'stats_reset', method: 'POST' };

Expand All @@ -508,13 +508,13 @@ class Api {
return this.makeRequest(path, method);
}

getStatsInfo() {
const { path, method } = this.STATS_INFO;
getStatsConfig() {
const { path, method } = this.GET_STATS_CONFIG;
return this.makeRequest(path, method);
}

setStatsConfig(data) {
const { path, method } = this.STATS_CONFIG;
const { path, method } = this.UPDATE_STATS_CONFIG;
const config = {
data,
};
Expand Down
41 changes: 23 additions & 18 deletions client/src/components/Settings/StatsConfig/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ import { Field, reduxForm } from 'redux-form';
import { Trans, withTranslation } from 'react-i18next';
import flow from 'lodash/flow';

import { renderRadioField, toNumber, CheckboxField } from '../../../helpers/form';
import { FORM_NAME, STATS_INTERVALS_DAYS, DISABLED_STATS_INTERVAL } from '../../../helpers/constants';
import {
renderRadioField,
toNumber,
CheckboxField,
renderTextareaField,
} from '../../../helpers/form';
import {
FORM_NAME,
STATS_INTERVALS_DAYS,
DAY,
} from '../../../helpers/constants';
import '../FormButton.css';

const getIntervalTitle = (interval, t) => {
switch (interval) {
const getIntervalTitle = (intervalMs, t) => {
switch (intervalMs / DAY) {
case 1:
return t('interval_24_hour');
default:
return t('interval_days', { count: interval });
return t('interval_days', { count: intervalMs / DAY });
}
};

const Form = (props) => {
const {
handleSubmit,
change,
processing,
submitting,
invalid,
Expand All @@ -38,13 +46,6 @@ const Form = (props) => {
component={CheckboxField}
placeholder={t('statistics_enable')}
disabled={processing}
onChange={(event) => {
if (event.target.checked) {
change('interval', STATS_INTERVALS_DAYS[0]);
} else {
change('interval', DISABLED_STATS_INTERVAL);
}
}}
/>
</div>
<label className="form__label form__label--with-desc">
Expand All @@ -65,15 +66,19 @@ const Form = (props) => {
placeholder={getIntervalTitle(interval, t)}
normalize={toNumber}
disabled={processing}
onChange={(event) => {
if (event.target.checked) {
change('enabled', true);
}
}}
/>
))}
</div>
</div>
<div className="form__group form__group--settings">
<Field
name="ignored"
type="textarea"
component={renderTextareaField}
placeholder={t('statistics_ignore_domains')}
disabled={processing}
/>
</div>
<div className="mt-5">
<button
type="submit"
Expand Down
15 changes: 11 additions & 4 deletions client/src/components/Settings/StatsConfig/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import Card from '../../ui/Card';
import Form from './Form';

class StatsConfig extends Component {
handleFormSubmit = (values) => {
handleFormSubmit = ({ enabled, interval, ignored }) => {
const { t, interval: prevInterval } = this.props;
const config = { interval: values.interval };
const config = {
enabled,
interval,
ignored: ignored ? ignored.split('\n') : [],
};

if (config.interval < prevInterval) {
if (window.confirm(t('statistics_retention_confirm'))) {
Expand All @@ -29,7 +33,7 @@ class StatsConfig extends Component {

render() {
const {
t, interval, processing, processingReset,
t, interval, processing, processingReset, ignored, enabled,
} = this.props;

return (
Expand All @@ -42,7 +46,8 @@ class StatsConfig extends Component {
<Form
initialValues={{
interval,
enabled: !!interval,
enabled,
ignored: ignored.join('\n'),
}}
onSubmit={this.handleFormSubmit}
processing={processing}
Expand All @@ -57,6 +62,8 @@ class StatsConfig extends Component {

StatsConfig.propTypes = {
interval: PropTypes.number.isRequired,
ignored: PropTypes.array.isRequired,
enabled: PropTypes.bool.isRequired,
processing: PropTypes.bool.isRequired,
processingReset: PropTypes.bool.isRequired,
setStatsConfig: PropTypes.func.isRequired,
Expand Down
4 changes: 4 additions & 0 deletions client/src/components/Settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Settings extends Component {
<div className="col-md-12">
<StatsConfig
interval={stats.interval}
ignored={stats.ignored}
enabled={stats.enabled}
processing={stats.processingSetConfig}
processingReset={stats.processingReset}
setStatsConfig={setStatsConfig}
Expand Down Expand Up @@ -139,6 +141,8 @@ Settings.propTypes = {
stats: PropTypes.shape({
processingGetConfig: PropTypes.bool,
interval: PropTypes.number,
enabled: PropTypes.bool,
ignored: PropTypes.array,
processingSetConfig: PropTypes.bool,
processingReset: PropTypes.bool,
}),
Expand Down
7 changes: 6 additions & 1 deletion client/src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ export const FILTERED = 'Filtered';
export const NOT_FILTERED = 'NotFiltered';

export const DISABLED_STATS_INTERVAL = 0;
export const STATS_INTERVALS_DAYS = [1, 7, 30, 90];

export const HOUR = 60 * 60 * 1000;

export const DAY = HOUR * 24;

export const STATS_INTERVALS_DAYS = [DAY, DAY * 7, DAY * 30, DAY * 90];

export const QUERY_LOG_INTERVALS_DAYS = [0.25, 1, 7, 30, 90];

Expand Down
4 changes: 2 additions & 2 deletions client/src/reducers/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ const stats = handleActions(
[actions.getStatsConfigFailure]: (state) => ({ ...state, processingGetConfig: false }),
[actions.getStatsConfigSuccess]: (state, { payload }) => ({
...state,
interval: payload.interval,
...payload,
processingGetConfig: false,
}),

[actions.setStatsConfigRequest]: (state) => ({ ...state, processingSetConfig: true }),
[actions.setStatsConfigFailure]: (state) => ({ ...state, processingSetConfig: false }),
[actions.setStatsConfigSuccess]: (state, { payload }) => ({
...state,
interval: payload.interval,
...payload,
processingSetConfig: false,
}),

Expand Down

0 comments on commit 24d75c4

Please sign in to comment.