UNPKG

@superset-ui/core

Version:
113 lines (106 loc) 3.89 kB
import _isEmpty from "lodash/isEmpty"; /** * 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 { 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 {var _response$json3; const response = await SupersetClient.get({ endpoint }); if (_isEmpty(shifts)) {var _response$json, _response$json$result, _response$json2, _response$json2$resul; const timeRangeString = buildTimeRangeString( (response == null ? void 0 : (_response$json = response.json) == null ? void 0 : (_response$json$result = _response$json.result[0]) == null ? void 0 : _response$json$result.since) || '', (response == null ? void 0 : (_response$json2 = response.json) == null ? void 0 : (_response$json2$resul = _response$json2.result[0]) == null ? void 0 : _response$json2$resul.until) || '' ); return { value: formatTimeRange(timeRangeString, columnPlaceholder) }; } const timeRanges = response == null ? void 0 : (_response$json3 = response.json) == null ? void 0 : _response$json3.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