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

Transformations: De-emphasize non-applicable transformations #76373

Merged
merged 20 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ export const formatTimeTransformer: DataTransformerInfo<FormatTimeTransformerOpt
name: 'Format Time',
description: 'Set the output format of a time field',
defaultOptions: { timeField: '', outputFormat: '', useTimezone: true },
applicator: (data) => {
// Search for a time field
// if there is one then we can use this transformation
for (const frame of data) {
for (const field of frame.fields) {
if (field.type === 'time') {
return true;
}
}
}

return false;
},
operator: (options) => (source) =>
source.pipe(
map((data) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ export const groupByTransformer: DataTransformerInfo<GroupByTransformerOptions>
defaultOptions: {
fields: {},
},

applicator: (data) => {
let maxFields = 0;

// Group by needs at least two fields
// a field to group on and a field to aggregate
// We make sure that at least one frame has at
// least two fields
for (const frame of data) {
if (frame.fields.length > maxFields) {
maxFields = frame.fields.length;
}
}

return maxFields >= 2;
},
/**
* Return a modified copy of the series. If the transform is not or should not
* be applied, just return the input series
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ export const groupingToMatrixTransformer: DataTransformerInfo<GroupingToMatrixTr
rowField: DEFAULT_ROW_FIELD,
valueField: DEFAULT_VALUE_FIELD,
},
/**
* Grouping to matrix requires at least 3 fields to work.
*/
applicator: (data) => {
let numFields = 0;

for (const frame of data) {
numFields += frame.fields.length;
}

return numFields >= 3;
},
operator: (options) => (source) =>
source.pipe(
map((data) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const mergeTransformer: DataTransformerInfo<MergeTransformerOptions> = {
name: 'Merge series/tables',
description: 'Merges multiple series/tables into a single serie/table',
defaultOptions: {},
applicator: (data) => {
return data.length > 1;
},
operator: (options) => (source) =>
source.pipe(
map((dataFrames) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/grafana-data/src/types/transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export interface DataTransformerInfo<TOptions = any> extends RegistryItemWithOpt
* @param options
*/
operator: (options: TOptions, context: DataTransformContext) => MonoTypeOperatorFunction<DataFrame[]>;
/**
* Function that is present will indicate whether a transformation is applicable
* given the current data.
* @param options
*/
applicator?: (data: DataFrame[]) => boolean;
codeincarnate marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ class UnThemedTransformationsEditor extends React.PureComponent<TransformationsE
<TransformationsGrid
showIllustrations={this.state.showIllustrations}
transformations={xforms}
data={this.state.data}
onClick={(id) => {
this.onTransformationAdd({ value: id });
}}
Expand Down Expand Up @@ -680,40 +681,50 @@ interface TransformationsGridProps {
transformations: Array<TransformerRegistryItem<any>>;
showIllustrations?: boolean;
onClick: (id: string) => void;
data: DataFrame[];
}

function TransformationsGrid({ showIllustrations, transformations, onClick }: TransformationsGridProps) {
function TransformationsGrid({ showIllustrations, transformations, onClick, data }: TransformationsGridProps) {
const styles = useStyles2(getStyles);

return (
<div className={styles.grid}>
{transformations.map((transform) => (
<Card
key={transform.id}
className={styles.newCard}
data-testid={selectors.components.TransformTab.newTransform(transform.name)}
onClick={() => onClick(transform.id)}
>
<Card.Heading className={styles.heading}>
<>
<span>{transform.name}</span>
<span className={styles.pluginStateInfoWrapper}>
<PluginStateInfo state={transform.state} />
</span>
</>
</Card.Heading>
<Card.Description className={styles.description}>
<>
<span>{getTransformationsRedesignDescriptions(transform.id)}</span>
{showIllustrations && (
<span>
<img className={styles.image} src={getImagePath(transform.id)} alt={transform.name} />
{transformations.map((transform) => {
// Check to see if the transform
// is applicable to the given data
let applicable = true;
if (transform.transformation.applicator !== undefined) {
applicable = transform.transformation.applicator(data);
}

return (
<Card
key={transform.id}
className={styles.newCard}
data-testid={selectors.components.TransformTab.newTransform(transform.name)}
onClick={() => onClick(transform.id)}
disabled={!applicable}
>
<Card.Heading className={styles.heading}>
<>
<span>{transform.name}</span>
<span className={styles.pluginStateInfoWrapper}>
<PluginStateInfo state={transform.state} />
</span>
)}
</>
</Card.Description>
</Card>
))}
</>
</Card.Heading>
<Card.Description className={styles.description}>
<>
<span>{getTransformationsRedesignDescriptions(transform.id)}</span>
{showIllustrations && (
<span>
<img className={styles.image} src={getImagePath(transform.id)} alt={transform.name} />
</span>
)}
</>
</Card.Description>
</Card>
)})}
</div>
);
}
Expand Down