Skip to content

Commit ae31ce3

Browse files
authored
Update RedisGears Script Editor Execution modes (#57)
* Update RedisGears Execution Modes * Fix tests
1 parent 12db1bb commit ae31ce3

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
lines changed

src/redis-gears-panel/components/redis-gears-panel/redis-gears-panel.test.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import React from 'react';
21
import { shallow } from 'enzyme';
2+
import React from 'react';
33
import { Observable } from 'rxjs';
44
import { FieldType, LoadingState, toDataFrame } from '@grafana/data';
5-
import { Alert, Button, Input, Switch, Table } from '@grafana/ui';
5+
import { Alert, Button, Input, RadioButtonGroup, Table } from '@grafana/ui';
6+
import { ExecutionMode } from '../../constants';
67
import { CodeEditor } from '../code-editor';
78
import { RedisGearsPanel } from './redis-gears-panel';
89

@@ -66,11 +67,18 @@ describe('RedisGearsPanel', () => {
6667

6768
it('Should update unblocking', () => {
6869
const wrapper = shallow<RedisGearsPanel>(getComponent());
69-
const component = wrapper.find(Switch);
70-
component.simulate('change', { target: { checked: true } });
70+
const component = wrapper.find(RadioButtonGroup);
71+
component.simulate('change', ExecutionMode.Unblocking);
7172
expect(wrapper.state().unblocking).toEqual(true);
7273
});
7374

75+
it('Should not update unblocking', () => {
76+
const wrapper = shallow<RedisGearsPanel>(getComponent());
77+
const component = wrapper.find(RadioButtonGroup);
78+
component.simulate('change', ExecutionMode.Blocking);
79+
expect(wrapper.state().unblocking).toEqual(false);
80+
});
81+
7482
/**
7583
* Run script button
7684
*/

src/redis-gears-panel/components/redis-gears-panel/redis-gears-panel.tsx

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { css } from 'emotion';
22
import React, { ChangeEvent, createRef, PureComponent, RefObject } from 'react';
3-
import { DefaultScript } from 'redis-gears-panel/constants';
43
import { Observable } from 'rxjs';
54
import {
65
DataFrame,
@@ -15,7 +14,8 @@ import {
1514
toDataFrame,
1615
} from '@grafana/data';
1716
import { getDataSourceSrv, toDataQueryError } from '@grafana/runtime';
18-
import { Alert, Button, InlineField, InlineFormLabel, Input, Switch, Table } from '@grafana/ui';
17+
import { Alert, Button, InlineField, InlineFormLabel, Input, RadioButtonGroup, Table } from '@grafana/ui';
18+
import { DefaultScript, ExecutionMode, ExecutionOptions } from '../../constants';
1919
import { PanelOptions } from '../../types';
2020
import { CodeEditor } from '../code-editor';
2121

@@ -141,7 +141,7 @@ export class RedisGearsPanel extends PureComponent<Props, State> {
141141
/**
142142
* Error
143143
*/
144-
if (!response || response.state === LoadingState.Error) {
144+
if (!response || response.state === LoadingState.Error || !response.data.length) {
145145
this.setState({
146146
result: undefined,
147147
isRunning: false,
@@ -166,7 +166,6 @@ export class RedisGearsPanel extends PureComponent<Props, State> {
166166
}
167167

168168
let resultDataFrame: DataFrame = response.data[0];
169-
170169
if (resultDataFrame.length === 0) {
171170
resultDataFrame = toDataFrame({
172171
fields: [
@@ -236,9 +235,9 @@ export class RedisGearsPanel extends PureComponent<Props, State> {
236235
*
237236
* @param event {HTMLInputElement}
238237
*/
239-
onChangeUnblocking = (event: ChangeEvent<HTMLInputElement>) => {
238+
onChangeUnblocking = (event?: ExecutionMode) => {
240239
this.setState({
241-
unblocking: event.target.checked,
240+
unblocking: event ? true : false,
242241
});
243242
};
244243

@@ -269,15 +268,6 @@ export class RedisGearsPanel extends PureComponent<Props, State> {
269268
const { height, width } = this.props;
270269
const { script, result, unblocking, requirements, isRunning, footerHeight, error } = this.state;
271270

272-
let resultComponent = null;
273-
274-
/**
275-
* Show result table If there is a result
276-
*/
277-
if (result) {
278-
resultComponent = <Table noHeader={true} data={result} width={width} height={100} />;
279-
}
280-
281271
return (
282272
<div
283273
className={css`
@@ -303,23 +293,31 @@ export class RedisGearsPanel extends PureComponent<Props, State> {
303293
<div ref={this.footerRef}>
304294
{error && error.message && <Alert title={error.message} onRemove={this.onClearError} />}
305295

306-
<div className="gf-form-inline">
296+
<div className="gf-form">
307297
<InlineField label={<InlineFormLabel width={6}>Requirements</InlineFormLabel>}>
308298
<Input css="" value={requirements} onChange={this.onChangeRequirements} width={40} />
309299
</InlineField>
310300

311-
<InlineField label={<InlineFormLabel width={6}>Unblocking</InlineFormLabel>}>
312-
<Switch css="" value={unblocking} onChange={this.onChangeUnblocking} />
313-
</InlineField>
314-
315-
<div className="gf-form">
316-
<Button onClick={this.onRunScript} disabled={isRunning}>
317-
{isRunning ? 'Running...' : 'Run script'}
318-
</Button>
319-
</div>
320-
321-
{resultComponent}
301+
<RadioButtonGroup
302+
className={css`
303+
margin-right: 4px;
304+
`}
305+
value={unblocking ? ExecutionMode.Unblocking : ExecutionMode.Blocking}
306+
options={ExecutionOptions}
307+
onChange={this.onChangeUnblocking}
308+
/>
309+
310+
<Button onClick={this.onRunScript} disabled={isRunning}>
311+
{isRunning ? 'Running...' : 'Run script'}
312+
</Button>
322313
</div>
314+
315+
{result && (
316+
<>
317+
<hr />
318+
<Table noHeader={true} data={result} width={width} height={100} />
319+
</>
320+
)}
323321
</div>
324322
</div>
325323
);

src/redis-gears-panel/constants.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,25 @@
22
* Default script
33
*/
44
export const DefaultScript = 'gb = GearsBuilder()';
5+
6+
/**
7+
* Execution Mode
8+
*/
9+
export enum ExecutionMode {
10+
Blocking = 0,
11+
Unblocking = 1,
12+
}
13+
14+
/**
15+
* Unblocking options
16+
*/
17+
export const ExecutionOptions = [
18+
{
19+
label: 'Blocking',
20+
value: ExecutionMode.Blocking,
21+
},
22+
{
23+
label: 'Unblocking',
24+
value: ExecutionMode.Unblocking,
25+
},
26+
];

0 commit comments

Comments
 (0)