Example #1
0
def solve():
    try:
        start_time = time.time()
        query = request.args

        # TEMPORARY
        search_type = query['location']
        headers = dict(request.headers)

        if AUTHORIZATION not in headers or HOST not in headers:
            set_transaction_details()
            log('authentication', {'reason': "Insufficient headers"}, status=2)
            return respond(400,
                           response_data=bytes(
                               json.dumps({'error': "Unable to resolve user"},
                                          default=str), 'utf-8'))

        user_data = authenticator(headers)
        if user_data.status_code != 200:
            set_transaction_details()
            log('auth-failed', {'reason': "Authentication Failed"}, status=2)
            return respond(user_data.status_code)

        # user_id, tenant_id = 'YwqYVDrSdpQ5aKebE5xxh60S7P42', 'amgentest-dwkd7'
        user_data = user_data.json()
        user_id, tenant_id, email_id = \
            user_data["user_id"], user_data["tenant_id"], user_data['email_id']

        set_transaction_details(tenant_id, user_id)

        # Authorize
        payload = {
            "user_id": user_id,
            "api_policies": json.dumps(constants.POLICIES),
            "required_creds": json.dumps(constants.CREDS_REQUIRED)
        }

        db_creds = authorizer(headers, payload)

        db_creds = db_creds.json()
        question = query[QUESTION]

        log(
            'trans-details', {
                "link":
                f"https://{headers[HOST]}/search/#/solve/search/{urllib.parse.quote(question, safe='')}",
                "email": email_id
            })

        doc_type = None
        if ':' in question and question.split(
                ':', 1)[0].lower() in constants.SUPPORTED_DOC_TYPES:
            parsed_question = question.split(':', 1)
            doc_type = parsed_question[0].lower()
            question = parsed_question[1]

        print("Question:", question)
        log('-', question)

        # Question text clean up
        # question = special_char_handling_obj.handle_all_special_chars(question)
        # TEMPORARY
        question = question.replace(",", " ")
        question = question.replace("'s ", " ")
        question = question.replace(".", " ")
        question = question.strip("[? ]")
        question = re.sub(r" +", " ", question).strip()
        log('pre-process', question)

        translator = None
        try:
            if search_type == 'app':
                translator = Translator(KB_TRIE, tenant_id, user_id,
                                        constants.USER_LEVEL_POLICY, question,
                                        doc_type)
            else:
                translator = Translator(KB_TRIE, tenant_id, user_id,
                                        db_creds['policy'], question, doc_type)
            queries_formed = translator.form_queries(session)
            session.modified = True
            session.permanent = True

            if not queries_formed:
                response = respond(400,
                                   response_data=bytes(
                                       "No meaningful word found in question",
                                       'utf-8'))
            else:
                save_time = time.time()
                translator.initialize_db(db_creds)
                translator.save_queries()
                timesd = time.time() - save_time
                translator.execute_queries()
                data = translator.format_response(search_type, KB_TRIE)
                # data['response']['savequerytime'] = timesd
                data['response']['result'] = session
                response = respond(200,
                                   response_data=bytes(
                                       json.dumps(data['response'],
                                                  default=str), 'utf-8'))
            log('end-flag', status=0, time=time.time() - start_time)
            return response
        except Exception:
            log('end-flag', status=1, time=time.time() - start_time)
            raise

    except Exception as e:
        raise
        sentry_sdk.capture_exception(e)
        # raise(e)
        return respond(502)

    finally:
        close_pubsub_client()
Example #2
0
def pagination():
    try:
        start_time = time.time()
        query = request.args
        trans_id = query['trans_id']
        offset = query['offset']
        offset = int(offset) * 10
        print(offset)

        # TEMPORARY
        search_type = query['location']
        headers = dict(request.headers)

        if AUTHORIZATION not in headers or HOST not in headers:
            set_transaction_details(trans_id=trans_id)
            log('authentication', {'reason': "Insufficient headers"}, status=2)
            return respond(400,
                           response_data=bytes(
                               json.dumps({'error': "Unable to resolve user"},
                                          default=str), 'utf-8'))

        user_data = authenticator(headers)
        if user_data.status_code != 200:
            set_transaction_details(trans_id=trans_id)
            log('auth-failed', {'reason': "Authentication Failed"}, status=2)
            return respond(user_data.status_code)

        # user_id, tenant_id = 'YwqYVDrSdpQ5aKebE5xxh60S7P42', 'amgentest-dwkd7'
        user_data = user_data.json()
        user_id, tenant_id, email_id = \
            user_data["user_id"], user_data["tenant_id"], user_data['email_id']

        set_transaction_details(tenant_id, user_id, trans_id=trans_id)

        # Authorize
        payload = {
            "user_id": user_id,
            "api_policies": json.dumps(constants.POLICIES),
            "required_creds": json.dumps(constants.CREDS_REQUIRED)
        }
        db_creds = authorizer(headers, payload)
        db_creds = db_creds.json()

        log(
            'trans-details', {
                "link":
                f"https://{headers[HOST]}/search/#/pagination//{urllib.parse.quote(trans_id, safe='')}",
                "email": email_id
            })
        fetch_redis_query = time.time()
        doc_type = None
        translator = None
        try:
            if search_type == 'app':
                translator = Translator(KB_TRIE, tenant_id, user_id,
                                        constants.USER_LEVEL_POLICY, doc_type)
            else:
                translator = Translator(KB_TRIE, tenant_id, user_id,
                                        db_creds['policy'], doc_type)
            translator.initialize_db(db_creds)
            queries_formed = translator.fetch_queries(trans_id, offset)
            stored_time = time.time() - fetch_redis_query
            print(stored_time)
            if not queries_formed:
                response = respond(400,
                                   response_data=bytes(
                                       "Query not found in redis", 'utf-8'))
            else:
                translator.execute_queries()
                data = translator.format_response(search_type, KB_TRIE)
                # data['response']['redisstored'] = stored_time
                data['response']['result'] = session
                # print(session['response'])
                response = respond(200,
                                   response_data=bytes(
                                       json.dumps(data['response'],
                                                  default=str), 'utf-8'))
            log('end-flag', status=0, time=time.time() - start_time)
            return response
        except Exception:
            log('end-flag', status=1, time=time.time() - start_time)
            raise

        finally:
            if translator:
                translator.close()

    except Exception as e:
        raise
        sentry_sdk.capture_exception(e)
        # raise(e)
        return respond(502)