没有不值得去解决的问题,也没有不值得去学习的技术!

Export Progress Now Displays the Actual Excel Export Progress

The current Excel export progress is displayed independently by the frontend and is not controlled by the backend

1. Currently, the Excel export progress is displayed independently by the frontend and is not controlled by the backend. As shown in Figure 1.

The current Excel export progress is displayed independently by the frontend and is not controlled by the backend
Figure 1

2. I decided to adjust it to display the actual Excel export progress. The final implemented rules are as follows:
When exporting an Excel file, generally speaking, there are a total of 4 steps after the backend receives the frontend request:
(1) Queuing. During this phase, the frontend progress always shows 0%.
(2) Then, when the queue job actually starts, if the Excel file has 10,000 rows, the progress increases by 9.9% for every 1,000 rows written. Once writing is complete, the progress reaches 99%.
(3) Next, it involves processing various Excel file formats, uploading the file to the cloud, generating the download file link, and so on. After this phase is completed, the frontend displays a progress of 100%.
(4) The frontend obtains the download link and proceeds to a successful download. During this phase, the progress is no longer displayed.

3. The final code implementation is roughly as follows:

PHP


    private string $cachedProgressPercentKey = 'job_status:progress_percent'; // 进度百分比的缓存键
    private int $cachedProgressPercentTtl = 24 * 3600; // 进度百分比的缓存过期时间

    public const EXPORT_EXCEL_WRITE_PROGRESS_PERCENT = 99; // 导出 Excel 时写入文件所占用的百分比

    private int $exportExcelMaxCount = 100000; // 限制最大导出行数为 100000
    private int $exportExcelChunkCount = 1000; // 导出时分块数量为 1000


	// 更新进度百分比
	$orderShippingLogCount = min($orderShippingLogQueryBuilder->count(), $this->exportExcelMaxCount);
	$chunkCount = (int)ceil($orderShippingLogCount / $this->exportExcelChunkCount); // 上舍入
	$i = 0;
	$uid = $jobStatus->uid;
	Log::info(
		'$orderShippingLog',
		[
			'$orderShippingLogCount' => $orderShippingLogCount,
			'$chunkCount' => $chunkCount,
		]
	);
	$orderShippingLogQueryBuilder->chunk($this->exportExcelChunkCount, function ($orderShippingLogs) use ($params, $excel, $columns, $chunkCount, $uid, &$i) {
		// 限制最大导出行数为 100000
		if ($i >= $chunkCount) {
			return false;
		}

		$data = [];

		foreach ($orderShippingLogs as $rowKey => $orderShippingLog) {
			$data[] = static::toResource($orderShippingLog, $params['timezone']);
		}
		$excel->writeRows($data);

		// 更新进度百分比
		$i++;
		$progressPercent = ($i / $chunkCount) * JobStatusService::EXPORT_EXCEL_WRITE_PROGRESS_PERCENT;
		app(JobStatusService::class)->setProgressPercent($uid, $progressPercent);

		Log::info(
			'$orderShippingLogChunk',
			[
				'$i' => $i,
				'$chunkCount' => $chunkCount,
				'$progressPercent' => $progressPercent
			]
		);

		return true;
	});

	// 更新进度百分比
	$jobStatusService->setProgressPercent($this->jobStatus->uid, 100);



    /**
     * 设置进度百分比
     * @param string $uid
     * @param float $progressPercent
     * @return void
     */
    public function setProgressPercent(string $uid, float $progressPercent): void
    {
        $cachedKey = sprintf($this->cachedProgressPercentKey . ':%s', $uid);
        Cache::store('redis')->put($cachedKey, round($progressPercent, 2), $this->cachedProgressPercentTtl);
    }

    /**
     * 获取进度百分比
     * @param string $uid
     * @return float
     * @throws InvalidArgumentException
     */
    private function getProgressPercent(string $uid): float
    {
        $cachedKey = sprintf($this->cachedProgressPercentKey . ':%s', $uid);
        $cachedProgressPercent = Cache::store('redis')->get($cachedKey);

        if (!$cachedProgressPercent) {
            return 0;
        }

        return (float)$cachedProgressPercent;
    }

4. The log output is as follows:

Plaintext

[2024-05-10 07:33:35] local.INFO: $orderShippingLog {"$orderShippingLogCount":4696,"$chunkCount":5} 
[2024-05-10 07:33:37] local.INFO: $orderShippingLogChunk {"$i":1,"$chunkCount":5,"$progressPercent":19.8} 
[2024-05-10 07:33:40] local.INFO: $orderShippingLogChunk {"$i":2,"$chunkCount":5,"$progressPercent":39.6} 
[2024-05-10 07:33:41] local.INFO: $orderShippingLogChunk {"$i":3,"$chunkCount":5,"$progressPercent":59.4} 
[2024-05-10 07:33:42] local.INFO: $orderShippingLogChunk {"$i":4,"$chunkCount":5,"$progressPercent":79.2} 
[2024-05-10 07:33:43] local.INFO: $orderShippingLogChunk {"$i":5,"$chunkCount":5,"$progressPercent":99} 

5. The API response structure is as follows:

JSON

{
    "label": "order",
    "type": "export",
    "status": "pending",
    "file_path": "",
    "progress_percent": 59.4
}

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系