-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/AN-4112 implement order by selection (#69)
* implements license fetching and interval selection * add tests for intervalUtils * fix ceiling of timestamp for DAY interval * add tests * fix linting * delete unnecessary export of enum * implement metric selection * implement dimension selection * fix linting errors * add metric support * add metric to query and check for null instead of undefined * implements groupBy selection * fix linting * implements reordering of GroupBys and fixes position of Add Button * make props readonly * implements order By selection * splits orderBy state into two seperate states to fix rendering and renames variables * lint
- Loading branch information
Showing
7 changed files
with
211 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
bitmovin-analytics-datasource/src/components/OrderByInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import { SelectableValue } from '@grafana/data'; | ||
import { HorizontalGroup, IconButton, RadioButtonGroup, Select } from '@grafana/ui'; | ||
|
||
import { QueryAttribute } from '../types/queryAttributes'; | ||
import { QueryAdAttribute } from '../types/queryAdAttributes'; | ||
import { QuerySortOrder } from '../types/queryOrderBy'; | ||
import { REORDER_DIRECTION } from './GroupByInput'; | ||
|
||
type Props = { | ||
readonly isAdAnalytics: boolean; | ||
readonly attribute: SelectableValue<QueryAttribute | QueryAdAttribute>; | ||
readonly selectableOrderByAttributes: Array<SelectableValue<QueryAttribute | QueryAdAttribute>>; | ||
readonly onAttributeChange: (newValue: SelectableValue<QueryAdAttribute | QueryAttribute>) => void; | ||
readonly sortOrder: QuerySortOrder; | ||
readonly onSortOrderChange: (newValue: QuerySortOrder) => void; | ||
readonly onDelete: () => void; | ||
readonly isFirst: boolean; | ||
readonly isLast: boolean; | ||
readonly onReorderOrderBy: (direction: REORDER_DIRECTION) => void; | ||
}; | ||
|
||
const sortOrderOption: Array<SelectableValue<QuerySortOrder>> = [ | ||
{ value: 'ASC', description: 'Sort by ascending', icon: 'sort-amount-up' }, | ||
{ value: 'DESC', description: 'Sort by descending', icon: 'sort-amount-down' }, | ||
]; | ||
|
||
export function OrderByInput(props: Props) { | ||
return ( | ||
<HorizontalGroup spacing="xs"> | ||
<Select | ||
value={props.attribute} | ||
onChange={(value) => props.onAttributeChange(value)} | ||
options={props.selectableOrderByAttributes} | ||
width={30} | ||
/> | ||
<RadioButtonGroup | ||
options={sortOrderOption} | ||
value={props.sortOrder} | ||
onChange={(value) => props.onSortOrderChange(value)} | ||
/> | ||
<IconButton | ||
tooltip="Move down" | ||
onClick={() => props.onReorderOrderBy(REORDER_DIRECTION.DOWN)} | ||
name="arrow-down" | ||
disabled={props.isLast} | ||
/> | ||
<IconButton | ||
tooltip="Move up" | ||
onClick={() => props.onReorderOrderBy(REORDER_DIRECTION.UP)} | ||
name="arrow-up" | ||
disabled={props.isFirst} | ||
/> | ||
<IconButton | ||
tooltip="Delete Group By" | ||
name="trash-alt" | ||
onClick={() => props.onDelete()} | ||
size="lg" | ||
variant="destructive" | ||
/> | ||
</HorizontalGroup> | ||
); | ||
} |
121 changes: 121 additions & 0 deletions
121
bitmovin-analytics-datasource/src/components/OrderByRow.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import React, { useState } from 'react'; | ||
import { Box, IconButton, VerticalGroup } from '@grafana/ui'; | ||
import { SelectableValue } from '@grafana/data'; | ||
import { difference } from 'lodash'; | ||
|
||
import { QueryAdAttribute, SELECTABLE_QUERY_AD_ATTRIBUTES } from '../types/queryAdAttributes'; | ||
import { QueryAttribute, SELECTABLE_QUERY_ATTRIBUTES } from '../types/queryAttributes'; | ||
import { QueryOrderBy, QuerySortOrder } from '../types/queryOrderBy'; | ||
import { OrderByInput } from './OrderByInput'; | ||
import { REORDER_DIRECTION } from './GroupByInput'; | ||
|
||
type Props = { | ||
readonly isAdAnalytics: boolean; | ||
readonly onChange: (newOrderBy: QueryOrderBy[]) => void; | ||
}; | ||
|
||
export function OrderByRow(props: Props) { | ||
const [selectedAttributes, setSelectedAttributes] = useState< | ||
Array<SelectableValue<QueryAdAttribute | QueryAttribute>> | ||
>([]); | ||
const [selectedSortOrders, setSelectedSortOrders] = useState<QuerySortOrder[]>([]); | ||
|
||
const mapOrderBysToSelectableValue = (): Array<SelectableValue<QueryAttribute | QueryAdAttribute>> => { | ||
if (props.isAdAnalytics) { | ||
return difference(SELECTABLE_QUERY_AD_ATTRIBUTES, selectedAttributes); | ||
} else { | ||
return difference(SELECTABLE_QUERY_ATTRIBUTES, selectedAttributes); | ||
} | ||
}; | ||
|
||
const mapSelectedValuesToQueryOrderBy = ( | ||
selectedAttributes: Array<SelectableValue<QueryAttribute | QueryAdAttribute>>, | ||
selectedSortOrders: QuerySortOrder[] | ||
): QueryOrderBy[] => { | ||
const queryOrderBys: QueryOrderBy[] = []; | ||
for (let i = 0; i < selectedAttributes.length; i++) { | ||
queryOrderBys.push({ | ||
name: selectedAttributes[i].value!, | ||
order: selectedSortOrders[i], | ||
}); | ||
} | ||
return queryOrderBys; | ||
}; | ||
|
||
const deleteOrderByInput = (index: number) => { | ||
const newSelectedAttributes = [...selectedAttributes]; | ||
newSelectedAttributes.splice(index, 1); | ||
|
||
const newSelectedSortOrders = [...selectedSortOrders]; | ||
newSelectedSortOrders.splice(index, 1); | ||
|
||
setSelectedAttributes(newSelectedAttributes); | ||
setSelectedSortOrders(newSelectedSortOrders); | ||
props.onChange(mapSelectedValuesToQueryOrderBy(newSelectedAttributes, newSelectedSortOrders)); | ||
}; | ||
|
||
const onAttributesChange = (index: number, newAttribute: SelectableValue<QueryAttribute | QueryAdAttribute>) => { | ||
const newSelectedAttributes = [...selectedAttributes]; | ||
newSelectedAttributes.splice(index, 1, newAttribute); | ||
setSelectedAttributes(newSelectedAttributes); | ||
|
||
props.onChange(mapSelectedValuesToQueryOrderBy(newSelectedAttributes, selectedSortOrders)); | ||
}; | ||
|
||
const onSortOrdersChange = (index: number, newSortOrder: QuerySortOrder) => { | ||
const newSelectedSortOrders = [...selectedSortOrders]; | ||
newSelectedSortOrders.splice(index, 1, newSortOrder); | ||
setSelectedSortOrders(newSelectedSortOrders); | ||
|
||
props.onChange(mapSelectedValuesToQueryOrderBy(selectedAttributes, newSelectedSortOrders)); | ||
}; | ||
|
||
const reorderOrderBy = (direction: REORDER_DIRECTION, index: number) => { | ||
const newIndex = direction === REORDER_DIRECTION.UP ? index - 1 : index + 1; | ||
|
||
const newSelectedAttributes = [...selectedAttributes]; | ||
const attributeToMove = newSelectedAttributes[index]; | ||
newSelectedAttributes.splice(index, 1); | ||
newSelectedAttributes.splice(newIndex, 0, attributeToMove); | ||
|
||
const newSelectedSortOrders = [...selectedSortOrders]; | ||
const sortOrderToMove = newSelectedSortOrders[index]; | ||
newSelectedSortOrders.splice(index, 1); | ||
newSelectedSortOrders.splice(newIndex, 0, sortOrderToMove); | ||
|
||
setSelectedAttributes(newSelectedAttributes); | ||
setSelectedSortOrders(newSelectedSortOrders); | ||
|
||
props.onChange(mapSelectedValuesToQueryOrderBy(newSelectedAttributes, newSelectedSortOrders)); | ||
}; | ||
const addOrderByInput = () => { | ||
setSelectedAttributes((prevState) => [...prevState, {}]); | ||
setSelectedSortOrders((prevState) => [...prevState, 'ASC']); | ||
}; | ||
|
||
return ( | ||
<VerticalGroup> | ||
{selectedAttributes.map((attribute, index, array) => ( | ||
<OrderByInput | ||
key={index} | ||
isAdAnalytics={props.isAdAnalytics} | ||
selectableOrderByAttributes={mapOrderBysToSelectableValue()} | ||
attribute={attribute} | ||
onAttributeChange={(newValue: SelectableValue<QueryAdAttribute | QueryAttribute>) => | ||
onAttributesChange(index, newValue) | ||
} | ||
sortOrder={selectedSortOrders[index]} | ||
onSortOrderChange={(newValue: QuerySortOrder) => onSortOrdersChange(index, newValue)} | ||
onDelete={() => deleteOrderByInput(index)} | ||
isFirst={index === 0} | ||
isLast={index === array.length - 1} | ||
onReorderOrderBy={(direction: REORDER_DIRECTION) => reorderOrderBy(direction, index)} | ||
/> | ||
))} | ||
|
||
<Box paddingTop={selectedAttributes.length === 0 ? 0.5 : 0}> | ||
<IconButton name="plus-square" tooltip="Add Group By" onClick={() => addOrderByInput()} size="xl" /> | ||
</Box> | ||
</VerticalGroup> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { QueryAttribute } from './queryAttributes'; | ||
import { QueryAdAttribute } from './queryAdAttributes'; | ||
|
||
export type QuerySortOrder = 'ASC' | 'DESC'; | ||
|
||
export type QueryOrderBy = { | ||
name: QueryAttribute | QueryAdAttribute; | ||
order: QuerySortOrder; | ||
}; |