Ejemplo n.º 1
0
    def get(self, asset_id: int):
        assets_id = Transactions.assets_id
        quotas = db.func.sum(Transactions.quotas).label('quotas')

        avg_price = db.func.round(db.func.avg(Transactions._price / 1e9), 2)
        avg_price = db.cast(avg_price, db.Float).label('avgPrice')

        max_price = db.func.round(db.func.max(Transactions._price / 1e9), 2)
        max_price = db.cast(max_price, db.Float).label('maxPrice')

        invested = db.func.round(
            db.func.sum(Transactions._price / 1e9 * Transactions.quotas), 2)
        invested = db.cast(invested, db.Float).label('invested')

        subquery = db.session.query(assets_id, quotas, avg_price, max_price, invested)\
            .group_by(Transactions.assets_id)\
            .having(Transactions.assets_id == asset_id)\
            .subquery()

        name = Assets._name.label('name')
        symbol = Assets._symbol.label('symbol')
        asset = db.session.query(subquery, name, symbol)\
            .filter(subquery.c.assets_id == Assets.assets_id)\
            .one_or_none()

        if asset is None:
            return "", 404

        price = Prices.current_asset(asset.symbol)
        asset = asset._asdict()
        asset['price'] = price[asset['symbol']]

        return asset, 200
Ejemplo n.º 2
0
    def get(self):
        asset_id = Transactions.assets_id
        quotas = db.func.sum(Transactions.quotas).label('quotas')

        avg_price = db.func.round(db.func.avg(Transactions._price / 1e9), 2)
        avg_price = db.cast(avg_price, db.Float).label('avgPrice')

        max_price = db.func.round(db.func.max(Transactions._price / 1e9), 2)
        max_price = db.cast(max_price, db.Float).label('maxPrice')

        invested = db.func.round(
            db.func.sum(Transactions._price / 1e9 * Transactions.quotas), 2)
        invested = db.cast(invested, db.Float).label('invested')

        subquery = db.session.query(asset_id, quotas, avg_price, max_price, invested)\
            .group_by(Transactions.assets_id)\
            .subquery()

        name = Assets._name.label('name')
        symbol = Assets._symbol.label('symbol')
        assets = db.session.query(subquery, name, symbol)\
            .filter(subquery.c.assets_id == Assets.assets_id)\
            .all()

        prices = Prices.current_asset([asset.symbol for asset in assets])
        assets = [asset._asdict() for asset in assets]

        for idx in range(len(assets)):
            ticker = assets[idx]['symbol']
            assets[idx]['price'] = prices[ticker]

        return assets, 200
Ejemplo n.º 3
0
 def _normalized_filters(table, search, ignore=[], cast=[]):
     filters = []
     for column in table.__table__.columns:
         if column.name in ignore:
             continue
         if column.name in cast:
             filters.append(db.cast(column, db.String).contains(search))
         else:
             filters.append(db.func.lower(column).contains(search))
     return filters
Ejemplo n.º 4
0
    def get(self):
        id = Transactions.transaction_id
        symbol = Assets._symbol.label('assetSymbol')
        name = Assets._name.label('assetName')
        quotas = Transactions.quotas
        transaction_type = Transactions._transaction_type.label(
            'transactionType')
        price = db.cast(db.func.round(
            (Transactions._price / 1e9), 2), db.Float).label('price')

        transactions = db.session.query(id,
                                        symbol,
                                        name,
                                        quotas,
                                        transaction_type,
                                        price)\
            .select_from(Assets)\
            .join(Transactions)\
            .all()

        return [t._asdict() for t in transactions], 200
Ejemplo n.º 5
0
    def get(self, transaction_id: int):
        id = Transactions.transaction_id
        symbol = Assets._symbol.label('assetSymbol')
        name = Assets._name.label('assetName')
        quotas = Transactions.quotas
        transaction_type = Transactions._transaction_type.label(
            'transactionType')
        price = db.cast(db.func.round(
            (Transactions._price / 1e9), 2), db.Float).label('price')

        transaction = db.session.query(id,
                                       symbol,
                                       name,
                                       quotas,
                                       transaction_type,
                                       price)\
            .filter(Transactions.transaction_id == transaction_id)\
            .one_or_none()

        if transaction is None:
            return "", 404
        return transaction._asdict(), 200
Ejemplo n.º 6
0
def upload_file():
    if request.method == 'POST':
        f_image = request.files.getlist('image')[0]
        f_audio = request.files.getlist('audio')[0]
        pi_id = request.form['pi_i']
        r_ids = json.loads(request.form['r_ids'])
        pi_serial = request.form['pi_s']
        r = True if request.form['r'] == 'T' else False
        d = request.form['d']
        k = request.form['k']
        i = request.form['i']
        w = request.form['w']
        s = request.form['s']
        spect = list(map(lambda x: array(x), json.loads(request.form['spe'])))
        laser = list(map(lambda x: string_t(x),
                         json.loads(request.form['la'])))
        led = list(map(lambda x: string_t(x), json.loads(request.form['le'])))
        uv = list(map(lambda x: string_t(x), json.loads(request.form['uv'])))
        read_image = f_image.read()
        read_audio = f_audio.read()
        s3_image_return = s3.Bucket(Config.S3_BUCKET).put_object(
            Key=k, Body=read_image)
        s3_audio_return = s3.Bucket(Config.S3_BUCKET).put_object(
            Key=i, Body=read_audio)
        if (len(spect[0]) and len(spect)):
            result = Result(pi_id=pi_id,
                            pi_serial=pi_serial,
                            s3_key=k,
                            etag=s3_image_return.e_tag,
                            ripe=r,
                            timestamp=d,
                            weather=w,
                            slap_type=s,
                            s3_audio_key=i,
                            etag_audio=s3_audio_return.e_tag)
            db.session.add(result)
            for i, s in enumerate(spect):
                reading = Reading(id=r_ids[i],
                                  timestamp=d,
                                  pi_id=pi_id,
                                  pi_serial=pi_serial,
                                  reading=db.cast(s, ARRAY(db.Integer)),
                                  laser=laser[i],
                                  led=led[i],
                                  uv=uv[i])
                result.readings.append(reading)
                db.session.add(reading)
        else:
            result = Result(pi_id=pi_id,
                            pi_serial=pi_serial,
                            s3_key=k,
                            etag=s3_image_return.e_tag,
                            ripe=r,
                            timestamp=d,
                            weather=w,
                            slap_type=s,
                            s3_audio_key=i,
                            etag_audio=s3_audio_return.e_tag)
            db.session.add(result)

        db.session.commit()
        #eval_result = predictor.eval_result(read_file)

        #print("result:",str(result))
        #print("eval:",str(eval_result))
        return (str(result))
    else:
        return ("POST API Endpoint only")
Ejemplo n.º 7
0
 async def is_mentor_available(cls, mentor: User):
     today = datetime.now().date()
     return await MenteeToMentor.query.where(
         db.cast(MenteeToMentor.created_at, db.Date) == today
         and MenteeToMentor.mentor_id == mentor.tg_id).gino.first() is None
Ejemplo n.º 8
0
 async def delete_previous_pairs(cls):
     max_date = await db.func.max(MenteeToMentor.created_at).gino.scalar()
     await MenteeToMentor.delete.where(
         db.cast(MenteeToMentor.created_at, db.Date) == max_date
     ).gino.status()