@@ -0,0 +1,2008 @@
+%0Aimport inspect%0A%0Afrom django.core.management.base import BaseCommand%0Afrom datetime import datetime%0Afrom dimagi.utils.chunked import chunked%0A%0Afrom casexml.apps.case.models import CommCareCase%0Afrom corehq.apps.es import CaseES%0Afrom corehq.elastic import ES_EXPORT_INSTANCE%0Afrom corehq.util.dates import iso_string_to_datetime%0Afrom corehq.util.couch_helpers import paginate_view%0A%0A%0Aclass Command(BaseCommand):%0A    %22%22%22%0A    Returns list of couch case_ids that are not updated in ES%0A%0A        $ ./manage.py stale_cases_in_es %3CDOMAIN%3E %3E case_ids.txt%0A%0A    %22%22%22%0A    help = inspect.cleandoc(__doc__).split('%5Cn')%5B0%5D%0A%0A    def add_arguments(self, parser):%0A        parser.add_argument('domain')%0A%0A    def handle(self, domain, **options):%0A        for case_id in get_server_modified_on_for_domain(domain):%0A            print(case_id)%0A%0A%0Adef get_server_modified_on_for_domain(domain):%0A    start_time = datetime.utcnow()%0A    chunk_size = 1000%0A    chunked_iterator = chunked(paginate_view(%0A        CommCareCase.get_db(),%0A        'cases_by_server_date/by_server_modified_on',%0A        chunk_size=chunk_size,%0A        startkey=%5Bdomain%5D,%0A        endkey=%5Bdomain, %7B%7D%5D,%0A        include_docs=False,%0A        reduce=False%0A    ), chunk_size)%0A    for chunk in chunked_iterator:%0A        case_ids = %5Brow%5B'id'%5D for row in chunk%5D%0A        results = (CaseES(es_instance_alias=ES_EXPORT_INSTANCE)%0A            .domain(domain)%0A            .case_ids(case_ids)%0A            .values_list('_id', 'server_modified_on'))%0A        es_modified_on_by_ids = %7B_id: modified_on for _id, modified_on in results %7D%0A        for row in chunk:%0A            case_id, couch_modified_on = row%5B'id'%5D, row%5B'value'%5D%0A            if iso_string_to_datetime(couch_modified_on) %3E start_time:%0A                # skip cases modified after the script started%0A                continue%0A            es_modified_on = es_modified_on_by_ids.get(case_id)%0A            if not es_modified_on or (es_modified_on != couch_modified_on):%0A                yield (case_id, es_modified_on, couch_modified_on)%0A

Add management command to get couch cases non updated in ES