Skip to content

Commit

Permalink
Feat: backup to csv (#137)
Browse files Browse the repository at this point in the history
Feat: backup to csv

- generate headers of csv from index mapping
- transform json data to csv

Refs: #23

---------

Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll authored Nov 20, 2024
1 parent 4f1a76d commit 0675e6e
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ module.exports = {
'no-console': 'error',
'no-debugger': process.env === 'development' ? 'warn' : 'error',
},
ignorePatterns: ['dist','src-tauri'],
ignorePatterns: ['dist', 'src-tauri', 'coverage'],
};
1 change: 0 additions & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
Expand Down
131 changes: 90 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
"devDependencies": {
"@tauri-apps/cli": "^1",
"@types/debug": "^4.1.12",
"@types/jest": "^29.5.12",
"@types/markdown-it": "^14.1.1",
"@types/jest": "^29.5.14",
"@types/lodash": "^4.17.12",
"@types/markdown-it": "^14.1.1",
"@typescript-eslint/eslint-plugin": "^7.14.1",
"@typescript-eslint/parser": "^7.14.1",
"@vicons/antd": "^0.12.0",
Expand All @@ -62,7 +62,7 @@
"naive-ui": "^2.38.2",
"prettier": "^3.3.2",
"sass": "^1.77.6",
"ts-jest": "^29.1.5",
"ts-jest": "^29.2.5",
"typescript": "^5.5.2",
"unplugin-auto-import": "^0.17.6",
"unplugin-vue-components": "^0.27.1",
Expand Down
57 changes: 32 additions & 25 deletions src/store/backupRestoreStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ export const useBackupRestoreStore = defineStore('backupRestoreStore', {
const filePath = `${input.backupFolder}/${input.backupFileName}.${input.backupFileType}`;
let searchAfter: any[] | undefined = undefined;
let hasMore = true;
let appendFile = false;

try {
this.backupProgress = {
complete: 0,
total: (await client.get(`/${input.index}/_count`)).count,
};
const { [input.index]: backupIndexMapping } = await client.get(`/${input.index}/_mapping`);
const csvHeaders = buildCsvHeaders(backupIndexMapping);
let dataToWrite = input.backupFileType === 'json' ? '' : csvHeaders.join(',') + '\r\n';

while (hasMore) {
const response = await client.get(
Expand Down Expand Up @@ -90,11 +94,13 @@ export const useBackupRestoreStore = defineStore('backupRestoreStore', {
hasMore = false;
} else {
searchAfter = hits[hits.length - 1].sort;
const dataToWrite =
dataToWrite +=
input.backupFileType === 'json'
? JSON.stringify(hits)
: JSON.stringify(convertToCsv(hits));
await sourceFileApi.saveFile(filePath, dataToWrite, true);
: convertToCsv(csvHeaders, hits);
await sourceFileApi.saveFile(filePath, dataToWrite, appendFile);
dataToWrite = '';
appendFile = true;
}
}
return filePath;
Expand All @@ -108,28 +114,29 @@ export const useBackupRestoreStore = defineStore('backupRestoreStore', {
},
});

const flattenObject = (obj: any, parent: string = '', res: any = {}) => {
for (let key in obj) {
const propName = parent ? `${parent}.${key}` : key;
if (typeof obj[key] === 'object' && obj[key] !== null) {
flattenObject(obj[key], propName, res);
} else {
res[propName] = obj[key];
}
}
return res;
const buildCsvHeaders = ({
mappings,
}: {
mappings: {
properties: {
[key: string]: unknown;
};
};
}) => {
return Object.keys(mappings.properties);
};

const convertToCsv = (data: any[]) => {
if (data.length === 0) {
return { headers: [], data: [] };
}

const flattenedData = data.map(row => flattenObject(row));
const headers = Array.from(new Set(flattenedData.flatMap(row => Object.keys(row))));
const csvRows = flattenedData.map(row =>
headers.map(header => JSON.stringify(row[header] ?? '')).join(','),
);

return { headers, data: csvRows };
const convertToCsv = (headers: Array<string>, data: unknown[]) => {
return data
.map(item =>
headers
.map(header => {
const data = get(item, `_source.${header}`, null);
return data === null || !['object', 'symbol', 'function'].includes(typeof data)
? data
: JSON.stringify(data);
})
.join(','),
)
.join('\r\n');
};
9 changes: 8 additions & 1 deletion src/views/backup-restore/components/backup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,14 @@ const submitBackup = async () => {
positiveText: lang.t('dialogOps.confirm'),
negativeText: lang.t('dialogOps.cancel'),
onPositiveClick: async () => {
await saveBackup(backupInput);
saveBackup(backupInput).catch(err => {
const error = err as CustomError;
message.error(`status: ${error.status}, details: ${error.details}`, {
closable: true,
keepAliveOnHover: true,
duration: 3600,
});
});
},
onNegativeClick: () => {},
});
Expand Down
Empty file added tests/fixtures/index.ts
Empty file.
Loading

0 comments on commit 0675e6e

Please sign in to comment.