You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@superset.apache.org by GitBox <gi...@apache.org> on 2018/04/09 18:35:24 UTC

[GitHub] betodealmeida closed pull request #4623: Use granularity for time step in play slider

betodealmeida closed pull request #4623: Use granularity for time step in play slider
URL: https://github.com/apache/incubator-superset/pull/4623
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/superset/assets/javascripts/explore/stores/controls.jsx b/superset/assets/javascripts/explore/stores/controls.jsx
index 5bd825a633..b675b03e50 100644
--- a/superset/assets/javascripts/explore/stores/controls.jsx
+++ b/superset/assets/javascripts/explore/stores/controls.jsx
@@ -232,7 +232,7 @@ export const controls = {
     description: t('Choose the position of the legend'),
     type: 'SelectControl',
     clearable: false,
-    default: 'Top right',
+    default: 'tr',
     choices: [
       ['tl', 'Top left'],
       ['tr', 'Top right'],
diff --git a/superset/assets/javascripts/modules/time.js b/superset/assets/javascripts/modules/time.js
new file mode 100644
index 0000000000..94bff5b8e0
--- /dev/null
+++ b/superset/assets/javascripts/modules/time.js
@@ -0,0 +1,38 @@
+const SECOND = 1000;  // milliseconds
+const MINUTE = 60 * SECOND;
+const HOUR = 60 * MINUTE;
+const DAY = 24 * HOUR;
+const WEEK = 7 * DAY;
+const MONTH = 30 * DAY;
+const YEAR = 365 * DAY;
+const QUARTER = YEAR / 4;
+
+const unitsToMilliSeconds = {
+  second: SECOND,
+  minute: MINUTE,
+  hour: HOUR,
+  'half hour': HOUR / 2,  // mssql
+  day: DAY,
+  week: WEEK,
+  month: MONTH,
+  quarter: QUARTER,
+  year: YEAR,
+};
+
+const pattern = new RegExp('^(\\d+)?\\s*(.*)$');
+
+export const parseTimeGrain = function (timeGrain) {
+  const match = pattern.exec(timeGrain);
+  const number = parseInt(match[1], 10) || 1;
+  const units = match[2];
+
+  let compiled;
+  let milliseconds = null;
+  Object.entries(unitsToMilliSeconds).forEach(([k, v]) => {
+    compiled = new RegExp(k);
+    if (compiled.test(units)) {
+      milliseconds = number * v;
+    }
+  });
+  return milliseconds;
+};
diff --git a/superset/assets/spec/javascripts/modules/time_spec.js b/superset/assets/spec/javascripts/modules/time_spec.js
new file mode 100644
index 0000000000..1fcdafd81e
--- /dev/null
+++ b/superset/assets/spec/javascripts/modules/time_spec.js
@@ -0,0 +1,37 @@
+import { it, describe } from 'mocha';
+import { expect } from 'chai';
+import { parseTimeGrain } from '../../../javascripts/modules/time';
+
+describe('parseTimeGrain', () => {
+  it('is a function', () => {
+    assert.isFunction(parseTimeGrain);
+  });
+
+  it('returns a number', () => {
+    expect(parseTimeGrain('minute')).to.be.a('number');
+  });
+
+  it('returns the expected output', () => {
+    expect(parseTimeGrain('second')).to.equal(1000);
+    expect(parseTimeGrain('5 seconds')).to.equal(5000);
+    expect(parseTimeGrain('30 seconds')).to.equal(30000);
+    expect(parseTimeGrain('minute')).to.equal(60000);
+    expect(parseTimeGrain('1 minute')).to.equal(60000);
+    expect(parseTimeGrain('5 minute')).to.equal(300000);
+    expect(parseTimeGrain('5 minutes')).to.equal(300000);
+    expect(parseTimeGrain('10 minute')).to.equal(600000);
+    expect(parseTimeGrain('hour')).to.equal(3600000);
+    expect(parseTimeGrain('half hour')).to.equal(1800000);
+    expect(parseTimeGrain('1 hour')).to.equal(3600000);
+    expect(parseTimeGrain('6 hour')).to.equal(21600000);
+    expect(parseTimeGrain('day')).to.equal(86400000);
+    expect(parseTimeGrain('1 day')).to.equal(86400000);
+    expect(parseTimeGrain('7 day')).to.equal(604800000);
+    expect(parseTimeGrain('week')).to.equal(604800000);
+    expect(parseTimeGrain('week_starting_sunday')).to.equal(604800000);
+    expect(parseTimeGrain('week_ending_saturday')).to.equal(604800000);
+    expect(parseTimeGrain('month')).to.equal(2592000000);
+    expect(parseTimeGrain('quarter')).to.equal(7884000000);
+    expect(parseTimeGrain('year')).to.equal(31536000000);
+  });
+});


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services