108 lines
3.4 KiB
Plaintext
108 lines
3.4 KiB
Plaintext
async def get_cpcwidget(params_kw={}):
|
|
ns = params_kw.copy()
|
|
cpcid = ns.get("cpcid")
|
|
if not cpcid:
|
|
return {'status': False,'msg': '无算力中心ID'}
|
|
|
|
debug(f'get_cpcwidget.dspy:{ns=}')
|
|
if not ns.get('page'):
|
|
ns['page'] = 1
|
|
if not ns.get('sort'):
|
|
|
|
ns['sort'] = 'id'
|
|
|
|
sql_old = f'''select * from cpcwidget where cpcid="{cpcid}" order by type'''
|
|
sql = f'''select min(id) as id, cpcid, type, model, unit_price, resource_unit, sum(stock) as stock, sum(consumed) as consumed, charge_unit, account_unit, max(update_time) as update_time, json_arrayagg(json_object('clusterid', clusterid,'clustername', (select name from cpccluster where id=clusterid), 'stock', stock, 'consumed', consumed)) as import_export from cpcwidget where cpcid="{cpcid}" group by cpcid, type, model, unit_price, resource_unit, charge_unit, account_unit order by type,update_time desc'''
|
|
|
|
filterjson = params_kw.get('data_filter')
|
|
if not filterjson:
|
|
fields = [ f['name'] for f in [
|
|
{
|
|
"name": "id",
|
|
"title": "id",
|
|
"type": "str",
|
|
"length": 32
|
|
},
|
|
{
|
|
"name": "type",
|
|
"title": "部件类型",
|
|
"type": "str",
|
|
"length": 255
|
|
},
|
|
{
|
|
"name": "unit_price",
|
|
"title": "单位单价",
|
|
"type": "float",
|
|
"length": 1
|
|
},
|
|
{
|
|
"name": "cpcid",
|
|
"title": "算力中心id",
|
|
"type": "str",
|
|
"length": 90
|
|
},
|
|
{
|
|
"name": "resource_unit",
|
|
"title": "资源消耗单位",
|
|
"type": "str"
|
|
},
|
|
{
|
|
"name": "model",
|
|
"title": "型号系列",
|
|
"type": "str",
|
|
"length": 99
|
|
},
|
|
{
|
|
"name": "stock",
|
|
"title": "库存量",
|
|
"type": "int",
|
|
"length": 99
|
|
},
|
|
{
|
|
"name": "charge_unit",
|
|
"title": "计费周期",
|
|
"type": "str",
|
|
"length": 32
|
|
},
|
|
{
|
|
"name": "account_unit",
|
|
"title": "计费人民币单位",
|
|
"type": "str"
|
|
},
|
|
{
|
|
"name": "update_time",
|
|
"title": "更新时间",
|
|
"type": "str"
|
|
}
|
|
] ]
|
|
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 = 'kboss'
|
|
async with db.sqlorContext(dbname) as sor:
|
|
ret_info = await sor.sqlExe(sql, {})
|
|
r = {"total":len(ret_info),"rows":[{**item, 'import_export': json.loads(item['import_export'])} for item in ret_info]}
|
|
#r = await sor.sqlPaging(sql, ns)
|
|
debug(f"r值:{r}")
|
|
return {'status': True,'msg': '获取算力部件成功','data': r}
|
|
return {'status': False,'msg': '获取算力部件失败'}
|
|
|
|
ret = await get_cpcwidget(params_kw)
|
|
return ret |