-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move fetchTimeRange to core package (#27852)
- Loading branch information
1 parent
afe7eaf
commit 1804edc
Showing
71 changed files
with
619 additions
and
425 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
superset-frontend/packages/superset-ui-core/src/time-comparison/fetchTimeRange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* 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 } from '@superset-ui/core'; | ||
|
||
export const SEPARATOR = ' : '; | ||
|
||
export const buildTimeRangeString = (since: string, until: string): string => | ||
`${since}${SEPARATOR}${until}`; | ||
|
||
const formatDateEndpoint = (dttm: string, isStart?: boolean): string => | ||
dttm.replace('T00:00:00', '') || (isStart ? '-∞' : '∞'); | ||
|
||
export const formatTimeRange = ( | ||
timeRange: string, | ||
columnPlaceholder = 'col', | ||
) => { | ||
const splitDateRange = timeRange.split(SEPARATOR); | ||
if (splitDateRange.length === 1) return timeRange; | ||
return `${formatDateEndpoint( | ||
splitDateRange[0], | ||
true, | ||
)} ≤ ${columnPlaceholder} < ${formatDateEndpoint(splitDateRange[1])}`; | ||
}; | ||
|
||
export const fetchTimeRange = async ( | ||
timeRange: string, | ||
columnPlaceholder = 'col', | ||
) => { | ||
const query = rison.encode_uri(timeRange); | ||
const endpoint = `/api/v1/time_range/?q=${query}`; | ||
try { | ||
const response = await SupersetClient.get({ endpoint }); | ||
const timeRangeString = buildTimeRangeString( | ||
response?.json?.result[0]?.since || '', | ||
response?.json?.result[0]?.until || '', | ||
); | ||
return { | ||
value: formatTimeRange(timeRangeString, columnPlaceholder), | ||
}; | ||
} catch (response) { | ||
const clientError = await getClientErrorObject(response); | ||
return { | ||
error: clientError.message || clientError.error || response.statusText, | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.