nodes: 0, module; 1, function_definition; 2, function_name:delete_inactive_unique_identities; 3, parameters; 4, block; 5, identifier:es; 6, identifier:sortinghat_db; 7, identifier:before_date; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, if_statement; 13, expression_statement; 14, while_statement; 15, expression_statement; 16, comment:"""Select the unique identities not seen before `before_date` and
      delete them from SortingHat.
  
      :param es: ElasticSearchDSL object
      :param sortinghat_db: instance of the SortingHat database
      :param before_date: datetime str to filter the identities
      """; 17, assignment; 18, assignment; 19, assignment; 20, comparison_operator:scroll_size == 0; 21, block; 22, assignment; 23, comparison_operator:scroll_size > 0; 24, block; 25, call; 26, identifier:page; 27, call; 28, identifier:sid; 29, subscript; 30, identifier:scroll_size; 31, subscript; 32, identifier:scroll_size; 33, integer:0; 34, expression_statement; 35, return_statement; 36, identifier:count; 37, integer:0; 38, identifier:scroll_size; 39, integer:0; 40, for_statement; 41, expression_statement; 42, expression_statement; 43, expression_statement; 44, attribute; 45, argument_list; 46, attribute; 47, argument_list; 48, identifier:page; 49, string; 50, subscript; 51, string; 52, call; 53, identifier:item; 54, subscript; 55, block; 56, assignment; 57, assignment; 58, assignment; 59, identifier:logger; 60, identifier:debug; 61, string:"[identities retention] Total inactive identities deleted from SH: %i"; 62, identifier:count; 63, identifier:es; 64, identifier:search; 65, keyword_argument; 66, keyword_argument; 67, keyword_argument; 68, keyword_argument; 69, string_content:_scroll_id; 70, identifier:page; 71, string; 72, string_content:total; 73, attribute; 74, argument_list; 75, subscript; 76, string; 77, expression_statement; 78, expression_statement; 79, comment:# increment the number of deleted identities only if the corresponding command was successful; 80, expression_statement; 81, identifier:page; 82, call; 83, identifier:sid; 84, subscript; 85, identifier:scroll_size; 86, call; 87, identifier:index; 88, identifier:IDENTITIES_INDEX; 89, identifier:scroll; 90, string:"360m"; 91, identifier:size; 92, identifier:SIZE_SCROLL_IDENTITIES_INDEX; 93, identifier:body; 94, dictionary; 95, string_content:hits; 96, identifier:logging; 97, identifier:warning; 98, string:"[identities retention] No inactive identities found in %s after %s!"; 99, identifier:IDENTITIES_INDEX; 100, identifier:before_date; 101, identifier:page; 102, string; 103, string_content:hits; 104, assignment; 105, assignment; 106, assignment; 107, attribute; 108, argument_list; 109, identifier:page; 110, string; 111, identifier:len; 112, argument_list; 113, pair; 114, string_content:hits; 115, identifier:to_delete; 116, subscript; 117, identifier:success; 118, call; 119, identifier:count; 120, conditional_expression:count + 1 if success else count; 121, identifier:es; 122, identifier:scroll; 123, keyword_argument; 124, keyword_argument; 125, string_content:_scroll_id; 126, subscript; 127, string:"query"; 128, dictionary; 129, subscript; 130, string; 131, attribute; 132, argument_list; 133, binary_operator:count + 1; 134, identifier:success; 135, identifier:count; 136, identifier:scroll_id; 137, identifier:sid; 138, identifier:scroll; 139, string; 140, subscript; 141, string; 142, pair; 143, identifier:item; 144, string; 145, string_content:sh_uuid; 146, identifier:SortingHat; 147, identifier:remove_unique_identity; 148, identifier:sortinghat_db; 149, identifier:to_delete; 150, identifier:count; 151, integer:1; 152, string_content:60m; 153, identifier:page; 154, string; 155, string_content:hits; 156, string:"range"; 157, dictionary; 158, string_content:_source; 159, string_content:hits; 160, pair; 161, string:"last_seen"; 162, dictionary; 163, pair; 164, string:"lte"; 165, identifier:before_date
code: def delete_inactive_unique_identities(es, sortinghat_db, before_date):
      """Select the unique identities not seen before `before_date` and
      delete them from SortingHat.
  
      :param es: ElasticSearchDSL object
      :param sortinghat_db: instance of the SortingHat database
      :param before_date: datetime str to filter the identities
      """
      page = es.search(
          index=IDENTITIES_INDEX,
          scroll="360m",
          size=SIZE_SCROLL_IDENTITIES_INDEX,
          body={
              "query": {
                  "range": {
                      "last_seen": {
                          "lte": before_date
                      }
                  }
              }
          }
      )
  
      sid = page['_scroll_id']
      scroll_size = page['hits']['total']
  
      if scroll_size == 0:
          logging.warning("[identities retention] No inactive identities found in %s after %s!",
                          IDENTITIES_INDEX, before_date)
          return
  
      count = 0
  
      while scroll_size > 0:
          for item in page['hits']['hits']:
              to_delete = item['_source']['sh_uuid']
              success = SortingHat.remove_unique_identity(sortinghat_db, to_delete)
              # increment the number of deleted identities only if the corresponding command was successful
              count = count + 1 if success else count
  
          page = es.scroll(scroll_id=sid, scroll='60m')
          sid = page['_scroll_id']
          scroll_size = len(page['hits']['hits'])
  
      logger.debug("[identities retention] Total inactive identities deleted from SH: %i", count)