Skip to content

Commit

Permalink
fix: refactor file write
Browse files Browse the repository at this point in the history
  • Loading branch information
aperron-ferlab committed Jul 4, 2024
1 parent e0f2c21 commit 311cc13
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { buildQuery } from '@arranger/middleware';
import { Client } from '@elastic/elasticsearch';
import xl from 'excel4node';
import noop from 'lodash/noop';
import { env } from 'process';

import { ES_PAGESIZE } from '../../env';
import { getExtendedConfigs, getNestedFields } from '../../utils/arrangerUtils';
import { executeSearchAfterQuery } from '../../utils/esUtils';
import ExtendedReportConfigs from '../../utils/extendedReportConfigs';
Expand All @@ -15,7 +15,8 @@ import generateTxtFile from '../utils/generateTxtFile';
import { addConditionAvailableInSqon } from '../utils/getAvailableBiospecimensFromSqon';

// eslint-disable-next-line max-len
const cbtn_instructions_mock = 'To request biospecimens from CBTN, please use the request form (https://airtable.com/apperYvVD82ti3021/pagdArwI0TxJQpiVW/form). General inquiries can be directed to [email protected].';
const cbtn_instructions_mock =
'To request biospecimens from CBTN, please use the request form (https://airtable.com/apperYvVD82ti3021/pagdArwI0TxJQpiVW/form). General inquiries can be directed to [email protected].';

/**
* Generate and write locally.
Expand Down Expand Up @@ -90,7 +91,7 @@ export default async function generateFiles(
}
},
onFinish: noop,
pageSize: Number(env.ES_PAGESIZE),
pageSize: ES_PAGESIZE,
});
} catch (err) {
console.error(`Error while fetching the data for biospecimen request`);
Expand Down
6 changes: 1 addition & 5 deletions src/reports/file-manifest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ const fileManifestReport = async (req: Request, res: Response): Promise<void> =>
wantedFields,
);
const fileIds = files?.map((f) => f.file_id);
console.log('OriginalFileIds: ', fileIds);
const newFileIds = withFamily ? await getFamilyIds(esClient, fileIds) : fileIds;
console.log('New unique file ids: ', newFileIds.length);

const filesInfos = await getInfosByConfig(esClient, reportConfig, newFileIds, 'file_id', esFileIndex);
console.log('filesInfos: ', filesInfos.length);

const path = `/tmp/${filename}.tsv`;
await generateTsvReport(filesInfos, path, reportConfig);
generateTsvReport(filesInfos, path, reportConfig);

res.setHeader('Content-Disposition', `attachment; filename="${filename}.tsv"`);
res.sendFile(path);
Expand Down
7 changes: 4 additions & 3 deletions src/reports/generateReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { Client } from '@elastic/elasticsearch';
import xl from 'excel4node';
import { Response } from 'express';
import flattenDeep from 'lodash/flattenDeep';
import noop from 'lodash/noop';
import uniq from 'lodash/uniq';

import * as env from '../env';
import { ES_PAGESIZE } from '../env';
import {
findValueInField,
generateColumnsForProperty,
Expand Down Expand Up @@ -149,8 +150,8 @@ export default async function generateReport(
wrapper.rowIndex += 1;
});
},
onFinish: () => {},
pageSize: env.ES_PAGESIZE,
onFinish: noop,
pageSize: ES_PAGESIZE,
});
console.timeEnd(`executeSearchAfterQuery ${sheetConfig.sheetName}`);
} catch (err) {
Expand Down
7 changes: 4 additions & 3 deletions src/reports/utils/generateExcelReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Client } from '@elastic/elasticsearch';
import xl from 'excel4node';
import { Response } from 'express';
import flattenDeep from 'lodash/flattenDeep';
import noop from 'lodash/noop';
import uniq from 'lodash/uniq';

import * as env from '../../env';
import { ES_PAGESIZE } from '../../env';
import {
findValueInField,
generateColumnsForProperty,
Expand Down Expand Up @@ -148,8 +149,8 @@ export default async function generateExcelReport(
wrapper.rowIndex += 1;
});
},
onFinish: () => {},
pageSize: env.ES_PAGESIZE,
onFinish: noop,
pageSize: ES_PAGESIZE,
});
console.timeEnd(`executeSearchAfterQuery ${sheetConfig.sheetName}`);
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/reports/utils/generateTsvReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import fs from 'fs';

import { SheetConfig } from '../types';

const generateTsvReport = async (data: { key: string }[], path: string, config: SheetConfig): Promise<void> => {
const generateTsvReport = (data: { key: string }[], path: string, config: SheetConfig): void => {
let tsvContent = config.columns.map((c) => c.header).join('\t') + '\n';

for (const row of data) {
const values = config.columns.map((c) => row[`${c.field}${c.fieldExtraSuffix || ''}`]);
tsvContent += values.join('\t') + '\n';
}

await fs.writeFileSync(path, tsvContent);
fs.writeFileSync(path, tsvContent);
};

export default generateTsvReport;
12 changes: 4 additions & 8 deletions src/reports/utils/getFamilyIds.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client } from '@elastic/elasticsearch';
import { env } from 'process';
import noop from 'lodash/noop';

import { ES_QUERY_MAX_SIZE, esFileIndex } from '../../env';
import { ES_PAGESIZE, ES_QUERY_MAX_SIZE, esFileIndex } from '../../env';
import { executeSearch, executeSearchAfterQuery } from '../../utils/esUtils';

interface IFileInfo {
Expand All @@ -21,10 +21,8 @@ const getFilesInfo = async (fileIds: string[], es: Client): Promise<IFileInfo[]>
onPageFetched: (pageHits) => {
sources.push(...pageHits);
},
onFinish: (total) => {
console.log(`Finished fetching all pages in getFilesInfo. Total hits: ${total}`);
},
pageSize: Number(env.ES_PAGESIZE),
onFinish: noop,
pageSize: ES_PAGESIZE,
});

const filesInfos = [];
Expand Down Expand Up @@ -91,9 +89,7 @@ const getFilesIdsMatched = async (filesInfos: IFileInfo[], es: Client): Promise<
*/
const getFamilyIds = async (es: Client, fileIds: string[]): Promise<string[]> => {
const filesInfos = await getFilesInfo(fileIds, es);
console.log('filesInfos: ', filesInfos.length);
const filesIdsMatched = await getFilesIdsMatched(filesInfos, es);
console.log('filesIdsMatched: ', filesIdsMatched.length);

return [...new Set([...fileIds, ...filesIdsMatched])];
};
Expand Down
10 changes: 4 additions & 6 deletions src/reports/utils/getFilesFromSqon.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { buildQuery } from '@arranger/middleware';
import { Client } from '@elastic/elasticsearch';
import { env } from 'process';
import noop from 'lodash/noop';

