-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8335353
commit 506fe13
Showing
3 changed files
with
145 additions
and
0 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
46 changes: 46 additions & 0 deletions
46
src/components/QueryEditor/VisualQueryEditor/Timeshift.test.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,46 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { QueryEditorExpressionType } from 'components/LegacyQueryEditor/editor/expressions'; | ||
import { mockDatasource, mockQuery } from 'components/__fixtures__/Datasource'; | ||
import React from 'react'; | ||
import { openMenu } from 'react-select-event'; | ||
import { QueryEditorPropertyType } from 'schema/types'; | ||
import Timeshift from './Timeshift'; | ||
|
||
const defaultProps = { | ||
datasource: mockDatasource(), | ||
query: mockQuery, | ||
templateVariableOptions: {}, | ||
tableSchema: { | ||
loading: false, | ||
value: [ | ||
{ | ||
Name: 'foo', | ||
CslType: 'string', | ||
}, | ||
], | ||
}, | ||
database: 'db', | ||
onChange: jest.fn(), | ||
onRunQuery: jest.fn(), | ||
}; | ||
|
||
describe('TimeshiftItem', () => { | ||
it('should select a time shift', () => { | ||
const onChange = jest.fn(); | ||
render(<Timeshift {...defaultProps} onChange={onChange} />); | ||
screen.getByLabelText('Add').click(); | ||
const sel = screen.getByLabelText('timeshift'); | ||
openMenu(sel); | ||
screen.getByText('Hour before').click(); | ||
expect(onChange).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
expression: expect.objectContaining({ | ||
timeshift: { | ||
property: { name: '1h', type: QueryEditorPropertyType.TimeSpan }, | ||
type: QueryEditorExpressionType.Property, | ||
}, | ||
}), | ||
}) | ||
); | ||
}); | ||
}); |
97 changes: 97 additions & 0 deletions
97
src/components/QueryEditor/VisualQueryEditor/Timeshift.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,97 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { Button, Select } from '@grafana/ui'; | ||
import { AccessoryButton, EditorField, EditorFieldGroup, EditorRow, InputGroup } from '@grafana/experimental'; | ||
|
||
import { AdxColumnSchema, KustoQuery } from '../../../types'; | ||
import { QueryEditorExpressionType } from 'components/LegacyQueryEditor/editor/expressions'; | ||
import { QueryEditorPropertyType } from 'schema/types'; | ||
import { AdxDataSource } from 'datasource'; | ||
import { AsyncState } from 'react-use/lib/useAsyncFn'; | ||
|
||
interface TimeshiftProps { | ||
datasource: AdxDataSource; | ||
query: KustoQuery; | ||
tableSchema: AsyncState<AdxColumnSchema[]>; | ||
onChange: (query: KustoQuery) => void; | ||
} | ||
|
||
const Timeshift: React.FC<TimeshiftProps> = (props) => { | ||
const { datasource, query, onChange, tableSchema } = props; | ||
const [displaySelect, setDisplaySelect] = useState(false); | ||
const onChangeValue = (value?: string) => { | ||
const newExpression = { | ||
...query.expression, | ||
timeshift: { | ||
property: { | ||
name: value || '', | ||
type: QueryEditorPropertyType.TimeSpan, | ||
}, | ||
type: QueryEditorExpressionType.Property, | ||
}, | ||
}; | ||
onChange({ | ||
...query, | ||
expression: newExpression, | ||
query: datasource.parseExpression(newExpression, tableSchema.value), | ||
}); | ||
}; | ||
|
||
return ( | ||
<EditorRow> | ||
<EditorFieldGroup> | ||
<EditorField label="Timeshift" optional={true}> | ||
<InputGroup> | ||
<Button | ||
onClick={() => setDisplaySelect(true)} | ||
variant="secondary" | ||
size="md" | ||
icon="plus" | ||
aria-label="Add" | ||
type="button" | ||
hidden={displaySelect} | ||
/> | ||
<div hidden={!displaySelect}> | ||
<Select | ||
width={'auto'} | ||
aria-label="timeshift" | ||
allowCustomValue | ||
options={[ | ||
{ | ||
label: 'No timeshift', | ||
value: '', | ||
}, | ||
{ | ||
label: 'Hour before', | ||
value: '1h', | ||
}, | ||
{ | ||
label: 'Day before', | ||
value: '1d', | ||
}, | ||
{ | ||
label: 'Week before', | ||
value: '7d', | ||
}, | ||
]} | ||
value={query.expression.timeshift?.property?.name || ''} | ||
onChange={(e) => onChangeValue(e.value)} | ||
/> | ||
<AccessoryButton | ||
aria-label="remove" | ||
icon="times" | ||
variant="secondary" | ||
onClick={() => { | ||
onChangeValue(); | ||
setDisplaySelect(false); | ||
}} | ||
/> | ||
</div> | ||
</InputGroup> | ||
</EditorField> | ||
</EditorFieldGroup> | ||
</EditorRow> | ||
); | ||
}; | ||
|
||
export default Timeshift; |