fix: 百万单位定价展示乘以单位值 + 生产混合格式走formula路径
1. get_pricing_display: unit_price 乘以 unit_val 得到人类可读展示价 (如 4e-06 * 1000000 = 4.0 元/百万tokens) 2. get_pricing_display: unit_values 默认值补全百万/秒/千等映射 3. is_new_format 判断增加类型检查: price_factors为list或unit_prices为dict 时走旧格式formula路径,避免生产数据TypeError 4. filters区间定价展示价同步乘以unit_val
This commit is contained in:
parent
e2d46f4074
commit
df1fa2cfe0
@ -560,7 +560,12 @@ order by b.enabled_date desc"""
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# 判断是旧格式还是新格式
|
# 判断是旧格式还是新格式
|
||||||
is_new_format = p.get('price_factors') is not None and p.get('unit_prices') is not None
|
# 新格式要求 price_factors 是标量(str)、unit_prices 是标量(number)
|
||||||
|
# 生产数据中 price_factors 可能是 list、unit_prices 可能是 dict,此时走旧格式 formula
|
||||||
|
_raw_pf = p.get('price_factors')
|
||||||
|
_raw_up = p.get('unit_prices')
|
||||||
|
is_new_format = (_raw_pf is not None and _raw_up is not None
|
||||||
|
and not isinstance(_raw_pf, list) and not isinstance(_raw_up, dict))
|
||||||
is_old_format = p.get('formula') is not None
|
is_old_format = p.get('formula') is not None
|
||||||
|
|
||||||
if not is_new_format and not is_old_format:
|
if not is_new_format and not is_old_format:
|
||||||
@ -793,25 +798,37 @@ async def get_pricing_display(ppid):
|
|||||||
label = fdef.get('label', k) if isinstance(fdef, dict) else k
|
label = fdef.get('label', k) if isinstance(fdef, dict) else k
|
||||||
filter_labels[label] = v
|
filter_labels[label] = v
|
||||||
|
|
||||||
# 新格式:price_factors + unit_prices + unit
|
# 新格式:price_factors(scalar) + unit_prices(scalar) + unit
|
||||||
is_new_format = p.get('price_factors') is not None and p.get('unit_prices') is not None
|
# 注意:生产数据中 price_factors 可能是 list、unit_prices 可能是 dict(混合格式),
|
||||||
|
# 此时应走旧格式 formula 路径
|
||||||
|
_raw_pf = p.get('price_factors')
|
||||||
|
_raw_up = p.get('unit_prices')
|
||||||
|
is_new_format = (_raw_pf is not None and _raw_up is not None
|
||||||
|
and not isinstance(_raw_pf, list) and not isinstance(_raw_up, dict))
|
||||||
|
|
||||||
if is_new_format:
|
if is_new_format:
|
||||||
factor_name = p['price_factors']
|
factor_name = _raw_pf
|
||||||
unit_price = p['unit_prices']
|
unit_price = _raw_up
|
||||||
unit_str = p.get('unit', '次')
|
unit_str = p.get('unit', '次')
|
||||||
|
|
||||||
# 获取 factor label
|
# 获取 factor label
|
||||||
fdef = fields.get(factor_name, {})
|
fdef = fields.get(factor_name, {})
|
||||||
factor_label = fdef.get('label', factor_name) if isinstance(fdef, dict) else factor_name
|
factor_label = fdef.get('label', factor_name) if isinstance(fdef, dict) else factor_name
|
||||||
|
|
||||||
|
# 单位换算:unit_price 存储的是原始单价(如 4e-06 元/token),
|
||||||
|
# 展示时需要乘以单位值(如 百万=1000000)得到人类可读价格(如 4 元/百万token)
|
||||||
|
unit_val = unit_values.get(unit_str, 1)
|
||||||
|
if isinstance(unit_val, str):
|
||||||
|
unit_val = float(unit_val)
|
||||||
|
display_price = unit_price * unit_val
|
||||||
|
|
||||||
# 构建 unit_label (元/单位)
|
# 构建 unit_label (元/单位)
|
||||||
unit_label = f"元/{unit_str}" if unit_str else "元"
|
unit_label = f"元/{unit_str}" if unit_str else "元"
|
||||||
|
|
||||||
price_factors_display = [{
|
price_factors_display = [{
|
||||||
'factor': factor_name,
|
'factor': factor_name,
|
||||||
'label': factor_label,
|
'label': factor_label,
|
||||||
'unit_price': unit_price,
|
'unit_price': display_price,
|
||||||
'unit': unit_str,
|
'unit': unit_str,
|
||||||
'unit_label': unit_label
|
'unit_label': unit_label
|
||||||
}]
|
}]
|
||||||
@ -821,10 +838,11 @@ async def get_pricing_display(ppid):
|
|||||||
tiered_pricing = []
|
tiered_pricing = []
|
||||||
for fi in p['filters']:
|
for fi in p['filters']:
|
||||||
fi_copy = fi.copy()
|
fi_copy = fi.copy()
|
||||||
|
raw_tier_price = fi.get('unit_prices', unit_price)
|
||||||
fi_copy.pop('unit_prices', None)
|
fi_copy.pop('unit_prices', None)
|
||||||
tiered_pricing.append({
|
tiered_pricing.append({
|
||||||
'filters': fi_copy,
|
'filters': fi_copy,
|
||||||
'unit_prices': fi.get('unit_prices', unit_price)
|
'unit_prices': raw_tier_price * unit_val
|
||||||
})
|
})
|
||||||
if tiered_pricing:
|
if tiered_pricing:
|
||||||
price_factors_display[0]['tiered'] = tiered_pricing
|
price_factors_display[0]['tiered'] = tiered_pricing
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user