Example #1
0
    def update(cls, es: 'Elasticsearch', old_document: 'BaseDocument',
               document: 'BaseDocument'):
        """
        Updates a document in the index

        :param es: Elasticsearch low-level client
        :param old_document: The old document that is already in the index
        :param document: The new document to update in the index
        :return: document, successful
        """
        if es is None or old_document is None or document is None:
            return None, False

        if not isinstance(document, BaseDocument):
            raise Exception(
                f'`document` is not an instance of {BaseDocument.__class__.__name__} class'
            )

        successful = False
        try:
            id, doc = document._update_doc_from_old_doc(
                old_document).parse_for_db()
            if id and doc:
                response = es.update(index=cls._index_name, id=id, doc=doc)
                successful = True
        except Exception as e:
            logger.exception(e)

        return document, successful
Example #2
0
    def create(cls, es: 'Elasticsearch', document: 'BaseDocument'):
        """
        Creates a document in the index

        :param es: Elasticsearch low-level client
        :param document: The document to insert in the index
        :return: document, successful
        """
        if es is None or document is None:
            return None, False

        if not isinstance(document, BaseDocument):
            raise Exception(
                f'`document` is not an instance of {BaseDocument.__class__.__name__} class'
            )

        successful = False
        try:
            id, doc = document.parse_for_db()
            if id and doc:
                response = es.create(index=cls._index_name,
                                     id=id,
                                     document=doc)
                successful = True
        except ConflictError as e:
            # Exception representing a 409 status code. Document exists in the index
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return document, successful
Example #3
0
 def get_or_create_audio(self, message: 'pyrogram.types.Message',
                         telegram_client_id: int):
     if message is None or message.audio is None:
         return
     try:
         self._es_db.get_or_create_audio(message)
         self._graph_db.get_or_create_audio(message)
         self._document_db.get_or_create_audio(message, telegram_client_id)
     except Exception as e:
         logger.exception(e)
Example #4
0
def _get_config_from_file(file_path: str) -> Optional['dict']:
    try:
        with open(file_path, 'rb') as f:
            return tomli.load(f)
    except tomli.TOMLDecodeError as e:
        logger.exception(e)
    except Exception as e:
        logger.exception(e)

    return None
Example #5
0
 def has_index(cls, es: 'Elasticsearch') -> bool:
     index_exists = False
     try:
         es.indices.get(index=cls._index_name)
         index_exists = True
     except NotFoundError as e:
         pass
     except Exception as e:
         logger.exception(e)
     return index_exists
Example #6
0
    def search(
        cls,
        es: 'Elasticsearch',
        query: str,
        from_: int = 0,
        size: int = 50,
    ):
        if es is None or query is None or from_ is None or size is None:
            return None

        db_docs = []
        search_metadata = {
            'duration': None,
            'total_hits': None,
            'total_rel': None,
            'max_score': None,
        }

        try:
            res: 'ObjectApiResponse' = es.search(
                index=cls._index_name,
                from_=from_,
                size=size,
                query={
                    "multi_match": {
                        "query": query,
                        "type": 'best_fields',
                        "minimum_should_match": "60%",
                        "fields": cls._search_fields,
                    },
                })

            hits = res.body['hits']['hits']

            duration = res.meta.duration
            total_hits = res.body['hits']['total']['value']
            total_rel = res.body['hits']['total']['relation']
            max_score = res.body['hits']['max_score']

            search_metadata = {
                'duration': duration,
                'total_hits': total_hits,
                'total_rel': total_rel,
                'max_score': max_score,
            }

            for index, hit in enumerate(hits, start=1):
                db_doc = cls.parse_from_db_hit(hit, len(hits) - index + 1)
                db_docs.append(db_doc)

        except Exception as e:
            logger.exception(e)

        return db_docs, search_metadata
