Skip to content

Commit

Permalink
feat(cubejs-client-core): Fill missing dates with custom measure value (
Browse files Browse the repository at this point in the history
#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]>
  • Loading branch information
nagulan23 and KSDaemon authored Dec 18, 2024
1 parent 10e47fc commit ed3f6c9
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
5 changes: 3 additions & 2 deletions docs/pages/reference/frontend/cubejs-client-core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,8 @@ resultSet.tablePivot({
Name | Type | Description |
------ | ------ | ------ |
aliasSeries? | string[] | Give each series a prefix alias. Should have one entry for each query:measure. See [chartPivot](#result-set-chart-pivot) |
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. |
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. |
fillWithValue? | string &#124; null | Value to autofill all the missing date's measure. |
x? | string[] | Dimensions to put on **x** or **rows** axis. |
y? | string[] | Dimensions to put on **y** or **columns** axis. |

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

[ref-query-format]: /product/apis-integrations/rest-api/query-format
[ref-security]: /product/auth
[ref-security]: /product/auth
6 changes: 5 additions & 1 deletion packages/cubejs-client-core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,13 @@ declare module '@cubejs-client/core' {
*/
y?: string[];
/**
* 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
* 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
*/
fillMissingDates?: boolean | null;
/**
* Value to autofill all the missing date's measure.
*/
fillWithValue?: string | number | null;
/**
* Give each series a prefix alias. Should have one entry for each query:measure. See [chartPivot](#result-set-chart-pivot)
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/cubejs-client-core/src/ResultSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class ResultSet {
const pivotImpl = (resultIndex = 0) => {
let groupByXAxis = groupByToPairs(({ xValues }) => this.axisValuesString(xValues));

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

if (
pivotConfig.fillMissingDates &&
Expand Down
120 changes: 120 additions & 0 deletions packages/cubejs-client-core/src/tests/ResultSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,126 @@ describe('ResultSet', () => {
]);
});

test('fill missing dates with custom value', () => {
const resultSet = new ResultSet({
query: {
measures: ['Orders.total'],
timeDimensions: [
{
dimension: 'Orders.createdAt',
granularity: 'day',
dateRange: ['2020-01-08T00:00:00.000', '2020-01-11T23:59:59.999']
}
],
filters: [],
timezone: 'UTC'
},
data: [
{
'Orders.createdAt': '2020-01-08T00:00:00.000',
'Orders.total': 1
},
{
'Orders.createdAt': '2020-01-10T00:00:00.000',
'Orders.total': 10
}
],
annotation: {
measures: {},
dimensions: {},
segments: {},
timeDimensions: {
'Orders.createdAt': {
title: 'Orders Created at',
shortTitle: 'Created at',
type: 'time'
}
}
}
});

expect(resultSet.tablePivot({
'fillWithValue': 5
})).toEqual([
{
'Orders.createdAt.day': '2020-01-08T00:00:00.000',
'Orders.total': 1
},
{
'Orders.createdAt.day': '2020-01-09T00:00:00.000',
'Orders.total': 5
},
{
'Orders.createdAt.day': '2020-01-10T00:00:00.000',
'Orders.total': 10
},
{
'Orders.createdAt.day': '2020-01-11T00:00:00.000',
'Orders.total': 5
}
]);
});

test('fill missing dates with custom string', () => {
const resultSet = new ResultSet({
query: {
measures: ['Orders.total'],
timeDimensions: [
{
dimension: 'Orders.createdAt',
granularity: 'day',
dateRange: ['2020-01-08T00:00:00.000', '2020-01-11T23:59:59.999']
}
],
filters: [],
timezone: 'UTC'
},
data: [
{
'Orders.createdAt': '2020-01-08T00:00:00.000',
'Orders.total': 1
},
{
'Orders.createdAt': '2020-01-10T00:00:00.000',
'Orders.total': 10
}
],
annotation: {
measures: {},
dimensions: {},
segments: {},
timeDimensions: {
'Orders.createdAt': {
title: 'Orders Created at',
shortTitle: 'Created at',
type: 'time'
}
}
}
});

expect(resultSet.tablePivot({
'fillWithValue': 'N/A'
})).toEqual([
{
'Orders.createdAt.day': '2020-01-08T00:00:00.000',
'Orders.total': 1
},
{
'Orders.createdAt.day': '2020-01-09T00:00:00.000',
'Orders.total': "N/A"
},
{
'Orders.createdAt.day': '2020-01-10T00:00:00.000',
'Orders.total': 10
},
{
'Orders.createdAt.day': '2020-01-11T00:00:00.000',
'Orders.total': "N/A"
}
]);
});

test('same dimension and time dimension without granularity', () => {
const resultSet = new ResultSet({
query: {
Expand Down

0 comments on commit ed3f6c9

Please sign in to comment.