Commit b2938d7c by ccran

add new token model;

parent 7230e122
#!/bin/bash
# 本地构建镜像,上传远端,重建 Compose 服务并等待健康检查通过。
# 用法:bash bin/docker-remote-deploy.sh [user@host]
# 示例:SSH_PORT=22 IMAGE_TAG=new-api:export bash bin/docker-remote-deploy.sh
# 示例:bash bin/docker-remote-deploy.sh(根据提示输入,回车使用默认值)
# SSH 使用本机 ~/.ssh/config、密钥或交互式密码认证,同次执行复用连接。
set -euo pipefail
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
sed -n '2,5p' "$0"
cat <<'HELP'
REMOTE_DIR 远端部署目录,默认 /opt/new-api(自动创建)
COMPOSE_FILE 本地 Compose 文件,默认项目根目录的 docker-compose.yml
SERVICE_NAME 要重建的服务,默认 new-api
IMAGE_TAG 必须与本地服务配置一致,默认 new-api:export
HEALTH_TIMEOUT 健康检查等待秒数,默认 180
SSH_PORT SSH 端口,默认 22
交互输入:SSH 目标、SSH 端口、New API 宿主机端口、远端目录、
Compose 文件、服务名、镜像标签、健康检查超时;回车使用提示中的默认值。
容器内端口仍为 3000;自定义 Compose 必须使用 ${NEW_API_PORT:-3000} 占位符。
无需设置环境变量;输入的宿主机端口会固化到上传的 Compose 文件。
远端需已安装 Docker 和 Compose v2,服务需配置 healthcheck,依赖服务需已启动。
自动上传本地 Compose 文件;远端原配置备份为 docker-compose.yml.bak。
只重建指定服务;检查失败时返回非零状态,不自动回滚。
......@@ -41,13 +39,32 @@ failed_line=''
trap 'failed_command=$BASH_COMMAND; failed_line=$LINENO' ERR
trap 'result=$?; if (( result != 0 )); then log "失败 [$current_step/10] 退出码=${result},行号=${failed_line:-未知},命令=${failed_command:-见上方错误}"; fi' EXIT
step 1 "检查本地参数和构建环境"
remote_host=${1:-root@8.136.9.68}
ssh_port=${SSH_PORT:-22}
image_tag=${IMAGE_TAG:-new-api:export}
remote_dir=${REMOTE_DIR:-/opt/new-api}
compose_file=${COMPOSE_FILE:-docker-compose.yml}
service_name=${SERVICE_NAME:-new-api}
health_timeout=${HEALTH_TIMEOUT:-180}
# 提示写入 stderr,输入保留原文;EOF 时退出,避免无人输入时误部署。
prompt() {
local value
printf '%s' "$2" >&2
if [[ -n "$3" ]]; then printf ' [%s]' "$3" >&2; fi
printf ': ' >&2
if ! IFS= read -r value; then
echo '输入已结束,取消部署。' >&2
exit 1
fi
printf -v "$1" '%s' "${value:-$3}"
}
remote_host=${1:-}
if [[ -z "$remote_host" ]]; then prompt remote_host 'SSH 目标(user@host)' 'root@8.136.9.68'; fi
prompt ssh_port 'SSH 端口' '22'
prompt new_api_port 'New API 宿主机部署端口' '3002'
prompt remote_dir '远端部署目录' '/opt/new-api'
prompt compose_file '本地 Compose 文件' 'docker-compose.yml'
prompt service_name 'Compose 服务名' 'new-api'
prompt image_tag '镜像标签(须与 Compose 一致)' 'new-api:export'
prompt health_timeout '健康检查超时(秒)' '180'
if [[ ! "$new_api_port" =~ ^[1-9][0-9]{0,4}$ ]] || (( new_api_port > 65535 )); then
echo "New API 部署端口必须为 1–65535。" >&2
exit 1
fi
project_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
if [[ "$compose_file" != /* ]]; then
compose_file="$project_dir/$compose_file"
......@@ -57,6 +74,11 @@ if [[ ! -f "$compose_file" ]]; then
exit 1
fi
if ! grep -Fq '${NEW_API_PORT:-3000}' "$compose_file"; then
echo 'Compose 必须包含 ${NEW_API_PORT:-3000} 端口占位符。' >&2
exit 1
fi
if [[ "$remote_dir" != /* || ! "$health_timeout" =~ ^[1-9][0-9]{0,5}$ || "$service_name" == -* ]]; then
echo "REMOTE_DIR 必须是绝对路径,HEALTH_TIMEOUT 必须为 1–999999 秒,SERVICE_NAME 不能以 - 开头。" >&2
exit 1
......@@ -66,8 +88,8 @@ if [[ "$remote_host" == -* || "$remote_host" == *[[:space:]]* || -z "$remote_hos
echo "无效的 SSH 目标:$remote_host" >&2
exit 1
fi
if [[ ! "$ssh_port" =~ ^[0-9]+$ ]]; then
echo "SSH_PORT 必须是端口号。" >&2
if [[ ! "$ssh_port" =~ ^[1-9][0-9]{0,4}$ ]] || (( ssh_port > 65535 )); then
echo "SSH 端口必须为 1–65535。" >&2
exit 1
fi
for command_name in docker ssh scp; do
......@@ -137,7 +159,9 @@ docker buildx build --progress plain --platform "$platform" --load \
log "完成 [4/10] 镜像构建成功"
step 5 "导出镜像并准备 Compose 配置"
cp -- "$compose_file" "$export_dir/docker-compose.yml"
# 固化端口到上传配置,避免后续远端 compose 操作恢复默认端口。
# 自定义 Compose 文件也应使用相同的端口占位符。
sed 's/${NEW_API_PORT:-3000}/'"$new_api_port"'/g' "$compose_file" > "$export_dir/docker-compose.yml"
docker save -o "$archive_path" "$image_tag"
log "完成 [5/10] 镜像归档大小:$(du -h "$archive_path" | cut -f1);配置=$compose_file"
......
#!/bin/bash
# 独立部署 HTTPS Nginx 到与 New API 相同的 Linux Docker 主机。
set -euo pipefail
if [[ "${1:-}" == --help || "${1:-}" == -h ]]; then
cat <<'HELP'
用法:bash bin/nginx-remote-deploy.sh [user@host]
启动后按提示输入 SSH 目标、SSH 端口、域名、HTTPS 端口、New API 端口、
证书路径、私钥路径、远端部署目录和 Nginx 镜像;回车使用显示的默认值。
域名、证书路径和私钥路径已有默认值;路径为远端已存在的绝对路径。
无需设置环境变量。
远端需要 Linux、Docker Compose v2、curl,证书须包含完整证书链。
使用 host 网络访问 127.0.0.1:输入的后端端口,不依赖 New API 的 Compose 网络。
仅监听输入的 HTTPS 端口,同端口 HTTP 请求以 308 跳转至 HTTPS。
不占用 80 端口,不自动签发证书,不修改 New API。
重复执行更新独立 Compose 项目 new-api-nginx;失败返回非零,不自动回滚。
证书更新后重新执行脚本。生成的配置和证书挂载保存在输入的远端部署目录。
HELP
exit 0
fi
fail() { echo "$*" >&2; exit 1; }
(( $# <= 1 )) || fail "用法:bash $0 [user@host]"
# 提示写入 stderr,输入保留原文;EOF 时退出,避免无人输入时误部署。
prompt() {
local value
printf '%s' "$2" >&2
if [[ -n "$3" ]]; then printf ' [%s]' "$3" >&2; fi
printf ': ' >&2
if ! IFS= read -r value; then
echo '输入已结束,取消部署。' >&2
exit 1
fi
printf -v "$1" '%s' "${value:-$3}"
}
remote_host=${1:-}
if [[ -z "$remote_host" ]]; then prompt remote_host 'SSH 目标(user@host)' 'root@8.136.9.68'; fi
prompt ssh_port 'SSH 端口' '22'
prompt domain 'HTTPS 域名' 'phslgld.hnluchuan.com'
prompt https_port '对外 HTTPS 端口' '3000'
prompt new_api_port '同一宿主机的 New API HTTP 端口' '3002'
prompt cert_file '远端完整证书链文件路径' '/etc/letsencrypt/live/phslgld.hnluchuan.com/fullchain.pem'
prompt key_file '远端私钥文件路径' '/etc/letsencrypt/live/phslgld.hnluchuan.com/privkey.pem'
prompt remote_dir '远端 Nginx 部署目录' '/opt/new-api-nginx'
prompt nginx_image 'Nginx 镜像' 'nginx:latest'
[[ -n "$remote_host" && "$remote_host" != -* && "$remote_host" != *[[:space:]]* ]] || fail "无效 SSH 目标。"
[[ ${#domain} -le 253 && "$domain" == *.* ]] || fail "域名必须是完整域名。"
IFS=. read -r -a labels <<< "$domain"
for label in "${labels[@]}"; do
[[ ${#label} -le 63 && "$label" =~ ^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$ ]] || fail "域名包含无效标签。"
done
[[ "$domain" != *. ]] || fail "域名不能以点结尾。"
for port in "$https_port" "$new_api_port" "$ssh_port"; do
[[ "$port" =~ ^[1-9][0-9]{0,4}$ ]] && (( port <= 65535 )) || fail "端口必须为 1–65535。"
done
[[ "$https_port" != "$new_api_port" ]] || fail "HTTPS 端口不能与 New API 端口相同。"
# 路径限制确保生成的 Compose YAML 和 Docker mount 参数无歧义。
for path in "$cert_file" "$key_file" "$remote_dir"; do
[[ "$path" =~ ^/[a-zA-Z0-9_./-]+$ ]] || fail "证书及部署目录须为绝对路径,仅支持字母、数字、下划线、点、斜杠和连字符。"
done
[[ "$nginx_image" =~ ^[a-zA-Z0-9][a-zA-Z0-9_./:@-]*$ ]] || fail "无效 Nginx 镜像。"
command -v ssh >/dev/null || fail "缺少 ssh。"
remote_args=()
single_quote_escape="'\\''"
for argument in "$domain" "$cert_file" "$key_file" "$https_port" "$new_api_port" "$remote_dir" "$nginx_image"; do
remote_args+=("'${argument//\'/$single_quote_escape}'")
done
ssh -p "$ssh_port" "$remote_host" "bash -s -- ${remote_args[*]}" <<'REMOTE'
set -euo pipefail
trap 'echo "Nginx 部署失败(行号 $LINENO),请检查配置和容器日志;未自动回滚。" >&2' ERR
[[ $(uname -s) == Linux ]] || { echo '仅支持 Linux Docker 主机。' >&2; exit 1; }
command -v curl >/dev/null
docker info >/dev/null
docker compose version
domain=$1
cert_file=$2
key_file=$3
https_port=$4
new_api_port=$5
remote_dir=$6
nginx_image=$7
[[ -s "$cert_file" && -s "$key_file" ]] || { echo '远端证书或私钥不存在或为空。' >&2; exit 1; }
curl --fail --silent --show-error --max-time 10 "http://127.0.0.1:$new_api_port/api/status" >/dev/null
mkdir -p "$remote_dir"
stage=$(mktemp -d "$remote_dir/release.XXXXXX")
chmod 755 "$stage"
cat > "$stage/nginx.conf" <<CONF
worker_processes auto;
events { worker_connections 1024; }
http {
map \$http_upgrade \$connection_upgrade {
default upgrade;
'' close;
}
access_log /dev/stdout;
error_log /dev/stderr warn;
server {
listen $https_port ssl;
server_name $domain;
error_page 497 =308 https://$domain:$https_port\$request_uri;
ssl_certificate /etc/nginx/tls/fullchain.pem;
ssl_certificate_key /etc/nginx/tls/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
client_max_body_size 100m;
location / {
proxy_pass http://127.0.0.1:$new_api_port;
proxy_http_version 1.1;
proxy_set_header Host \$http_host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Host \$http_host;
proxy_set_header X-Forwarded-Port $https_port;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection \$connection_upgrade;
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
}
}
CONF
cat > "$stage/compose.yml" <<CONF
services:
nginx:
image: $nginx_image
restart: unless-stopped
network_mode: host
volumes:
- $stage/nginx.conf:/etc/nginx/nginx.conf:ro
- $cert_file:/etc/nginx/tls/fullchain.pem:ro
- $key_file:/etc/nginx/tls/privkey.pem:ro
CONF
compose=(docker compose -p new-api-nginx -f "$stage/compose.yml")
"${compose[@]}" config --quiet
"${compose[@]}" pull nginx
# 先验证证书、私钥和语法;此时不停止现有 Nginx。
# SSH heredoc 使用 stdin 传递后续命令,检查容器不能读取该输入。
"${compose[@]}" run --rm --no-deps -T --interactive=false nginx nginx -t </dev/null
echo 'Nginx 配置检查通过,开始启动服务。'
if [[ -f "$remote_dir/compose.yml" ]]; then
cp "$remote_dir/compose.yml" "$remote_dir/compose.yml.bak"
fi
cp "$stage/compose.yml" "$remote_dir/compose.yml"
"${compose[@]}" up -d --no-deps --force-recreate nginx
# 使用真实域名进行 SNI 和证书链校验,不跳过 TLS 校验。
# 容器启动后监听端口可能尚未就绪,仅在全部重试失败时展示 curl 错误。
echo '等待 Nginx HTTPS 服务就绪……'
for attempt in {1..15}; do
if curl --noproxy '*' --fail --silent --show-error --max-time 5 \
--resolve "$domain:$https_port:127.0.0.1" \
"https://$domain:$https_port/api/status" > "$stage/status.json" 2> "$stage/curl-error.log"; then
echo "Nginx 已部署:https://$domain:$https_port -> http://127.0.0.1:$new_api_port"
echo "配置:$remote_dir/compose.yml;证书更新后请重新执行部署脚本。"
exit 0
fi
sleep 2
done
cat "$stage/curl-error.log" >&2
"${compose[@]}" logs --tail 50 nginx >&2
echo 'HTTPS 验收失败,请检查证书域名、信任链、端口占用和后端状态。' >&2
exit 1
REMOTE
......@@ -281,10 +281,18 @@ func getImportVendorID(vendorName string, vendorIDByName map[string]int) (int, e
var vendor model.Vendor
if err := model.DB.Select("id").Where("name = ?", vendorName).First(&vendor).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return 0, fmt.Errorf("供应商不存在:%s", vendorName)
if !errors.Is(err, gorm.ErrRecordNotFound) {
return 0, err
}
vendor = model.Vendor{Name: vendorName}
if err := vendor.Insert(); err != nil {
// Another import may have created the vendor after our lookup.
var existing model.Vendor
if lookupErr := model.DB.Select("id").Where("name = ?", vendorName).First(&existing).Error; lookupErr != nil {
return 0, fmt.Errorf("创建供应商 %s 失败: %w", vendorName, err)
}
vendor = existing
}
return 0, err
}
vendorIDByName[vendorName] = vendor.Id
......
......@@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/QuantumNous/new-api/common"
......@@ -220,3 +221,41 @@ func TestModelMetadataExcelPricing(t *testing.T) {
})
}
}
func TestModelImportCreatesMissingVendors(t *testing.T) {
for _, dialect := range []struct{ kind, env string }{{"sqlite", ""}, {"mysql", "TEST_MYSQL_DSN"}, {"postgres", "TEST_POSTGRES_DSN"}} {
t.Run(dialect.kind, func(t *testing.T) {
dsn := os.Getenv(dialect.env)
if dialect.env != "" && dsn == "" {
t.Skip("set " + dialect.env)
}
db := modelManagementDB(t, dialect.kind, dsn)
existing := model.Vendor{Name: "已有供应商", Description: "保留描述"}
require.NoError(t, existing.Insert())
rows := [][]string{{"TeleChat3-438B-A24B", "天翼云"}, {"Xing4.0-29B-A4B", " 天翼云 "}, {"BGE-reranker-v2-m3", "北京智源"}, {"BGE-m3", "北京智源"}, {"existing", "已有供应商"}, {"empty", " "}}
headers := map[string]int{"model_name": 0, "vendor_name": 1}
result := importModelRows(rows, headers, false)
require.Equal(t, 6, result.Created, "%+v", result.Rows)
assert.Zero(t, result.Failed)
// A second import must reuse the persisted vendors, not just the batch cache.
result = importModelRows(rows, headers, true)
require.Equal(t, 6, result.Updated, "%+v", result.Rows)
var vendors []model.Vendor
require.NoError(t, db.Find(&vendors).Error)
require.Len(t, vendors, 3)
byName := map[string]int{}
for _, vendor := range vendors {
byName[vendor.Name] = vendor.Id
assert.Equal(t, 1, vendor.Status)
}
assert.Equal(t, existing.Id, byName[existing.Name])
require.NoError(t, db.First(&existing, existing.Id).Error)
assert.Equal(t, "保留描述", existing.Description)
for _, row := range rows {
var imported model.Model
require.NoError(t, db.Where("model_name = ?", row[0]).First(&imported).Error)
assert.Equal(t, byName[strings.TrimSpace(row[1])], imported.VendorID)
}
})
}
}
......@@ -21,7 +21,7 @@ services:
restart: always
command: --log-dir /app/logs
ports:
- "3000:3000"
- "${NEW_API_PORT:-3000}:3000"
volumes:
- ./data:/data
- ./logs:/app/logs
......
......@@ -209,8 +209,15 @@ it('exports every enabled overview panel, account totals and performance without
'Summary chart',
])
)
expect(wb.Sheets['Model performance'].D2.v).toBe(0.995)
expect(wb.Sheets.Uptime.E2.v).toBe(0.999)
const summary = XLSX.utils.sheet_to_json(wb.Sheets['Usage at a glance'])
expect(summary).not.toContainEqual(
expect.objectContaining({ Metric: 'Runway' })
)
expect(summary).toContainEqual(
expect.objectContaining({ Metric: 'Credit remaining (USD)' })
)
expect(wb.Sheets['Model performance'].D2.v).toBe(99.5)
expect(wb.Sheets.Uptime.E2.v).toBe(99.9)
expect(wb.Sheets.Announcements.D2.v).toBe('Full notice')
expect(wb.Sheets.FAQ.B2.v).toBe('Like this.')
const allValues = JSON.stringify(
......
......@@ -265,16 +265,24 @@ export function SummaryCards() {
[
[t('Start time'), new Date(summaryTimeRange.start_timestamp * 1000)],
[t('End time'), new Date(summaryTimeRange.end_timestamp * 1000)],
[t('Credit remaining'), quotaValue(remainQuota)],
[
`${t('Credit remaining')} (${getCurrencyLabel(t)})`,
quotaValue(remainQuota),
],
[t('Remaining Quota (raw)'), remainQuota],
[t('Total Used'), quotaValue(usedQuota)],
[
`${t('Total Used')} (${getCurrencyLabel(t)})`,
quotaValue(usedQuota),
],
[t('Used Quota (raw)'), usedQuota],
[t('Requests'), requestCount],
[t('Last 24h usage'), quotaValue(recentUsage)],
[
`${t('Last 24h usage')} (${getCurrencyLabel(t)})`,
quotaValue(recentUsage),
],
[t('Quota (raw)'), recentUsage],
[t('Currency'), currencyLabel],
[t('Currency'), getCurrencyLabel(t)],
[t('Status'), t(healthCfg.labelKey)],
[t('Runway'), runwayDisplay],
[t('Runway (days)'), runwayDays],
],
t
......
......@@ -20,6 +20,7 @@ import i18next from 'i18next'
import { afterEach, describe, expect, it } from 'vitest'
import * as XLSX from 'xlsx'
import zh from '@/i18n/locales/zh.json'
import {
DEFAULT_CURRENCY_CONFIG,
useSystemConfigStore,
......@@ -136,7 +137,7 @@ it('uses the configured currency conversion while retaining raw quota', () => {
expect(quotaValue(500000)).toBe(500000)
})
it('preserves fractions for performance percentages rather than multiplying rates by 100 twice', async () => {
it('puts percentage units in headers and exports percentage points as plain numbers', async () => {
const sheets = performanceSheets(
[
{
......@@ -152,9 +153,10 @@ it('preserves fractions for performance percentages rather than multiplying rate
const wb = await buildDashboardWorkbook(sheets, t)
expect(wb.Sheets['Model performance'].D2).toMatchObject({
t: 'n',
v: 0.995,
z: '0.00%',
v: 99.5,
z: '0.00',
})
expect(wb.Sheets['Model performance'].D1.v).toBe('Success rate (%)')
expect(wb.Sheets['Model performance'].E2).toBeUndefined()
})
......@@ -312,3 +314,41 @@ it('does not build a workbook after cancellation', async () => {
buildDashboardWorkbook([], t, controller.signal)
).rejects.toThrow()
})
it('exports ranking shares as numeric percentages without percent signs in cells', async () => {
const workbook = await buildDashboardWorkbook(
usageSheets(usage, 'hour', 'model_name', t),
t
)
const parsed = XLSX.read(
XLSX.write(workbook, { type: 'array', bookType: 'xlsx' }),
{ type: 'array' }
)
const sheet = parsed.Sheets['Usage ranking']
expect(sheet.F1.v).toBe('Request share (%)')
expect(sheet.G1.v).toBe('Consumption share (%)')
expect(sheet.F2).toMatchObject({ t: 'n', v: 60, w: '60.00' })
expect(sheet.G2).toMatchObject({ t: 'n', v: 25, w: '25.00' })
})
it.each([
['CNY', '元 / CNY', 7],
['USD', '美元 / USD', 1],
['TOKENS', '词元 / Tokens', 500000],
] as const)(
'uses Chinese and English units together in %s export headers without changing numeric values',
async (displayType, unit, amount) => {
const instance = i18next.createInstance()
await instance.init({ lng: 'zh', resources: { zh } })
useSystemConfigStore.getState().setConfig({
currency: {
...DEFAULT_CURRENCY_CONFIG,
quotaDisplayType: displayType,
usdExchangeRate: 7,
},
})
const sheet = usageSheets(usage, 'hour', 'model_name', instance.t)[0]
expect(sheet.headers.at(-1)).toBe(`消耗 (${unit})`)
expect(sheet.rows[0].at(-1)).toBe(amount)
}
)
......@@ -20,7 +20,7 @@ import type { TFunction } from 'i18next'
import type { WorkBook } from 'xlsx'
import type { PerfModelSummary } from '@/features/performance-metrics/types'
import { getCurrencyDisplay, getCurrencyLabel } from '@/lib/currency'
import { getCurrencyLabel, quotaValue } from '@/lib/currency'
import { formatChartTime, type TimeGranularity } from '@/lib/time'
import type { DashboardFlowGraph, QuotaDataItem } from '../types'
......@@ -34,15 +34,10 @@ export interface DashboardExportSheet {
}
export const MAX_DASHBOARD_EXPORT_ROWS = 100_000
export function quotaValue(raw: number): number {
const { config, meta } = getCurrencyDisplay()
return meta.kind === 'tokens'
? raw
: (raw / config.quotaPerUnit) * meta.exchangeRate
}
export { quotaValue } from '@/lib/currency'
export function quotaHeader(t: TFunction): string {
return `${t('Consumption')} (${getCurrencyLabel()})`
return `${t('Consumption')} (${getCurrencyLabel(t)})`
}
export function metricSheet(
......@@ -247,7 +242,10 @@ export async function buildDashboardWorkbook(
throw new Error(t('A dashboard cell exceeds the Excel text limit.'))
}
const rows = sheet.rows.map((row) =>
row.map((cell) => {
row.map((cell, column) => {
if (typeof cell === 'number' && sheet.formats?.[column] === '0.00%') {
return Number.isFinite(cell) ? cell * 100 : null
}
if (cell instanceof Date) {
if (!Number.isFinite(cell.getTime())) return null
// Excel stores local wall-clock dates without a timezone. UTC arithmetic
......@@ -269,7 +267,10 @@ export async function buildDashboardWorkbook(
return typeof cell === 'number' && !Number.isFinite(cell) ? null : cell
})
)
const worksheet = XLSX.utils.aoa_to_sheet([sheet.headers, ...rows], {
const headers = sheet.headers.map((header, column) =>
sheet.formats?.[column] === '0.00%' ? `${header} (%)` : header
)
const worksheet = XLSX.utils.aoa_to_sheet([headers, ...rows], {
dateNF: 'yyyy-mm-dd hh:mm:ss',
})
worksheet['!cols'] = sheet.headers.map((header, column) => ({
......@@ -300,10 +301,12 @@ export async function buildDashboardWorkbook(
for (let c = 0; c < sheet.headers.length; c++) {
const cell = worksheet[XLSX.utils.encode_cell({ r, c })]
if (cell?.t === 'n') {
const format = sheet.formats?.[c]
const numberFormat = format === '0.00%' ? '0.00' : format
cell.z =
sheet.rows[r - 1][c] instanceof Date
? 'yyyy-mm-dd hh:mm:ss'
: (sheet.formats?.[c] ?? '#,##0.########')
: (numberFormat ?? '#,##0.########')
}
}
}
......
......@@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { describe, expect, test } from 'vitest'
import { afterEach, describe, expect, test, vi } from 'vitest'
import {
buildRequestRuleExpr,
......@@ -29,6 +29,43 @@ import {
type TimeFunc,
tryParseRequestRuleExpr,
} from '../billing-expr'
import { evalExprLocally } from '../tier-expr'
describe('time-based cost preview', () => {
afterEach(() => vi.useRealTimers())
test.each([
['Asia/Shanghai', 0, 15, 0, 11, 1],
[' UTC ', 16, 15, 6, 10, 31],
['', 16, 15, 6, 10, 31],
['Invalid/Timezone', 16, 15, 6, 10, 31],
['America/New_York', 12, 15, 6, 10, 31],
])(
'evaluates time rules in %s',
(timezone, hour, minute, weekday, month, day) => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2026-10-31T16:15:00Z'))
const tz = JSON.stringify(timezone)
const condition = `hour(${tz}) == ${hour} && minute(${tz}) == ${minute} && weekday(${tz}) == ${weekday} && month(${tz}) == ${month} && day(${tz}) == ${day}`
expect(
evalExprLocally(
`tier("base", p * 2 + c * 4) * (${condition} ? 0.5 : 1)`,
100,
50,
{
cacheReadTokens: 0,
cacheCreateTokens: 0,
cacheCreate1hTokens: 0,
imageTokens: 0,
imageOutputTokens: 0,
audioInputTokens: 0,
audioOutputTokens: 0,
}
)
).toEqual({ cost: 200, matchedTier: 'base', error: null })
}
)
})
function timeCondition(overrides: Partial<TimeCondition> = {}): TimeCondition {
return {
......
......@@ -16,7 +16,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { BILLING_CACHE_VAR_MAP } from './billing-expr'
import {
BILLING_CACHE_VAR_MAP,
TIME_FUNCS,
type TimeFunc,
} from './billing-expr'
export const CACHE_MODE_TIMED = 'timed'
export const CACHE_MODE_GENERIC = 'generic'
......@@ -234,7 +238,7 @@ export function tryParseVisualConfig(
const cfg = normalizeVisualConfig({ tiers })
const regenerated = generateExprFromVisualConfig(cfg)
if (regenerated.replace(/\s+/g, '') !== body.replace(/\s+/g, '')) {
if (regenerated.replaceAll(/\s+/g, '') !== body.replaceAll(/\s+/g, '')) {
return null
}
return cfg
......@@ -268,6 +272,40 @@ export type EvalResult = {
error: string | null
}
function billingTimeValue(
now: Date,
timezone: string,
field: TimeFunc
): number {
const options: Intl.DateTimeFormatOptions = {
timeZone: timezone.trim() || 'UTC',
hourCycle: 'h23',
hour: 'numeric',
minute: 'numeric',
weekday: 'short',
month: 'numeric',
day: 'numeric',
}
let formatter: Intl.DateTimeFormat
try {
formatter = new Intl.DateTimeFormat('en-US', options)
} catch {
// Match the backend's UTC fallback for unknown timezones.
formatter = new Intl.DateTimeFormat('en-US', {
...options,
timeZone: 'UTC',
})
}
const value = formatter
.formatToParts(now)
.find((part) => part.type === field)?.value
if (value === undefined) throw new Error(`Missing time component: ${field}`)
if (field === 'weekday') {
return ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'].indexOf(value)
}
return Number(value)
}
export function evalExprLocally(
exprStr: string,
promptTokens: number,
......@@ -302,6 +340,12 @@ export function evalExprLocally(
for (const field of ESTIMATOR_VARS) {
env[field.var] = extraTokenValues[field.stateKey] || 0
}
// Use one instant for all time conditions in this preview.
const now = new Date()
for (const field of TIME_FUNCS) {
env[field] = (timezone: string): number =>
billingTimeValue(now, timezone, field)
}
const fn = new Function(
...Object.keys(env),
`"use strict"; return (${exprStr});`
......
......@@ -144,6 +144,7 @@ it('writes valid Excel cells preserving zero, booleans and formula-like text, om
'Output Tokens': 12,
Stream: false,
Content: '=SUM(1,2)',
'Cost (USD)': 0.001,
Quota: 500,
}),
])
......@@ -201,9 +202,12 @@ it('exports drawing millisecond timestamps and task second timestamps consistent
expect(XLSX.utils.sheet_to_json(drawingBook.Sheets.Logs)[0]).toMatchObject({
'Task ID': 'mj-1',
Prompt: 'A tree',
'Progress (%)': 100,
})
expect(XLSX.utils.sheet_to_json(taskBook.Sheets.Logs)[0]).toMatchObject({
'Task ID': 'task-1',
'Cost (USD)': 0.001,
'Progress (%)': '',
Quota: 500,
'User ID': 9,
'Channel ID': 3,
......
......@@ -20,7 +20,8 @@ import type { TFunction } from 'i18next'
import type { WorkBook } from 'xlsx'
import { getChannel } from '@/features/channels/api'
import { formatLogQuota, formatTimestampToDate } from '@/lib/format'
import { getCurrencyLabel, quotaValue } from '@/lib/currency'
import { formatTimestampToDate } from '@/lib/format'
import type { UsageLog } from '../data/schema'
import type { FetchLogsConfig, MidjourneyLog, TaskLog } from '../types'
......@@ -143,7 +144,7 @@ export async function buildLogsWorkbook(
t('Group'),
t('Input Tokens'),
t('Output Tokens'),
t('Cost'),
`${t('Cost')} (${getCurrencyLabel(t)})`,
t('Quota'),
t('Duration (seconds)'),
t('Stream'),
......@@ -162,7 +163,7 @@ export async function buildLogsWorkbook(
log.group,
log.prompt_tokens,
log.completion_tokens,
formatLogQuota(log.quota),
quotaValue(log.quota),
log.quota,
log.use_time,
log.is_stream,
......@@ -183,7 +184,7 @@ export async function buildLogsWorkbook(
t('Finish Time'),
t('Action'),
t('Status'),
t('Progress'),
`${t('Progress')} (%)`,
t('Fail Reason'),
]
if (config.logCategory === 'drawing') {
......@@ -194,24 +195,30 @@ export async function buildLogsWorkbook(
formatTimestampToDate(log.finish_time, 'milliseconds'),
log.action,
log.status,
log.progress,
log.progress ? Number(log.progress.replace('%', '')) : '',
log.fail_reason || '',
log.prompt,
])
} else {
headers.push(t('Platform'), t('Model'), t('Group'), t('Cost'), t('Quota'))
headers.push(
t('Platform'),
t('Model'),
t('Group'),
`${t('Cost')} (${getCurrencyLabel(t)})`,
t('Quota')
)
rows = (logs as TaskLog[]).map((log) => [
log.task_id,
formatTimestampToDate(log.submit_time),
formatTimestampToDate(log.finish_time),
log.action,
log.status,
log.progress || '',
log.progress ? Number(log.progress.replace('%', '')) : '',
log.fail_reason || '',
log.platform,
log.properties?.origin_model_name || '',
log.group,
formatLogQuota(log.quota),
quotaValue(log.quota),
log.quota,
])
}
......
......@@ -26,6 +26,10 @@ import { expect, it, vi } from 'vitest'
import * as XLSX from 'xlsx'
import { api } from '@/lib/api'
import {
DEFAULT_CURRENCY_CONFIG,
useSystemConfigStore,
} from '@/stores/system-config-store'
import { buildUsersWorkbook, collectExportUsers } from '../../lib/export'
import { userSchema } from '../../types'
......@@ -165,6 +169,8 @@ it('writes valid Excel with numeric quotas, Chinese text, deleted status and no
expect(rows[0]).toMatchObject({
Username: '=SUM(1,2)',
'Display Name': '测试用户',
'Available Balance (USD)': 0,
'Total Used (USD)': 1,
'Remaining Quota (raw)': 0,
'Used Quota (raw)': 500000,
'Last Login': '',
......@@ -254,3 +260,39 @@ it('disables export while the table is loading or empty', () => {
render(<UsersExportButton filters={filters} disabled />)
expect(screen.getByRole('button', { name: 'Export Excel' })).toBeDisabled()
})
it.each([
['CNY', 'CNY', 7],
['USD', 'USD', 1],
['CUSTOM', 'credits', 3],
['TOKENS', 'Tokens', 500000],
] as const)(
'exports %s units only in headers and retains unabridged numeric balances',
async (displayType, unit, amount) => {
const initial = useSystemConfigStore.getState()
try {
useSystemConfigStore.getState().setConfig({
currency: {
...DEFAULT_CURRENCY_CONFIG,
quotaDisplayType: displayType,
usdExchangeRate: 7,
customCurrencySymbol: 'credits',
customCurrencyExchangeRate: 3,
},
})
const workbook = await buildUsersWorkbook(
[{ ...user, quota: 1000000000 }],
i18next.t
)
const rows = XLSX.utils.sheet_to_json(workbook.Sheets.Users)
expect(rows[0]).toMatchObject({
[`Available Balance (${unit})`]: amount * 2000,
[`Total Used (${unit})`]: amount,
})
expect(workbook.Sheets.Users.H2.t).toBe('n')
expect(workbook.Sheets.Users.I2.t).toBe('n')
} finally {
useSystemConfigStore.setState(initial, true)
}
}
)
......@@ -19,7 +19,7 @@ For commercial licensing, please contact support@quantumnous.com
import type { TFunction } from 'i18next'
import type { WorkBook } from 'xlsx'
import { formatQuotaWithCurrency } from '@/lib/currency'
import { getCurrencyLabel, quotaValue } from '@/lib/currency'
import { formatTimestampToDate } from '@/lib/format'
import { getUsers, searchUsers } from '../api'
......@@ -104,8 +104,8 @@ export async function buildUsersWorkbook(
t('Role'),
t('Status'),
t('Group'),
t('Available Balance'),
t('Total Used'),
`${t('Available Balance')} (${getCurrencyLabel(t)})`,
`${t('Total Used')} (${getCurrencyLabel(t)})`,
t('Remaining Quota (raw)'),
t('Used Quota (raw)'),
t('Request Count'),
......@@ -125,8 +125,8 @@ export async function buildUsersWorkbook(
role ? t(role.labelKey) : String(user.role),
statusLabel,
user.group,
formatQuotaWithCurrency(user.quota),
formatQuotaWithCurrency(user.used_quota),
quotaValue(user.quota),
quotaValue(user.used_quota),
user.quota,
user.used_quota,
user.request_count,
......
{
"translation": {
"CNY_unit": "元 / CNY",
"USD_unit": "美元 / USD",
"Tokens_unit": "詞元 / Tokens",
"A dashboard cell exceeds the Excel text limit.": "看板中有文本超过 Excel 单元格长度限制。",
"API Key name": "API 密钥名称",
"API endpoint": "API 地址",
......
{
"translation": {
"CNY_unit": "元 / CNY",
"USD_unit": "美元 / USD",
"Tokens_unit": "词元 / Tokens",
"A dashboard cell exceeds the Excel text limit.": "看板中有文本超过 Excel 单元格长度限制。",
"API Key name": "API 密钥名称",
"API endpoint": "API 地址",
......
......@@ -78,6 +78,8 @@ For commercial licensing, please contact support@quantumnous.com
* 4. **Billing displays**: Use formatBillingCurrencyFromUSD() to avoid token display
* 5. **Effective exchange rate**: When quotaDisplayType is 'USD', use rate of 1 regardless of config
*/
import type { TFunction } from 'i18next'
import {
useSystemConfigStore,
DEFAULT_CURRENCY_CONFIG,
......@@ -543,21 +545,21 @@ export function formatQuotaWithCurrency(
* - Table column headers
* - Form field labels
*/
export function getCurrencyLabel(): string {
export function getCurrencyLabel(t?: TFunction): string {
const { config, meta } = getCurrencyDisplay()
if (meta.kind === 'tokens') {
return 'Tokens'
return t ? t('Tokens', { context: 'unit' }) : 'Tokens'
}
switch (config.quotaDisplayType) {
case 'CNY':
return 'CNY'
return t ? t('CNY', { context: 'unit' }) : 'CNY'
case 'CUSTOM':
return meta.kind === 'custom' ? meta.symbol : 'Custom'
case 'USD':
default:
return 'USD'
return t ? t('USD', { context: 'unit' }) : 'USD'
}
}
......@@ -635,3 +637,11 @@ export function formatLocalCurrencyAmount(
return formatCurrencyValue(amount, merged, meta)
}
// Numeric quota conversion for exports; units belong in column headers.
export function quotaValue(raw: number): number {
const { config, meta } = getCurrencyDisplay()
return meta.kind === 'tokens'
? raw
: (raw / config.quotaPerUnit) * meta.exchangeRate
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment