Skip to content

Commit

Permalink
fix(exports): fix csv/excel exports by removing workers (#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
cberthou committed Aug 23, 2022
1 parent 52cdb25 commit 1dafcca
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 302 deletions.
19 changes: 19 additions & 0 deletions src/renderer/exporters/csv/csv-exporter-types.ts
@@ -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;
}
20 changes: 4 additions & 16 deletions src/renderer/exporters/csv/csv-exporter.controller.ts
@@ -1,3 +1,4 @@
import { arrayToCsv } from "@common/utils/csv";
import type { HashesMap } from "@common/utils/hashes-types";
import type { Observable } from "rxjs";

Expand All @@ -8,13 +9,8 @@ import type {
} 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";
import { translations } from "../../translations/translations";
import { createAsyncWorkerForChildProcessControllerFactory } from "../../utils/async-worker/child-process";
import { backgroundWorkerProcess$ } from "../../utils/batch-process";
import type {
ErrorMessage,
ResultMessage,
} from "../../utils/batch-process/types";
import { generateArrayExport$ } from "../../utils/array-export/array-export";
import type { ResultMessage } from "../../utils/batch-process/types";

export interface GenerateCsvExportOptions {
aliases: AliasMap;
Expand All @@ -33,12 +29,4 @@ export interface GenerateCsvExportOptions {
*/
export const generateCsvExport$ = (
data: GenerateCsvExportOptions
): Observable<ErrorMessage | ResultMessage> => {
const { language } = translations;
return backgroundWorkerProcess$(
{ ...data, language },
createAsyncWorkerForChildProcessControllerFactory(
"exporters/csv/csv-exporter.fork.ts"
)
);
};
): Observable<ResultMessage> => generateArrayExport$(data, arrayToCsv);
16 changes: 0 additions & 16 deletions src/renderer/exporters/csv/csv-exporter.fork.ts

This file was deleted.

73 changes: 0 additions & 73 deletions src/renderer/exporters/csv/csv-exporter.impl.ts

This file was deleted.

40 changes: 29 additions & 11 deletions src/renderer/exporters/csv/tree-csv-exporter.controller.ts
@@ -1,13 +1,15 @@
import type { Observable } from "rxjs";
import { arrayToCsv } from "@common/utils/csv";
import { flatten } from "lodash";
import { Observable } from "rxjs";
import { tap, toArray } from "rxjs/operators";

import type { FilesAndFoldersMap } from "../../reducers/files-and-folders/files-and-folders-types";
import { translations } from "../../translations/translations";
import { createAsyncWorkerForChildProcessControllerFactory } from "../../utils/async-worker/child-process";
import { backgroundWorkerProcess$ } from "../../utils/batch-process";
import type {
ErrorMessage,
ResultMessage,
} from "../../utils/batch-process/types";
import { MessageTypes } from "../../utils/batch-process/types";
import { computeTreeStructureArray } from "../../utils/tree-representation";

/**
* Asynchronously generates a tree csv export
Expand All @@ -17,11 +19,27 @@ import type {
export const generateTreeCsvExport$ = (
filesAndFolders: FilesAndFoldersMap
): Observable<ErrorMessage | ResultMessage> => {
const { language } = translations;
return backgroundWorkerProcess$(
{ filesAndFolders, language },
createAsyncWorkerForChildProcessControllerFactory(
"exporters/csv/tree-csv-exporter.fork.ts"
)
);
return new Observable<ResultMessage>((subscriber) => {
const header = [""];
void computeTreeStructureArray(filesAndFolders)
.pipe(
tap((lineComputed) => {
subscriber.next({
result: lineComputed.length,
type: MessageTypes.RESULT,
});
}),
toArray()
)
.toPromise()
.then(flatten)
.then((rows) => {
subscriber.next({
result: arrayToCsv([header, ...rows]),
type: MessageTypes.RESULT,
});

subscriber.complete();
});
});
};
16 changes: 0 additions & 16 deletions src/renderer/exporters/csv/tree-csv-exporter.fork.ts

This file was deleted.

43 changes: 0 additions & 43 deletions src/renderer/exporters/csv/tree-csv-exporter.impl.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/renderer/exporters/excel/excel-exporter-common.ts
@@ -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 src/renderer/exporters/excel/excel-exporter-controller.ts
@@ -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();
}
);
});
};
16 changes: 0 additions & 16 deletions src/renderer/exporters/excel/excel-exporter.fork.ts

This file was deleted.

0 comments on commit 1dafcca

Please sign in to comment.