feat: integrate chatbox research with datasets and backtests
This commit is contained in:
+21
-1
@@ -13,15 +13,20 @@ from sqlalchemy import delete, func, select, update
|
||||
from .alphas import list_statement, sorted_statement, summary
|
||||
from .jobs import ACTIVE
|
||||
from .models import Account, Alpha, Job, JobItem, Pnl, Research, ResearchTag, SelfCorrelation, now
|
||||
from .research.provenance import alpha_sources, source_kinds
|
||||
from .schemas import AlphaDetail, AlphaPage, BulkUpdate, JobInput, JobOutput, ResearchUpdate, normalize_tags
|
||||
|
||||
|
||||
class Business:
|
||||
def __init__(self, db, ai_context=None):
|
||||
from .backtests.service import Backtests
|
||||
from .catalog.service import Catalog
|
||||
from .research.service import ResearchBuilder
|
||||
|
||||
self.db = db
|
||||
self.backtests = Backtests(db, ai_context)
|
||||
self.catalog = Catalog(db)
|
||||
self.research_builder = ResearchBuilder(db, self.backtests)
|
||||
|
||||
async def search_alphas(self, filters):
|
||||
query = list_statement(filters)
|
||||
@@ -41,8 +46,16 @@ class Business:
|
||||
)
|
||||
).all()
|
||||
}
|
||||
sources = await source_kinds(self.db, [a.id for a, _ in rows])
|
||||
return AlphaPage(
|
||||
items=[{**summary(a, r), "local_correlation": correlations.get(a.id)} for a, r in rows],
|
||||
items=[
|
||||
{
|
||||
**summary(a, r),
|
||||
"local_correlation": correlations.get(a.id),
|
||||
"source_kinds": sources.get(a.id, []),
|
||||
}
|
||||
for a, r in rows
|
||||
],
|
||||
total=total,
|
||||
limit=filters.limit,
|
||||
offset=filters.offset,
|
||||
@@ -97,14 +110,21 @@ class Business:
|
||||
select(func.count()).select_from(Research).where(Research.favorite.is_(True))
|
||||
)
|
||||
result["last_sync"] = await self.db.scalar(select(func.max(Alpha.synced_at)))
|
||||
result["source"] = sorted(
|
||||
{kind for kinds in (await source_kinds(self.db)).values() for kind in kinds}
|
||||
)
|
||||
return result
|
||||
|
||||
async def get_alpha_sources(self, alpha_id, limit=25, offset=0):
|
||||
return await alpha_sources(self.db, alpha_id, limit, offset)
|
||||
|
||||
async def get_alpha(self, alpha_id):
|
||||
a, r = await self.db.get(Alpha, alpha_id), await self.db.get(Research, alpha_id)
|
||||
if a is None or r is None:
|
||||
raise HTTPException(404, "Alpha 尚未同步")
|
||||
return AlphaDetail(
|
||||
**summary(a, r),
|
||||
source_kinds=(await source_kinds(self.db, [alpha_id])).get(alpha_id, []),
|
||||
**{
|
||||
key: getattr(a, key)
|
||||
for key in (
|
||||
|
||||
Reference in New Issue
Block a user