Change sequential requests to parallel. (#909)

Parallel get label.json files.

Atomic update tagCount.
This commit is contained in:
SimoTw 2021-03-30 14:59:28 +08:00 коммит произвёл GitHub
Родитель e445dccfcb
Коммит 99b589201d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 24 добавлений и 15 удалений

Просмотреть файл

@ -171,7 +171,7 @@ export default class LocalFileSystem implements IStorageProvider {
folderPath = [folderPath, folderName].join("/"); folderPath = [folderPath, folderName].join("/");
const result: IAsset[] = []; const result: IAsset[] = [];
const files = await this.listFiles(path.normalize(folderPath)); const files = await this.listFiles(path.normalize(folderPath));
for (const file of files) { await Promise.all(files.map(async (file) => {
const fileParts = file.split(/[\\\/]/); const fileParts = file.split(/[\\\/]/);
const fileName = fileParts[fileParts.length - 1]; const fileName = fileParts[fileParts.length - 1];
const asset = await AssetService.createAssetFromFilePath(file, folderName + "/" + fileName, true); const asset = await AssetService.createAssetFromFilePath(file, folderName + "/" + fileName, true);
@ -191,11 +191,9 @@ export default class LocalFileSystem implements IStorageProvider {
} else { } else {
asset.state = AssetState.NotVisited; asset.state = AssetState.NotVisited;
} }
result.push(asset); result.push(asset);
} }
} }));
return result; return result;
} }

Просмотреть файл

@ -205,7 +205,7 @@ export class AzureBlobStorage implements IStorageProvider {
public async getAssets(folderPath?: string, folderName?: string): Promise<IAsset[]> { public async getAssets(folderPath?: string, folderName?: string): Promise<IAsset[]> {
const files: string[] = await this.listFiles(folderPath); const files: string[] = await this.listFiles(folderPath);
const result: IAsset[] = []; const result: IAsset[] = [];
for (const file of files) { await Promise.all(files.map(async (file) => {
const url = this.getUrl(file); const url = this.getUrl(file);
const asset = await AssetService.createAssetFromFilePath(url, this.getFileName(url)); const asset = await AssetService.createAssetFromFilePath(url, this.getFileName(url));
if (this.isSupportedAssetType(asset.type)) { if (this.isSupportedAssetType(asset.type)) {
@ -228,7 +228,7 @@ export class AzureBlobStorage implements IStorageProvider {
} }
result.push(asset); result.push(asset);
} }
} }));
return result; return result;
} }

Просмотреть файл

@ -259,7 +259,7 @@ export default class ProjectService implements IProjectService {
try { try {
const blobs = new Set<string>(await storageProvider.listFiles(project.folderPath)); const blobs = new Set<string>(await storageProvider.listFiles(project.folderPath));
const assetLabel = asset ? asset + constants.labelFileExtension : undefined; const assetLabel = asset ? asset + constants.labelFileExtension : undefined;
for (const blob of blobs) { await Promise.all(Array.from(blobs).map(async (blob) => {
const blobFolderPath = blob.substr(0, blob.lastIndexOf("/")); const blobFolderPath = blob.substr(0, blob.lastIndexOf("/"));
if (blobFolderPath === project.folderPath if (blobFolderPath === project.folderPath
&& blob.endsWith(constants.labelFileExtension) && blob.endsWith(constants.labelFileExtension)
@ -267,6 +267,7 @@ export default class ProjectService implements IProjectService {
try { try {
if (!assetLabel || assetLabel === blob) { if (!assetLabel || assetLabel === blob) {
const content = JSON.parse(await storageProvider.readText(blob)) as ILabelData; const content = JSON.parse(await storageProvider.readText(blob)) as ILabelData;
const localTagDocumentCount = {};
content.labels.forEach((label) => { content.labels.forEach((label) => {
if (constants.supportedLabelsSchemas.has(content?.$schema) && label.label.split("/").length > 1) { if (constants.supportedLabelsSchemas.has(content?.$schema) && label.label.split("/").length > 1) {
return; return;
@ -277,22 +278,32 @@ export default class ProjectService implements IProjectService {
} else { } else {
labelName = label.label labelName = label.label
} }
tagNameSet.add(labelName); if (localTagDocumentCount[labelName]) {
if (tagDocumentCount[labelName]) { localTagDocumentCount[labelName] += 1;
tagDocumentCount[labelName] += 1;
} else { } else {
tagDocumentCount[labelName] = 1; localTagDocumentCount[labelName] = 1;
} }
}); });
} return localTagDocumentCount;
if (assetLabel && assetLabel === blob) {
break;
} }
} catch (err) { } catch (err) {
// ignore err // ignore err
} }
} }
} })).then(localTagDocumentCounts => {
for (const localTagDocumentCount of localTagDocumentCounts) {
if (_.isPlainObject(localTagDocumentCount)) {
for (const [labelName, labelCount] of Object.entries(localTagDocumentCount)) {
tagNameSet.add(labelName);
if (tagDocumentCount[labelName]) {
tagDocumentCount[labelName] += labelCount;
} else {
tagDocumentCount[labelName] = labelCount;
}
}
}
}
});
const tagNameArray = Array.from(tagNameSet); const tagNameArray = Array.from(tagNameSet);
if (tagNameArray.containsDuplicates((name) => name)) { if (tagNameArray.containsDuplicates((name) => name)) {
const reason = interpolate( const reason = interpolate(