From 28910a94c3ca5e1cced834b3ece8e70da2e022f4 Mon Sep 17 00:00:00 2001 From: Thomas Eidens Date: Tue, 5 Oct 2021 15:33:46 +0200 Subject: [PATCH] Added new Docmap queries [Issue: #13] --- neoflask/queries.py | 44 +++++++++++++++++++++++---------- neoflask/views.py | 60 +++++++++++++++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/neoflask/queries.py b/neoflask/queries.py index f96ab371..999719d4 100644 --- a/neoflask/queries.py +++ b/neoflask/queries.py @@ -425,21 +425,18 @@ class DOCMAP_BY_DOI(_DOCMAP): ''' map_docmap_filtering = {'doi': {'req_param': 'doi', 'default': ''}} -class DOCMAPS_BY_REVSERVICE_AND_INTERVAL(_DOCMAP): +class DOCMAPS_FROM_REVSERVICE_IN_INTERVAL(_DOCMAP): code_docmap_filtering = ''' -MATCH ( - col:VizCollection {name: "refereed-preprints"} -)-[:HasSubCol]->( - subcol:VizSubCollection -)-[:HasPaper]->( - paper:VizPaper -)-[:HasReviewDate]->( - revdate:VizReviewDate -) -WHERE DATETIME(revdate.date) >= DATETIME($start_date) - AND DATETIME(revdate.date) < DATETIME($end_date) +MATCH + (col:VizCollection)-[:HasSubCol]->(subcol:VizSubCollection)-[:HasPaper]->(paper:VizPaper), + (preprint:Preprint)-[:inputs]->(Step)<-[:actions]-(Action)<-[:outputs]-(review:RefereeReport) +WHERE col.name = "refereed-preprints" AND subcol.name = $reviewing_service -WITH COLLECT(DISTINCT paper.doi) AS doiList + AND paper.doi = preprint.doi +WITH preprint, DATETIME(MAX(review.published)) AS publish_date_newest_review +WHERE publish_date_newest_review >= DATETIME($start_date) + AND publish_date_newest_review < DATETIME($end_date) +WITH COLLECT(DISTINCT preprint.doi) AS doiList UNWIND doiList AS doi ''' map_docmap_filtering = { @@ -457,6 +454,27 @@ class DOCMAPS_BY_REVSERVICE_AND_INTERVAL(_DOCMAP): }, } +class DOCMAPS_IN_INTERVAL(_DOCMAP): + code_docmap_filtering = ''' +MATCH + (preprint:Preprint)-[:inputs]->(Step)<-[:actions]-(Action)<-[:outputs]-(review:RefereeReport) +WITH preprint, DATETIME(MAX(review.published)) AS publish_date_newest_review +WHERE publish_date_newest_review >= DATETIME($start_date) + AND publish_date_newest_review < DATETIME($end_date) +WITH COLLECT(DISTINCT preprint.doi) AS doiList +UNWIND doiList AS doi + ''' + map_docmap_filtering = { + 'start_date': { + 'req_param': 'start_date', + 'default': '1900-01-01', + }, + 'end_date': { + 'req_param': 'end_date', + 'default': '2900-01-01', + }, + } + class SEARCH_REVIEWS(Query): code = ''' MATCH ( diff --git a/neoflask/views.py b/neoflask/views.py index b58e336b..fdfbf793 100644 --- a/neoflask/views.py +++ b/neoflask/views.py @@ -1,3 +1,4 @@ +from datetime import date, timedelta from flask import ( abort, jsonify, @@ -12,7 +13,8 @@ STATS, BY_DOI, FIG_BY_DOI_IDX, DESCRIBE_REVIEWING_SERVICES, REVIEW_PROCESS_BY_DOI, REVIEW_MATERIAL_BY_ID, - DOCMAP_BY_DOI, DOCMAPS_BY_REVSERVICE_AND_INTERVAL, + DOCMAP_BY_DOI, DOCMAPS_FROM_REVSERVICE_IN_INTERVAL, + DOCMAPS_IN_INTERVAL, BY_AUTO_TOPICS, BY_REVIEWING_SERVICE, AUTOMAGIC, LUCENE_SEARCH, SEARCH_DOI, COVID19, REFEREED_PREPRINTS, @@ -254,31 +256,59 @@ def docmap_semantic_doi(doi: str): j = ask_neo(DOCMAP_BY_DOI(), doi=doi, root=root) return jsonify(j) -@app.route('/api/v2//docmap///', methods=['GET', 'POST']) -def docmap_search(reviewing_service: str, start_date: str, end_date: str, pagination: int): - app.logger.info(f"Searching for docmaps with parameters {reviewing_service}, {start_date}, {end_date}, {pagination}") - if pagination < 0: +def do_paginated_docmap_query(query, page=0, page_size=100, **kwargs): + if page < 0: abort(400) # pagination paramater must be 0 or greater - page_size = 100 - offset = pagination * page_size + + offset = page * page_size root = url_for('root', _external=True) result = ask_neo( - DOCMAPS_BY_REVSERVICE_AND_INTERVAL(), - reviewing_service=reviewing_service, - start_date=start_date, - end_date=end_date, + query, + root=root, offset=offset, page_size=page_size, - root=root, + **kwargs, ) return jsonify(result) -# @app.route('/api/v2/docmap///', methods=['GET', 'POST']) +@app.route('/api/v2//docmap///', methods=['GET', 'POST']) +def docmaps_from_revservice_in_interval(reviewing_service: str, start_date: str, end_date: str, pagination: int): + app.logger.info(f"Getting docmaps for reviewing service \"{reviewing_service}\" from {start_date} to {end_date}, page {pagination}") + return do_paginated_docmap_query( + DOCMAPS_FROM_REVSERVICE_IN_INTERVAL(), + reviewing_service=reviewing_service, + start_date=start_date, + end_date=end_date, + page=pagination, + ) + +@app.route('/api/v2/docmap///', methods=['GET', 'POST']) +def docmaps_in_interval(start_date: str, end_date: str, pagination: int): + app.logger.info(f"Getting docmaps from {start_date} to {end_date}, page {pagination}") + return do_paginated_docmap_query( + DOCMAPS_IN_INTERVAL(), + start_date=start_date, + end_date=end_date, + page=pagination, + ) -# @app.route('/api/v2//docmap//', methods=['GET', 'POST']) +@app.route('/api/v2//docmap/d/', methods=['GET', 'POST']) +def docmaps_from_revservice_in_last_days(reviewing_service: str, days: int, pagination: int): + app.logger.info(f"Getting docmaps for reviewing service \"{reviewing_service}\" with reviews in the last {days} days, page {pagination}") + # The Docmap query is exclusive for the end of the interval. We want any reviews published today so add a day here. + end_date = date.today() + timedelta(days=1) + # With this start date we get any reviews published today and in the last n-1 days, so reviews from a total of n days. + start_date = end_date - timedelta(days=days) + return do_paginated_docmap_query( + DOCMAPS_FROM_REVSERVICE_IN_INTERVAL(), + reviewing_service=reviewing_service, + start_date=str(start_date), + end_date=str(end_date), + page=pagination, + ) -# @app.route('/api/v2//docmap/d/', methods=['GET', 'POST']) +# @app.route('/api/v2//docmap//', methods=['GET', 'POST']) # date of docmap creation and date of docmap update. filter on publishing data or on docmap creation date. \ No newline at end of file