Skip to content

Commit

Permalink
timeseries - hide fields, group selected, time grp (#72)
Browse files Browse the repository at this point in the history
* timeseries - hide fields, group selected, time grp
  • Loading branch information
scottlepp authored Feb 10, 2022
1 parent 59dcbaa commit c6c22bf
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.11.0

Feature - Time series: Hide fields, use group by in select, use time field in group by

## 0.10.0

Feature - Ad-Hoc sourced by database or table

## 0.9.13

Fix - update sdk to show streaming errors
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clickhouse-datasource",
"version": "0.10.0",
"version": "0.11.0",
"description": "Clickhouse Datasource",
"scripts": {
"build": "grafana-toolkit plugin:build",
Expand Down
2 changes: 1 addition & 1 deletion src/components/queryBuilder/DatabaseSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const DatabaseSelect = (props: Props) => {
onChange={(e) => onChange(e.value!)}
options={list}
value={props.value}
menuPlacement={'auto'}
menuPlacement={'bottom'}
></Select>
</>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/queryBuilder/Fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const FieldsEditor = (props: FieldsEditorProps) => {
onChange={onChange}
onBlur={onUpdateField}
allowCustomValue={true}
menuPlacement={'bottom'}
/>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/components/queryBuilder/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,15 @@ export const FilterEditor = (props: {
onOpenMenu={() => setIsOpen(true)}
onCloseMenu={() => setIsOpen(false)}
onChange={(e) => onFilterNameChange(e.value!)}
menuPlacement={'bottom'}
/>
<Select
value={filter.operator}
width={34}
className={styles.Common.inlineSelect}
options={getFilterOperatorsByType(filter.type)}
onChange={(e) => onFilterOperatorChange(e.value!)}
menuPlacement={'bottom'}
/>
<FilterValueEditor filter={filter} onFilterChange={onFilterValueChange} fieldsList={fieldsList} />
</>
Expand Down
1 change: 1 addition & 0 deletions src/components/queryBuilder/GroupBy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const GroupByEditor = (props: GroupByEditorProps) => {
onChange={onChange}
onBlur={() => props.onGroupByChange(groupBy)}
value={groupBy}
menuPlacement={'bottom'}
/>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/components/queryBuilder/Metrics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const MetricEditor = (props: {
options={aggregationTypes}
onChange={(e) => onMetricAggregationChange(e.value!)}
value={metric.aggregation}
menuPlacement={'bottom'}
/>
<Select<string>
width={28}
Expand All @@ -62,6 +63,7 @@ const MetricEditor = (props: {
onCloseMenu={() => setIsOpen(false)}
onChange={onMetricFieldChange}
value={metric.field}
menuPlacement={'bottom'}
/>
<InlineFormLabel width={2} className="query-keyword">
{ALIAS.label}
Expand Down
2 changes: 2 additions & 0 deletions src/components/queryBuilder/OrderBy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ const OrderByItem = (props: {
width={20}
options={columns}
onChange={(e) => onOrderBySortFieldUpdate(e.value!)}
menuPlacement={'bottom'}
></Select>
<Select<OrderByDirection>
value={orderByItem.dir}
className={styles.Common.inlineSelect}
width={12}
options={sortOptions}
onChange={(e) => onOrderBySortDirectionUpdate(e.value!)}
menuPlacement={'bottom'}
/>
</>
);
Expand Down
5 changes: 4 additions & 1 deletion src/components/queryBuilder/QueryBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ export const QueryBuilder = (props: QueryBuilderProps) => {
fieldsList={fieldsList}
/>
)}
<FieldsEditor fields={builder.fields || []} onFieldsChange={onFieldsChange} fieldsList={fieldsList} />
{builder.mode !== BuilderMode.Trend && (
<FieldsEditor fields={builder.fields || []} onFieldsChange={onFieldsChange} fieldsList={fieldsList} />
)}

{(builder.mode === BuilderMode.Aggregate || builder.mode === BuilderMode.Trend) && (
<MetricsEditor metrics={builder.metrics || []} onMetricsChange={onMetricsChange} fieldsList={fieldsList} />
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/queryBuilder/TableSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const TableSelect = (props: Props) => {
onChange={(e) => onChange(e.value!)}
options={list}
value={table}
menuPlacement={'auto'}
menuPlacement={'bottom'}
></Select>
</>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/queryBuilder/TimeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const TimeFieldEditor = (props: TimeFieldEditorProps) => {
width={20}
onChange={(e) => props.onTimeFieldChange(e.value, getColumnType(e.value))}
value={props.timeField}
menuPlacement={'bottom'}
/>
</div>
);
Expand Down
27 changes: 17 additions & 10 deletions src/components/queryBuilder/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ const getAggregationQuery = (
const getTrendByQuery = (
database = '',
table = '',
fields: string[] = [],
metrics: BuilderMetricField[] = [],
groupBy: string[] = [],
timeField = '',
timeFieldType = ''
): string => {
Expand All @@ -101,12 +101,12 @@ const getTrendByQuery = (
})
.join(', ');
if (metricsQuery !== '') {
const selected = fields.length > 0 ? `${fields.join(', ')},` : '';
metricsQuery = `${timeField}, ${selected}${metricsQuery}`;
const group = groupBy.length > 0 ? `${groupBy.join(', ')},` : '';
metricsQuery = `${timeField}, ${group} ${metricsQuery}`;
} else if (groupBy.length > 0) {
metricsQuery = `${timeField}, ${groupBy.join(', ')}`;
} else {
const selected = fields.length > 0 ? `${fields.join(', ')}` : '';
const sep = selected !== '' ? ',' : '';
metricsQuery = `${timeField}${sep}${selected}`;
metricsQuery = `${timeField}`;
}

const sep = database === '' || table === '' ? '' : '.';
Expand Down Expand Up @@ -183,8 +183,15 @@ const getFilters = (filters: Filter[]): string => {
}, '');
};

const getGroupBy = (groupBy: string[] = []): string => {
return groupBy.length > 0 ? ` GROUP BY ` + groupBy.map((g) => g).join(', ') : '';
const getGroupBy = (groupBy: string[] = [], timeField?: string): string => {
const clause = groupBy.length > 0 ? ` GROUP BY ${groupBy.join(', ')}` : '';
if (timeField === undefined) {
return clause;
}
if (groupBy.length === 0) {
return ` GROUP BY ${timeField}`;
}
return `${clause}, ${timeField}`;
};

const getOrderBy = (orderBy?: OrderBy[]): string => {
Expand Down Expand Up @@ -219,8 +226,8 @@ export const getSQLFromQueryOptions = (options: SqlBuilderOptions): string => {
query += getTrendByQuery(
options.database,
options.table,
options.fields,
options.metrics,
options.groupBy,
options.timeField,
options.timeFieldType
);
Expand All @@ -231,7 +238,7 @@ export const getSQLFromQueryOptions = (options: SqlBuilderOptions): string => {
}
const trendFilters = getFilters(options.filters || []);
query += trendFilters ? ` AND ${trendFilters}` : '';
query += getGroupBy(options.groupBy);
query += getGroupBy(options.groupBy, options.timeField);
break;
case BuilderMode.List:
default:
Expand Down
2 changes: 1 addition & 1 deletion src/views/CHQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export const CHQueryEditor = (props: CHQueryEditorProps) => {
if (format !== query.format) {
onChange({ ...query, format });
}
onRunQuery();
}
onRunQuery();
};

return (
Expand Down

0 comments on commit c6c22bf

Please sign in to comment.