<reponame>Aurora-Network-Global/sdg_query_execution
################
#    imports   #
################

import json

from elasticsearch import Elasticsearch
from flask import Response

from model.AbstractText import AbstractText
from service import project_service
from flask import current_app as app
from . import analysis_blueprint

es = Elasticsearch()


################
#    routes    #
################

@analysis_blueprint.route('/prepare_abstracts/<project_id>', methods=['POST'])
def count_keywords(project_id):
    """
    returns a list of abstracts contained in the project
    :param project_id: the ID of the current project
    :return: a JSON formatted list of the abstracts
    """
    project = project_service.load_project(project_id)
    with app.app_context():
        location = app.config.get("LIBINTEL_DATA_DIR")
    out_dir = location + '/out/' + project['project_id'] + '/'
    result = es.search(index=project['project_id'], doc_type='all_data',
                       filter_path=["hits.hits._source.scopus_abtract_retrieval.abstract", "hits.hits._id"],
                       request_timeout=600)
    keyword_list = []
    for hit in result["hits"]["hits"]:
        keyword_list.append(AbstractText(hit['_id'], hit["_source"]["scopus_abtract_retrieval"]["abstract"]))
    with open(out_dir + 'abstracts.json', 'w') as json_file:
        json_file.write(json.dumps([ob.__dict__ for ob in keyword_list]))
        json_file.close()
    return Response({"status": "FINISHED"}, status=204)


def get_length(x):
    if x is not None:
        return len(x)
    else:
        return 0


