-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support import data of Web Activity Time Tracker (#216)
- Loading branch information
Showing
22 changed files
with
726 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright (c) 2021 Hengyang Zhang | ||
* | ||
* This software is released under the MIT License. | ||
* https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
import { t } from "@app/locale" | ||
import { Upload } from "@element-plus/icons-vue" | ||
import Immigration from "@service/components/immigration" | ||
import { deserialize } from "@util/file" | ||
import { ElButton, ElLoading, ElMessage } from "element-plus" | ||
import { Ref, defineComponent, h, ref } from "vue" | ||
|
||
const immigration: Immigration = new Immigration() | ||
|
||
async function handleFileSelected(fileInputRef: Ref<HTMLInputElement>, callback: () => void) { | ||
const files: FileList | null = fileInputRef?.value?.files | ||
if (!files || !files.length) { | ||
return | ||
} | ||
const loading = ElLoading.service({ fullscreen: true }) | ||
const file: File = files[0] | ||
const fileText = await file.text() | ||
const data = deserialize(fileText) | ||
if (!data) { | ||
ElMessage.error(t(msg => msg.dataManage.importError)) | ||
} | ||
await immigration.importData(data) | ||
loading.close() | ||
callback?.() | ||
ElMessage.success(t(msg => msg.dataManage.migrated)) | ||
} | ||
|
||
const _default = defineComponent({ | ||
emits: { | ||
import: () => true | ||
}, | ||
setup(_, ctx) { | ||
const fileInputRef = ref<HTMLInputElement>() | ||
return () => h(ElButton, { | ||
size: 'large', | ||
type: 'primary', | ||
icon: Upload, | ||
onClick: () => fileInputRef.value.click() | ||
}, () => [ | ||
t(msg => msg.item.operation.importWholeData), | ||
h('input', { | ||
ref: fileInputRef, | ||
type: 'file', | ||
accept: '.json', | ||
style: { display: 'none' }, | ||
onChange: () => handleFileSelected(fileInputRef, () => ctx.emit('import')) | ||
}) | ||
]) | ||
} | ||
}) | ||
|
||
export default _default |
36 changes: 36 additions & 0 deletions
36
src/app/components/data-manage/migration/import-other-button/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { t } from "@app/locale" | ||
import { Upload } from "@element-plus/icons-vue" | ||
import { ElButton, ElDialog } from "element-plus" | ||
import { Ref, defineComponent, h, ref } from "vue" | ||
import Sop from "./sop" | ||
import "./style" | ||
|
||
const _default = defineComponent({ | ||
emits: { | ||
import: () => true | ||
}, | ||
setup(_) { | ||
const dialogVisible: Ref<boolean> = ref(false) | ||
const close = () => dialogVisible.value = false | ||
return () => [ | ||
h(ElButton, { | ||
size: 'large', | ||
type: 'warning', | ||
icon: Upload, | ||
onClick: () => dialogVisible.value = true | ||
}, () => t(msg => msg.item.operation.importOtherData)), | ||
h(ElDialog, { | ||
top: '10vh', | ||
modelValue: dialogVisible.value, | ||
title: t(msg => msg.item.operation.importOtherData), | ||
width: '80%', | ||
closeOnClickModal: false, | ||
}, () => h(Sop, { | ||
onCancel: close, | ||
onImport: close, | ||
})) | ||
] | ||
} | ||
}) | ||
|
||
export default _default |
81 changes: 81 additions & 0 deletions
81
src/app/components/data-manage/migration/import-other-button/processor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import StatDatabase from "@db/stat-database" | ||
import { isNotZeroResult } from "@util/stat" | ||
|
||
const statDatabase: StatDatabase = new StatDatabase(chrome.storage.local) | ||
|
||
export type OtherExtension = "web_activity_time_tracker" | ||
|
||
export type ActionType = 'overwrite' | 'accumulate' | ||
|
||
export type ImportedRow = Required<timer.stat.RowKey> & Partial<timer.stat.Result> & { | ||
exist?: timer.stat.Result | ||
} | ||
|
||
export type ImportedData = { | ||
// Whether there is data for this dimension | ||
[dimension in timer.stat.Dimension]?: boolean | ||
} & { | ||
rows: ImportedRow[] | ||
} | ||
|
||
/** | ||
* Parse the content to rows | ||
* | ||
* @param type extension type | ||
* @param file selected file | ||
* @returns row data | ||
*/ | ||
export async function parseFile(ext: OtherExtension, file: File): Promise<ImportedData> { | ||
const text = await file.text() | ||
if (ext === 'web_activity_time_tracker') { | ||
const lines = text.split('\n').map(line => line.trim()).filter(line => !!line).splice(1) | ||
const rows: ImportedRow[] = lines.map(line => { | ||
const [host, date, seconds] = line.split(',').map(cell => cell.trim()) | ||
const [year, month, day] = date.split('/') | ||
const realDate = `${year}${month.length == 2 ? month : '0' + month}${day.length == 2 ? day : '0' + day}` | ||
return { host, date: realDate, focus: parseInt(seconds) * 1000 } | ||
}) | ||
await doIfExist(rows, (row, exist) => row.exist = exist) | ||
return { rows, focus: true } | ||
} else { | ||
return { rows: [] } | ||
} | ||
} | ||
|
||
async function doIfExist<T extends timer.stat.RowKey>(items: T[], processor: (item: T, existVal: timer.stat.Result) => any): Promise<void> { | ||
await Promise.all(items.map(async item => { | ||
const { host, date } = item | ||
const exist = await statDatabase.get(host, date) | ||
isNotZeroResult(exist) && processor(item, exist) | ||
})) | ||
} | ||
|
||
/** | ||
* Import data | ||
*/ | ||
export async function processImport(data: ImportedData, action: ActionType): Promise<void> { | ||
if (action === 'overwrite') { | ||
return processOverwrite(data) | ||
} else { | ||
return processAcc(data) | ||
} | ||
} | ||
|
||
function processOverwrite(data: ImportedData): Promise<any> { | ||
const { rows, focus, time } = data | ||
return Promise.all(rows.map(async row => { | ||
const { host, date } = row | ||
const exist = await statDatabase.get(host, date) | ||
focus && (exist.focus = row.focus || 0) | ||
time && (exist.time = row.time || 0) | ||
await statDatabase.forceUpdate({ host, date, ...exist }) | ||
})) | ||
} | ||
|
||
function processAcc(data: ImportedData): Promise<any> { | ||
const { rows } = data | ||
return Promise.all(rows.map(async row => { | ||
const { host, date, focus = 0, time = 0 } = row | ||
await statDatabase.accumulate(host, date, { focus, time }) | ||
})) | ||
} |
Oops, something went wrong.