Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ export class AsyncQueryService extends ProjectService {
? formatItemValue(
field,
row[c.reference],
false,
undefined,
)
: String(rawValue);
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const formatPivotValueLabel = (
const pivotValue = pivotReference.pivotValues[0].value;

if (pivotField && isField(pivotField)) {
return formatItemValue(pivotField, pivotValue, false, undefined);
return formatItemValue(pivotField, pivotValue, undefined, undefined);
}

return friendlyName(String(pivotValue));
Expand Down
6 changes: 4 additions & 2 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ export function formatRow(
itemsMap: ItemsMap,
pivotValuesColumns?: Record<string, PivotValuesColumn> | null,
parameters?: Record<string, unknown>,
timezone?: string,
): ResultRow {
const resultRow: ResultRow = {};
const columnNames = Object.keys(row || {});
Expand All @@ -759,7 +760,7 @@ export function formatRow(
resultRow[columnName] = {
value: {
raw: formatRawValue(item, value),
formatted: formatItemValue(item, value, false, parameters),
formatted: formatItemValue(item, value, timezone, parameters),
},
};
}
Expand All @@ -772,9 +773,10 @@ export function formatRows(
itemsMap: ItemsMap,
pivotValuesColumns?: Record<string, PivotValuesColumn> | null,
parameters?: Record<string, unknown>,
timezone?: string,
): ResultRow[] {
return rows.map((row) =>
formatRow(row, itemsMap, pivotValuesColumns, parameters),
formatRow(row, itemsMap, pivotValuesColumns, parameters, timezone),
);
}

Expand Down
18 changes: 14 additions & 4 deletions packages/common/src/pivot/pivotQueryResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,12 @@ const combinedRetrofit = (
if (!isSummable(item)) {
return null;
}
const formattedValue = formatItemValue(item, total, false, undefined);
const formattedValue = formatItemValue(
item,
total,
undefined,
undefined,
);

return {
raw: total,
Expand All @@ -274,7 +279,12 @@ const combinedRetrofit = (
if (!field || !field.fieldId) throw new Error('Invalid pivot data');
const item = getField(field.fieldId);

const formattedValue = formatItemValue(item, total, false, undefined);
const formattedValue = formatItemValue(
item,
total,
undefined,
undefined,
);

return {
raw: total,
Expand Down Expand Up @@ -1024,7 +1034,7 @@ export const convertSqlPivotedRowsToPivotData = ({
? formatItemValue(
field,
pivotValue.value,
true,
'UTC',
undefined,
)
: String(pivotValue.value));
Expand Down Expand Up @@ -1402,7 +1412,7 @@ export const convertSqlPivotedRowsToPivotData = ({
? formatItemValue(
field,
rowTotalValue,
false,
undefined,
undefined,
)
: String(rowTotalValue);
Expand Down
3 changes: 1 addition & 2 deletions packages/common/src/utils/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,8 @@ export const getFilterRuleWithDefaultValue = <T extends FilterRule>(
fieldTimeInterval === TimeFrames.QUARTER
? undefined
: fieldTimeInterval, // Use the field's time interval if it has one
false,
)
: formatDate(defaultDate, undefined, false);
: formatDate(defaultDate);

filterRuleDefaults.values = [dateValue];
}
Expand Down
112 changes: 93 additions & 19 deletions packages/common/src/utils/formatting.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from 'dayjs';
import moment from 'moment';
import moment from 'moment-timezone';
import {
Compact,
CustomFormatType,
Expand All @@ -16,8 +16,10 @@ import {
applyDefaultFormat,
convertCustomFormatToFormatExpression,
currencies,
formatDate,
formatItemValue,
formatNumberValue,
formatTimestamp,
formatValueWithExpression,
getCustomFormatFromLegacy,
isMomentInput,
Expand Down Expand Up @@ -1593,7 +1595,7 @@ describe('Formatting', () => {
formatItemValue(
metricWithConditionalFormat,
1234.56,
false,
undefined,
parameters,
),
).toBe('$1,234.56');
Expand All @@ -1611,7 +1613,7 @@ describe('Formatting', () => {
formatItemValue(
metricWithConditionalFormat,
1234.56,
false,
undefined,
parameters,
),
).toBe('€1,234.56');
Expand All @@ -1625,15 +1627,25 @@ describe('Formatting', () => {
};

expect(
formatItemValue(metricWithSimpleFormat, 1234.56, false, {
symbol: '£',
}),
formatItemValue(
metricWithSimpleFormat,
1234.56,
undefined,
{
symbol: '£',
},
),
).toBe('£1,234.56');

expect(
formatItemValue(metricWithSimpleFormat, 1234.56, false, {
symbol: '¥',
}),
formatItemValue(
metricWithSimpleFormat,
1234.56,
undefined,
{
symbol: '¥',
},
),
).toBe('¥1,234.56');
});

Expand All @@ -1649,7 +1661,7 @@ describe('Formatting', () => {
formatItemValue(
metricWithConditionalFormat,
1234.56,
false,
undefined,
undefined,
),
).toBe('1,234.56');
Expand All @@ -1666,7 +1678,7 @@ describe('Formatting', () => {
formatItemValue(
metricWithConditionalFormat,
1234.567,
false,
undefined,
{ precision: 'high' },
),
).toBe('$1,234.57');
Expand All @@ -1675,7 +1687,7 @@ describe('Formatting', () => {
formatItemValue(
metricWithConditionalFormat,
1234.567,
false,
undefined,
{ precision: 'low' },
),
).toBe('$1,235');
Expand All @@ -1695,15 +1707,20 @@ describe('Formatting', () => {
};

expect(
formatItemValue(metricWithLdPrefix, 1234.56, false, {
formatItemValue(metricWithLdPrefix, 1234.56, undefined, {
symbol: '$',
}),
).toBe('$1,234.56');

expect(
formatItemValue(metricWithLightdashPrefix, 1234.56, false, {
symbol: '€',
}),
formatItemValue(
metricWithLightdashPrefix,
1234.56,
undefined,
{
symbol: '€',
},
),
).toBe('€1,234.56');
});

Expand All @@ -1715,23 +1732,80 @@ describe('Formatting', () => {
};

expect(
formatItemValue(metricWithFormat, 1000.5, false, {
formatItemValue(metricWithFormat, 1000.5, undefined, {
symbol: '$',
}),
).toBe('$1,000.50');

expect(
formatItemValue(metricWithFormat, 1000.5, false, {
formatItemValue(metricWithFormat, 1000.5, undefined, {
symbol: '€',
}),
).toBe('€1,000.50');

expect(
formatItemValue(metricWithFormat, 1000.5, false, {
formatItemValue(metricWithFormat, 1000.5, undefined, {
symbol: '£',
}),
).toBe('£1,000.50');
});
});
});

describe('timezone-aware date formatting', () => {
// 2020-04-04 02:00 UTC = 2020-04-03 22:00 New York (EDT, UTC-4)
const utcTimestamp = '2020-04-04T02:00:00.000Z';

describe('formatDate', () => {
test('formats in UTC when timezone is UTC', () => {
expect(formatDate(utcTimestamp, TimeFrames.DAY, 'UTC')).toBe(
'2020-04-04',
);
});

test('formats in specified timezone', () => {
expect(
formatDate(
utcTimestamp,
TimeFrames.DAY,
'America/New_York',
),
).toBe('2020-04-03');
});

test('formats in process timezone when no timezone provided (flag off)', () => {
expect(formatDate(utcTimestamp, TimeFrames.DAY)).toBe(
'2020-04-04',
);
});
});

describe('formatTimestamp', () => {
test('formats in UTC when timezone is UTC', () => {
expect(
formatTimestamp(
utcTimestamp,
TimeFrames.MILLISECOND,
'UTC',
),
).toBe('2020-04-04, 02:00:00:000 (+00:00)');
});

test('formats in specified timezone', () => {
expect(
formatTimestamp(
utcTimestamp,
TimeFrames.MILLISECOND,
'America/New_York',
),
).toBe('2020-04-03, 22:00:00:000 (-04:00)');
});

test('formats in process timezone when no timezone provided (flag off)', () => {
expect(
formatTimestamp(utcTimestamp, TimeFrames.MILLISECOND),
).toBe('2020-04-04, 02:00:00:000 (+00:00)');
});
});
});
});
Loading
Loading