import time

import documents_pb2_grpc
import documents_pb2
import init
import doc_client
import sys
import signal


def sync_documents():
    while True:
        documents_response = doc_client.stub.GetNoVectorDocuments(documents_pb2.GetNoVectorDocumentsRequest()).documents

        # # get all documents with no vector
        for document in documents_response:
            docContent = document.title + "\n" + document.content

            print("正在更新向量...")
            text_vector = init.text_to_vector(docContent)

            # update vector
            update_vector_response = init.insert_document(document.id, document.user_id, text_vector)
            print(update_vector_response)

            # update vector_id
            update_vector_id_response = doc_client.stub.UpdateDocument(documents_pb2.UpdateDocumentRequest(
                id=document.id,
                vector_id=update_vector_response
            ))

            print(update_vector_id_response)
            print("更新向量完成")

            time.sleep(1 * 5)


if __name__ == '__main__':
    sync_documents()