Example #1
0
    def mutate(root,
               info,
               username,
               poolDisplay,
               poolType=None,
               poolDesc=None):
        if not username:
            raise GraphQLError('invalid input')

        class USER_NAMESPACE:
            bytes = bytes(username, 'utf-8')

        poolId = str(uuid5(USER_NAMESPACE, username + poolDisplay))
        pool = PoolListModel(poolId=poolId,
                             username=username,
                             poolDisplay=poolDisplay,
                             poolType=poolType,
                             poolDesc=poolDesc)
        try:
            dbsession.add(pool)
            dbsession.commit()
            ok = True
        except IntegrityError:
            dbsession.rollback()
            raise GraphQLError('integrity error')
            ok = False
        except:
            dbsession.rollback()
            raise GraphQLError('Unknown error!')
            ok = False
        return CreateSecPool(ok=ok, payload=pool)
Example #2
0
    async def resolve_pools(self, info, username):
        def fetchRec():
            query = PoolList.get_query(info)
            return query.filter(PoolListModel.username == username).all()

        res = fetchRec()
        try:
            # query.filter(PoolListModel.poolType == 'default').all()[-1]
            res[-1]
            return res
        except (NoResultFound, IndexError, ValueError):
            cfg.applogger.info(
                f'No pool record found for {username}, creating a default one...'
            )

            class USER_NAMESPACE:
                bytes = bytes(username, 'utf-8')

            default_rec = PoolListModel(
                poolId=str(uuid5(USER_NAMESPACE, username + 'Watchlist')),
                username=username,
                poolDisplay='Watchlist',
                poolType='default',
                poolDesc='System default list, cannot be deleted',
                oderId=0)

            dbsession.add(default_rec)
            dbsession.commit()
            return fetchRec()
Example #3
0
 def mutate(root, info, primaryKey):
     userInbox = dbsession.query(UserInboxModel).filter(
         UserInboxModel.primaryKey == primaryKey).first()
     userInbox.read = True
     ok = True
     dbsession.commit()
     return markReadUserInbox(ok=ok, payload=userInbox)
Example #4
0
 def mutate(root, info, primaryKey):
     userInbox = dbsession.query(UserInboxModel).filter(
         UserInboxModel.primaryKey == primaryKey).first()
     try:
         dbsession.delete(userInbox)
         dbsession.commit()
         ok = True
     except:
         dbsession.rollback()
         print('DeleteUserInbox rolled back!')
         ok = False
     return DeleteUserInbox(ok=ok, payload=userInbox)
Example #5
0
 def mutate(root, info, poolId):
     pool = dbsession.query(PoolListModel).filter(
         PoolListModel.poolId == poolId).first()
     try:
         dbsession.delete(pool)
         dbsession.commit()
         ok = True
     except:
         dbsession.rollback()
         print('DeleteSecPool rolled back!')
         ok = False
     return DeleteSecPool(ok=ok, payload=pool)
Example #6
0
    def mutate(root, info, poolId, ts_code):
        if not all([poolId, ts_code]):
            raise GraphQLError('invalid input')

        item = PoolItemModel(poolId=poolId, ts_code=ts_code)
        try:
            dbsession.add(item)
            dbsession.commit()
            ok = True
        except IntegrityError:
            dbsession.rollback()
            raise GraphQLError('integrity error')
            ok = False
        except:
            dbsession.rollback()
            raise GraphQLError('Unknown error!')
            ok = False
        return CreatePoolItem(ok=ok, payload=item)
Example #7
0
 def mutate(root,
            info,
            poolId,
            poolDisplay=None,
            poolType=None,
            poolDesc=None):
     # try:
     pool = dbsession.query(PoolListModel).filter(
         PoolListModel.poolId == poolId).first()
     pool.poolDisplay = poolDisplay if poolDisplay else pool.poolDisplay
     pool.poolType = poolType if poolType else pool.poolType
     pool.poolDesc = poolDesc if poolDesc else pool.poolDesc
     dbsession.commit()
     ok = True
     # except:
     #     dbsession.rollback()
     #     raise GraphQLError('Unknown error!')
     #     ok = False
     return UpdateSecPool(ok=ok, payload=pool)
Example #8
0
 def callback(fut):
     # if fut resolves to be of cancelled state
     if fut.cancelled():
         tasks_db[fut.uuid].status = 'cancelled'
     # if fut resolves to be done with result = result, or done with result = 'error'
     elif fut.done():
         tasks_db[fut.uuid].finished = datetime.now()
         tasks_db[fut.uuid].status = 'done'
         error = fut.exception()
         if error:
             # print(fut.uuid,f': return error: {error}')
             tasks_db[fut.uuid].result = 'error'
             usermsg = user_msg.append(UserInboxModel(username = current_user.username, source = 'task', read = False,
             title = 'Task Failed', content = f'{tasks_db[fut.uuid].uuname}',createDate = datetime.now()))
         else:
             result = fut.result()
             # print(fut.uuid,': return result:',result)
             tasks_db[fut.uuid].result = result
             usermsg = user_msg.append(UserInboxModel(username = current_user.username, source = 'task', read = False,
             title = 'Task Completed', content = f'{tasks_db[fut.uuid].uuname}',createDate = datetime.now()))
     
     # try pick up a new task and submit to process, i.e. the first one with "to_start" status, if no one found, do nothing
     for task in tasks_db.values():
         if task.status == 'to_start' and task.worker==fut.worker:
             ex = request.app.state.executor if task.worker == 'sys' else request.app.state.emulator
             task.status = 'in_progress'
             task.started = datetime.now()
             newfut = ex.submit(API_MAP[task.uuname]['func'], uuid=task.uuid)
             newfut.uuid = task.uuid
             newfut.worker = task.worker
             tasks_db[task.uuid].fut_handle = newfut
             newfut.add_done_callback(callback)
             break
     
     cfg.evtlogger.info(libs.logtag('task','u'))
     dbsession.add_all(user_msg)
     dbsession.commit()
     cfg.evtlogger.info(libs.logtag('notify','task completed.'))