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

feature/AN-3212/AN-4161_change-the-add-filter-icon-in-query-editor #77

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions bitmovin-analytics-datasource/src/components/FilterInput.tsx

This file was deleted.

205 changes: 49 additions & 156 deletions bitmovin-analytics-datasource/src/components/FilterRow.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
import React, { useEffect, useState } from 'react';
import { differenceWith } from 'lodash';
import { SelectableValue } from '@grafana/data';
import React, { useState } from 'react';
import { Box, HorizontalGroup, IconButton, InlineLabel, VerticalGroup } from '@grafana/ui';

import { QueryFilter, QueryFilterOperator, QueryFilterValue } from '../types/queryFilter';
import { QueryAdAttribute, SELECTABLE_QUERY_AD_ATTRIBUTES } from '../types/queryAdAttributes';
import { QueryAttribute, SELECTABLE_QUERY_ATTRIBUTES } from '../types/queryAttributes';
import { FilterInput } from './FilterInput';
import { convertFilterValueToProperType, mapQueryFilterValueToRawFilterValue } from '../utils/filterUtils';

export type FilterRowData = {
attribute: QueryAdAttribute | QueryAttribute | undefined;
operator: QueryFilterOperator | undefined;
rawFilterValue: string;
convertedFilterValue: QueryFilterValue;
parsingValueError: string;
};

const mapFilterAttributesToSelectableValue = (
filters: FilterRowData[],
isAdAnalytics: boolean
): Array<SelectableValue<QueryAttribute | QueryAdAttribute>> => {
if (isAdAnalytics) {
return differenceWith(
SELECTABLE_QUERY_AD_ATTRIBUTES,
filters,
(selectableValue, selectedValue) => selectableValue.value === selectedValue.attribute
);
} else {
return differenceWith(
SELECTABLE_QUERY_ATTRIBUTES,
filters,
(selectableValue, selectedValue) => selectableValue.value === selectedValue.attribute
);
}
};

const mapFilterRowsToQueryFilters = (filters: FilterRowData[]): QueryFilter[] => {
return filters.map((filter) => {
return {
name: filter.attribute!,
operator: filter.operator!,
value: filter.convertedFilterValue,
};
});
};
import { QueryFilter } from '../types/queryFilter';
import {
ATTRIBUTE_COMPONENT_WIDTH,
OPERATOR_COMPONENT_WIDTH,
QueryFilterInput,
VALUE_COMPONENT_WIDTH,
} from './QueryFilterInput';

