From 5e29de0c44c6ba6fba740e6a2a47a220124136df Mon Sep 17 00:00:00 2001 From: Torma Date: Tue, 20 Oct 2020 00:15:37 +0200 Subject: [PATCH] remove so many transactions --- src/resources.py | 95 +++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/src/resources.py b/src/resources.py index f0ce022..cec4e9f 100644 --- a/src/resources.py +++ b/src/resources.py @@ -4,7 +4,6 @@ from xeger import Xeger from flask_restful import Resource from flask import request, current_app import requests -from sentry_sdk import start_transaction import pika from db import db from models import SampleMetadata @@ -35,37 +34,32 @@ class SampleResource(Resource): Post request send to the endpoint :return: """ - with start_transaction(op="get-file", name="get-file-from-request"): - if 'file' not in request.files: - return {"err_msg": "no file found"}, 469 - else: - soundfile = request.files['file'] + if 'file' not in request.files: + return {"err_msg": "no file found"}, 469 + else: + soundfile = request.files['file'] - with start_transaction(op="get-description", name="get-description-from-request"): - if 'description' not in request.form: - return {"err_msg": "no description found"}, 470 - else: - description = request.form.get("description") + if 'description' not in request.form: + return {"err_msg": "no description found"}, 470 + else: + description = request.form.get("description") - with start_transaction(op="validate-wave", name="validate-wave-type"): - if soundfile.content_type != 'audio/wave': - current_app.logger.info( - f"Input file was not WAV.") - return {'err_msg': 'Input file not a wave file.'}, 415 + if soundfile.content_type != 'audio/wave': + current_app.logger.info( + f"Input file was not WAV.") + return {'err_msg': 'Input file not a wave file.'}, 415 - with start_transaction(op="validate-description", name="validate-description-schema"): - try: - desc = self.sampleschema.loads(description) - except Exception as e: - current_app.logger.exception(e) - return {'err_msg': 'Input JSON schema invalid'}, 417 + try: + desc = self.sampleschema.loads(description) + except Exception as e: + current_app.logger.exception(e) + return {'err_msg': 'Input JSON schema invalid'}, 417 - with start_transaction(op="tag-generate", name="generate-tag"): - xeger = Xeger(limit=30) - while True: - generated_tag = xeger.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$')[:32] - if len(generated_tag) > 2: # Ensure minimum length - break + xeger = Xeger(limit=30) + while True: + generated_tag = xeger.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$')[:32] + if len(generated_tag) > 2: # Ensure minimum length + break record = SampleMetadata( device_id=desc['device_id'], @@ -82,21 +76,21 @@ class SampleResource(Resource): soundfile, soundfile.content_type, {'Content-Length': soundfile.content_length})}).raise_for_status() - with start_transaction(op="rabbitmq-send", name="rabbitmq-connect-and-publish"): - credentials = pika.PlainCredentials(current_app.config['FLASK_PIKA_PARAMS']['username'], - current_app.config['FLASK_PIKA_PARAMS']['password']) - connection = pika.BlockingConnection( - pika.ConnectionParameters(host=current_app.config['FLASK_PIKA_PARAMS']['host'], - credentials=credentials, - heartbeat=0, - socket_timeout=5)) - channel = connection.channel() - channel.exchange_declare(exchange=current_app.config['EXCHANGE_NAME'], - exchange_type='fanout') - channel.basic_publish(exchange=current_app.config['EXCHANGE_NAME'], - routing_key='feature', - body=json.dumps({'tag': generated_tag}).encode('UTF-8')) - connection.close() + credentials = pika.PlainCredentials(current_app.config['FLASK_PIKA_PARAMS']['username'], + current_app.config['FLASK_PIKA_PARAMS']['password']) + connection = pika.BlockingConnection( + pika.ConnectionParameters(host=current_app.config['FLASK_PIKA_PARAMS']['host'], + credentials=credentials, + heartbeat=0, + socket_timeout=5)) + channel = connection.channel() + channel.exchange_declare(exchange=current_app.config['EXCHANGE_NAME'], + exchange_type='fanout') + channel.basic_publish(exchange=current_app.config['EXCHANGE_NAME'], + routing_key='feature', + body=json.dumps({'tag': generated_tag}).encode('UTF-8')) + connection.close() + except Exception as e: current_app.logger.exception(e) db.session.rollback() @@ -106,13 +100,14 @@ class SampleResource(Resource): db.session.commit() return {"tag": generated_tag}, 200 - def get(self): - """ - Get all stored items - :return: - """ - samples = SampleMetadata.query.all() - return self.samplemetadataschema.dump(list(samples)), 200 + +def get(self): + """ + Get all stored items + :return: + """ + samples = SampleMetadata.query.all() + return self.samplemetadataschema.dump(list(samples)), 200 class SampleParameterResource(Resource):