Example #7
0
    def get(cls, es: 'Elasticsearch', doc_id: str):
        if es is None:
            return None

        obj = None
        try:
            response = es.get(index=cls._index_name, id=doc_id)
            obj = cls.parse_from_db(response)
        except NotFoundError as e:
            # audio does not exist in the index
            pass
        except Exception as e:
            logger.exception(e)
        return obj
    def update(cls, old_edge: 'BaseEdge', edge: 'BaseEdge'):
        """
        Update an object in the database

        :param old_edge: The edge that is already in the database
        :param edge: The edge used for updating the object in the database
        :return: self, successful
        """
        if old_edge is None or edge is None:
            return None, False

        if not isinstance(edge, BaseEdge):
            raise Exception(f'`edge` is not an instance of {BaseEdge.__class__.__name__} class')

        successful = False
        try:
            metadata = cls._db.update(edge._update_metadata_from_old_edge(old_edge).parse_for_graph())
            edge._update_from_metadata(metadata)
            successful = True
        except DocumentUpdateError as e:
            # Failed to update document.
            logger.exception(f"{cls.__name__} : {e}")
        except DocumentRevisionError as e:
            # The expected and actual document revisions mismatched.
            logger.exception(f"{cls.__name__} : {e}")
        except Exception as e:
            logger.exception(f"{cls.__name__} : {e}")
        return edge, successful
Example #9
0
    def update(cls, old_vertex: 'BaseVertex', vertex: 'BaseVertex'):
        """
        Update an object in the database

        :param old_vertex:  The vertex that is already in the database
        :param vertex: The vertex used for updating the object in the database
        :return: self, successful
        """
        if not isinstance(vertex, BaseVertex):
            raise Exception(f'`vertex` is not an instance of {BaseVertex.__class__.__name__} class')

        if old_vertex is None or vertex is None:
            return None, False

        successful = False
        try:
            metadata = cls._db.update(vertex._update_metadata_from_old_vertex(old_vertex).parse_for_graph())
            vertex._update_from_metadata(metadata)
            successful = True
        except DocumentUpdateError as e:
            # Failed to update document.
            logger.exception(f"{cls.__name__} : {e}")
        except DocumentRevisionError as e:
            # The expected and actual document revisions mismatched.
            logger.exception(f"{cls.__name__} : {e}")
        except Exception as e:
            logger.exception(f"{cls.__name__} : {e}")
        return vertex, successful
    def update(cls, db: 'StandardCollection', old_doc: 'BaseDocument',
               doc: 'BaseDocument'):
        """
        Update a document in the database

        :param db: The StandardCollection to use for updating the document
        :param old_doc:  The document that is already in the database
        :param doc: The document used for updating the object in the database
        :return: self, successful
        """
        if not isinstance(doc, BaseDocument):
            raise Exception(
                f'`document` is not an instance of {BaseDocument.__class__.__name__} class'
            )

        if db is None or old_doc is None or doc is None:
            return None, False

        successful = False
        try:
            metadata = db.update(
                doc._update_metadata_from_old_document(old_doc).parse_for_db())
            doc._update_from_metadata(metadata)
            successful = True
        except DocumentUpdateError as e:
            # Failed to update document.
            logger.exception(e)
        except DocumentRevisionError as e:
            # The expected and actual document revisions mismatched.
            logger.exception(e)
        except Exception as e:
            logger.exception(e)
        return doc, successful
    def create(cls, edge: 'BaseEdge'):
        """
        Insert an object into the database

        :param edge: The edge to insert into the database
        :return: self, successful
        """

        if edge is None:
            return None, False

        successful = False
        try:
            metadata = cls._db.insert(edge.parse_for_graph())
            edge._update_from_metadata(metadata)
            successful = True
        except DocumentInsertError as e:
            # Failed to insert the document
            logger.exception(f"{cls.__name__} : {e}")
        except Exception as e:
            logger.exception(f"{cls.__name__} : {e}")
        return edge, successful
