Skip to content

Commit

Permalink
Pass granularity from backend to frontend as ISO duration (apache#4755)
Browse files Browse the repository at this point in the history
* Add ISO duration to time grains

* Use ISO duration

* Remove debugging code

* Add module to yarn.lock

* Remove autolint

* Druid granularity as ISO

* Remove dangling comma
  • Loading branch information
betodealmeida authored and mistercrunch committed Apr 6, 2018
1 parent 96106a4 commit a2cfd4d
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 139 deletions.
30 changes: 15 additions & 15 deletions superset/assets/javascripts/explore/stores/controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -699,21 +699,21 @@ export const controls = {
freeForm: true,
label: t('Time Granularity'),
default: 'one day',
choices: formatSelectOptions([
'all',
'5 seconds',
'30 seconds',
'1 minute',
'5 minutes',
'1 hour',
'6 hour',
'1 day',
'7 days',
'week',
'week_starting_sunday',
'week_ending_saturday',
'month',
]),
choices: [
[null, 'all'],
['PT5S', '5 seconds'],
['PT30S', '30 seconds'],
['PT1M', '1 minute'],
['PT5M', '5 minutes'],
['PT1H', '1 hour'],
['PT6H', '6 hour'],
['P1D', '1 day'],
['P7D', '7 days'],
['P1W', 'week'],
['P1W', 'week_starting_sunday'],
['P1W', 'week_ending_saturday'],
['P1M', 'month'],
],
description: t('The time granularity for the visualization. Note that you ' +
'can type and use simple natural language as in `10 seconds`, ' +
'`1 day` or `56 weeks`'),
Expand Down
1 change: 1 addition & 0 deletions superset/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"object.entries": "^1.0.4",
"object.keys": "^0.1.0",
"object.values": "^1.0.4",
"parse-iso-duration": "^1.0.0",
"po2json": "^0.4.5",
"prop-types": "^15.6.0",
"react": "^15.6.2",
Expand Down
26 changes: 3 additions & 23 deletions superset/assets/visualizations/deckgl/layers/scatter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';

import parseIsoDuration from 'parse-iso-duration';
import { ScatterplotLayer } from 'deck.gl';

import AnimatableDeckGLContainer from '../AnimatableDeckGLContainer';
Expand All @@ -14,27 +15,6 @@ import { getColorFromScheme, hexToRGB } from '../../../javascripts/modules/color
import { unitToRadius } from '../../../javascripts/modules/geo';
import sandboxedEval from '../../../javascripts/modules/sandbox';

function getStep(timeGrain) {
// grain in milliseconds
const MINUTE = 60 * 1000;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const WEEK = 7 * DAY;
const MONTH = 30 * DAY;
const YEAR = 365 * DAY;

const milliseconds = {
'Time Column': MINUTE,
min: MINUTE,
hour: HOUR,
day: DAY,
week: WEEK,
month: MONTH,
year: YEAR,
};

return milliseconds[timeGrain];
}

function getPoints(data) {
return data.map(d => d.position);
Expand Down Expand Up @@ -117,15 +97,15 @@ class DeckGLScatter extends React.PureComponent {
/* eslint-disable no-unused-vars */
static getDerivedStateFromProps(nextProps, prevState) {
const fd = nextProps.slice.formData;
const timeGrain = fd.time_grain_sqla || fd.granularity || 'min';
const timeGrain = fd.time_grain_sqla || fd.granularity || 'PT1M';

// find start and end based on the data
const timestamps = nextProps.payload.data.features.map(f => f.__timestamp);
let start = Math.min(...timestamps);
let end = Math.max(...timestamps);

// lock start and end to the closest steps
const step = getStep(timeGrain);
const step = parseIsoDuration(timeGrain);
start -= start % step;
end += step - end % step;

Expand Down
4 changes: 4 additions & 0 deletions superset/assets/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6528,6 +6528,10 @@ parse-glob@^3.0.4:
is-extglob "^1.0.0"
is-glob "^2.0.0"

parse-iso-duration@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/parse-iso-duration/-/parse-iso-duration-1.0.0.tgz#b923ab898a8ff8f42bdc9ee5db6e22808c48a864"

parse-json@^2.1.0, parse-json@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-2.2.0.tgz#f480f40434ef80741f8469099f8dea18f55a4dc9"
Expand Down
2 changes: 1 addition & 1 deletion superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def data(self):
if self.type == 'table':
grains = self.database.grains() or []
if grains:
grains = [(g.name, g.name) for g in grains]
grains = [(g.duration, g.name) for g in grains]
d['granularity_sqla'] = utils.choicify(self.dttm_cols)
d['time_grain_sqla'] = grains
return d
Expand Down
Loading

0 comments on commit a2cfd4d

Please sign in to comment.