def prepare_for_serialization(obj): """Converts object_id to a string and a datetime object to ctime format :param obj: probably a task or a job document :return: the object reference """ mongo.serialize_object_id(obj) if obj.get('ttl_time_completed'): obj['ttl_time_completed'] = obj['ttl_time_completed'].ctime() return obj
async def get_job_status(self, job_id): """Get the status of job tasks :param job_id: the id of a job :return: Job object contain task status objects or None """ error_states = ['ERROR', 'TIMEOUT', 'EXCEPTION'] job = await self.jobs_collection.find_one({'job_id': job_id}) if not job: return tasks = self.tasks_collection.find({'job_id': job_id}, { 'task_id': 1, 'status': 1, '_id': 0 }) job['has_failures'] = False job['tasks'] = [] async for task in tasks: job['tasks'].append(mongo.serialize_object_id(task)) if task['status'] in error_states: job['has_failures'] = True return self.prepare_for_serialization(job)
async def update_one(self, mercury_id, update_data): if 'mercury_id' in update_data: raise EndpointError('Cannot update mercury_id with this method', 'update_one', update_data) return { 'object_id': serialize_object_id(await self.db.update_one(mercury_id, update_data)) }
async def query(self, q, projection, limit=0, sort_direction=1): c = self.db.query(query=deserialize_object_id(q), extra_projection=projection, limit=limit, sort_direction=sort_direction) total_items = await c.count() items = [] async for document in c: items.append(serialize_object_id(document)) return { 'total': total_items, 'limit': limit, 'items': items, 'direction': sort_direction > 0 and 'ASCENDING' or 'DESCENDING' }
async def query(self, q, projection, limit=0, sort='id', sort_direction=1, offset_id=None): """ :param q: :param projection: :param limit: :param sort: :param sort_direction: :param offset_id: :return: """ c = self.db.query(query=deserialize_object_id(q), extra_projection=projection, limit=limit, sort=sort, sort_direction=sort_direction, offset_id=self.convert_bson_offset_id(offset_id)) total_items = await c.count() items = [] async for document in c: items.append(serialize_object_id(document)) return { 'total': total_items, 'limit': limit, 'items': items, 'sort_direction': sort_direction > 0 and 'ASCENDING' or 'DESCENDING' }
async def get_one(self, mercury_id, projection=None): return serialize_object_id(await self.db.get_one(mercury_id=mercury_id, projection=projection))