Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Columns Being Displayed #78

Merged
merged 4 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/column_display.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit for a future diff: Array<SelectableValue<number>> shows up so often in this plugin now that maybe we should alias it in types.ts, like export type SelectableColumns = Array<SelectableValue<number>> or some other useful name.

allColumnOptions: Array<SelectableValue<number>>
): string {
// Setting script default to all the columns in the script
let script = allColumnOptions.map(({ label }) => `'${label}'`).join();

// Updating the script if the user selected columns to filter
if (chosenOptions?.length > 0) {
script = chosenOptions.map(({ label }) => `'${label}'`).join();
}

return script;
}

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

// Finds which column was removed between the old and new cols selected
const oldCols = query.queryMeta?.selectedColDisplay?.map(({ label }) => label)!;
const chosenCols = chosenOptions.map(({ label }) => label);
const colToRemove = oldCols.filter((c) => !chosenCols.includes(c))[0];

// Update Groupby Array with correct columns to display
const groupByArr = query.queryMeta?.selectedColGroupby?.filter(({ label }) => {
return label !== colToRemove;
});

// Update Aggregate Array with correct columns to display
const aggArr = query.queryMeta?.aggData?.filter(({ aggColumn }) => {
return aggColumn !== colToRemove;
});
const aggData = groupByArr?.length ? aggArr ?? [] : [];

onChange({
...query,
queryMeta: {
...query.queryMeta,
selectedColDisplay: chosenOptions,
selectedColGroupby: groupByArr,
aggData: 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