Skip to content

Commit

Permalink
fix: Do not pad last 24 hours interval to day
Browse files Browse the repository at this point in the history
Fixes #361
  • Loading branch information
paveltiunov committed Feb 4, 2020
1 parent 9e339f7 commit 6554611
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/cubejs-api-gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ const querySchema = Joi.object().keys({
ungrouped: Joi.boolean()
});

const DateRegex = /^\d\d\d\d-\d\d-\d\d$/;

const normalizeQuery = (query) => {
// eslint-disable-next-line no-unused-vars
const { error, value } = Joi.validate(query, querySchema);
Expand Down Expand Up @@ -222,7 +224,13 @@ const normalizeQuery = (query) => {
}
return {
...td,
dateRange
dateRange: dateRange.map(
(d, i) => (
i === 0 ?
moment.utc(d).format(d.match(DateRegex) ? 'YYYY-MM-DDT00:00:00.000' : moment.HTML5_FMT.DATETIME_LOCAL_MS) :
moment.utc(d).format(d.match(DateRegex) ? 'YYYY-MM-DDT23:59:59.999' : moment.HTML5_FMT.DATETIME_LOCAL_MS)
)
)
};
}).concat(regularToTimeDimension)
};
Expand Down
17 changes: 16 additions & 1 deletion packages/cubejs-api-gateway/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const compilerApi = jest.fn().mockImplementation(() => ({
return {
sql: ['SELECT * FROM test', []],
aliasNameToMember: {
foo__bar: 'Foo.bar'
foo__bar: 'Foo.bar',
foo__time: 'Foo.time'
}
};
},
Expand All @@ -23,6 +24,8 @@ const compilerApi = jest.fn().mockImplementation(() => ({
}],
dimensions: [{
name: 'Foo.id'
}, {
name: 'Foo.time'
}]
}
}];
Expand Down Expand Up @@ -75,4 +78,16 @@ describe(`API Gateway`, () => {
console.log(res.body);
expect(res.body && res.body.data).toStrictEqual([{ 'Foo.bar': 42 }]);
});

test(`date range padding`, async () => {
const res = await request(app)
.get('/cubejs-api/v1/load?query={"measures":["Foo.bar"],"timeDimensions":[{"dimension":"Foo.time","granularity":"hour","dateRange":["2020-01-01","2020-01-01"]}]}')
.set('Authorization', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M')
.expect(200);
console.log(res.body);
expect(res.body.query.timeDimensions[0].dateRange).toStrictEqual([
"2020-01-01T00:00:00.000",
"2020-01-01T23:59:59.999"
]);
});
});
6 changes: 5 additions & 1 deletion packages/cubejs-client-core/src/ResultSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const TIME_SERIES = {
.map(d => d.startOf('isoweek').format('YYYY-MM-DDT00:00:00.000'))
};

const DateRegex = /^\d\d\d\d-\d\d-\d\d$/;

/**
* Provides a convenient interface for data manipulation.
*/
Expand Down Expand Up @@ -171,7 +173,9 @@ class ResultSet {
if (!dateRange) {
return null;
}
const padToDay = timeDimension.granularity !== 'minute' && timeDimension.granularity !== 'second';
const padToDay = timeDimension.dateRange ?
timeDimension.dateRange.find(d => d.match(DateRegex)) :
['hour', 'minute', 'second'].indexOf(timeDimension.granularity) === -1;
const start = moment(dateRange[0]).format(padToDay ? 'YYYY-MM-DDT00:00:00.000' : moment.HTML5_FMT.DATETIME_LOCAL_MS);
const end = moment(dateRange[1]).format(padToDay ? 'YYYY-MM-DDT23:59:59.999' : moment.HTML5_FMT.DATETIME_LOCAL_MS);
const range = moment.range(start, end);
Expand Down
16 changes: 16 additions & 0 deletions packages/cubejs-client-core/src/ResultSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ describe('ResultSet', () => {
];
expect(resultSet.timeSeries(timeDimension)).toEqual(output);
});

test('it generates array of dates - granularity hour - not full day', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2015-01-01T10:30:00.000', '2015-01-01T13:59:00.000'],
granularity: 'hour',
timeDimension: 'Events.time'
};
const output = [
'2015-01-01T10:00:00.000',
'2015-01-01T11:00:00.000',
'2015-01-01T12:00:00.000',
'2015-01-01T13:00:00.000'
];
expect(resultSet.timeSeries(timeDimension)).toEqual(output);
});
});

describe('normalizePivotConfig', () => {
Expand Down

0 comments on commit 6554611

Please sign in to comment.