Skip to content

Commit

Permalink
refactor exhaust export csv to comply with functional arch
Browse files Browse the repository at this point in the history
Signed-off-by: Paz Barda <[email protected]>
  • Loading branch information
pazbardanl committed Feb 18, 2024
1 parent c6b8484 commit e1583a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
9 changes: 4 additions & 5 deletions src/lib/exhaust.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import {ExhaustCsvExporter} from '../models/exhaust-csv-export';
import {ExhaustPluginInterface} from '../models/exhaust-plugin';
import {ExhaustExportCsv} from '../models/exhaust-export-csv';

const createExhaustPlugin = (pluginTypeName: string) => {
switch (pluginTypeName) {
case 'csv':
return new ExhaustCsvExporter();
return ExhaustExportCsv;
default:
throw new Error(`unkonwn exhaust plugin type: ${pluginTypeName}`);
}
};

const initialize = (pipeline: string[]) => {
const exhaustPlugins: ExhaustPluginInterface[] = pipeline.map(
exhaustPluginName => createExhaustPlugin(exhaustPluginName)
const exhaustPlugins: any[] = pipeline.map(exhaustPluginName =>
createExhaustPlugin(exhaustPluginName)
);
return exhaustPlugins;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,12 @@ import {ERRORS} from '../util/errors';
import {ExhaustPluginInterface} from './exhaust-plugin';
const {InputValidationError} = ERRORS;

export class ExhaustCsvExporter implements ExhaustPluginInterface {
private createCsvContent(tree: any, headers: string[]): string {
return [
headers.join(','),
...tree.map((row: {[x: string]: any}) =>
headers.map(fieldName => row[fieldName]).join(',')
),
].join('\r\n');
}

/**
* Export to CSV
*/
async execute(
export const ExhaustExportCsv = (): ExhaustPluginInterface => {
const execute: (
context: any,
tree: any,
basePath: string
): Promise<[any, any, string]> {
) => Promise<[any, any, string]> = async (context, tree, basePath) => {
// create directory in base path, if doesnt exist
try {
await fs.mkdir(basePath, {recursive: true});
Expand All @@ -36,7 +24,12 @@ export class ExhaustCsvExporter implements ExhaustPluginInterface {
const headers = Object.keys(tree[0]);

// create csv content from tree with headers
const contents = this.createCsvContent(tree, headers);
const contents = [
headers.join(','),
...tree.map((row: {[x: string]: any}) =>
headers.map(fieldName => row[fieldName]).join(',')
),
].join('\r\n');

// write content to csv file
const outputPath = path.join(basePath, 'csv-export.csv');
Expand All @@ -50,5 +43,7 @@ export class ExhaustCsvExporter implements ExhaustPluginInterface {
}

return Promise.resolve([context, tree, basePath]);
}
}
};

return {execute};
};
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {TimeSyncModel} from './time-sync';
export {ExhaustExportCsv} from './exhaust-export-csv';

Check failure on line 2 in src/models/index.ts

View workflow job for this annotation

GitHub Actions / build

Insert `⏎`

Check failure on line 2 in src/models/index.ts

View workflow job for this annotation

GitHub Actions / build

Newline required at end of file but not found

0 comments on commit e1583a5

Please sign in to comment.