# Path: fixes/cleanup_index.py
# @default_index
# def cleanup_index_all(index=None, batch=5, synchronous=False):
#     # Find entries in elasticsearch which are from previous versions of submissions and remove
#     # Get all finished HEPSubmission ids with version numbers less than the max
#     # finished version by doing a left outer join of hepsubmission with itself
#     h1 = aliased(HEPSubmission)
#     h2 = aliased(HEPSubmission)
# 
#     qry = db.session.query(distinct(h1.id)) \
#         .join(h2,
#               and_(h1.publication_recid == h2.publication_recid,
#                    h1.version < h2.version,
#                    h2.overall_status == 'finished'),
#               isouter=True) \
#         .filter(h2.id != None) \
#         .order_by(h1.id)
#     res = qry.all()
#     ids = [x[0] for x in res]
# 
#     count = 0
#     while count < len(ids):
#         batch_ids = ids[count:min(count + batch, len(ids))]
#         if synchronous:
#             cleanup_index_batch(batch_ids, index)
#         else:
#             print('Sending batch of IDs {0} to {1} to celery'.format(batch_ids[0], batch_ids[-1]))
#             cleanup_index_batch.delay(batch_ids, index)
#         count += batch
# 
# @shared_task
# def cleanup_index_batch(hepsubmission_record_ids, index):
#     log.info('Cleaning up index for data records for hepsubmission IDs {0} to {1}'.format(hepsubmission_record_ids[0], hepsubmission_record_ids[-1]))
#     # Find all datasubmission entries matching the given hepsubmission ids,
#     # where the version is not the highest version present (i.e. there is not
#     # a v2 record with the same associated_recid)
#     d1 = aliased(DataSubmission)
#     d2 = aliased(DataSubmission)
#     qry = db.session.query(d1.associated_recid) \
#         .join(HEPSubmission,
#               and_(HEPSubmission.publication_recid == d1.publication_recid,
#                    HEPSubmission.version == d1.version),
#               isouter=True) \
#         .join(d2,
#               and_(d1.associated_recid == d2.associated_recid,
#                    d1.version < d2.version),
#               isouter=True) \
#         .filter(HEPSubmission.id.in_(hepsubmission_record_ids), d2.id == None) \
#         .order_by(d1.id)
#     res = qry.all()
# 
#     ids = [x[0] for x in res]
#     if ids:
#         log.info(f'Deleting entries from index with ids {ids}')
#         s = RecordsSearch(index=index).filter('terms', _id=ids)
#         s.delete()


from invenio_db import db
from unittest.mock import call
from hepdata.modules.submission.models import HEPSubmission, DataSubmission
from fixes.cleanup_index import cleanup_index_all, cleanup_index_batch
    from invenio_search import RecordsSearch




def test_cleanup_index_all(app, load_default_data, identifiers, mocker):
    index = app.config.get('ELASTICSEARCH_INDEX')

    m = mocker.patch('fixes.cleanup_index.cleanup_index_batch')

    # Should be no calls made at first as there is only one version of all submissions
    cleanup_index_all(index=index, synchronous=True)
    m.assert_not_called()
    m.reset_mock()

    # Create a new version for ins1283842
    new_submission = HEPSubmission(publication_recid=1, inspire_id=identifiers[0]["inspire_id"], version=2, overall_status='finished')
    db.session.add(new_submission)
    db.session.commit()
    # New id should be 3
    assert(new_submission.id == 3)

    # Cleanup should now clean up id 1

# Path: fixes/cleanup_index.py
# @default_index
# def cleanup_index_all(index=None, batch=5, synchronous=False):
#     # Find entries in elasticsearch which are from previous versions of submissions and remove
#     # Get all finished HEPSubmission ids with version numbers less than the max
#     # finished version by doing a left outer join of hepsubmission with itself
#     h1 = aliased(HEPSubmission)
#     h2 = aliased(HEPSubmission)
# 
#     qry = db.session.query(distinct(h1.id)) \
#         .join(h2,
#               and_(h1.publication_recid == h2.publication_recid,
#                    h1.version < h2.version,
#                    h2.overall_status == 'finished'),
#               isouter=True) \
#         .filter(h2.id != None) \
#         .order_by(h1.id)
#     res = qry.all()
#     ids = [x[0] for x in res]
# 
#     count = 0
#     while count < len(ids):
#         batch_ids = ids[count:min(count + batch, len(ids))]
#         if synchronous:
#             cleanup_index_batch(batch_ids, index)
#         else:
#             print('Sending batch of IDs {0} to {1} to celery'.format(batch_ids[0], batch_ids[-1]))
#             cleanup_index_batch.delay(batch_ids, index)
#         count += batch
# 
# @shared_task
# def cleanup_index_batch(hepsubmission_record_ids, index):
#     log.info('Cleaning up index for data records for hepsubmission IDs {0} to {1}'.format(hepsubmission_record_ids[0], hepsubmission_record_ids[-1]))
#     # Find all datasubmission entries matching the given hepsubmission ids,
#     # where the version is not the highest version present (i.e. there is not
#     # a v2 record with the same associated_recid)
#     d1 = aliased(DataSubmission)
#     d2 = aliased(DataSubmission)
#     qry = db.session.query(d1.associated_recid) \
#         .join(HEPSubmission,
#               and_(HEPSubmission.publication_recid == d1.publication_recid,
#                    HEPSubmission.version == d1.version),
#               isouter=True) \
#         .join(d2,
#               and_(d1.associated_recid == d2.associated_recid,
#                    d1.version < d2.version),
#               isouter=True) \
#         .filter(HEPSubmission.id.in_(hepsubmission_record_ids), d2.id == None) \
#         .order_by(d1.id)
#     res = qry.all()
# 
#     ids = [x[0] for x in res]
#     if ids:
#         log.info(f'Deleting entries from index with ids {ids}')
#         s = RecordsSearch(index=index).filter('terms', _id=ids)
#         s.delete()


# Path: tests/fixes_test.py
from invenio_db import db
from unittest.mock import call
from hepdata.modules.submission.models import HEPSubmission, DataSubmission
from fixes.cleanup_index import cleanup_index_all, cleanup_index_batch
    from invenio_search import RecordsSearch





def test_cleanup_index_all(app, load_default_data, identifiers, mocker):
    index = app.config.get('ELASTICSEARCH_INDEX')

    m = mocker.patch('fixes.cleanup_index.cleanup_index_batch')

    # Should be no calls made at first as there is only one version of all submissions
    cleanup_index_all(index=index, synchronous=True)
    m.assert_not_called()
    m.reset_mock()

    # Create a new version for ins1283842
    new_submission = HEPSubmission(publication_recid=1, inspire_id=identifiers[0]["inspire_id"], version=2, overall_status='finished')
    db.session.add(new_submission)
    db.session.commit()
    # New id should be 3
    assert(new_submission.id == 3)

    # Cleanup should now clean up id 1

    cleanup_index_all(index=index, synchronous=True)