Skip to content

Commit ed3f6c9

Browse files
nagulan23KSDaemon
andauthored
feat(cubejs-client-core): Fill missing dates with custom measure value (#8843)
* feat(cubejs-client-core): fill missing dates with custom measure value * test: fill missing dates with custom value * feat(cubejs-client-core): modify cubejs-client-core doc * test: fill missing dates with custom string * fix type for fillWithValue --------- Co-authored-by: Konstantin Burkalev <[email protected]>
1 parent 10e47fc commit ed3f6c9

File tree

4 files changed

+129
-4
lines changed

4 files changed

+129
-4
lines changed

docs/pages/reference/frontend/cubejs-client-core.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,8 @@ resultSet.tablePivot({
838838
Name | Type | Description |
839839
------ | ------ | ------ |
840840
aliasSeries? | string[] | Give each series a prefix alias. Should have one entry for each query:measure. See [chartPivot](#result-set-chart-pivot) |
841-
fillMissingDates? | boolean &#124; null | **`true` by default.** If set to `true`, missing dates on the time dimensions will be filled with `0` for all measures. Note: setting this option to `true` will override any `order` applied to the query. |
841+
fillMissingDates? | boolean &#124; null | **`true` by default.** If set to `true`, missing dates on the time dimensions will be filled with `fillWithValue` or `0` by default for all measures. Note: setting this option to `true` will override any `order` applied to the query. |
842+
fillWithValue? | string &#124; null | Value to autofill all the missing date's measure. |
842843
x? | string[] | Dimensions to put on **x** or **rows** axis. |
843844
y? | string[] | Dimensions to put on **y** or **columns** axis. |
844845

@@ -1055,4 +1056,4 @@ values? | never |
10551056
[link-mdn-max-safe-integer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
10561057

10571058
[ref-query-format]: /product/apis-integrations/rest-api/query-format
1058-
[ref-security]: /product/auth
1059+
[ref-security]: /product/auth

packages/cubejs-client-core/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,13 @@ declare module '@cubejs-client/core' {
252252
*/
253253
y?: string[];
254254
/**
255-
* If `true` missing dates on the time dimensions will be filled with `0` for all measures.Note: the `fillMissingDates` option set to `true` will override any **order** applied to the query
255+
* If `true` missing dates on the time dimensions will be filled with fillWithValue or `0` by default for all measures.Note: the `fillMissingDates` option set to `true` will override any **order** applied to the query
256256
*/
257257
fillMissingDates?: boolean | null;
258+
/**
259+
* Value to autofill all the missing date's measure.
260+
*/
261+
fillWithValue?: string | number | null;
258262
/**
259263
* Give each series a prefix alias. Should have one entry for each query:measure. See [chartPivot](#result-set-chart-pivot)
260264
*/

packages/cubejs-client-core/src/ResultSet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class ResultSet {
341341
const pivotImpl = (resultIndex = 0) => {
342342
let groupByXAxis = groupByToPairs(({ xValues }) => this.axisValuesString(xValues));
343343

344-
const measureValue = (row, measure) => row[measure] || 0;
344+
const measureValue = (row, measure) => row[measure] || pivotConfig.fillWithValue || 0;
345345

346346
if (
347347
pivotConfig.fillMissingDates &&

packages/cubejs-client-core/src/tests/ResultSet.test.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,126 @@ describe('ResultSet', () => {
14181418
]);
14191419
});
14201420

1421+
test('fill missing dates with custom value', () => {
1422+
const resultSet = new ResultSet({
1423+
query: {
1424+
measures: ['Orders.total'],
1425+
timeDimensions: [
1426+
{
1427+
dimension: 'Orders.createdAt',
1428+
granularity: 'day',
1429+
dateRange: ['2020-01-08T00:00:00.000', '2020-01-11T23:59:59.999']
1430+
}
1431+
],
1432+
filters: [],
1433+
timezone: 'UTC'
1434+
},
1435+
data: [
1436+
{
1437+
'Orders.createdAt': '2020-01-08T00:00:00.000',
1438+
'Orders.total': 1
1439+
},
1440+
{
1441+
'Orders.createdAt': '2020-01-10T00:00:00.000',
1442+
'Orders.total': 10
1443+
}
1444+
],
1445+
annotation: {
1446+
measures: {},
1447+
dimensions: {},
1448+
segments: {},
1449+
timeDimensions: {
1450+
'Orders.createdAt': {
1451+
title: 'Orders Created at',
1452+
shortTitle: 'Created at',
1453+
type: 'time'
1454+
}
1455+
}
1456+
}
1457+
});
1458+
1459+
expect(resultSet.tablePivot({
1460+
'fillWithValue': 5
1461+
})).toEqual([
1462+
{
1463+
'Orders.createdAt.day': '2020-01-08T00:00:00.000',
1464+
'Orders.total': 1
1465+
},
1466+
{
1467+
'Orders.createdAt.day': '2020-01-09T00:00:00.000',
1468+
'Orders.total': 5
1469+
},
1470+
{
1471+
'Orders.createdAt.day': '2020-01-10T00:00:00.000',
1472+
'Orders.total': 10
1473+
},
1474+
{
1475+
'Orders.createdAt.day': '2020-01-11T00:00:00.000',
1476+
'Orders.total': 5
1477+
}
1478+
]);
1479+
});
1480+
1481+
test('fill missing dates with custom string', () => {
1482+
const resultSet = new ResultSet({
1483+
query: {
1484+
measures: ['Orders.total'],
1485+
timeDimensions: [
1486+
{
1487+
dimension: 'Orders.createdAt',
1488+
granularity: 'day',
1489+
dateRange: ['2020-01-08T00:00:00.000', '2020-01-11T23:59:59.999']
1490+
}
1491+
],
1492+
filters: [],
1493+
timezone: 'UTC'
1494+
},
1495+
data: [
1496+
{
1497+
'Orders.createdAt': '2020-01-08T00:00:00.000',
1498+
'Orders.total': 1
1499+
},
1500+
{
1501+
'Orders.createdAt': '2020-01-10T00:00:00.000',
1502+
'Orders.total': 10
1503+
}
1504+
],
1505+
annotation: {
1506+
measures: {},
1507+
dimensions: {},
1508+
segments: {},
1509+
timeDimensions: {
1510+
'Orders.createdAt': {
1511+
title: 'Orders Created at',
1512+
shortTitle: 'Created at',
1513+
type: 'time'
1514+
}
1515+
}
1516+
}
1517+
});
1518+
1519+
expect(resultSet.tablePivot({
1520+
'fillWithValue': 'N/A'
1521+
})).toEqual([
1522+
{
1523+
'Orders.createdAt.day': '2020-01-08T00:00:00.000',
1524+
'Orders.total': 1
1525+
},
1526+
{
1527+
'Orders.createdAt.day': '2020-01-09T00:00:00.000',
1528+
'Orders.total': "N/A"
1529+
},
1530+
{
1531+
'Orders.createdAt.day': '2020-01-10T00:00:00.000',
1532+
'Orders.total': 10
1533+
},
1534+
{
1535+
'Orders.createdAt.day': '2020-01-11T00:00:00.000',
1536+
'Orders.total': "N/A"
1537+
}
1538+
]);
1539+
});
1540+
14211541
test('same dimension and time dimension without granularity', () => {
14221542
const resultSet = new ResultSet({
14231543
query: {

0 commit comments

Comments
 (0)