import { esFileAlias, esFileIndex } from '../../env';
import { ES_PAGESIZE, esFileAlias, esFileIndex } from '../../env';
import { getExtendedConfigs, getNestedFields } from '../../utils/arrangerUtils';
import { executeSearchAfterQuery } from '../../utils/esUtils';
import { Sqon } from '../../utils/setsTypes';
Expand Down Expand Up @@ -40,10 +40,8 @@ const getFilesFromSqon = async (
onPageFetched: (pageHits) => {
results.push(...pageHits);
},
onFinish: (total) => {
console.log(`Finished fetching all pages in getFilesFromSqon. Total hits: ${total}`);
},
pageSize: Number(env.ES_PAGESIZE),
onFinish: noop,
pageSize: ES_PAGESIZE,
});

return results;
Expand Down
9 changes: 4 additions & 5 deletions src/reports/utils/getInfosByConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Client } from '@elastic/elasticsearch';
import get from 'lodash/get';
import { env } from 'process';
import noop from 'lodash/noop';

import { ES_PAGESIZE } from '../../env';
import { executeSearchAfterQuery } from '../../utils/esUtils';
import { SheetConfig } from '../types';

Expand Down Expand Up @@ -34,10 +35,8 @@ const getInfosByConfig = async (
onPageFetched: (pageHits) => {
sources.push(...pageHits);
},
onFinish: (total) => {
console.log(`Finished fetching all pages in getInfosByConfig. Total hits: ${total}`);
},
pageSize: Number(env.ES_PAGESIZE),
onFinish: noop,
pageSize: ES_PAGESIZE,
});

return (sources as any).map((source) =>
Expand Down

0 comments on commit 311cc13

Please sign in to comment.