Skip to content

[server] webui DB import and export #14347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tools/server/webui/package-lock.json

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

1 change: 1 addition & 0 deletions tools/server/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"autoprefixer": "^10.4.20",
"daisyui": "^5.0.12",
"dexie": "^4.0.11",
"dexie-export-import": "^4.0.11",
"highlight.js": "^11.10.0",
"katex": "^0.16.15",
"pdfjs-dist": "^5.2.133",
Expand Down
70 changes: 70 additions & 0 deletions tools/server/webui/src/components/SettingDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,76 @@ const SETTING_SECTIONS: SettingSection[] = [
);
},
},
{
type: SettingInputType.CUSTOM,
key: 'custom', // dummy key, won't be used
component: () => {
const exportDB = async () => {
const blob = await StorageUtils.exportDB();
const a = document.createElement('a');
document.body.appendChild(a);
a.href = URL.createObjectURL(blob);
document.body.appendChild(a);
a.download = `aa_dump.json`;
a.click();
document.body.removeChild(a);
};
return (
<button className="btn" onClick={exportDB}>
export database
</button>
);
},
},
{
type: SettingInputType.CUSTOM,
key: 'custom', // dummy key, won't be used
component: () => {
const importDB = async (e: React.ChangeEvent<HTMLInputElement>) => {
console.log(e);
if (!e.target.files) {
throw new Error('e.target.files cant be null');
}
if (e.target.files.length != 1)
throw new Error(
'Number of selected files for DB import must be 1 but was ' +
e.target.files.length +
'.'
);
const file = e.target.files[0];
try {
if (!file) throw new Error('No DB found to import.');
console.log('Importing DB ' + file.name);
await StorageUtils.importDB(file);
console.log('Import complete');
window.location.reload();
} catch (error) {
console.error('' + error);
}
};
return (
<div>
<label
htmlFor="db-import"
className="btn"
role="button"
tabIndex={0}
>
{' '}
reset and import database{' '}
</label>
<input
id="db-import"
type="file"
accept=".json"
className="file-upload"
onInput={importDB}
hidden
/>
</div>
);
},
},
{
type: SettingInputType.CHECKBOX,
label: 'Show tokens per second',
Expand Down
11 changes: 11 additions & 0 deletions tools/server/webui/src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { CONFIG_DEFAULT } from '../Config';
import { Conversation, Message, TimingReport } from './types';
import Dexie, { Table } from 'dexie';
import { exportDB as exportDexieDB } from 'dexie-export-import';

const event = new EventTarget();

Expand Down Expand Up @@ -35,6 +36,16 @@ const StorageUtils = {
/**
* manage conversations
*/
async exportDB() {
return await exportDexieDB(db);
},

async importDB(file: File) {
await db.delete();
await db.open();
return await db.import(file);
},

async getAllConversations(): Promise<Conversation[]> {
await migrationLStoIDB().catch(console.error); // noop if already migrated
return (await db.conversations.toArray()).sort(
Expand Down