Skip to content

Commit

Permalink
Update Columns Being Displayed (#78)
Browse files Browse the repository at this point in the history
* Preset all select options for tabular scripts

Signed-off-by: Kartik Pattaswamy <[email protected]>

* Deleting col display updates groupby and aggregate

Signed-off-by: Kartik Pattaswamy <[email protected]>

* Removed unused component

Signed-off-by: Kartik Pattaswamy <[email protected]>

* Added clarity to check for duplicates in arrays and creating script to display cols

Signed-off-by: Kartik Pattaswamy <[email protected]>
  • Loading branch information
kpattaswamy authored Jul 15, 2022
1 parent 35c55ad commit 10c82d7
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 63 deletions.
90 changes: 90 additions & 0 deletions src/column_display.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2018- The Pixie Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

import defaults from 'lodash/defaults';
import React, { PureComponent } from 'react';
import { MultiSelect, InlineLabel } from '@grafana/ui';
import { QueryEditorProps, SelectableValue } from '@grafana/data';
import { defaultQuery, PixieDataSourceOptions, PixieDataQuery } from './types';
import { DataSource } from './datasource';

type Props = QueryEditorProps<DataSource, PixieDataQuery, PixieDataSourceOptions>;

export function getColumnsScript(
chosenOptions: Array<SelectableValue<number>>,
allColumnOptions: Array<SelectableValue<number>>
): string {
const options = chosenOptions?.length ? chosenOptions : allColumnOptions;

return options.map(({ label }) => `'${label}'`).join(',');
}

export class ColDisplayComponents extends PureComponent<Props> {
onColSelect(chosenOptions: Array<SelectableValue<number>>) {
if (chosenOptions === undefined) {
return;
}
const { onChange, query, onRunQuery } = this.props;

// Update columns to groupby/aggregate based on any updates to the columns being displayed
const colsToDisplay = new Set(chosenOptions.map(({ label }) => label));

const groupByArr = query.queryMeta?.selectedColGroupby?.filter(({ label }) => colsToDisplay.has(label)) ?? [];

const aggData = groupByArr?.length
? query.queryMeta?.aggData?.filter(({ aggColumn }) => colsToDisplay.has(aggColumn)) ?? []
: [];

onChange({
...query,
queryMeta: {
...query.queryMeta,
selectedColDisplay: chosenOptions,
selectedColGroupby: groupByArr,
aggData,
},
});
onRunQuery();
}

render() {
const query = defaults(this.props.query, defaultQuery);

return (
<div style={{ margin: '10px', display: 'flex' }}>
{query.queryMeta?.isColDisplay && (
<>
<InlineLabel transparent={false} width="auto">
Columns Displayed
</InlineLabel>
<MultiSelect
placeholder="Select Columns to Display"
options={query.queryMeta.columnOptions}
onChange={this.onColSelect.bind(this)}
closeMenuOnSelect={false}
width={32}
isClearable={true}
inputId="column-selection"
value={query.queryMeta.selectedColDisplay ?? undefined}
/>
</>
)}
</div>
);
}
}
33 changes: 0 additions & 33 deletions src/column_filtering.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
CLUSTER_VARIABLE_NAME as CLUSTER_VARIABLE_NAME,
QueryType,
} from './types';
import { getColumnsScript } from './column_filtering';
import { getColumnsScript } from './column_display';
import { getGroupByScript } from './groupby';
import { checkExhaustive, getClusterId } from 'utils';

Expand Down
1 change: 1 addition & 0 deletions src/groupby.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class GroupbyComponents extends PureComponent<Props> {
onChange={this.onGroupBySelect.bind(this)}
closeMenuOnSelect={false}
width={34}
isClearable={true}
value={query.queryMeta?.selectedColGroupby ?? undefined}
/>
</div>
Expand Down
40 changes: 11 additions & 29 deletions src/query_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import defaults from 'lodash/defaults';
import React, { PureComponent } from 'react';
import { Select, MultiSelect, Button, InlineLabel } from '@grafana/ui';
import { Select, Button, InlineLabel } from '@grafana/ui';
import { QueryEditorProps, SelectableValue } from '@grafana/data';
import Editor from 'react-simple-code-editor';
import { highlight, languages } from 'prismjs';
Expand All @@ -29,6 +29,7 @@ import { DataSource } from './datasource';
import { scriptOptions, Script } from './pxl_scripts';
import { defaultQuery, PixieDataSourceOptions, PixieDataQuery, QueryType } from './types';
import { GroupbyComponents } from './groupby';
import { ColDisplayComponents } from './column_display';

type Props = QueryEditorProps<DataSource, PixieDataQuery, PixieDataSourceOptions>;

Expand Down Expand Up @@ -66,20 +67,11 @@ export class QueryEditor extends PureComponent<Props> {
isGroupBy: option.value.isGroupBy || false,
columnOptions: option.columnOptions,
groupByColOptions: option.groupByColOptions,
selectedColDisplay: [],
selectedColDisplay: option.columnOptions,
selectedColGroupby: [],
aggData: [],
},
});

onRunQuery();
}
}

onColSelect(chosenOptions: Array<SelectableValue<number>>) {
if (chosenOptions !== undefined) {
const { onChange, query, onRunQuery } = this.props;
onChange({ ...query, queryMeta: { ...query.queryMeta, selectedColDisplay: chosenOptions } });
onRunQuery();
}
}
Expand All @@ -104,24 +96,14 @@ export class QueryEditor extends PureComponent<Props> {
/>
</div>

<div style={{ margin: '10px', display: 'flex' }}>
{query.queryMeta?.isColDisplay && (
<>
<InlineLabel transparent={false} width="auto">
Columns Displayed
</InlineLabel>
<MultiSelect
placeholder="Select Columns to Display"
options={query.queryMeta.columnOptions}
onChange={this.onColSelect.bind(this)}
closeMenuOnSelect={false}
width={32}
inputId="column-selection"
value={query.queryMeta.selectedColDisplay}
/>
</>
)}
</div>
{query.queryMeta?.isColDisplay && (
<ColDisplayComponents
datasource={this.props.datasource}
query={query}
onRunQuery={onRunQuery}
onChange={onChange}
/>
)}

{query.queryMeta?.isGroupBy && (
<GroupbyComponents
Expand Down

0 comments on commit 10c82d7

Please sign in to comment.