ns = params_kw.copy() debug(f'get_pipeline_usage_log.dspy:{ns=}') if not ns.get('page'): ns['page'] = 1 if not ns.get('sort'): ns['sort'] = ["called_at desc"] sql = '''select a.*, b.pipeline_id_text, c.status_text from (select * from pipeline_usage_log where 1=1 [[filterstr]]) a left join (select id as pipeline_id, name as pipeline_id_text from pipelines where 1 = 1) b on a.pipeline_id = b.pipeline_id left join (select k as status, v as status_text from appcodes_kv where parentid='usage_status') c on a.status = c.status''' filterjson = params_kw.get('data_filter') if filterjson and isinstance(filterjson, str): try: filterjson = json.loads(filterjson) except (json.JSONDecodeError, TypeError): filterjson = None fields_str=r'''[ { "name": "id", "title": "主键", "type": "str", "length": 32, "nullable": "no" }, { "name": "pipeline_id", "title": "产线ID", "type": "str", "length": 32, "nullable": "no" }, { "name": "user_id", "title": "用户ID", "type": "str", "length": 32 }, { "name": "distributor_id", "title": "分销商ID", "type": "str", "length": 32 }, { "name": "call_count", "title": "调用次数", "type": "int", "default": "1" }, { "name": "token_input", "title": "输入Token数", "type": "int", "default": "0" }, { "name": "token_output", "title": "输出Token数", "type": "int", "default": "0" }, { "name": "amount", "title": "金额", "type": "double", "length": 15, "dec": 4, "default": "0" }, { "name": "status", "title": "状态", "type": "str", "length": 20 }, { "name": "error_message", "title": "错误信息", "type": "text" }, { "name": "called_at", "title": "调用时间", "type": "timestamp" } ]''' ori_fields = json.loads(fields_str) if not filterjson: fields = [ f['name'] for f in ori_fields ] filterjson = default_filterjson(fields, ns) filterdic = ns.copy() filterdic['filterstr'] = '' filterdic['userorgid'] = '${userorgid}$' filterdic['userid'] = '${userid}$' if filterjson: dbf = DBFilter(filterjson) conds = dbf.gen(ns) if conds: ns.update(dbf.consts) conds = f' and {conds}' filterdic['filterstr'] = conds ac = ArgsConvert('[[', ']]') vars = ac.findAllVariables(sql) NameSpace = {v:'${' + v + '}$' for v in vars if v != 'filterstr' } filterdic.update(NameSpace) sql = ac.convert(sql, filterdic) debug(f'{sql=}') db = DBPools() dbname = get_module_dbname('pipeline_ops') async with db.sqlorContext(dbname) as sor: r = await sor.sqlPaging(sql, ns) return r return { "total":0, "rows":[] }