Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(exports): fix csv/excel exports by removing workers (#1464)
- Loading branch information
Showing
14 changed files
with
189 additions
and
302 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { HashesMap } from "@common/utils/hashes-types"; | ||
|
||
import type { | ||
AliasMap, | ||
CommentsMap, | ||
FilesAndFoldersMap, | ||
} from "../../reducers/files-and-folders/files-and-folders-types"; | ||
import type { FilesAndFoldersMetadataMap } from "../../reducers/files-and-folders-metadata/files-and-folders-metadata-types"; | ||
import type { TagMap } from "../../reducers/tags/tags-types"; | ||
|
||
export interface CsvExporterData { | ||
aliases: AliasMap; | ||
comments: CommentsMap; | ||
elementsToDelete: string[]; | ||
filesAndFolders: FilesAndFoldersMap; | ||
filesAndFoldersMetadata: FilesAndFoldersMetadataMap; | ||
hashes?: HashesMap; | ||
tags: TagMap; | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
export const TREE_CSV_PROGRESS_WEIGHT = 1; | ||
export const CSV_EXPORT_PROGRESS_WEIGHT = 10; | ||
|
||
export const getExcelExportProgressGoal = ( | ||
filesAndFoldersCount: number | ||
): number => | ||
(TREE_CSV_PROGRESS_WEIGHT + CSV_EXPORT_PROGRESS_WEIGHT) * | ||
filesAndFoldersCount; | ||
|
||
export interface CreateExcelWorkbookParams { | ||
csvArray: string[][]; | ||
treeCsv: string[][]; | ||
} |
94 changes: 83 additions & 11 deletions
94
src/renderer/exporters/excel/excel-exporter-controller.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 |
---|---|---|
@@ -1,23 +1,95 @@ | ||
import type { Observable } from "rxjs"; | ||
import type { TFunction } from "i18next"; | ||
import { flatten } from "lodash"; | ||
import { Observable } from "rxjs"; | ||
import { tap, toArray } from "rxjs/operators"; | ||
import { utils, write } from "xlsx"; | ||
|
||
import { translations } from "../../translations/translations"; | ||
import { createAsyncWorkerForChildProcessControllerFactory } from "../../utils/async-worker/child-process"; | ||
import { backgroundWorkerProcess$ } from "../../utils/batch-process"; | ||
import { exportToCsv } from "../../utils/array-export/array-export"; | ||
import type { | ||
ErrorMessage, | ||
ResultMessage, | ||
} from "../../utils/batch-process/types"; | ||
import type { CsvExporterData } from "../csv/csv-exporter.impl"; | ||
import { MessageTypes } from "../../utils/batch-process/types"; | ||
import { computeTreeStructureArray } from "../../utils/tree-representation"; | ||
import type { CsvExporterData } from "../csv/csv-exporter-types"; | ||
import type { CreateExcelWorkbookParams } from "./excel-exporter-common"; | ||
import { | ||
CSV_EXPORT_PROGRESS_WEIGHT, | ||
TREE_CSV_PROGRESS_WEIGHT, | ||
} from "./excel-exporter-common"; | ||
|
||
const createExcelWorkbook = ( | ||
{ treeCsv, csvArray }: CreateExcelWorkbookParams, | ||
translator: TFunction | ||
) => { | ||
const workbook = utils.book_new(); | ||
const csvWorkSheet = utils.aoa_to_sheet(csvArray); | ||
const treeCsvWorkSheet = utils.aoa_to_sheet(treeCsv); | ||
utils.book_append_sheet( | ||
workbook, | ||
csvWorkSheet, | ||
translator("export.treeStats") | ||
); | ||
utils.book_append_sheet( | ||
workbook, | ||
treeCsvWorkSheet, | ||
translator("export.treeVisualizing") | ||
); | ||
return workbook; | ||
}; | ||
|
||
export const generateExcelExport$ = ( | ||
data: CsvExporterData | ||
): Observable<ErrorMessage | ResultMessage> => { | ||
const { language } = translations; | ||
return new Observable<ErrorMessage | ResultMessage>((subscriber) => { | ||
const translator = translations.t.bind(translations); | ||
const treeCsvPromise = computeTreeStructureArray(data.filesAndFolders) | ||
.pipe( | ||
tap((lines) => { | ||
subscriber.next({ | ||
result: TREE_CSV_PROGRESS_WEIGHT * lines.length, | ||
type: MessageTypes.RESULT, | ||
}); | ||
}), | ||
toArray() | ||
) | ||
.toPromise() | ||
.then(flatten); | ||
|
||
return backgroundWorkerProcess$( | ||
{ ...data, language }, | ||
createAsyncWorkerForChildProcessControllerFactory( | ||
"exporters/excel/excel-exporter.fork.ts" | ||
) | ||
); | ||
const csvArrayPromise = exportToCsv({ | ||
...data, | ||
translator, | ||
}) | ||
.pipe( | ||
tap((lines) => { | ||
subscriber.next({ | ||
result: CSV_EXPORT_PROGRESS_WEIGHT * lines.length, | ||
type: MessageTypes.RESULT, | ||
}); | ||
}), | ||
toArray() | ||
) | ||
.toPromise() | ||
.then(flatten); | ||
|
||
void Promise.all([treeCsvPromise, csvArrayPromise]).then( | ||
([treeCsv, csvArray]) => { | ||
const xlsxWorkbook = createExcelWorkbook( | ||
{ csvArray, treeCsv }, | ||
translator | ||
); | ||
|
||
const binaryXlsx = write(xlsxWorkbook, { | ||
type: "binary", | ||
}); | ||
|
||
subscriber.next({ | ||
result: binaryXlsx, | ||
type: MessageTypes.RESULT, | ||
}); | ||
subscriber.complete(); | ||
} | ||
); | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.