-
Notifications
You must be signed in to change notification settings - Fork 966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/data importer #9408
Feature/data importer #9408
Changes from 3 commits
501ce52
b62e78d
22a3644
2fd4543
86d92c6
445156e
44db71a
77bd9b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,11 @@ | |
EuiPageContent, | ||
EuiPageContentHeader, | ||
EuiPageHeader, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiTitle, | ||
EuiPageSideBar, | ||
EuiBasicTable, | ||
EuiLoadingSpinner, | ||
EuiFieldText, | ||
EuiSpacer, | ||
} from '@elastic/eui'; | ||
|
@@ -40,13 +43,10 @@ | |
import { PublicConfigSchema } from '../../config'; | ||
import { ImportTextContentBody } from './import_text_content'; | ||
import { ImportFileContentBody } from './import_file_content'; | ||
import { | ||
CSV_FILE_TYPE, | ||
CSV_SUPPORTED_DELIMITERS, | ||
PLUGIN_NAME_AS_TITLE, | ||
} from '../../common/constants'; | ||
import { CSV_FILE_TYPE, CSV_SUPPORTED_DELIMITERS } from '../../common/constants'; | ||
import { DelimiterSelect } from './delimiter_select'; | ||
import { previewFile } from '../lib/preview'; | ||
import { PreviewComponent } from './preview_table'; | ||
|
||
interface DataImporterPluginAppProps { | ||
basename: string; | ||
|
@@ -69,21 +69,27 @@ | |
dataSourceEnabled, | ||
dataSourceManagement, | ||
}: DataImporterPluginAppProps) => { | ||
const DataSourceMenu = dataSourceManagement?.ui.getDataSourceMenu<DataSourceSelectableConfig>(); | ||
const DataSourceMenuComponent = dataSourceManagement?.ui.getDataSourceMenu< | ||
DataSourceSelectableConfig | ||
>(); | ||
const [indexName, setIndexName] = useState<string>(); | ||
const [importType, setImportType] = useState<ImportChoices>(IMPORT_CHOICE_FILE); | ||
const [disableImport, setDisableImport] = useState<boolean>(); | ||
const [dataType, setDataType] = useState<string | undefined>( | ||
config.enabledFileTypes.length > 0 ? config.enabledFileTypes[0] : undefined | ||
); | ||
const [filePreviewData, setFilePreviewData] = useState<any[]>([]); | ||
const [inputText, setText] = useState<string | undefined>(); | ||
const [inputFile, setInputFile] = useState<File | undefined>(); | ||
const [dataSourceId, setDataSourceId] = useState<string | undefined>(); | ||
const [selectedDataSource, setSelectedDataSource] = useState<DataSourceOption | undefined>(); | ||
const [filePreviewColumns, setFilePreviewColumns] = useState<any[]>([]); | ||
const [isLoadingPreview, setIsLoadingPreview] = useState<boolean>(false); | ||
const [showDelimiterChoice, setShowDelimiterChoice] = useState<boolean>(shouldShowDelimiter()); | ||
const [delimiter, setDelimiter] = useState<string | undefined>( | ||
dataType === CSV_FILE_TYPE ? CSV_SUPPORTED_DELIMITERS[0] : undefined | ||
); | ||
const [visibleRows, setVisibleRows] = useState<number>(10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are using |
||
|
||
const onImportTypeChange = (type: ImportChoices) => { | ||
if (type === IMPORT_CHOICE_FILE) { | ||
|
@@ -107,8 +113,32 @@ | |
|
||
const onFileInput = (file?: File) => { | ||
setInputFile(file); | ||
if (file) { | ||
setIsLoadingPreview(true); | ||
const reader = new FileReader(); | ||
reader.onload = (event) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we avoid reading the entire file into memory? This can be problematic for larger files. I added in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
const text = event.target?.result as string; | ||
const rows = text.split('\n').map((row) => row.split(',')); | ||
const columns = rows[0].map((header, index) => ({ | ||
field: `column_${index}`, | ||
name: header, | ||
})); | ||
const data = rows.slice(1, 11).map((row) => | ||
row.reduce((acc: { [key: string]: string }, value, index) => { | ||
acc[`column_${index}`] = value; | ||
return acc; | ||
}, {}) | ||
); | ||
setFilePreviewColumns(columns); | ||
setFilePreviewData(data); | ||
setIsLoadingPreview(false); | ||
}; | ||
reader.readAsText(file); | ||
} else { | ||
setFilePreviewColumns([]); | ||
setFilePreviewData([]); | ||
} | ||
}; | ||
|
||
const onTextInput = (text: string) => { | ||
setText(text); | ||
}; | ||
|
@@ -132,7 +162,7 @@ | |
http, | ||
inputFile, | ||
// TODO This should be determined from the index name textbox/selectable | ||
false, | ||
true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a reason this is set to |
||
// TODO This should be determined from the file type selectable | ||
fileExtension, | ||
indexName!, | ||
|
@@ -241,18 +271,24 @@ | |
const renderDataSourceComponent = useMemo(() => { | ||
return ( | ||
<div> | ||
<DataSourceMenu | ||
dataSourceManagement={dataSourceManagement} | ||
componentType={'DataSourceSelectable'} | ||
componentConfig={{ | ||
fullWidth: true, | ||
savedObjects: savedObjects.client, | ||
notifications, | ||
onSelectedDataSources: onDataSourceSelect, | ||
selectedOption: selectedDataSource, | ||
}} | ||
/> | ||
<EuiSpacer size="m" /> | ||
{DataSourceMenuComponent && ( | ||
<> | ||
<DataSourceMenuComponent | ||
componentType={'DataSourceSelectable'} | ||
componentConfig={{ | ||
fullWidth: true, | ||
savedObjects: savedObjects.client, | ||
notifications, | ||
onSelectedDataSources: onDataSourceSelect, | ||
onManageDataSource: () => {}, // Add a proper handler if needed | ||
}} | ||
onManageDataSource={function (): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this? The |
||
throw new Error('Function not implemented.'); | ||
}} | ||
/> | ||
<EuiSpacer size="m" /> | ||
</> | ||
)} | ||
</div> | ||
); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
|
@@ -281,87 +317,86 @@ | |
); | ||
} | ||
|
||
const loadMoreRows = () => { | ||
setVisibleRows((prevVisibleRows) => prevVisibleRows + 10); | ||
}; | ||
|
||
return ( | ||
<Router basename={basename}> | ||
<I18nProvider> | ||
<> | ||
<navigation.ui.TopNavMenu appName={PLUGIN_ID} useDefaultBehaviors={true} /> | ||
<EuiPage> | ||
<EuiPageSideBar> | ||
<ImportTypeSelector | ||
updateSelection={onImportTypeChange} | ||
initialSelection={importType} | ||
/> | ||
{showDelimiterChoice && ( | ||
<DelimiterSelect | ||
onDelimiterChange={onDelimiterChange} | ||
initialDelimiter={delimiter} | ||
/> | ||
)} | ||
<EuiTitle size="xs"> | ||
<span> | ||
{i18n.translate('dataImporter.dataSource', { | ||
defaultMessage: 'Data Source Options', | ||
})} | ||
</span> | ||
</EuiTitle> | ||
<EuiFieldText placeholder="Index name" onChange={onIndexNameChange} /> | ||
<EuiSpacer size="m" /> | ||
{dataSourceEnabled && renderDataSourceComponent} | ||
<EuiButton fullWidth={true} isDisabled={disableImport} onClick={importData}> | ||
Import | ||
</EuiButton> | ||
<EuiSpacer size="m" /> | ||
<EuiButton fullWidth={true} isDisabled={disableImport} onClick={previewData}> | ||
Preview | ||
</EuiButton> | ||
</EuiPageSideBar> | ||
<EuiPageBody component="main"> | ||
<EuiPageHeader> | ||
<EuiTitle size="l"> | ||
<h1> | ||
<FormattedMessage | ||
id="dataImporter.mainTitle" | ||
defaultMessage="{title}" | ||
values={{ title: PLUGIN_NAME_AS_TITLE }} | ||
/> | ||
<FormattedMessage id="dataImporter.mainTitle" defaultMessage="Data Importer" /> | ||
</h1> | ||
</EuiTitle> | ||
</EuiPageHeader> | ||
<EuiPageContent> | ||
<EuiPageContentHeader> | ||
<EuiTitle> | ||
<h2> | ||
{importType === IMPORT_CHOICE_TEXT && ( | ||
<FormattedMessage | ||
id="dataImporter.textTitle" | ||
defaultMessage="Import Data" | ||
/> | ||
)} | ||
{importType === IMPORT_CHOICE_FILE && ( | ||
<FormattedMessage | ||
id="dataImporter.fileTitle" | ||
defaultMessage="Import Data from File" | ||
/> | ||
)} | ||
</h2> | ||
</EuiTitle> | ||
</EuiPageContentHeader> | ||
{importType === IMPORT_CHOICE_TEXT && ( | ||
<ImportTextContentBody | ||
onTextChange={onTextInput} | ||
enabledFileTypes={config.enabledFileTypes} | ||
initialFileType={dataType!} | ||
characterLimit={config.maxTextCount} | ||
onFileTypeChange={onDataTypeChange} | ||
/> | ||
)} | ||
{importType === IMPORT_CHOICE_FILE && ( | ||
<ImportFileContentBody | ||
enabledFileTypes={config.enabledFileTypes} | ||
onFileUpdate={onFileInput} | ||
/> | ||
)} | ||
<EuiFlexGroup> | ||
<EuiFlexItem grow={1}> | ||
<ImportTypeSelector | ||
updateSelection={onImportTypeChange} | ||
initialSelection={importType} | ||
/> | ||
{showDelimiterChoice && ( | ||
<DelimiterSelect | ||
onDelimiterChange={onDelimiterChange} | ||
initialDelimiter={delimiter} | ||
/> | ||
)} | ||
<EuiTitle size="xs"> | ||
<span> | ||
{i18n.translate('dataImporter.dataSource', { | ||
defaultMessage: 'Data Source Options', | ||
})} | ||
</span> | ||
</EuiTitle> | ||
<EuiFieldText placeholder="Index name" onChange={onIndexNameChange} /> | ||
<EuiSpacer size="m" /> | ||
{dataSourceEnabled && renderDataSourceComponent} | ||
{importType === IMPORT_CHOICE_FILE && ( | ||
<ImportFileContentBody | ||
enabledFileTypes={config.enabledFileTypes} | ||
onFileUpdate={onFileInput} | ||
/> | ||
)} | ||
<EuiButton fullWidth={true} isDisabled={disableImport} onClick={previewData}> | ||
Preview | ||
</EuiButton> | ||
<EuiSpacer size="m" /> | ||
<EuiButton fullWidth={true} isDisabled={disableImport} onClick={importData}> | ||
Import | ||
</EuiButton> | ||
</EuiFlexItem> | ||
<EuiFlexItem grow={2}> | ||
{importType === IMPORT_CHOICE_TEXT && ( | ||
<ImportTextContentBody | ||
onTextChange={onTextInput} | ||
enabledFileTypes={config.enabledFileTypes} | ||
initialFileType={dataType!} | ||
characterLimit={config.maxTextCount} | ||
onFileTypeChange={onDataTypeChange} | ||
/> | ||
)} | ||
{importType === IMPORT_CHOICE_FILE && ( | ||
<div> | ||
{isLoadingPreview ? ( | ||
<EuiLoadingSpinner size="xl" /> | ||
) : ( | ||
<PreviewComponent | ||
previewData={filePreviewData} | ||
visibleRows={visibleRows} | ||
loadMoreRows={loadMoreRows} | ||
/> | ||
)} | ||
</div> | ||
)} | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
</EuiPageContent> | ||
</EuiPageBody> | ||
</EuiPage> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// stylelint-disable-next-line @osd/stylelint/no_modifying_global_selectors | ||
.customSearchBar .euiFormControlLayoutIcons { | ||
height: 20px; | ||
display: flex; | ||
align-items: center; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd recommend not source controlling this config file. historically, people have created PRs with passwords or IPs that they had to clean up later.
You can add a new package.json script or you can start OSD like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, I usually use git skip to locally ignore config file changes. But TIL you can add configs on the fly