def get_trigger_query_values(query_args_func, valid_keys): """Handy function to get all the query args in a batch for trigger APIs. :param query_args_func: A function used to return a list of the query arguments. :type query_args_func: function :param valid_keys: A list containing the valid keys that should be retrieved. :type valid_keys: list :return 6-tuple: spec, fields, skip, limit, compared. """ spec = get_query_spec(query_args_func, valid_keys) created_on = get_created_on_date(query_args_func) add_created_on_date(spec, created_on) get_and_add_date_range(spec, query_args_func, created_on) get_and_add_gte_lt_keys(spec, query_args_func, valid_keys) get_and_add_time_range(spec, query_args_func) utils.update_id_fields(spec) sort = get_query_sort(query_args_func) fields = get_query_fields(query_args_func) skip, limit = get_skip_and_limit(query_args_func) compared = get_compared_value(query_args_func) return spec, sort, fields, skip, limit, compared
def count_one_collection(collection, collection_name, query_args_func, valid_keys): """Count all the available documents in the provide collection. :param collection: The collection whose elements should be counted. :param collection_name: The name of the collection to count. :type collection_name: str :param query_args_func: A function used to return a list of the query arguments. :type query_args_func: function :param valid_keys: A list containing the valid keys that should be retrieved. :type valid_keys: list :return A list containing a dictionary with the `collection`, `count` and optionally the `fields` fields. """ result = [] spec = handlers.common.query.get_query_spec(query_args_func, valid_keys) handlers.common.query.get_and_add_date_range(spec, query_args_func) utils.update_id_fields(spec) if spec: _, number = utils.db.find_and_count(collection, 0, 0, spec, COUNT_FIELDS) if not number: number = 0 result.append(dict(collection=collection_name, count=number)) else: result.append( dict(collection=collection_name, count=utils.db.count(collection))) return result
def test_update_id_fields(self): spec = { "job_id": "123344567", "_id": "0123456789ab0123456789ab", "foo": 1234, "build_id": "0123456789ab0123456789ab" } utils.update_id_fields(spec) expected = { "_id": bson.objectid.ObjectId("0123456789ab0123456789ab"), "foo": 1234, "build_id": bson.objectid.ObjectId("0123456789ab0123456789ab") } self.assertDictEqual(expected, spec)
def _search_docs(compare_to, database, errors): """Search for the docs to compare against. :param compare_to: The list of JSON objects to search. :type compare_to: list :param database: The database connection. :param errors: Where errors should be added. :type errors: dict :return A list of CompareJob documents. """ compare_obj = [] for comp_spec in compare_to: is_valid, _ = utils.validator.is_valid_json( comp_spec, models.compare.JOB_DELTA_COMPARE_TO_VALID_KEYS) if is_valid: utils.update_id_fields(comp_spec) docs = utils.db.find( database[models.BUILD_COLLECTION], 0, 0, spec=comp_spec, fields=BUILD_DOC_FIELDS, sort=BUILD_DOC_SORT ) if docs: d_docs = docs.clone() compare_obj.append(CompareJob([x for x in d_docs])) docs.close() else: ADD_ERR( errors, 404, "No compare documents found for: %s" % str(comp_spec) ) else: ADD_ERR( errors, 400, "Invalid data found in compare section for: %s" % str(comp_spec) ) return compare_obj
def get_values(query_args_func, valid_keys): spec = get_query_spec(query_args_func, valid_keys) created_on = get_created_on_date(query_args_func) add_created_on_date(spec, created_on) get_and_add_date_range(spec, query_args_func, created_on) get_and_add_gte_lt_keys(spec, query_args_func, valid_keys) get_and_add_time_range(spec, query_args_func) utils.update_id_fields(spec) # First the spec. yield spec # Then the sort. yield get_query_sort(query_args_func) # Then the actual fields. yield get_query_fields(query_args_func) # Then the skip value. skip, limit = get_skip_and_limit(query_args_func) yield skip yield limit # Then the aggregate values. yield get_aggregate_value(query_args_func)
def count_one_collection( collection, collection_name, query_args_func, valid_keys): """Count all the available documents in the provide collection. :param collection: The collection whose elements should be counted. :param collection_name: The name of the collection to count. :type collection_name: str :param query_args_func: A function used to return a list of the query arguments. :type query_args_func: function :param valid_keys: A list containing the valid keys that should be retrieved. :type valid_keys: list :return A list containing a dictionary with the `collection`, `count` and optionally the `fields` fields. """ result = [] spec = handlers.common.query.get_query_spec( query_args_func, valid_keys) handlers.common.query.get_and_add_date_range(spec, query_args_func) utils.update_id_fields(spec) if spec: _, number = utils.db.find_and_count( collection, 0, 0, spec, COUNT_FIELDS) if not number: number = 0 result.append(dict(collection=collection_name, count=number)) else: result.append( dict( collection=collection_name, count=utils.db.count(collection)) ) return result
def count_all_collections(database, query_args_func, valid_keys): """Count all the available documents in the database collections. :param database: The datase connection to use. :param query_args_func: A function used to return a list of the query arguments. :type query_args_func: function :param valid_keys: A list containing the valid keys that should be retrieved. :type valid_keys: list :return A list containing a dictionary with the `collection` and `count` fields. """ result = [] spec = handlers.common.query.get_query_spec(query_args_func, valid_keys) handlers.common.query.get_and_add_date_range(spec, query_args_func) utils.update_id_fields(spec) if spec: for collection in models.COUNT_COLLECTIONS: _, number = utils.db.find_and_count(database[collection], 0, 0, spec, COUNT_FIELDS) if not number: number = 0 result.append(dict(collection=collection, count=number)) else: for collection in models.COUNT_COLLECTIONS: result.append({ models.COLLECTION_KEY: collection, models.COUNT_KEY: utils.db.count(database[collection]) }) return result
def count_all_collections(database, query_args_func, valid_keys): """Count all the available documents in the database collections. :param database: The datase connection to use. :param query_args_func: A function used to return a list of the query arguments. :type query_args_func: function :param valid_keys: A list containing the valid keys that should be retrieved. :type valid_keys: list :return A list containing a dictionary with the `collection` and `count` fields. """ result = [] spec = handlers.common.query.get_query_spec(query_args_func, valid_keys) handlers.common.query.get_and_add_date_range(spec, query_args_func) utils.update_id_fields(spec) if spec: for collection in models.COUNT_COLLECTIONS: _, number = utils.db.find_and_count( database[collection], 0, 0, spec, COUNT_FIELDS) if not number: number = 0 result.append(dict(collection=collection, count=number)) else: for collection in models.COUNT_COLLECTIONS: result.append( { models.COLLECTION_KEY: collection, models.COUNT_KEY: utils.db.count(database[collection]) } ) return result