Skip to content

feat: Allow users to define custom column aliases for charts #996

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .changeset/chilled-goats-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@hyperdx/common-utils": patch
"@hyperdx/api": patch
"@hyperdx/app": patch
---

feat: Allow users to define custom column aliases for charts
6 changes: 5 additions & 1 deletion packages/app/src/ChartUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ export function formatResponseForTimeChart({
};
} = {};

const isSingleValueColumn = valueColumns.length === 1;
const hasGroupColumns = groupColumns.length > 0;

for (const row of data) {
const date = new Date(row[timestampColumn.name]);
const ts = date.getTime() / 1000;
Expand All @@ -491,7 +494,8 @@ export function formatResponseForTimeChart({
const tsBucket = tsBucketMap.get(ts) ?? {};

const keyName = [
valueColumn.name,
// Simplify the display name if there's only one series and a group by
...(isSingleValueColumn && hasGroupColumns ? [] : [valueColumn.name]),
...groupColumns.map(g => row[g.name]),
].join(' · ');

Expand Down
14 changes: 13 additions & 1 deletion packages/app/src/components/DBEditTimeChartForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import HDXMarkdownChart from '../HDXMarkdownChart';

import { AggFnSelectControlled } from './AggFnSelect';
import DBNumberChart from './DBNumberChart';
import { InputControlled } from './InputControlled';
import { InputControlled, TextInputControlled } from './InputControlled';
import { MetricNameSelect } from './MetricNameSelect';
import { NumberFormatInput } from './NumberFormat';
import { SourceSelectControlled } from './SourceSelect';
Expand Down Expand Up @@ -163,6 +163,17 @@ function ChartSeriesEditorComponent({
<Divider
label={
<Group gap="xs">
<Text size="xxs">Alias</Text>

<div style={{ width: 150 }}>
<TextInputControlled
name={`${namePrefix}alias`}
control={control}
placeholder="Series alias"
onChange={() => onSubmit()}
size="xs"
/>
</div>
{(index ?? -1) > 0 && (
<Button
variant="subtle"
Expand All @@ -179,6 +190,7 @@ function ChartSeriesEditorComponent({
c="dark.2"
labelPosition="right"
mb={8}
mt="sm"
/>
<Flex gap="sm" mt="xs" align="center">
<div
Expand Down
28 changes: 28 additions & 0 deletions packages/app/src/components/InputControlled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
InputProps,
PasswordInput,
PasswordInputProps,
TextInput,
TextInputProps,
} from '@mantine/core';

interface InputControlledProps<T extends FieldValues>
Expand All @@ -23,6 +25,32 @@ interface PasswordInputControlledProps<T extends FieldValues>
rules?: Parameters<Control<T>['register']>[1];
}

interface TextInputControlledProps<T extends FieldValues>
extends Omit<TextInputProps, 'name' | 'style'>,
Omit<React.InputHTMLAttributes<HTMLInputElement>, 'name' | 'size'> {
name: Path<T>;
control: Control<T>;
rules?: Parameters<Control<T>['register']>[1];
}

export function TextInputControlled<T extends FieldValues>({
name,
control,
rules,
...props
}: TextInputControlledProps<T>) {
return (
<Controller
name={name}
control={control}
rules={rules}
render={({ field, fieldState: { error } }) => (
<TextInput {...props} {...field} error={error?.message} />
)}
/>
);
}

export function InputControlled<T extends FieldValues>({
name,
control,
Expand Down
4 changes: 2 additions & 2 deletions packages/common-utils/src/renderChartConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const setChartSelectsAlias = (config: ChartConfigWithOptDateRange) => {
...config,
select: config.select.map(s => ({
...s,
alias: s.alias ?? `${s.aggFn}(${s.metricName})`, // use an alias if one isn't already set
alias: s.alias || `${s.aggFn}(${s.metricName})`, // use an alias if one isn't already set
})),
};
}
Expand Down Expand Up @@ -396,7 +396,7 @@ async function renderSelectList(
}

return chSql`${expr}${
select.alias != null
select.alias != null && select.alias.trim() !== ''
? chSql` AS "${{ UNSAFE_RAW_SQL: select.alias }}"`
: []
}`;
Expand Down