Example #12
0
    def create(cls, vertex: 'BaseVertex'):
        """
        Insert an object into the database

        :param vertex: The vertex to insert into the database
        :return: self, successful
        """

        if vertex is None:
            return None, False

        successful = False
        try:
            metadata = cls._db.insert(vertex.parse_for_graph())
            vertex._update_from_metadata(metadata)
            successful = True
        except DocumentInsertError as e:
            # Failed to insert the document
            logger.exception(f"{cls.__name__} : {e}")
        except Exception as e:
            logger.exception(f"{cls.__name__} : {e}")
        return vertex, successful
    def create(cls, db: 'StandardCollection', doc: 'BaseDocument'):
        """
        Insert a document into the database

        :param db: The StandardCollection to use for inserting the document
        :param doc: The document to insert into the database
        :return: self, successful
        """

        if db is None or doc is None:
            return None, False

        successful = False
        try:
            metadata = db.insert(doc.parse_for_db())
            doc._update_from_metadata(metadata)
            successful = True
        except DocumentInsertError as e:
            # Failed to insert the document
            logger.exception(e)
        except Exception as e:
            logger.exception(e)
        return doc, successful
Example #14
0
 def wrap(*args, **kwargs):
     try:
         func(*args, **kwargs)
         logger.info(f'Task Finished')
     except Exception as e:
         logger.exception(e)
 def wrap(*args, **kwargs):
     try:
         func(*args, **kwargs)
     except Exception as e:
         logger.exception(e)
Example #16
0
    def on_inline_query(self, client: 'pyrogram.Client', inline_query: 'pyrogram.types.InlineQuery'):
        logger.debug(f"on_inline_query: {inline_query}")
        query_date = get_timestamp()

        # todo: fix this
        db_from_user = self.db.get_user_by_user_id(inline_query.from_user.id)
        if not db_from_user:
            # update the user
            db_from_user = self.db.update_or_create_user(inline_query.from_user)

        found_any = True
        from_ = 0
        results = []
        temp_res = []
        next_offset = None

        if inline_query.query is None or not len(inline_query.query):
            # todo: query is empty
            found_any = False
        else:
            if inline_query.offset is not None and len(inline_query.offset):
                from_ = int(inline_query.offset)

            db_audio_docs, query_metadata = self.db.search_audio(inline_query.query, from_, size=15)

            if not db_audio_docs or not len(db_audio_docs) or not len(query_metadata):
                found_any = False

            db_audio_docs: List['elasticsearch_models.Audio'] = db_audio_docs

            chats_dict = self.update_audio_cache(db_audio_docs)

            for db_audio_doc in db_audio_docs:
                db_audio_file_cache = self.db.get_audio_file_from_cache(db_audio_doc, self.telegram_client.telegram_id)

                #  todo: Some audios have null titles, solution?
                if not db_audio_file_cache or not db_audio_doc.title:
                    continue

                # todo: telegram cannot handle these mime types, any alternative?
                if db_audio_doc.mime_type in forbidden_mime_types:
                    continue

                temp_res.append((db_audio_file_cache, db_audio_doc))

            next_offset = str(from_ + len(temp_res) + 1) if len(temp_res) else None
            db_inline_query, db_hits = self.db.get_or_create_inline_query(
                self.telegram_client.telegram_id,
                inline_query,
                query_date=query_date,
                query_metadata=query_metadata,
                audio_docs=db_audio_docs,
                next_offset=next_offset
            )

            if db_inline_query and db_hits:
                for (db_audio_file_cache, db_audio_doc), db_hit in zip(temp_res, db_hits):
                    results.append(
                        AudioItem.get_item(
                            db_audio_file_cache,
                            db_from_user,
                            db_audio_doc,
                            inline_query,
                            chats_dict,
                            db_hit,
                        )
                    )

            # logger.info(
            #     f"{inline_query.id} : {inline_query.query} ({len(results)}) => {inline_query.offset} : {next_offset}")

        if found_any and len(results):
            try:
                inline_query.answer(results, cache_time=1, next_offset=next_offset)
            except Exception as e:
                logger.exception(e)
        else:
            # todo: No results matching the query found, what now?
            if from_ is None or from_ == 0:
                inline_query.answer([NoResultItem.get_item(db_from_user)], cache_time=1)