Skip to content

Commit

Permalink
improve performance of data import and loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
domechn committed Jun 8, 2024
1 parent 139e63e commit 00e7ac6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/page-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const App = ({
}

// query range is not loaded yet
if (dateRange.start === emptyDate || dateRange.end === emptyDate) {
if (dateRange.start.getTime() === emptyDate.getTime() || dateRange.end.getTime() === emptyDate.getTime()) {
return <LoadingPage />;
}

Expand Down
18 changes: 16 additions & 2 deletions src/middlelayers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { DATA_MANAGER, ExportData } from './datamanager'
import { getAutoBackupDirectory, getLastAutoImportAt, getLastAutoBackupAt, saveLastAutoImportAt, saveLastAutoBackupAt } from './configuration'
import { CoinPriceQuerier, CoinPriceQuery, ProCoinPriceQuery } from './datafetch/coins/price'
import { TonAnalyzer } from './datafetch/coins/ton'
import { getClientID } from '@/utils/app'


export async function queryCoinPrices(symbols: string[], userInfo: UserLicenseInfo): Promise<{ [k: string]: number }> {
Expand Down Expand Up @@ -205,18 +206,31 @@ export async function autoImportHistoricalData(): Promise<boolean> {
return false
}

// check if client is same, if the same, no need to import
const client = await getClientID()
if (ed.client && ed.client === client) {
console.debug("the same client, no need to auto import")
return false
}

const aia = await getLastAutoImportAt()
const exportAt = new Date(ed.exportAt)
const needImport = aia.getTime() < exportAt.getTime()

if (!needImport) {
console.debug("no need to auto import")
console.debug("latest data, no need to auto import")
return false
}

console.debug("start to import backup data")

await DATA_MANAGER.importHistoricalData("IGNORE", ed)
await DATA_MANAGER.importHistoricalData("IGNORE", ed, (datas) => {
// only import data that is after last auto import at
return _(datas).filter(d => {
const createdAt = new Date(d.createdAt)
return createdAt.getTime() > aia.getTime()
}).value()
})
} catch (e) {
console.error("failed to auto import", e)
return false
Expand Down
10 changes: 8 additions & 2 deletions src/middlelayers/datamanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { exportConfigurationString, importRawConfiguration } from './configurati
import { writeTextFile, readTextFile } from "@tauri-apps/api/fs"
import { ASSET_HANDLER, AssetHandlerImpl } from './entities/assets'
import { ASSET_PRICE_HANDLER, AssetPriceHandlerImpl } from './entities/asset-prices'
import { getClientID } from '@/utils/app'

export interface DataManager {
readHistoricalData(filePath: string): Promise<ExportData>
Expand All @@ -14,6 +15,8 @@ export interface DataManager {
}

export type ExportData = {
// to record the client who exported the data
client?: string
exportAt: string
configuration?: string
historicalData: PartlyHistoricalData
Expand Down Expand Up @@ -48,6 +51,7 @@ class DataManagement implements DataManager {

const exportData = {
exportAt,
client: await getClientID(),
historicalData: _(historicalData).map(d => ({
createdAt: d.createdAt,
total: d.total,
Expand Down Expand Up @@ -119,7 +123,7 @@ class DataManagement implements DataManager {
await this.assetPriceHandler.savePrices(assetPriceModels, conflictResolver)
}

async importHistoricalData(conflictResolver: 'REPLACE' | 'IGNORE', data: ExportData): Promise<void> {
async importHistoricalData(conflictResolver: 'REPLACE' | 'IGNORE', data: ExportData, dataFilter?: (origin: PartlyHistoricalData) => PartlyHistoricalData): Promise<void> {
const { exportAt, md5V2: md5Str, configuration, historicalData } = data

// !compatible with older versions logic ( before 0.3.3 )
Expand All @@ -137,8 +141,10 @@ class DataManagement implements DataManager {
throw new Error("invalid data: errorCode 001")
}

const savedData = dataFilter ? dataFilter(historicalData) : historicalData

// start to import
await this.saveHistoricalDataAssets(historicalData, conflictResolver)
await this.saveHistoricalDataAssets(savedData, conflictResolver)

// import configuration if exported
if (configuration) {
Expand Down

0 comments on commit 00e7ac6

Please sign in to comment.