type Props = {
readonly isAdAnalytics: boolean;
Expand All @@ -53,135 +16,65 @@ type Props = {
};

export function FilterRow(props: Props) {
const [filterInputs, setFilterInputs] = useState<FilterRowData[]>([]);

/** Map QueryFilters to FilterRowData */
useEffect(() => {
const filterRows = props.filters.map((filter) => {
return {
attribute: filter.name,
operator: filter.operator,
rawFilterValue: mapQueryFilterValueToRawFilterValue(filter.value),
convertedFilterValue: filter.value,
parsingValueError: '',
};
});
setFilterInputs(filterRows);
}, [props.filters]);

const addFilterInput = () => {
const newFilterInputs = [...filterInputs];
newFilterInputs.push({
attribute: undefined,
operator: undefined,
rawFilterValue: '',
convertedFilterValue: '',
parsingValueError: '',
});
setFilterInputs(newFilterInputs);
};

const onSaveFilter = (index: number) => {
const filter = filterInputs[index];
try {
const convertedValue = convertFilterValueToProperType(
filter.rawFilterValue,
filter.attribute!,
filter.attribute!,
filter.operator!,
props.isAdAnalytics
);

const newFilter: FilterRowData = { ...filter, convertedFilterValue: convertedValue, parsingValueError: '' };

const newFilters = [...filterInputs];
newFilters.splice(index, 1, newFilter);

setFilterInputs(filterInputs);
const [hasNewQueryFilter, setHasNewQueryFilter] = useState<boolean>(false);

props.onQueryFilterChange(mapFilterRowsToQueryFilters(newFilters));
} catch (e: unknown) {
if (e instanceof Error) {
const errorMessage = e.message;
const newFilter: FilterRowData = { ...filter, parsingValueError: errorMessage };

const newFilters = [...filterInputs];
newFilters.splice(index, 1, newFilter);

setFilterInputs(newFilters);
}
}
};

const deleteFilterInput = (index: number) => {
const newFilters = [...filterInputs];
newFilters.splice(index, 1);

setFilterInputs(newFilters);

props.onQueryFilterChange(mapFilterRowsToQueryFilters(newFilters));
};

const onAttributesChange = (index: number, newAttribute: SelectableValue<QueryAttribute | QueryAdAttribute>) => {
const filter = filterInputs[index];
const newFilter: FilterRowData = { ...filter, attribute: newAttribute.value! };
const newFilters = [...filterInputs];
newFilters.splice(index, 1, newFilter);

setFilterInputs(newFilters);
};

const onOperatorsChange = (index: number, newOperator: SelectableValue<QueryFilterOperator>) => {
const filter = filterInputs[index];
const newFilter: FilterRowData = { ...filter, operator: newOperator.value! };
const newFilters = [...filterInputs];
newFilters.splice(index, 1, newFilter);

setFilterInputs(newFilters);
};
function handleQueryFilterDelete(queryFilterIndex: number) {
const newQueryFilters = [...props.filters];
newQueryFilters.splice(queryFilterIndex, 1);
props.onQueryFilterChange(newQueryFilters);
}

const onValuesChange = (index: number, newValue: string) => {
const filter = filterInputs[index];
const newFilter: FilterRowData = { ...filter, rawFilterValue: newValue };
const newFilters = [...filterInputs];
newFilters.splice(index, 1, newFilter);
function handleQueryFilterChange(queryFilterIndex: number, changedQueryFilter: QueryFilter) {
const newQueryFilters = [...props.filters];
newQueryFilters.splice(queryFilterIndex, 1, changedQueryFilter);
props.onQueryFilterChange(newQueryFilters);
}

setFilterInputs(newFilters);
};
function handleNewQueryFilterChange(newQueryFilter: QueryFilter) {
const newQueryFilters = [...props.filters, newQueryFilter];
props.onQueryFilterChange(newQueryFilters);
setHasNewQueryFilter(false);
}

return (
<VerticalGroup>
{filterInputs.length > 0 && (
{(props.filters.length > 0 || hasNewQueryFilter) && (
<HorizontalGroup spacing={'none'}>
<InlineLabel width={30} tooltip="">
Dimension
<InlineLabel width={ATTRIBUTE_COMPONENT_WIDTH} tooltip="">
Attribute
</InlineLabel>
<InlineLabel width={15} tooltip="">
<InlineLabel width={OPERATOR_COMPONENT_WIDTH} tooltip="">
Operator
</InlineLabel>
<InlineLabel width={30} tooltip="">
<InlineLabel width={VALUE_COMPONENT_WIDTH} tooltip="">
Value
</InlineLabel>
</HorizontalGroup>
)}
{filterInputs.map((filter, index, filtersArray) => (
<FilterInput
key={index}

{props.filters.map((queryFilter, queryFilterIdx) => (
<QueryFilterInput
isAdAnalytics={props.isAdAnalytics}
filter={filter}
selectableFilterAttributes={mapFilterAttributesToSelectableValue(filtersArray, props.isAdAnalytics)}
onAttributeChange={(newValue: SelectableValue<QueryAdAttribute | QueryAttribute>) =>
onAttributesChange(index, newValue)
}
onOperatorChange={(newValue: SelectableValue<QueryFilterOperator>) => onOperatorsChange(index, newValue)}
onValueChange={(newValue: string) => onValuesChange(index, newValue)}
onDelete={() => deleteFilterInput(index)}
onSaveFilter={() => onSaveFilter(index)}
value={queryFilter}
onChange={(changedQueryFilter) => handleQueryFilterChange(queryFilterIdx, changedQueryFilter)}
onDelete={() => handleQueryFilterDelete(queryFilterIdx)}
selectedQueryFilters={props.filters}
key={queryFilterIdx}
/>
))}

<Box paddingTop={filterInputs.length === 0 ? 0.5 : 0}>
<IconButton name="plus-square" tooltip="Add Filter" onClick={() => addFilterInput()} size="xl" />
<Box paddingTop={props.filters.length === 0 ? 0.5 : 0}>
{hasNewQueryFilter ? (
<QueryFilterInput
isAdAnalytics={props.isAdAnalytics}
value={undefined}
onChange={handleNewQueryFilterChange}
onDelete={() => setHasNewQueryFilter(false)}
selectedQueryFilters={props.filters}
/>
) : (
<IconButton name="plus-square" tooltip="Add Filter" onClick={() => setHasNewQueryFilter(true)} size="xl" />
)}
</Box>
</VerticalGroup>
);
Expand Down
Loading
Loading