运营报表小数点后精确到8位

This commit is contained in:
ping 2026-06-18 15:13:40 +08:00
parent 9ed276a313
commit b2837683cb

View File

@ -103,6 +103,30 @@ def _group_key(dt, group_by):
return dt.strftime('%Y-%m-%d')
def _round_amount(value):
return round(float(value or 0), 8)
def _format_amount(value):
return '%.8f' % _round_amount(value)
def _format_amount_item(item):
formatted = dict(item)
formatted['amount'] = _format_amount(formatted.get('amount'))
return formatted
def _format_amount_items(items):
return [_format_amount_item(item) for item in items]
def _format_amount_summary(summary):
formatted = dict(summary)
formatted['amount'] = _format_amount(formatted.get('amount'))
return formatted
def _normalize_usage_row(row, bill_amount_map=None):
usage = _parse_usage_content(row.get('usage_content'))
orderid = row.get('orderid')
@ -118,7 +142,7 @@ def _normalize_usage_row(row, bill_amount_map=None):
'prompt_tokens': int(usage.get('prompt_tokens') or 0),
'completion_tokens': int(usage.get('completion_tokens') or 0),
'total_tokens': int(usage.get('total_tokens') or 0),
'amount': round(amount, 8),
'amount': _round_amount(amount),
'bill_status': row.get('bill_status'),
'orderid': orderid,
'usage_time': row.get('created_at'),
@ -303,7 +327,7 @@ def _aggregate_admin_summary(items, user_map, org_map):
bucket['prompt_tokens'] += item.get('prompt_tokens') or 0
bucket['completion_tokens'] += item.get('completion_tokens') or 0
bucket['total_tokens'] += item.get('total_tokens') or 0
bucket['amount'] = round(bucket['amount'] + float(item.get('amount') or 0), 8)
bucket['amount'] = _round_amount(bucket['amount'] + float(item.get('amount') or 0))
bucket['request_count'] += 1
usage_time = item.get('usage_time')
if usage_time:
@ -350,7 +374,7 @@ def _aggregate_items(items, group_by=None):
bucket['prompt_tokens'] += item.get('prompt_tokens') or 0
bucket['completion_tokens'] += item.get('completion_tokens') or 0
bucket['total_tokens'] += item.get('total_tokens') or 0
bucket['amount'] = round(bucket['amount'] + float(item.get('amount') or 0), 8)
bucket['amount'] = _round_amount(bucket['amount'] + float(item.get('amount') or 0))
bucket['request_count'] += 1
return sorted(buckets.values(), key=lambda x: x['period'], reverse=True)
@ -362,7 +386,7 @@ def _summarize(items):
'prompt_tokens': sum(i.get('prompt_tokens') or 0 for i in items),
'completion_tokens': sum(i.get('completion_tokens') or 0 for i in items),
'total_tokens': sum(i.get('total_tokens') or 0 for i in items),
'amount': round(sum(float(i.get('amount') or 0) for i in items), 8),
'amount': _round_amount(sum(float(i.get('amount') or 0) for i in items)),
}
@ -429,9 +453,9 @@ async def model_usage_user_report(ns={}):
'userid': userid,
'start_time': _format_datetime(start_dt),
'end_time': _format_datetime(end_dt),
'summary': _summarize(all_items),
'summary': _format_amount_summary(_summarize(all_items)),
'group_by': group_by,
'groups': grouped,
'groups': _format_amount_items(grouped),
},
}
@ -449,11 +473,11 @@ async def model_usage_user_report(ns={}):
'userid': userid,
'start_time': _format_datetime(start_dt),
'end_time': _format_datetime(end_dt),
'summary': _summarize(all_items),
'summary': _format_amount_summary(_summarize(all_items)),
'total_count': total_count,
'current_page': current_page,
'page_size': page_size,
'items': items,
'items': _format_amount_items(items),
},
}
except Exception as e:
@ -519,7 +543,7 @@ async def model_usage_admin_report(ns={}):
'orgid': orgid,
'start_time': _format_datetime(start_dt),
'end_time': _format_datetime(end_dt),
'summary': _summarize([]),
'summary': _format_amount_summary(_summarize([])),
'total_count': 0,
'current_page': current_page,
'page_size': page_size,
@ -530,7 +554,7 @@ async def model_usage_admin_report(ns={}):
'orgid': orgid,
'start_time': _format_datetime(start_dt),
'end_time': _format_datetime(end_dt),
'summary': _summarize([]),
'summary': _format_amount_summary(_summarize([])),
'group_by': group_by,
'groups': [],
}
@ -564,9 +588,9 @@ async def model_usage_admin_report(ns={}):
'orgid': orgid,
'start_time': _format_datetime(start_dt),
'end_time': _format_datetime(end_dt),
'summary': _summarize(all_items),
'summary': _format_amount_summary(_summarize(all_items)),
'group_by': group_by,
'groups': grouped,
'groups': _format_amount_items(grouped),
},
}
@ -581,11 +605,11 @@ async def model_usage_admin_report(ns={}):
'orgid': orgid,
'start_time': _format_datetime(start_dt),
'end_time': _format_datetime(end_dt),
'summary': _summarize(all_items),
'summary': _format_amount_summary(_summarize(all_items)),
'total_count': total_count,
'current_page': current_page,
'page_size': page_size,
'items': page_items,
'items': _format_amount_items(page_items),
},
}
except Exception as e: