mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 21:35:19 +00:00
fix: use seconds to update progress
This commit is contained in:
@@ -3,7 +3,7 @@ import { fileExists, fullPathForFile } from "@/lib/files"
|
|||||||
import { EXPORT_AND_IMPORT_FIELD_MAP, ExportFields, ExportFilters } from "@/models/export_and_import"
|
import { EXPORT_AND_IMPORT_FIELD_MAP, ExportFields, ExportFilters } from "@/models/export_and_import"
|
||||||
import { getFields } from "@/models/fields"
|
import { getFields } from "@/models/fields"
|
||||||
import { getFilesByTransactionId } from "@/models/files"
|
import { getFilesByTransactionId } from "@/models/files"
|
||||||
import { incrementProgress, updateProgress } from "@/models/progress"
|
import { updateProgress } from "@/models/progress"
|
||||||
import { getTransactions } from "@/models/transactions"
|
import { getTransactions } from "@/models/transactions"
|
||||||
import { format } from "@fast-csv/format"
|
import { format } from "@fast-csv/format"
|
||||||
import { formatDate } from "date-fns"
|
import { formatDate } from "date-fns"
|
||||||
@@ -15,7 +15,7 @@ import { Readable } from "stream"
|
|||||||
|
|
||||||
const TRANSACTIONS_CHUNK_SIZE = 300
|
const TRANSACTIONS_CHUNK_SIZE = 300
|
||||||
const FILES_CHUNK_SIZE = 50
|
const FILES_CHUNK_SIZE = 50
|
||||||
const PROGRESS_UPDATE_INTERVAL = 10 // files
|
const PROGRESS_UPDATE_INTERVAL_MS = 2000 // 2 seconds
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
const url = new URL(request.url)
|
const url = new URL(request.url)
|
||||||
@@ -98,6 +98,7 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
let totalFilesProcessed = 0
|
let totalFilesProcessed = 0
|
||||||
let totalFilesToProcess = 0
|
let totalFilesToProcess = 0
|
||||||
|
let lastProgressUpdate = Date.now()
|
||||||
|
|
||||||
// First count total files to process
|
// First count total files to process
|
||||||
for (const transaction of transactions) {
|
for (const transaction of transactions) {
|
||||||
@@ -145,9 +146,11 @@ export async function GET(request: Request) {
|
|||||||
fileData
|
fileData
|
||||||
)
|
)
|
||||||
|
|
||||||
// Update progress every PROGRESS_UPDATE_INTERVAL files
|
// Update progress every PROGRESS_UPDATE_INTERVAL_MS milliseconds
|
||||||
if (progressId && totalFilesProcessed % PROGRESS_UPDATE_INTERVAL === 0) {
|
const now = Date.now()
|
||||||
await incrementProgress(user.id, progressId)
|
if (progressId && now - lastProgressUpdate >= PROGRESS_UPDATE_INTERVAL_MS) {
|
||||||
|
await updateProgress(user.id, progressId, { current: totalFilesProcessed })
|
||||||
|
lastProgressUpdate = now
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`Skipping missing file: ${file.filename} for transaction ${transaction.id}`)
|
console.log(`Skipping missing file: ${file.filename} for transaction ${transaction.id}`)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { getCurrentUser } from "@/lib/auth"
|
import { getCurrentUser } from "@/lib/auth"
|
||||||
import { fileExists, getUserUploadsDirectory } from "@/lib/files"
|
import { fileExists, getUserUploadsDirectory } from "@/lib/files"
|
||||||
import { MODEL_BACKUP, modelToJSON } from "@/models/backups"
|
import { MODEL_BACKUP, modelToJSON } from "@/models/backups"
|
||||||
import { incrementProgress, updateProgress } from "@/models/progress"
|
import { updateProgress } from "@/models/progress"
|
||||||
import fs from "fs/promises"
|
import fs from "fs/promises"
|
||||||
import JSZip from "jszip"
|
import JSZip from "jszip"
|
||||||
import { NextResponse } from "next/server"
|
import { NextResponse } from "next/server"
|
||||||
@@ -9,7 +9,7 @@ import path from "path"
|
|||||||
|
|
||||||
const MAX_FILE_SIZE = 64 * 1024 * 1024 // 64MB
|
const MAX_FILE_SIZE = 64 * 1024 * 1024 // 64MB
|
||||||
const BACKUP_VERSION = "1.0"
|
const BACKUP_VERSION = "1.0"
|
||||||
const PROGRESS_UPDATE_INTERVAL = 10 // files
|
const PROGRESS_UPDATE_INTERVAL_MS = 2000 // 2 seconds
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
const user = await getCurrentUser()
|
const user = await getCurrentUser()
|
||||||
@@ -63,6 +63,8 @@ export async function GET(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let processedFiles = 0
|
let processedFiles = 0
|
||||||
|
let lastProgressUpdate = Date.now()
|
||||||
|
|
||||||
for (const file of uploadedFiles) {
|
for (const file of uploadedFiles) {
|
||||||
try {
|
try {
|
||||||
// Check file size before reading
|
// Check file size before reading
|
||||||
@@ -80,9 +82,12 @@ export async function GET(request: Request) {
|
|||||||
uploadsFolder.file(file.replace(userUploadsDirectory, ""), fileContent)
|
uploadsFolder.file(file.replace(userUploadsDirectory, ""), fileContent)
|
||||||
|
|
||||||
processedFiles++
|
processedFiles++
|
||||||
// Update progress every PROGRESS_UPDATE_INTERVAL files
|
|
||||||
if (progressId && processedFiles % PROGRESS_UPDATE_INTERVAL === 0) {
|
// Update progress every PROGRESS_UPDATE_INTERVAL_MS milliseconds
|
||||||
await incrementProgress(user.id, progressId)
|
const now = Date.now()
|
||||||
|
if (progressId && now - lastProgressUpdate >= PROGRESS_UPDATE_INTERVAL_MS) {
|
||||||
|
await updateProgress(user.id, progressId, { current: processedFiles })
|
||||||
|
lastProgressUpdate = now
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error reading file ${file}:`, error)
|
console.error(`Error reading file ${file}:`, error)
|
||||||
|
|||||||
Reference in New Issue
Block a user