Skip to content

Commit 8c695cb

Browse files
committed
Allow to set auto mode per scale
1 parent b6b06cb commit 8c695cb

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

src/TimeSeriesPanel.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ export const TimeSeriesPanel = ({
113113

114114
const onUpsertTimescale = useCallback(
115115
async (formData: TimescaleItem) => {
116-
const { min, max, description, scale } = formData;
116+
const { min, max, description, scale, auto } = formData;
117117
const user = config.bootData.user;
118118
const userId = user?.id;
119119
const sanitizedDescription = description.replace(/\"|\'/g, '');
120-
const rawSql = `insert into scales values ('${well}', ${userId}, '${scale}', ${min}, ${max}, '${sanitizedDescription}') on conflict (well, user_id, metric) do update set min = excluded.min, max = excluded.max;`;
120+
const rawSql = `insert into scales values ('${well}', ${userId}, '${scale}', ${auto ? null : min}, ${
121+
auto ? null : max
122+
}, '${sanitizedDescription}') on conflict (well, user_id, metric) do update set min = excluded.min, max = excluded.max;`;
121123
const target = data.request?.targets[0];
122124
const datasourceId = target?.datasource?.uid;
123125
const refId = target?.refId;

src/plugins/timescales/TimescaleEditorForm.tsx

+29-8
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,28 @@ export interface TimescaleItem {
2929
*
3030
* @type {number}
3131
*/
32-
min: number;
32+
min: number | null;
3333

3434
/**
3535
* Max
3636
*
3737
* @type {number}
3838
*/
39-
max: number;
39+
max: number | null;
4040

4141
/**
4242
* Description
4343
*
4444
* @type {string}
4545
*/
4646
description: string;
47+
48+
/**
49+
* Auto
50+
*
51+
* @type {boolean}
52+
*/
53+
auto: boolean;
4754
}
4855

4956
interface TimescaleEditorFormProps extends HTMLAttributes<HTMLDivElement> {
@@ -134,12 +141,19 @@ export const TimescaleEditorForm = React.forwardRef<HTMLDivElement, TimescaleEdi
134141
* Set Table Data
135142
*/
136143
setEditableTableData(
137-
scales.map((scale) => ({
138-
scale,
139-
min: scaleValuesMap.get(scale)?.min ?? 0,
140-
max: scaleValuesMap.get(scale)?.max ?? 0,
141-
description: '',
142-
}))
144+
scales.map((scale) => {
145+
const min = scaleValuesMap.get(scale)?.min;
146+
const max = scaleValuesMap.get(scale)?.max;
147+
const auto = min === null || max === null;
148+
149+
return {
150+
scale,
151+
min: min ?? 0,
152+
max: max ?? 0,
153+
description: '',
154+
auto,
155+
};
156+
})
143157
);
144158
}, [timescalesFrame, scales]);
145159

@@ -155,6 +169,12 @@ export const TimescaleEditorForm = React.forwardRef<HTMLDivElement, TimescaleEdi
155169
cell: ({ getValue }) => getValue(),
156170
enableResizing: false,
157171
},
172+
{
173+
id: 'auto',
174+
accessorKey: 'auto',
175+
header: () => 'Auto',
176+
enableResizing: false,
177+
},
158178
{
159179
id: 'min',
160180
accessorKey: 'min',
@@ -223,6 +243,7 @@ export const TimescaleEditorForm = React.forwardRef<HTMLDivElement, TimescaleEdi
223243
setEditableTableData((prev) =>
224244
prev.map((scale) => ({
225245
...scale,
246+
auto: false,
226247
min: 0,
227248
max: 0,
228249
}))

src/plugins/timescales/components/EditableTable/EditableTable.styles.ts

+3
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@ export const Styles = (theme: GrafanaTheme2) => {
4242
border-bottom: 0;
4343
}
4444
`,
45+
cellCenter: css`
46+
text-align: center;
47+
`,
4548
};
4649
};

src/plugins/timescales/components/EditableTable/EditableTable.tsx

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useState } from 'react';
2-
import { Input, useStyles2 } from '@grafana/ui';
2+
import { Checkbox, Input, useStyles2 } from '@grafana/ui';
33
import { ColumnDef, flexRender, getCoreRowModel, RowData, useReactTable } from '@tanstack/react-table';
44
import { cx } from '@emotion/css';
55
import { Styles } from './EditableTable.styles';
@@ -34,7 +34,7 @@ declare module '@tanstack/react-table' {
3434
* Default Column
3535
*/
3636
const defaultColumn: Partial<ColumnDef<any>> = {
37-
cell: ({ getValue, row: { index }, column: { id }, table }) => {
37+
cell: ({ getValue, row: { index, original }, column: { id }, table }) => {
3838
const initialValue = getValue();
3939
/**
4040
* We need to keep and update the state of the cell normally
@@ -45,7 +45,7 @@ const defaultColumn: Partial<ColumnDef<any>> = {
4545
/**
4646
* When the input is blurred, we'll call our table meta's updateData function
4747
*/
48-
const onBlur = () => {
48+
const onSaveValue = (value: unknown) => {
4949
table.options.meta?.updateData(index, id, value);
5050
};
5151

@@ -57,12 +57,26 @@ const defaultColumn: Partial<ColumnDef<any>> = {
5757
setValue(initialValue);
5858
}, [initialValue]);
5959

60+
if (id === 'auto') {
61+
return (
62+
<Checkbox
63+
value={value as boolean}
64+
onChange={(e) => {
65+
setValue(e.currentTarget.checked);
66+
onSaveValue(e.currentTarget.checked);
67+
}}
68+
/>
69+
);
70+
}
71+
6072
return (
6173
<Input
62-
value={value as string}
74+
value={original.auto ? '' : (value as string)}
75+
placeholder={original ? 'Auto' : ''}
6376
onChange={(e) => setValue(e.currentTarget.value)}
64-
onBlur={onBlur}
77+
onBlur={() => onSaveValue(value)}
6578
type={typeof initialValue === 'number' ? 'number' : 'text'}
79+
disabled={original.auto}
6680
/>
6781
);
6882
},
@@ -121,7 +135,16 @@ export const EditableTable = <TData,>({ data, columns, onUpdateData }: Props<TDa
121135
return (
122136
<tr key={row.id} className={styles.row}>
123137
{row.getVisibleCells().map((cell) => {
124-
return <td key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</td>;
138+
return (
139+
<td
140+
key={cell.id}
141+
className={cx({
142+
[styles.cellCenter]: cell.column.id === 'auto',
143+
})}
144+
>
145+
{flexRender(cell.column.columnDef.cell, cell.getContext())}
146+
</td>
147+
);
125148
})}
126149
</tr>
127150
);

0 commit comments

Comments
 (0)