UNPKG

@superset-ui/core

Version:
74 lines 3.46 kB
/** * 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 rison from 'rison'; import { isEmpty } from 'lodash'; import { SupersetClient, getClientErrorObject, ensureIsArray, } from '@superset-ui/core'; export const SEPARATOR = ' : '; export const buildTimeRangeString = (since, until) => `${since}${SEPARATOR}${until}`; const formatDateEndpoint = (dttm, isStart) => dttm.replace('T00:00:00', '') || (isStart ? '-∞' : '∞'); export const formatTimeRange = (timeRange, columnPlaceholder = 'col') => { const splitDateRange = timeRange.split(SEPARATOR); if (splitDateRange.length === 1) return timeRange; return `${formatDateEndpoint(splitDateRange[0], true)}${columnPlaceholder} < ${formatDateEndpoint(splitDateRange[1])}`; }; export const formatTimeRangeComparison = (initialTimeRange, shiftedTimeRange, columnPlaceholder = 'col') => { const splitInitialDateRange = initialTimeRange.split(SEPARATOR); const splitShiftedDateRange = shiftedTimeRange.split(SEPARATOR); return `${columnPlaceholder}: ${formatDateEndpoint(splitInitialDateRange[0], true)} to ${formatDateEndpoint(splitInitialDateRange[1])} vs ${formatDateEndpoint(splitShiftedDateRange[0], true)} to ${formatDateEndpoint(splitShiftedDateRange[1])}`; }; export const fetchTimeRange = async (timeRange, columnPlaceholder = 'col', shifts) => { let query; let endpoint; if (!isEmpty(shifts)) { const timeRanges = ensureIsArray(shifts).map(shift => ({ timeRange, shift, })); query = rison.encode_uri([{ timeRange }, ...timeRanges]); endpoint = `/api/v1/time_range/?q=${query}`; } else { query = rison.encode_uri(timeRange); endpoint = `/api/v1/time_range/?q=${query}`; } try { const response = await SupersetClient.get({ endpoint }); if (isEmpty(shifts)) { const timeRangeString = buildTimeRangeString(response?.json?.result[0]?.since || '', response?.json?.result[0]?.until || ''); return { value: formatTimeRange(timeRangeString, columnPlaceholder), }; } const timeRanges = response?.json?.result.map((result) => buildTimeRangeString(result.since, result.until)); return { value: timeRanges .slice(1) .map((timeRange) => formatTimeRangeComparison(timeRanges[0], timeRange, columnPlaceholder)), }; } catch (response) { const clientError = await getClientErrorObject(response); return { error: clientError.message || clientError.error || response.statusText, }; } }; //# sourceMappingURL=fetchTimeRange.js.map