Skip to content

Commit 9c69910

Browse files
[ui-storagebrowser] adds and reads configs from .ini files (#3922)
* [ui-storagebrowser] adds and reads configs from .ini files * removes show download button from global constants * moves the storage browser config to separate group * adds storage_browser key in config
1 parent fe17f6a commit 9c69910

File tree

13 files changed

+97
-49
lines changed

13 files changed

+97
-49
lines changed

desktop/core/src/desktop/api2.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@
7878
uuid_default,
7979
)
8080
from desktop.views import get_banner_message, serve_403_error
81-
from filebrowser.conf import RESTRICT_FILE_EXTENSIONS
81+
from filebrowser.conf import CONCURRENT_MAX_CONNECTIONS, FILE_UPLOAD_CHUNK_SIZE, RESTRICT_FILE_EXTENSIONS, SHOW_DOWNLOAD_BUTTON
8282
from filebrowser.tasks import check_disk_usage_and_clean_task, document_cleanup_task
83+
from filebrowser.views import MAX_FILEEDITOR_SIZE
8384
from hadoop.cluster import is_yarn
8485
from metadata.catalog_api import (
8586
_highlight,
@@ -124,10 +125,14 @@ def get_config(request):
124125
config = get_cluster_config(request.user)
125126
config['hue_config']['is_admin'] = is_admin(request.user)
126127
config['hue_config']['is_yarn_enabled'] = is_yarn()
127-
config['hue_config']['enable_new_storage_browser'] = ENABLE_NEW_STORAGE_BROWSER.get()
128-
config['hue_config']['enable_chunked_file_uploader'] = ENABLE_CHUNKED_FILE_UPLOADER.get()
129128
config['hue_config']['enable_task_server'] = TASK_SERVER_V2.ENABLED.get()
130-
config['hue_config']['restrict_file_extensions'] = RESTRICT_FILE_EXTENSIONS.get()
129+
config['storage_browser']['enable_chunked_file_upload'] = ENABLE_CHUNKED_FILE_UPLOADER.get()
130+
config['storage_browser']['enable_new_storage_browser'] = ENABLE_NEW_STORAGE_BROWSER.get()
131+
config['storage_browser']['restrict_file_extensions'] = RESTRICT_FILE_EXTENSIONS.get()
132+
config['storage_browser']['concurrent_max_connection'] = CONCURRENT_MAX_CONNECTIONS.get()
133+
config['storage_browser']['file_upload_chunk_size'] = FILE_UPLOAD_CHUNK_SIZE.get()
134+
config['storage_browser']['enable_file_download_button'] = SHOW_DOWNLOAD_BUTTON.get()
135+
config['storage_browser']['max_file_editor_size'] = MAX_FILEEDITOR_SIZE
131136
config['clusters'] = list(get_clusters(request.user).values())
132137
config['documents'] = {
133138
'types': list(Document2.objects.documents(user=request.user).order_by().values_list('type', flat=True).distinct())

desktop/core/src/desktop/js/apps/storageBrowser/StorageFilePage/StorageFilePage.test.tsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import '@testing-library/jest-dom';
2222
import userEvent from '@testing-library/user-event';
2323
import { DOWNLOAD_API_URL } from '../../../reactComponents/FileChooser/api';
2424
import huePubSub from '../../../utils/huePubSub';
25-
import { hueWindow } from '../../../types/types';
2625

2726
jest.mock('../../../utils/dateTimeUtils', () => ({
2827
...jest.requireActual('../../../utils/dateTimeUtils'),
@@ -50,6 +49,16 @@ jest.mock('../../../utils/hooks/useLoadData', () => {
5049
}));
5150
});
5251

52+
const mockConfig = {
53+
storage_browser: {
54+
enable_file_download_button: true,
55+
max_file_editor_size: 1000000000
56+
}
57+
};
58+
jest.mock('../../../config/hueConfig', () => ({
59+
getLastKnownConfig: () => mockConfig
60+
}));
61+
5362
const mockFileStats: FileStats = {
5463
path: '/path/to/file.txt',
5564
size: 123456,
@@ -67,19 +76,6 @@ const mockFileName = 'file.txt';
6776
const mockReload = jest.fn();
6877

6978
describe('StorageFilePage', () => {
70-
let oldShowDownloadButton: boolean;
71-
let oldMaxFileEditorSize: number;
72-
beforeEach(() => {
73-
oldShowDownloadButton = (window as hueWindow).SHOW_DOWNLOAD_BUTTON as boolean;
74-
oldMaxFileEditorSize = (window as hueWindow).MAX_FILEEDITOR_SIZE as number;
75-
(window as hueWindow).SHOW_DOWNLOAD_BUTTON = true;
76-
(window as hueWindow).MAX_FILEEDITOR_SIZE = 1000000000;
77-
});
78-
afterAll(() => {
79-
(window as hueWindow).SHOW_DOWNLOAD_BUTTON = oldShowDownloadButton;
80-
(window as hueWindow).MAX_FILEEDITOR_SIZE = oldMaxFileEditorSize;
81-
});
82-
8379
it('should render file metadata and content', () => {
8480
render(
8581
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />
@@ -224,7 +220,7 @@ describe('StorageFilePage', () => {
224220
});
225221

226222
it('should not render the download button when show_download_button is false', () => {
227-
(window as hueWindow).SHOW_DOWNLOAD_BUTTON = false;
223+
mockConfig.storage_browser.enable_file_download_button = false;
228224

229225
render(
230226
<StorageFilePage fileName={mockFileName} fileStats={mockFileStats} onReload={mockReload} />

desktop/core/src/desktop/js/apps/storageBrowser/StorageFilePage/StorageFilePage.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// limitations under the License.
1616

1717
import React, { useMemo, useState } from 'react';
18-
import { hueWindow } from 'types/types';
1918
import {
2019
BrowserViewType,
2120
FilePreview,
@@ -39,6 +38,7 @@ import {
3938
} from '../../../utils/constants/storageBrowser';
4039
import { Spin } from 'antd';
4140
import useLoadData from '../../../utils/hooks/useLoadData';
41+
import { getLastKnownConfig } from '../../../config/hueConfig';
4242

4343
interface StorageFilePageProps {
4444
onReload: () => void;
@@ -47,7 +47,9 @@ interface StorageFilePageProps {
4747
}
4848

4949
const StorageFilePage = ({ fileName, fileStats, onReload }: StorageFilePageProps): JSX.Element => {
50+
const config = getLastKnownConfig();
5051
const { t } = i18nReact.useTranslation();
52+
5153
const [isEditing, setIsEditing] = useState<boolean>(false);
5254
const [fileContent, setFileContent] = useState<FilePreview['contents']>();
5355

@@ -114,7 +116,8 @@ const StorageFilePage = ({ fileName, fileStats, onReload }: StorageFilePageProps
114116

115117
const isEditingEnabled =
116118
!isEditing &&
117-
(window as hueWindow).MAX_FILEEDITOR_SIZE > fileStats.size &&
119+
config?.storage_browser.max_file_editor_size &&
120+
config?.storage_browser.max_file_editor_size > fileStats.size &&
118121
EDITABLE_FILE_FORMATS[fileType] &&
119122
fileData?.compression?.toLocaleLowerCase() === 'none';
120123

@@ -167,7 +170,7 @@ const StorageFilePage = ({ fileName, fileStats, onReload }: StorageFilePageProps
167170
</Button>
168171
</>
169172
)}
170-
{(window as hueWindow).SHOW_DOWNLOAD_BUTTON && (
173+
{config?.storage_browser.enable_file_download_button && (
171174
<a href={`${DOWNLOAD_API_URL}${fileStats.path}`}>
172175
<PrimaryButton
173176
data-testid="preview--download--button"

desktop/core/src/desktop/js/config/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ export enum AppType {
6060
sdkapps = 'sdkapps'
6161
}
6262

63+
interface StorageBrowserConfig {
64+
concurrent_max_connection: number;
65+
enable_chunked_file_uploader: boolean;
66+
enable_file_download_button: boolean;
67+
enable_new_storage_browser: boolean;
68+
file_upload_chunk_size: number;
69+
max_file_editor_size: number;
70+
}
71+
6372
export interface HueConfig extends GenericApiResponse {
6473
app_config: {
6574
[AppType.browser]?: AppConfig<BrowserInterpreter>;
@@ -83,7 +92,11 @@ export interface HueConfig extends GenericApiResponse {
8392
hue_config: {
8493
enable_sharing: boolean;
8594
collect_usage: boolean;
95+
enable_task_server: boolean;
96+
is_admin: boolean;
97+
is_yarn_enabled: boolean;
8698
};
99+
storage_browser: StorageBrowserConfig;
87100
hue_version?: string;
88101
img_version?: string;
89102
vw_name?: string;

desktop/core/src/desktop/js/reactComponents/FileUploadQueue/FileUploadQueue.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ import StatusStoppedIcon from '@cloudera/cuix-core/icons/react/StatusStoppedIcon
2727
import StatusErrorIcon from '@cloudera/cuix-core/icons/react/StatusErrorIcon';
2828
import { UploadItem } from '../../utils/hooks/useFileUpload/util';
2929
import useFileUpload from '../../utils/hooks/useFileUpload/useFileUpload';
30-
import { FileUploadStatus } from '../../utils/constants/storageBrowser';
30+
import {
31+
DEFAULT_ENABLE_CHUNK_UPLOAD,
32+
FileUploadStatus
33+
} from '../../utils/constants/storageBrowser';
34+
import { getLastKnownConfig } from '../../config/hueConfig';
3135

3236
interface FileUploadQueueProps {
3337
filesQueue: UploadItem[];
@@ -47,11 +51,14 @@ const sortOrder = [
4751
}, {});
4852

4953
const FileUploadQueue: React.FC<FileUploadQueueProps> = ({ filesQueue, onClose, onComplete }) => {
54+
const config = getLastKnownConfig();
55+
const isChunkUpload =
56+
config?.storage_browser.enable_chunked_file_uploader ?? DEFAULT_ENABLE_CHUNK_UPLOAD;
5057
const { t } = i18nReact.useTranslation();
5158
const [isExpanded, setIsExpanded] = useState(true);
5259

5360
const { uploadQueue, onCancel } = useFileUpload(filesQueue, {
54-
isChunkUpload: true,
61+
isChunkUpload,
5562
onComplete
5663
});
5764

desktop/core/src/desktop/js/types/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ export interface hueWindow {
5959
HUE_LANG?: string;
6060
HUE_VERSION?: string;
6161
LOGGED_USERNAME?: string;
62-
MAX_FILEEDITOR_SIZE?: number;
63-
SHOW_DOWNLOAD_BUTTON?: boolean;
6462
SQL_ANALYZER_MODE?: string;
6563
USER_IS_ADMIN?: boolean;
6664
USER_IS_HUE_ADMIN?: boolean;

desktop/core/src/desktop/js/utils/constants/storageBrowser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
export const DEFAULT_PAGE_SIZE = 50;
1818
export const DEFAULT_CHUNK_SIZE = 5 * 1024 * 1024; // 5 MiB
19-
export const DEFAULT_CONCURRENT_UPLOAD = 3;
20-
export const DEFAULT_CONCURRENT_CHUNK_UPLOAD = 3;
19+
export const DEFAULT_CONCURRENT_MAX_CONNECTIONS = 3;
20+
export const DEFAULT_ENABLE_CHUNK_UPLOAD = false;
2121

2222
export enum SupportedFileTypes {
2323
IMAGE = 'image',

desktop/core/src/desktop/js/utils/hooks/useFileUpload/useChunkUpload.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
// limitations under the License.
1616

1717
import { useEffect, useState } from 'react';
18+
import { getLastKnownConfig } from '../../../config/hueConfig';
1819
import useSaveData from '../useSaveData';
1920
import useQueueProcessor from '../useQueueProcessor';
2021
import {
2122
DEFAULT_CHUNK_SIZE,
22-
DEFAULT_CONCURRENT_CHUNK_UPLOAD,
23+
DEFAULT_CONCURRENT_MAX_CONNECTIONS,
2324
FileUploadStatus
2425
} from '../../constants/storageBrowser';
2526
import useLoadData from '../useLoadData';
@@ -42,14 +43,18 @@ interface UseUploadQueueResponse {
4243
}
4344

4445
interface ChunkUploadOptions {
46+
concurrentProcess?: number;
4547
onStatusUpdate: (item: UploadItem, newStatus: FileUploadStatus) => void;
4648
onComplete: () => void;
4749
}
4850

4951
const useChunkUpload = ({
52+
concurrentProcess = DEFAULT_CONCURRENT_MAX_CONNECTIONS,
5053
onStatusUpdate,
5154
onComplete
5255
}: ChunkUploadOptions): UseUploadQueueResponse => {
56+
const config = getLastKnownConfig();
57+
const chunkSize = config?.storage_browser?.file_upload_chunk_size ?? DEFAULT_CHUNK_SIZE;
5358
const [pendingItems, setPendingItems] = useState<UploadItem[]>([]);
5459

5560
const { save } = useSaveData(undefined, {
@@ -99,30 +104,30 @@ const useChunkUpload = ({
99104
};
100105

101106
const onChunksUploadComplete = async (item: UploadItem) => {
102-
const { url, payload } = getChunksCompletePayload(item);
107+
const { url, payload } = getChunksCompletePayload(item, chunkSize);
103108
return save(payload, {
104109
url,
105110
onSuccess: () => addItemToPending(item)
106111
});
107112
};
108113

109114
const uploadChunk = async (chunkItem: UploadChunkItem) => {
110-
const { url, payload } = getChunkItemPayload(chunkItem);
115+
const { url, payload } = getChunkItemPayload(chunkItem, chunkSize);
111116
return save(payload, { url });
112117
};
113118

114119
const { enqueueAsync } = useQueueProcessor<UploadChunkItem>(uploadChunk, {
115-
concurrentProcess: DEFAULT_CONCURRENT_CHUNK_UPLOAD
120+
concurrentProcess
116121
});
117122

118123
const uploadItemInChunks = async (item: UploadItem) => {
119-
const chunks = createChunks(item, DEFAULT_CHUNK_SIZE);
124+
const chunks = createChunks(item, chunkSize);
120125
await enqueueAsync(chunks);
121126
return onChunksUploadComplete(item);
122127
};
123128

124129
const uploadItemInSingleChunk = (item: UploadItem) => {
125-
const { url, payload } = getChunkSinglePayload(item);
130+
const { url, payload } = getChunkSinglePayload(item, chunkSize);
126131
return save(payload, {
127132
url,
128133
onSuccess: () => addItemToPending(item)
@@ -131,7 +136,7 @@ const useChunkUpload = ({
131136

132137
const uploadItem = async (item: UploadItem) => {
133138
onStatusUpdate(item, FileUploadStatus.Uploading);
134-
const chunks = getTotalChunk(item.file.size, DEFAULT_CHUNK_SIZE);
139+
const chunks = getTotalChunk(item.file.size, chunkSize);
135140
if (chunks === 1) {
136141
return uploadItemInSingleChunk(item);
137142
}

desktop/core/src/desktop/js/utils/hooks/useFileUpload/useFileUpload.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import { useCallback, useEffect, useState } from 'react';
1818
import useRegularUpload from './useRegularUpload';
1919
import useChunkUpload from './useChunkUpload';
2020
import { getNewFileItems, UploadItem } from './util';
21-
import { FileUploadStatus } from '../../constants/storageBrowser';
21+
import {
22+
DEFAULT_CONCURRENT_MAX_CONNECTIONS,
23+
FileUploadStatus
24+
} from '../../constants/storageBrowser';
25+
import { getLastKnownConfig } from '../../../config/hueConfig';
2226

2327
interface UseUploadQueueResponse {
2428
uploadQueue: UploadItem[];
@@ -35,6 +39,10 @@ const useFileUpload = (
3539
filesQueue: UploadItem[],
3640
{ isChunkUpload = false, onComplete }: UploadQueueOptions
3741
): UseUploadQueueResponse => {
42+
const config = getLastKnownConfig();
43+
const concurrentProcess =
44+
config?.storage_browser.concurrent_max_connection ?? DEFAULT_CONCURRENT_MAX_CONNECTIONS;
45+
3846
const [uploadQueue, setUploadQueue] = useState<UploadItem[]>([]);
3947

4048
const onStatusUpdate = (item: UploadItem, newStatus: FileUploadStatus) =>
@@ -52,6 +60,7 @@ const useFileUpload = (
5260
removeFile: removeFromChunkUpload,
5361
isLoading: isChunkLoading
5462
} = useChunkUpload({
63+
concurrentProcess,
5564
onStatusUpdate,
5665
onComplete
5766
});
@@ -61,6 +70,7 @@ const useFileUpload = (
6170
removeFile: removeFromRegularUpload,
6271
isLoading: isNonChunkLoading
6372
} = useRegularUpload({
73+
concurrentProcess,
6474
onStatusUpdate,
6575
onComplete
6676
});

desktop/core/src/desktop/js/utils/hooks/useFileUpload/useRegularUpload.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
import useQueueProcessor from '../useQueueProcessor';
1818
import { UPLOAD_FILE_URL } from '../../../reactComponents/FileChooser/api';
19-
import { DEFAULT_CONCURRENT_UPLOAD, FileUploadStatus } from '../../constants/storageBrowser';
19+
import {
20+
DEFAULT_CONCURRENT_MAX_CONNECTIONS,
21+
FileUploadStatus
22+
} from '../../constants/storageBrowser';
2023
import useSaveData from '../useSaveData';
2124
import { UploadItem } from './util';
2225

@@ -27,11 +30,13 @@ interface UseUploadQueueResponse {
2730
}
2831

2932
interface UploadQueueOptions {
33+
concurrentProcess?: number;
3034
onStatusUpdate: (item: UploadItem, newStatus: FileUploadStatus) => void;
3135
onComplete: () => void;
3236
}
3337

3438
const useRegularUpload = ({
39+
concurrentProcess = DEFAULT_CONCURRENT_MAX_CONNECTIONS,
3540
onStatusUpdate,
3641
onComplete
3742
}: UploadQueueOptions): UseUploadQueueResponse => {
@@ -68,7 +73,7 @@ const useRegularUpload = ({
6873
dequeue: removeFile,
6974
isLoading
7075
} = useQueueProcessor<UploadItem>(processUploadItem, {
71-
concurrentProcess: DEFAULT_CONCURRENT_UPLOAD,
76+
concurrentProcess,
7277
onSuccess: onComplete
7378
});
7479

0 commit comments

Comments
 (0)