feat: 为脚本操作增加流量信息操作 flowUtils

This commit is contained in:
xream
2023-11-07 16:42:28 +08:00
parent a91f9d7728
commit 6d51774d36
4 changed files with 35 additions and 17 deletions

View File

@@ -6,7 +6,7 @@ import {
} from './errors';
import { deleteByName, findByName, updateByName } from '@/utils/database';
import { SUBS_KEY, COLLECTIONS_KEY, ARTIFACTS_KEY } from '@/constants';
import { getFlowHeaders } from '@/utils/flow';
import { getFlowHeaders, parseFlowHeaders } from '@/utils/flow';
import { success, failed } from './response';
import $ from '@/core/app';
@@ -68,20 +68,7 @@ async function getFlowInfo(req, res) {
return;
}
// unit is KB
const uploadMatch = flowHeaders.match(/upload=(-?)(\d+)/);
const upload = Number(uploadMatch[1] + uploadMatch[2]);
const downloadMatch = flowHeaders.match(/download=(-?)(\d+)/);
const download = Number(downloadMatch[1] + downloadMatch[2]);
const total = Number(flowHeaders.match(/total=(\d+)/)[1]);
// optional expire timestamp
const match = flowHeaders.match(/expire=(\d+)/);
const expires = match ? Number(match[1]) : undefined;
success(res, { expires, total, usage: { upload, download } });
success(res, parseFlowHeaders(flowHeaders));
} catch (err) {
failed(
res,