zhaoyongjie commented on a change in pull request #11418:
URL: 
https://github.com/apache/incubator-superset/pull/11418#discussion_r545168009



##########
File path: 
superset-frontend/src/explore/components/controls/DateFilterControl/DateFilterControl.tsx
##########
@@ -0,0 +1,872 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useState, useEffect } from 'react';
+import rison from 'rison';
+import moment, { Moment } from 'moment';
+import {
+  SupersetClient,
+  styled,
+  supersetTheme,
+  t,
+  TimeRangeEndpoints,
+} from '@superset-ui/core';
+import {
+  buildTimeRangeString,
+  formatTimeRange,
+  SEPARATOR,
+} from 'src/explore/dateFilterUtils';
+import { getClientErrorObject } from 'src/utils/getClientErrorObject';
+import Button from 'src/components/Button';
+import ControlHeader from 'src/explore/components/ControlHeader';
+import Label from 'src/components/Label';
+import Modal from 'src/common/components/Modal';
+import {
+  Col,
+  DatePicker,
+  Divider,
+  Input,
+  InputNumber,
+  Radio,
+  Row,
+} from 'src/common/components';
+import Icon from 'src/components/Icon';
+import { Select } from 'src/components/Select';
+import {
+  TimeRangeFrameType,
+  CommonRangeType,
+  CalendarRangeType,
+  CustomRangeType,
+  CustomRangeDecodeType,
+  CustomRangeKey,
+  PreviousCalendarWeek,
+  PreviousCalendarMonth,
+  PreviousCalendarYear,
+} from './types';
+import {
+  COMMON_RANGE_OPTIONS,
+  CALENDAR_RANGE_OPTIONS,
+  RANGE_FRAME_OPTIONS,
+  SINCE_GRAIN_OPTIONS,
+  UNTIL_GRAIN_OPTIONS,
+  SINCE_MODE_OPTIONS,
+  UNTIL_MODE_OPTIONS,
+} from './constants';
+
+const MOMENT_FORMAT = 'YYYY-MM-DD[T]HH:mm:ss';
+const DEFAULT_SINCE = moment()
+  .utc()
+  .startOf('day')
+  .subtract(7, 'days')
+  .format(MOMENT_FORMAT);
+const DEFAULT_UNTIL = moment().utc().startOf('day').format(MOMENT_FORMAT);
+
+const customTimeRangeDecode = (timeRange: string): CustomRangeDecodeType => {
+  const splitDateRange = timeRange.split(SEPARATOR);
+  const DATETIME_CONSTANT = ['now', 'today'];
+  const defaultCustomRange: CustomRangeType = {
+    sinceDatetime: DEFAULT_SINCE,
+    sinceMode: 'relative',
+    sinceGrain: 'day',
+    sinceGrainValue: -7,
+    untilDatetime: DEFAULT_UNTIL,
+    untilMode: 'specific',
+    untilGrain: 'day',
+    untilGrainValue: 7,
+    anchorMode: 'now',
+    anchorValue: 'now',
+  };
+
+  /**
+   * RegExp to test a string for a full ISO 8601 Date
+   * Does not do any sort of date validation, only checks if the string is 
according to the ISO 8601 spec.
+   *  YYYY-MM-DDThh:mm:ss
+   *  YYYY-MM-DDThh:mm:ssTZD
+   *  YYYY-MM-DDThh:mm:ss.sTZD
+   * @see: https://www.w3.org/TR/NOTE-datetime
+   */
+  const iso8601 = 
String.raw`\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?(?:(?:[+-]\d\d:\d\d)|Z)?`;
+  const datetimeConstant = String.raw`TODAY|NOW`;
+  const grainValue = String.raw`[+-]?[1-9][0-9]*`;
+  const grain = String.raw`YEAR|QUARTER|MONTH|WEEK|DAY|HOUR|MINUTE|SECOND`;
+  const CUSTOM_RANGE_EXPRESSION = RegExp(
+    
String.raw`^DATEADD\(DATETIME\("(${iso8601}|${datetimeConstant})"\),\s(${grainValue}),\s(${grain})\)$`,
+    'i',
+  );
+  const ISO8601_AND_CONSTANT = RegExp(
+    String.raw`^${iso8601}$|^${datetimeConstant}$`,
+    'i',
+  );
+
+  if (splitDateRange.length === 2) {
+    const [since, until] = [...splitDateRange];
+
+    // specific : specific
+    if (
+      since.match(ISO8601_AND_CONSTANT) &&
+      until.match(ISO8601_AND_CONSTANT)
+    ) {
+      const sinceMode = DATETIME_CONSTANT.includes(since) ? since : 'specific';
+      const untilMode = DATETIME_CONSTANT.includes(until) ? until : 'specific';
+      return {
+        customRange: {
+          ...defaultCustomRange,
+          sinceDatetime: since,
+          untilDatetime: until,
+          sinceMode,
+          untilMode,
+        },
+        matchedFlag: true,
+      };
+    }
+
+    // relative : specific
+    const sinceCapturedGroup = since.match(CUSTOM_RANGE_EXPRESSION);
+    if (
+      sinceCapturedGroup &&
+      until.match(ISO8601_AND_CONSTANT) &&
+      since.includes(until)
+    ) {
+      const [dttm, grainValue, grain] = [...sinceCapturedGroup.slice(1)];
+      const untilMode = DATETIME_CONSTANT.includes(until) ? until : 'specific';
+      return {
+        customRange: {
+          ...defaultCustomRange,
+          sinceGrain: grain,
+          sinceGrainValue: parseInt(grainValue, 10),
+          untilDatetime: dttm,
+          sinceMode: 'relative',
+          untilMode,
+        },
+        matchedFlag: true,
+      };
+    }
+
+    // specific : relative
+    const untilCapturedGroup = until.match(CUSTOM_RANGE_EXPRESSION);
+    if (
+      since.match(ISO8601_AND_CONSTANT) &&
+      untilCapturedGroup &&
+      until.includes(since)
+    ) {
+      const [dttm, grainValue, grain] = [...untilCapturedGroup.slice(1)];
+      const sinceMode = DATETIME_CONSTANT.includes(since) ? since : 'specific';
+      return {
+        customRange: {
+          ...defaultCustomRange,
+          untilGrain: grain,
+          untilGrainValue: parseInt(grainValue, 10),
+          sinceDatetime: dttm,
+          untilMode: 'relative',
+          sinceMode,
+        },
+        matchedFlag: true,
+      };
+    }
+
+    // relative : relative
+    if (sinceCapturedGroup && untilCapturedGroup) {
+      const [sinceDttm, sinceGrainValue, sinceGrain] = [
+        ...sinceCapturedGroup.slice(1),
+      ];
+      const [untileDttm, untilGrainValue, untilGrain] = [
+        ...untilCapturedGroup.slice(1),
+      ];
+      if (sinceDttm === untileDttm) {
+        return {
+          customRange: {
+            ...defaultCustomRange,
+            sinceGrain,
+            sinceGrainValue: parseInt(sinceGrainValue, 10),
+            untilGrain,
+            untilGrainValue: parseInt(untilGrainValue, 10),
+            anchorValue: sinceDttm,
+            sinceMode: 'relative',
+            untilMode: 'relative',
+            anchorMode: sinceDttm === 'now' ? 'now' : 'specific',
+          },
+          matchedFlag: true,
+        };
+      }
+    }
+  }
+
+  return {
+    customRange: defaultCustomRange,
+    matchedFlag: false,
+  };
+};
+
+const customTimeRangeEncode = (customRange: CustomRangeType): string => {
+  const SPECIFIC_MODE = ['specific', 'today', 'now'];
+  const {
+    sinceDatetime,
+    sinceMode,
+    sinceGrain,
+    sinceGrainValue,
+    untilDatetime,
+    untilMode,
+    untilGrain,
+    untilGrainValue,
+    anchorValue,
+  } = { ...customRange };
+  // specific : specific
+  if (SPECIFIC_MODE.includes(sinceMode) && SPECIFIC_MODE.includes(untilMode)) {
+    const since = sinceMode === 'specific' ? sinceDatetime : sinceMode;
+    const until = untilMode === 'specific' ? untilDatetime : untilMode;
+    return `${since} : ${until}`;
+  }
+
+  // specific : relative
+  if (SPECIFIC_MODE.includes(sinceMode) && untilMode === 'relative') {
+    const since = sinceMode === 'specific' ? sinceDatetime : sinceMode;
+    const until = `DATEADD(DATETIME("${since}"), ${untilGrainValue}, 
${untilGrain})`;
+    return `${since} : ${until}`;
+  }
+
+  // relative : specific
+  if (sinceMode === 'relative' && SPECIFIC_MODE.includes(untilMode)) {
+    const until = untilMode === 'specific' ? untilDatetime : untilMode;
+    const since = `DATEADD(DATETIME("${until}"), 
${-Math.abs(sinceGrainValue)}, ${sinceGrain})`;  // eslint-disable-line
+    return `${since} : ${until}`;
+  }
+
+  // relative : relative
+  const since = `DATEADD(DATETIME("${anchorValue}"), 
${-Math.abs(sinceGrainValue)}, ${sinceGrain})`;  // eslint-disable-line
+  const until = `DATEADD(DATETIME("${anchorValue}"), ${untilGrainValue}, 
${untilGrain})`;
+  return `${since} : ${until}`;
+};
+
+const guessTimeRangeFrame = (timeRange: string): TimeRangeFrameType => {
+  if (COMMON_RANGE_OPTIONS.map(_ => _.value).indexOf(timeRange) > -1) {

Review comment:
       I forgot that ES6 has the SET data structure already




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to