ck 查询
This commit is contained in:
parent
baa8635a17
commit
a802cb2d12
@ -7,6 +7,7 @@ from .endpoints import dashboard
|
|||||||
from .endpoints import report
|
from .endpoints import report
|
||||||
from .endpoints import authority
|
from .endpoints import authority
|
||||||
from .endpoints import table_struct
|
from .endpoints import table_struct
|
||||||
|
from .endpoints import query
|
||||||
|
|
||||||
api_router = APIRouter()
|
api_router = APIRouter()
|
||||||
|
|
||||||
@ -20,3 +21,5 @@ api_router.include_router(report.router, tags=["报表接口"], prefix='/report'
|
|||||||
api_router.include_router(authority.router, tags=["权限管理接口"], prefix='/authority')
|
api_router.include_router(authority.router, tags=["权限管理接口"], prefix='/authority')
|
||||||
|
|
||||||
api_router.include_router(table_struct.router, tags=["表结构"], prefix='/table_struct')
|
api_router.include_router(table_struct.router, tags=["表结构"], prefix='/table_struct')
|
||||||
|
|
||||||
|
api_router.include_router(query.router, tags=["ck"], prefix='/ck')
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from aioredis import Redis
|
import aioch
|
||||||
|
import pandas as pd
|
||||||
from fastapi import APIRouter, Depends, Request
|
from fastapi import APIRouter, Depends, Request
|
||||||
import crud, schemas
|
import crud, schemas
|
||||||
|
|
||||||
from api import deps
|
from api import deps
|
||||||
from db.redisdb import get_redis_pool
|
from db.ckdb import get_ck_db
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@ -13,11 +14,24 @@ router = APIRouter()
|
|||||||
@router.post("/sql")
|
@router.post("/sql")
|
||||||
async def query_sql(
|
async def query_sql(
|
||||||
request: Request,
|
request: Request,
|
||||||
data_in: schemas.GetTable,
|
data_in: schemas.Sql,
|
||||||
rdb: Redis = Depends(get_redis_pool),
|
ckdb: aioch.Client = Depends(get_ck_db),
|
||||||
current_user: schemas.UserDB = Depends(deps.get_current_user)
|
current_user: schemas.UserDB = Depends(deps.get_current_user)
|
||||||
) -> schemas.Msg:
|
) -> schemas.Msg:
|
||||||
"""原 sql 查询 """
|
"""原 sql 查询 """
|
||||||
data = await rdb.get(f'{data_in.game}_{data_in.name}')
|
data, columns = await ckdb.execute(data_in.sql, with_column_types=True, columnar=True)
|
||||||
return schemas.Msg(code=0, msg='ok', data=json.loads(data))
|
df = pd.DataFrame({col[0]: d for d, col in zip(data, columns)})
|
||||||
|
return schemas.Msg(code=0, msg='ok', data=df.to_dict())
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/query")
|
||||||
|
async def query(
|
||||||
|
request: Request,
|
||||||
|
data_in: schemas.CkQuery,
|
||||||
|
ckdb: aioch.Client = Depends(get_ck_db),
|
||||||
|
current_user: schemas.UserDB = Depends(deps.get_current_user)
|
||||||
|
) -> schemas.Msg:
|
||||||
|
""" json解析 sql 查询"""
|
||||||
|
# data, columns = await ckdb.execute(data_in.sql, with_column_types=True, columnar=True)
|
||||||
|
# df = pd.DataFrame({col[0]: d for d, col in zip(data, columns)})
|
||||||
|
return schemas.Msg(code=0, msg='ok', data=data_in)
|
||||||
|
@ -10,14 +10,14 @@ from db.redisdb import get_redis_pool
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.post("/event")
|
@router.post("/")
|
||||||
async def read_event(
|
async def read_table_struct(
|
||||||
request: Request,
|
request: Request,
|
||||||
data_in: schemas.GetTable,
|
data_in: schemas.GetTable,
|
||||||
rdb: Redis = Depends(get_redis_pool),
|
rdb: Redis = Depends(get_redis_pool),
|
||||||
current_user: schemas.UserDB = Depends(deps.get_current_user)
|
current_user: schemas.UserDB = Depends(deps.get_current_user)
|
||||||
) -> schemas.Msg:
|
) -> schemas.Msg:
|
||||||
"""获取时间表结构"""
|
"""获取表结构"""
|
||||||
data = await rdb.get(f'{data_in.game}_{data_in.name}')
|
data = await rdb.get(f'{data_in.game}_{data_in.name}')
|
||||||
return schemas.Msg(code=0, msg='ok', data=json.loads(data))
|
return schemas.Msg(code=0, msg='ok', data=json.loads(data))
|
||||||
|
|
||||||
|
10
ck_test.py
10
ck_test.py
@ -7,9 +7,9 @@ from core.config import settings
|
|||||||
|
|
||||||
|
|
||||||
async def exec_progress():
|
async def exec_progress():
|
||||||
client = Client(**settings.CK_CONFIG)
|
client = Client('119.29.176.224')
|
||||||
|
|
||||||
progress = await client.execute_with_progress('LONG AND COMPLICATED QUERY')
|
progress = await client.execute_with_progress('show databases')
|
||||||
timeout = 20
|
timeout = 20
|
||||||
started_at = datetime.now()
|
started_at = datetime.now()
|
||||||
|
|
||||||
@ -26,10 +26,10 @@ async def exec_progress():
|
|||||||
|
|
||||||
|
|
||||||
async def exec_no_progress():
|
async def exec_no_progress():
|
||||||
client = Client('localhost')
|
client = Client(**settings.CK_CONFIG)
|
||||||
rv = await client.execute('LONG AND COMPLICATED QUERY')
|
rv = await client.execute('show databases')
|
||||||
print(rv)
|
print(rv)
|
||||||
|
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.run_until_complete(asyncio.wait([exec_progress(), exec_no_progress()]))
|
loop.run_until_complete(asyncio.wait([exec_no_progress()]))
|
||||||
|
12
db/ckdb.py
12
db/ckdb.py
@ -1,12 +1,12 @@
|
|||||||
from aioredis import create_redis_pool, Redis
|
from aioch import Client
|
||||||
|
|
||||||
|
|
||||||
class RedisBase:
|
class CKBase:
|
||||||
client: Redis = None
|
client: Client = None
|
||||||
|
|
||||||
|
|
||||||
rdb = RedisBase()
|
ckdb = CKBase()
|
||||||
|
|
||||||
|
|
||||||
def get_redis_pool() -> Redis:
|
def get_ck_db() -> Client:
|
||||||
return rdb.client
|
return ckdb.client
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
import aioredis
|
from aioch import Client
|
||||||
from aioredis import create_redis_pool, Redis
|
|
||||||
|
|
||||||
from core.config import settings
|
from core.config import settings
|
||||||
from .redisdb import rdb
|
from .ckdb import ckdb
|
||||||
|
|
||||||
|
|
||||||
async def connect_to_redis():
|
async def connect_to_ck():
|
||||||
rdb.client = await create_redis_pool(**settings.REDIS_CONF)
|
ckdb.client = Client(**settings.CK_CONFIG)
|
||||||
|
|
||||||
|
|
||||||
async def close_redis_connection():
|
async def close_ck_connection():
|
||||||
rdb.client.close()
|
await ckdb.client.disconnect()
|
||||||
await rdb.client.wait_closed()
|
|
||||||
|
3
main.py
3
main.py
@ -8,6 +8,7 @@ from starlette.middleware.authentication import AuthenticationMiddleware
|
|||||||
from fastapi_authz import CasbinMiddleware
|
from fastapi_authz import CasbinMiddleware
|
||||||
|
|
||||||
from db import connect_to_mongo, close_mongo_connection, get_database
|
from db import connect_to_mongo, close_mongo_connection, get_database
|
||||||
|
from db.ckdb_utils import connect_to_ck, close_ck_connection
|
||||||
from db.redisdb_utils import connect_to_redis, close_redis_connection
|
from db.redisdb_utils import connect_to_redis, close_redis_connection
|
||||||
from utils import *
|
from utils import *
|
||||||
from api.api_v1.api import api_router
|
from api.api_v1.api import api_router
|
||||||
@ -19,9 +20,11 @@ app.include_router(api_router, prefix=settings.API_V1_STR)
|
|||||||
|
|
||||||
app.add_event_handler("startup", connect_to_mongo)
|
app.add_event_handler("startup", connect_to_mongo)
|
||||||
app.add_event_handler("startup", connect_to_redis)
|
app.add_event_handler("startup", connect_to_redis)
|
||||||
|
app.add_event_handler("startup", connect_to_ck)
|
||||||
|
|
||||||
app.add_event_handler("shutdown", close_mongo_connection)
|
app.add_event_handler("shutdown", close_mongo_connection)
|
||||||
app.add_event_handler("shutdown", close_redis_connection)
|
app.add_event_handler("shutdown", close_redis_connection)
|
||||||
|
app.add_event_handler("shutdown", close_ck_connection)
|
||||||
|
|
||||||
|
|
||||||
class CurrentUser(BaseUser):
|
class CurrentUser(BaseUser):
|
||||||
|
@ -6,4 +6,6 @@ from .space import *
|
|||||||
from .dashboard import *
|
from .dashboard import *
|
||||||
from .report import *
|
from .report import *
|
||||||
from .authotity import *
|
from .authotity import *
|
||||||
from .table_struct import *
|
from .table_struct import *
|
||||||
|
|
||||||
|
from .sql import *
|
||||||
|
11
schemas/sql.py
Normal file
11
schemas/sql.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class Sql(BaseModel):
|
||||||
|
sql: str
|
||||||
|
|
||||||
|
|
||||||
|
class CkQuery(BaseModel):
|
||||||
|
report_id: List[str]
|
Loading…
Reference in New Issue
Block a user