-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sergey Fedoseev <[email protected]>
- Loading branch information
1 parent
cfa4a4a
commit 53fb1bb
Showing
22 changed files
with
28,916 additions
and
18,342 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { default as Display, bind as display } from './Display' | ||
export { default as render } from './render' | ||
export { default as load, getRenderProps } from './load' | ||
export { PreviewData, PreviewError } from './types' | ||
export { PreviewData, PreviewError, CONTEXT } from './types' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import * as R from 'ramda' | ||
import * as React from 'react' | ||
|
||
import { HTTPError } from 'utils/APIConnector' | ||
import * as AWS from 'utils/AWS' | ||
import * as Config from 'utils/Config' | ||
import * as Data from 'utils/Data' | ||
import mkSearch from 'utils/mkSearch' | ||
import type { S3HandleBase } from 'utils/s3paths' | ||
|
||
import { CONTEXT, PreviewData } from '../types' | ||
|
||
import * as Csv from './Csv' | ||
import * as Excel from './Excel' | ||
import * as Parquet from './Parquet' | ||
import * as utils from './utils' | ||
|
||
const isJsonl = R.pipe(utils.stripCompression, utils.extIs('.jsonl')) | ||
|
||
export const detect = R.anyPass([Csv.detect, Excel.detect, Parquet.detect, isJsonl]) | ||
|
||
type TabularType = 'csv' | 'jsonl' | 'excel' | 'parquet' | 'tsv' | 'txt' | ||
|
||
const detectTabularType: (type: string) => TabularType = R.cond([ | ||
[Csv.isCsv, R.always('csv')], | ||
[Csv.isTsv, R.always('tsv')], | ||
[Excel.detect, R.always('excel')], | ||
[Parquet.detect, R.always('parquet')], | ||
[isJsonl, R.always('jsonl')], | ||
[R.T, R.always('txt')], | ||
]) | ||
|
||
function getQuiltInfo(headers: Headers): { truncated: boolean } | null { | ||
try { | ||
const header = headers.get('x-quilt-info') | ||
return header ? JSON.parse(header) : null | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error) | ||
return null | ||
} | ||
} | ||
|
||
function getContentLength(headers: Headers): number | null { | ||
try { | ||
const header = headers.get('content-length') | ||
return header ? Number(header) : null | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error) | ||
return null | ||
} | ||
} | ||
|
||
async function getCsvFromResponse(r: Response): Promise<ArrayBuffer | string> { | ||
const isArrow = r.headers.get('content-type') === 'application/vnd.apache.arrow.file' | ||
return isArrow ? r.arrayBuffer() : r.text() | ||
} | ||
|
||
interface LoadTabularDataArgs { | ||
compression?: 'gz' | 'bz2' | ||
endpoint: string | ||
handle: S3HandleBase | ||
sign: (h: S3HandleBase) => string | ||
type: TabularType | ||
size: 'small' | 'medium' | 'large' | ||
} | ||
|
||
interface TabularDataOutput { | ||
csv: ArrayBuffer | string | ||
size: number | null | ||
truncated: boolean | ||
} | ||
|
||
const loadTabularData = async ({ | ||
compression, | ||
endpoint, | ||
size, | ||
handle, | ||
sign, | ||
type, | ||
}: LoadTabularDataArgs): Promise<TabularDataOutput> => { | ||
const url = sign(handle) | ||
const r = await fetch( | ||
`${endpoint}/tabular-preview${mkSearch({ | ||
compression, | ||
input: type, | ||
size, | ||
url, | ||
})}`, | ||
) | ||
try { | ||
if (r.status >= 400) { | ||
throw new HTTPError(r) | ||
} | ||
|
||
const csv = await getCsvFromResponse(r) | ||
|
||
const quiltInfo = getQuiltInfo(r.headers) | ||
const contentLength = getContentLength(r.headers) | ||
|
||
return { | ||
csv, | ||
size: contentLength, | ||
truncated: !!quiltInfo?.truncated, | ||
} | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Error loading tabular preview', e) | ||
// eslint-disable-next-line no-console | ||
console.error(e) | ||
throw e | ||
} | ||
} | ||
|
||
function getNeededSize(context: string, gated: boolean) { | ||
switch (context) { | ||
case CONTEXT.FILE: | ||
return gated ? 'medium' : 'large' | ||
case CONTEXT.LISTING: | ||
return gated ? 'small' : 'large' | ||
// no default | ||
} | ||
} | ||
|
||
interface TabularLoaderProps { | ||
children: (result: $TSFixMe) => React.ReactNode | ||
handle: S3HandleBase | ||
options: { context: string } // TODO: restrict type | ||
} | ||
|
||
export const Loader = function TabularLoader({ | ||
handle, | ||
children, | ||
options, | ||
}: TabularLoaderProps) { | ||
const [gated, setGated] = React.useState(true) | ||
const endpoint = Config.use().binaryApiGatewayEndpoint | ||
const sign = AWS.Signer.useS3Signer() | ||
const type = React.useMemo(() => detectTabularType(handle.key), [handle.key]) | ||
const onLoadMore = React.useCallback(() => setGated(false), [setGated]) | ||
const size = React.useMemo( | ||
() => getNeededSize(options.context, gated), | ||
[options.context, gated], | ||
) | ||
const compression = utils.getCompression(handle.key) | ||
const data = Data.use(loadTabularData, { | ||
compression, | ||
endpoint, | ||
size, | ||
handle, | ||
sign, | ||
type, | ||
}) | ||
// TODO: get correct sises from API | ||
const processed = utils.useProcessing( | ||
data.result, | ||
({ csv, truncated }: TabularDataOutput) => | ||
PreviewData.Perspective({ | ||
context: options.context, | ||
data: csv, | ||
handle, | ||
onLoadMore: truncated && size !== 'large' ? onLoadMore : null, | ||
truncated, | ||
}), | ||
) | ||
return children(utils.useErrorHandling(processed, { handle, retry: data.fetch })) | ||
} |
103 changes: 103 additions & 0 deletions
103
catalog/app/components/Preview/renderers/Perspective.tsx
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,103 @@ | ||
import cx from 'classnames' | ||
import * as React from 'react' | ||
import * as M from '@material-ui/core' | ||
|
||
import * as perspective from 'utils/perspective' | ||
import type { S3HandleBase } from 'utils/s3paths' | ||
|
||
import { CONTEXT } from '../types' | ||
|
||
const useTruncatedWarningStyles = M.makeStyles((t) => ({ | ||
root: { | ||
alignItems: 'center', | ||
display: 'flex', | ||
}, | ||
message: { | ||
color: t.palette.text.secondary, | ||
marginRight: t.spacing(2), | ||
}, | ||
icon: { | ||
display: 'inline-block', | ||
fontSize: '1.25rem', | ||
marginRight: t.spacing(0.5), | ||
verticalAlign: '-5px', | ||
}, | ||
})) | ||
|
||
interface TruncatedWarningProps { | ||
className: string | ||
onLoadMore: () => void | ||
} | ||
|
||
function TruncatedWarning({ className, onLoadMore }: TruncatedWarningProps) { | ||
const classes = useTruncatedWarningStyles() | ||
return ( | ||
<div className={cx(classes.root, className)}> | ||
<span className={classes.message}> | ||
<M.Icon fontSize="small" color="inherit" className={classes.icon}> | ||
info_outlined | ||
</M.Icon> | ||
Partial preview | ||
</span> | ||
|
||
{!!onLoadMore && ( | ||
<M.Button startIcon={<M.Icon>refresh</M.Icon>} size="small" onClick={onLoadMore}> | ||
Load more | ||
</M.Button> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
const useStyles = M.makeStyles((t) => ({ | ||
root: { | ||
width: '100%', | ||
}, | ||
viewer: { | ||
height: ({ context }: { context: 'file' | 'listing' }) => | ||
context === CONTEXT.LISTING ? t.spacing(30) : t.spacing(50), | ||
overflow: 'auto', | ||
resize: 'vertical', | ||
}, | ||
warning: { | ||
marginBottom: t.spacing(1), | ||
}, | ||
})) | ||
|
||
interface PerspectiveProps extends React.HTMLAttributes<HTMLDivElement> { | ||
context: 'file' | 'listing' | ||
data: string | ArrayBuffer | ||
handle: S3HandleBase | ||
onLoadMore: () => void | ||
truncated: boolean | ||
} | ||
|
||
function Perspective({ | ||
children, | ||
className, | ||
context, | ||
data, | ||
handle, | ||
onLoadMore, | ||
truncated, | ||
...props | ||
}: PerspectiveProps) { | ||
const classes = useStyles({ context }) | ||
|
||
const [root, setRoot] = React.useState<HTMLDivElement | null>(null) | ||
|
||
const attrs = React.useMemo(() => ({ className: classes.viewer }), [classes]) | ||
perspective.use(root, data, attrs) | ||
|
||
return ( | ||
<div className={cx(className, classes.root)} ref={setRoot} {...props}> | ||
{truncated && ( | ||
<TruncatedWarning className={classes.warning} onLoadMore={onLoadMore} /> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default (data: PerspectiveProps, props: PerspectiveProps) => ( | ||
<Perspective {...data} {...props} /> | ||
) |
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.