async def source_create(data: SourceSchema) -> List[SourceSchema]: response = [] async with db.transaction(): await model.create(**data.private_dict()) async for s in model.query.order_by(model.id).gino.iterate(): response.append(schema.from_orm(s)) return response
async def put(self: web.View) -> Response: await check_permission(self.request, Permissions.WRITE_OBJECTS) try: strategy_id = int(self.request.match_info["id"]) raw_data = await self.request.json() should_live = bool(raw_data["live"]) async with db.transaction(): strategy = await StrategyModel.get(strategy_id) strategy_model = self.schema.from_orm(strategy) strategy_is_live = bool(strategy_model.live_session_id) if strategy_is_live == should_live: return web.json_response(strategy_model.dict()) if should_live: session = LiveSessionSchema(start_datetime=datetime.now()) session.config_json = strategy_model.config_json live_session = await LiveSessionModel.create( **session.private_dict()) strategy_model.live_session_id = live_session.id await strategy.update(**strategy_model.private_dict() ).apply() else: live_session = await LiveSessionModel.get( strategy_model.live_session_id) session = LiveSessionSchema.from_orm(live_session) session.end_datetime = datetime.now() await live_session.update(**session.private_dict()).apply() strategy_model.live_session_id = None await strategy.update(live_session_id=None).apply() return web.json_response(strategy_model.dict()) except Exception as e: traceback.print_exc(file=sys.stdout) raise web.HTTPBadRequest
async def source_delete(data: SourceSchema) -> List[SourceSchema]: response = [] async with db.transaction(): await model.delete.where(model.id == data.id).gino.status() async for s in model.query.order_by(model.id).gino.iterate(): response.append(schema.from_orm(s)) return response
async def post(self: web.View) -> Response: await check_permission(self.request, Permissions.READ_HISTORY) raw_data = await self.request.json() incoming: BacktestSessionInputSchema = BacktestSessionInputSchema( **raw_data) async with db.transaction(): sessions = await get_sessions_with_cached_data(incoming) if len(sessions) > 0: incoming.cached_session_id = sessions[0].id else: incoming.cached_session_id = None new_session_model = await BacktestSessionModel.create( **incoming.private_dict(without=["sources_ids"])) if incoming.sources_ids: for order, source_id in enumerate(incoming.sources_ids): await BacktestSessionSourceModel.create( backtest_session_id=new_session_model.id, source_id=source_id, order=order + 1, ) query = (BacktestSessionModel.outerjoin( BacktestSessionSourceModel).outerjoin(Source).select()) session_with_sources = (await query.where( BacktestSessionModel.id == new_session_model.id ).gino.load( BacktestSessionModel.distinct(BacktestSessionModel.id).load( add_source=Source.distinct(Source.id))).all()) session = self.schema.from_orm(session_with_sources[0]) # session.backtest_sources contain sources bound to this session session.config_json = {"tick_duration_seconds": 8} strategy = await StrategyModel.get(session.strategy_id) for source in session.backtest_sources: if source.config_json["label"] == "btcuah": source.config_json[ "data_type"] = DATA_TYPES.ticks_secondary else: source.config_json["data_type"] = DATA_TYPES.ticks_primary try: from utils.processing.async_queue import AsyncProcessQueue db_q = AsyncProcessQueue() async def execute_backwrite(): value = await db_q.coro_get() await (BacktestSessionModel.update.values(**value).where( BacktestSessionModel.id == session.id).gino.status()) print("Executed backwrite") asyncio.create_task(execute_backwrite()) iterator = start_subprocess_and_listen(backtest, session, strategy, db_q=db_q) return await create_streaming_response(self.request, iterator) except Exception as e: print(e) raise web.HTTPBadRequest
async def get(self: web.View) -> Response: await check_permission(self.request, Permissions.READ) response = [] async with db.transaction(): async for s in Source.query.order_by(Source.id).gino.iterate(): validated = self.schema.from_orm(s) response.append(validated.dict()) return web.json_response(response)
async def delete(self: web.View) -> Response: await check_permission(self.request, Permissions.WRITE_OBJECTS) raw_data = await self.request.json() incoming = self.schema(**raw_data) response = [] async with db.transaction(): await Source.delete.where(Source.id == incoming.id).gino.status() async for s in Source.query.order_by(Source.id).gino.iterate(): validated = self.schema.from_orm(s) response.append(validated.dict()) return web.json_response(response)
async def get(self: web.View) -> Response: await check_permission(self.request, Permissions.READ) response = [] async with db.transaction(): query = (StrategyModel.load( live_session_model=LiveSessionModel).load( resource_model=ResourceModel.on( StrategyModel.resource_id == ResourceModel.id)).order_by(StrategyModel.id)) async for s in query.gino.iterate(): validated = self.schema.from_orm(s) response.append(json.loads(validated.json())) return web.json_response(response)
async def post(self: web.View) -> Response: await check_permission(self.request, Permissions.WRITE_OBJECTS) response = [] try: raw_data = await self.request.json() incoming = self.schema(**raw_data) async with db.transaction(): await Source.create(**incoming.private_dict()) async for s in Source.query.order_by(Source.id).gino.iterate(): response.append(self.schema.from_orm(s).dict()) except Exception: raise web.HTTPBadRequest return web.json_response(response)
async def put(self: web.View) -> Response: await check_permission(self.request, Permissions.WRITE_OBJECTS) strategy_id = int(self.request.match_info["id"]) response = None try: raw_data = await self.request.json() incoming = self.schema(**raw_data) async with db.transaction(): obj = await StrategyModel.get(strategy_id) await obj.update(**incoming.private_dict()).apply() response = self.schema.from_orm(await StrategyModel.get(strategy_id)) except Exception as e: raise web.HTTPBadRequest return web.json_response(response.dict())
async def delete(self: web.View) -> Response: await check_permission(self.request, Permissions.WRITE_OBJECTS) raw_data = await self.request.json() incoming = self.schema(**raw_data) response = [] async with db.transaction(): await StrategyModel.delete.where(StrategyModel.id == incoming.id ).gino.status() async for s in StrategyModel.load( live_session_model=LiveSessionModel).load( resource_model=ResourceModel.on( StrategyModel.resource_id == ResourceModel.id)).query.order_by( StrategyModel.id).gino.iterate(): validated = self.schema.from_orm(s) response.append(json.loads(validated.json())) return web.json_response(response)
async def get(self: web.View) -> Response: await check_permission(self.request, Permissions.READ) source_id = int(self.request.match_info["id"]) async with db.transaction(): source = await Source.get(source_id) source_model = self.schema.from_orm(source) source_config = json.loads(source_model.config_json) table_fullname = source_config["table_name"] if not table_fullname: raise web.HTTPConflict( text="table_name not defined in source config") source_executor: AbstractSource = LIVE_SOURCES[source_model.typename] availability_intervals = await source_executor.list_availability_intervals( interval=3600, table_fullname=table_fullname) source_model.available_intervals = [] for res in availability_intervals: source_model.available_intervals.append([res[0], res[1]]) return web.json_response(body=source_model.json())
async def post(self: web.View) -> Response: await check_permission(self.request, Permissions.WRITE_OBJECTS) response = [] try: raw_data = await self.request.json() incoming = self.schema(**raw_data) async with db.transaction(): await StrategyModel.create(**incoming.private_dict()) async for s in StrategyModel.load( live_session_model=LiveSessionModel).load( resource_model=ResourceModel.on( StrategyModel.resource_id == ResourceModel.id)).query.order_by( StrategyModel.id).gino.iterate(): response.append(json.loads(self.schema.from_orm(s).json())) except Exception as e: raise web.HTTPBadRequest return web.json_response(response)
async def get(self: web.View) -> Response: await check_permission(self.request, Permissions.READ) source_id = int(self.request.match_info["id"]) async with db.transaction(): source = await Source.get(source_id) source_model = self.schema.from_orm(source) source_config = json.loads(source_model.config_json) table_fullname = source_config["table_name"] if not table_fullname: raise web.HTTPConflict( text="table_name not defined in source config") source_executor: AbstractSource = LIVE_SOURCES[source_model.typename] res = await source_executor.list_data_in_interval( table_fullname=table_fullname, start=self.request.query.get("start"), end=self.request.query.get("end"), ) source_model.data = res return web.json_response(body=source_model.json())
async def resources_get_with_sources() -> List[ResourceSchema]: response: List[ResourceSchema] = [] primary_live_source = Source.alias() secondary_live_source = Source.alias() primary_backtest_source = Source.alias() secondary_backtest_source = Source.alias() async with db.transaction(): async for s in model.load( primary_live_source_model=primary_live_source.on( primary_live_source.id == model.primary_live_source_id) ).load(secondary_live_source_model=secondary_live_source.on( secondary_live_source.id == model.secondary_live_source_id )).load(primary_backtest_source_model=primary_backtest_source.on( primary_backtest_source.id == model.primary_backtest_source_id )).load(secondary_backtest_source_model=secondary_backtest_source.on( secondary_backtest_source.id == model.secondary_backtest_source_id)).order_by( model.id).gino.iterate(): response.append(schema.from_orm(s)) return response
async def get_sessions_with_cached_data( session: BacktestSessionInputSchema) -> List[BacktestSessionSchema]: response: List[BacktestSessionSchema] = [] sources_ids = session.sources_ids async with db.transaction(): query = (db.select(BacktestSessionModel).select_from( BacktestSessionModel.join(BacktestSessionSourceModel)).where( and_( BacktestSessionModel.cached_session_id == None, BacktestSessionModel.finished_datetime.isnot(None), BacktestSessionModel.start_datetime <= session.start_datetime, BacktestSessionModel.end_datetime >= session.end_datetime, )).group_by(BacktestSessionModel.id).having( db.func.array_agg( aggregate_order_by( BacktestSessionSourceModel.source_id, BacktestSessionSourceModel.source_id.asc())) == sources_ids)) async for s in query.gino.iterate(): response.append(BacktestSessionSchema.from_orm(s)) return response
async def sources_get() -> List[SourceSchemaWithStats]: response: List[SourceSchemaWithStats] = [] async with db.transaction(): async for s in model.query.order_by(model.id).gino.iterate(): response.append(schema.from_orm(s)) return response