async def get_matched_paths(path: str, db: AsyncIOMotorCollection): matches = [] async for ctx in db.find({}): if pydash.has(ctx, 'path'): match = re.match(ctx['path'], path) match and matches.append(pydash.merge( ctx, {'regex_groups': match.groups()})) return matches
async def find(self, entityName: str, query: dict = None, projection: dict = None, collection: AsyncIOMotorCollection = None) -> List[dict]: if collection is None: collection = await self._getCollection(entityName) return [d async for d in collection.find(query, projection=projection)]
async def get_all(db: AsyncIOMotorCollection): """ gets all circuit breakers @param db: mongo instance """ res = db.find({}) return await res.to_list(100)
async def get_all(db: AsyncIOMotorCollection): """ gets all insights @param db: mongo collection instance """ res = db.find({}) return await res.to_list(100)
async def get_all(db: AsyncIOMotorCollection): """ Gets all request validation entries @param db: (object) db connection @return: the documents with the provided serviceId """ res = db.find({}) return await res.to_list(100)
async def get_by_status_code(status_code: int, db: AsyncIOMotorCollection): """ gets insights by status code @param status_code: (str) status_code to get insights by @param db: mongo collection instance """ res = db.find({'status_code': status_code}) return await res.to_list(100)
async def get_by_circuit_breaker_id(_id: str, db: AsyncIOMotorCollection): """ gets event by circuit breaker id @param id: (str) circuit breaker id to get event by @param db: mongo collection instance """ res = db.find({'circuit_breaker_id': _id}) return await res.to_list(100)
async def get_by_method(method: str, db: AsyncIOMotorCollection): """ gets cirbuit breaker by method @param metod: (str) method of cirbuit breaker @param db: mongo instance """ res = db.find({'method': method}) return await res.to_list(100)
async def get_by_threshold(threshold: float, db: AsyncIOMotorCollection): """ gets cirbuit breaker by threshold @param threshold: (float) threshold of cirbuit breaker @param db: mongo instance """ res = db.find({'threshold': threshold}) return await res.to_list(100)
async def get_by_service_id(service_id: str, db: AsyncIOMotorCollection): """ gets cirbuit breaker by service id @param service_id: (str) service id of cirbuit breaker @param db: mongo instance """ res = db.find({'service_id': service_id}) return await res.to_list(100)
async def get_by_status_code(status_code: int, db: AsyncIOMotorCollection): """ gets cirbuit breaker by status code @param status_code: (int) status code of cirbuit breaker @param db: mongo instance """ res = db.find({'status_code': status_code}) return await res.to_list(100)
async def get_by_path(path: str, db: AsyncIOMotorCollection): """ gets insights by path @param path: (str) path to get insights by @param db: mongo collection instance """ res = db.find({'path': path}) return await res.to_list(100)
async def get_by_method(method: str, db: AsyncIOMotorCollection): """ gets insights by method @param method: (str) method to get insights by @param db: mongo collection instance """ res = db.find({'method': method}) return await res.to_list(100)
async def get_by_remote_ip(remote_ip: str, db: AsyncIOMotorCollection): """ gets insights by remote ip @param remote_id: (str) remote_ip to get insights by @param db: mongo collection instance """ res = db.find({'remote_ip': remote_ip}) return await res.to_list(100)
async def get_by_target(target: str, db: AsyncIOMotorCollection): """ gets event by target @param target: (str) target to get event by @param db: mongo collection instance """ res = db.find({'target': target}) return await res.to_list(100)
async def get_by_scheme(scheme: str, db: AsyncIOMotorCollection): """ gets insights by scheme @param scheme: (str) scheme to get insights by @param db: mongo collection instance """ res = db.find({'scheme': scheme}) return await res.to_list(100)
async def get_by_service_id(_id: str, db: AsyncIOMotorCollection): """ gets insights by service id @param id: (str) id to get insights by @param db: mongo collection instance """ res = db.find({'service_id': _id}) return await res.to_list(100)
async def get_by_service_id(service_id: str, db: AsyncIOMotorCollection): """ Gets a request validation entry by the service_id provided @param service_id: (string) ID for the service which the request is (coming from? targeting?) @param db: (object) db connection @return: the documents with the provided serviceId """ res = db.find({"service_id": service_id}) return await res.to_list(100)
async def get_by_method(method: str, db: AsyncIOMotorCollection): """ Gets a request validation entry by the method provided @param method: (string) HTTP method that the request is transmitted by @param db: (object) db connection @return: the documents using the provided method """ res = db.find({"method": method}) return await res.to_list(100)
def test_collection(self): # Test that we can create a collection directly, not just from # database accessors. collection = AsyncIOMotorCollection(self.db, 'test_collection') # Make sure we got the right collection and it can do an operation self.assertEqual('test_collection', collection.name) yield from collection.delete_many({}) yield from collection.insert_one({'_id': 1}) doc = yield from collection.find_one({'_id': 1}) self.assertEqual(1, doc['_id']) # If you pass kwargs to PyMongo's Collection(), it calls # db.create_collection(). Motor can't do I/O in a constructor # so this is prohibited. self.assertRaises(TypeError, AsyncIOMotorCollection, self.db, 'test_collection', capped=True)
async def get_active_leaves( history: AsyncIOMotorCollection, current_date: date, ) -> List[Dict]: current_datetime = datetime.combine(current_date, time.min) return await ( history.find({ 'date_from': {'$lte': current_datetime}, 'date_to': {'$gte': current_datetime}, 'approval_status': 'approved' }).to_list(None) )
def test_collection(self): # Test that we can create a collection directly, not just from # database accessors. collection = AsyncIOMotorCollection(self.db, 'test_collection') # Make sure we got the right collection and it can do an operation self.assertEqual('test_collection', collection.name) yield from collection.remove() yield from collection.insert({'_id': 1}) doc = yield from collection.find_one({'_id': 1}) self.assertEqual(1, doc['_id']) # If you pass kwargs to PyMongo's Collection(), it calls # db.create_collection(). Motor can't do I/O in a constructor # so this is prohibited. self.assertRaises( TypeError, AsyncIOMotorCollection, self.db, 'test_collection', capped=True)
async def find_one(collection: AsyncIOMotorCollection, match, pipeline=[]): cursor: AsyncIOMotorCursor = collection.aggregate([ { '$match': match }, { '$limit': 1 }, *pipeline, ]) if await cursor.fetch_next: return cursor.next_object() else: return None
async def find(collection: AsyncIOMotorCollection, match={}, pipeline=[], sort=None, limit=None, skip=0, max_len=MAX_NODES): pipe: list = [] if match: pipe.append({'$match': match}) if sort: pipe.append({'$sort': sort}) if limit: pipe.append({"$limit": skip + limit}) if skip: pipe.append({"$skip": skip}) pipeline = pipeline + pipe cursor: AsyncIOMotorCursor = collection.aggregate(pipeline) return await cursor.to_list(max_len)
async def list(cls, collection: AsyncIOMotorCollection, projection_model: Type[BaseModel], request: Request, page: int, records_per_page: int, search_fields: Set[str], keyword: Optional[str] = None, sort: Optional[List[Tuple[str, int]]] = None) -> DataList: """List documents. :param collection: Collection reference :param projection_model: Projection model :param request: HTTP request :param page: Page number :param records_per_page: Records per page :param search_fields: Search fields :param keyword: Keyword for searching data :param sort: Sort ( Default is [("updated_at", pymongo.DESCENDING)]. ) :return: A list of documents :raises HTTPResponseException: If there were some errors during the database operation. """ if sort is None: sort = [("updated_at", pymongo.DESCENDING)] query: dict = {} if keyword is None else await cls.__get_regex_filters(keyword, search_fields) try: documents: AsyncIOMotorCursor = collection.find( filter=query, sort=sort, skip=(page - 1) * records_per_page, limit=records_per_page, projection=await cls.__get_projection(projection_model) ) pagination: dict = await cls._get_pagination(request, page, records_per_page, await collection.count_documents(query) ) pagination.update({"data": await documents.to_list(length=records_per_page)}) return pagination except PyMongoError as database_server_error: await cls._handle_database_server_error(database_server_error)
async def init_async(cls, remote_collection: AsyncIOMotorCollection): cursor = remote_collection.find({}) collection = {doc['_id']: Document(doc) async for doc in cursor} return cls(collection, remote_collection)