本文整理汇总了Python中vj4.db.coll函数的典型用法代码示例。如果您正苦于以下问题:Python coll函数的具体用法?Python coll怎么用?Python coll使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了coll函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
async def run(domain_id: str):
_logger.info('Clearing previous statuses')
await db.coll('document.status').update_many(
{'domain_id': domain_id, 'doc_type': document.TYPE_PROBLEM},
{'$unset': {'journal': '', 'rev': '', 'status': '', 'rid': '',
'num_submit': '', 'num_accept': ''}})
pdocs = problem.get_multi(domain_id=domain_id, fields={'_id': 1, 'doc_id': 1}).sort('doc_id', 1)
dudoc_factory = functools.partial(dict, num_submit=0, num_accept=0)
dudoc_updates = collections.defaultdict(dudoc_factory)
status_coll = db.coll('document.status')
async for pdoc in pdocs:
_logger.info('Problem {0}'.format(pdoc['doc_id']))
# TODO(twd2): ignore no effect statuses like system error, ...
rdocs = record.get_multi(domain_id=domain_id, pid=pdoc['doc_id'],
type=constant.record.TYPE_SUBMISSION,
fields={'_id': 1, 'uid': 1,
'status': 1, 'score': 1}).sort('_id', 1)
_logger.info('Reading records, counting numbers, updating statuses')
factory = functools.partial(dict, num_submit=0, num_accept=0, status=0, rid='')
psdocs = collections.defaultdict(factory)
pdoc_update = {'num_submit': 0, 'num_accept': 0}
async for rdoc in rdocs:
accept = True if rdoc['status'] == constant.record.STATUS_ACCEPTED else False
pdoc_update['num_submit'] += 1
psdocs[rdoc['uid']]['num_submit'] += 1
dudoc_updates[rdoc['uid']]['num_submit'] += 1
if psdocs[rdoc['uid']]['status'] != constant.record.STATUS_ACCEPTED:
psdocs[rdoc['uid']]['status'] = rdoc['status']
psdocs[rdoc['uid']]['rid'] = rdoc['_id']
if accept:
pdoc_update['num_accept'] += 1
dudoc_updates[rdoc['uid']]['num_accept'] += 1
status_bulk = status_coll.initialize_unordered_bulk_op()
execute = False
for uid, psdoc in psdocs.items():
execute = True
(status_bulk.find({'domain_id': domain_id, 'doc_type': document.TYPE_PROBLEM,
'doc_id': pdoc['doc_id'], 'uid': uid})
.upsert().update_one({'$set': {**psdoc}}))
if execute:
_logger.info('Committing')
await status_bulk.execute()
_logger.info('Updating problem')
await document.set(domain_id, document.TYPE_PROBLEM, pdoc['doc_id'], **pdoc_update)
# users' num_submit, num_accept
execute = False
user_coll = db.coll('domain.user')
user_bulk = user_coll.initialize_unordered_bulk_op()
_logger.info('Updating users')
for uid, dudoc_update in dudoc_updates.items():
execute = True
(user_bulk.find({'domain_id': domain_id, 'uid': uid})
.upsert().update_one({'$set': dudoc_update}))
if execute:
_logger.info('Committing')
await user_bulk.execute()
示例2: problem_solution
async def problem_solution(domain_id: str):
_logger.info('Problem Solution Votes')
pipeline = [
{
'$match': {'domain_id': domain_id, 'doc_type': document.TYPE_PROBLEM_SOLUTION}
},
{
'$group': {
'_id': '$doc_id',
'vote': {'$sum': '$vote'}
}
}
]
coll = db.coll('document')
await coll.update_many({'domain_id': domain_id, 'doc_type': document.TYPE_PROBLEM_SOLUTION},
{'$set': {'vote': 0}})
bulk = coll.initialize_unordered_bulk_op()
execute = False
_logger.info('Counting')
async for adoc in await db.coll('document.status').aggregate(pipeline):
bulk.find({'domain_id': domain_id,
'doc_type': document.TYPE_PROBLEM_SOLUTION,
'doc_id': adoc['_id']}) \
.update_one({'$set': {'vote': adoc['vote']}})
execute = True
if execute:
_logger.info('Committing')
await bulk.execute()
_logger.info('Problem Solution Votes group by user')
pipeline = [
{
'$match': {'domain_id': domain_id, 'doc_type': document.TYPE_PROBLEM_SOLUTION}
},
{
'$group': {
'_id': '$owner_uid',
'num_liked': {'$sum': '$vote'}
}
}
]
user_coll = db.coll('domain.user')
await user_coll.update_many({'domain_id': domain_id},
{'$set': {'num_liked': 0}})
user_bulk = user_coll.initialize_unordered_bulk_op()
execute = False
_logger.info('Counting')
async for adoc in await db.coll('document').aggregate(pipeline):
user_bulk.find({'domain_id': domain_id,
'uid': adoc['_id']}) \
.upsert().update_one({'$set': {'num_liked': adoc['num_liked']}})
execute = True
if execute:
_logger.info('Committing')
await user_bulk.execute()
示例3: update_problem
async def update_problem(domain_id: str, pid: document.convert_doc_id):
dudoc_incs = {}
pdoc = await problem.get(domain_id, pid)
_logger.info('Domain {0} Problem {1}'.format(domain_id, pdoc['doc_id']))
status_coll = db.coll('document.status')
status_bulk = status_coll.initialize_unordered_bulk_op()
# Accepteds adjustment
psdocs = problem.get_multi_status(domain_id=domain_id,
doc_id=pdoc['doc_id'],
status=constant.record.STATUS_ACCEPTED).sort('rid', 1)
order = 0
rp_func = get_rp_func(pdoc)
async for psdoc in psdocs:
order += 1
rp = rp_func(order)
delta_rp = rp - psdoc.get('rp', 0.0)
status_bulk.find({'_id': psdoc['_id']}).update_one({'$set': {'rp': rp}})
# (pid, uid) is unique.
assert psdoc['uid'] not in dudoc_incs
dudoc_incs[psdoc['uid']] = {'rp': delta_rp}
if order != pdoc['num_accept']:
_logger.warning('{0} != {1}'.format(order, pdoc['num_accept']))
_logger.warning('Problem {0} num_accept may be inconsistent.'.format(pdoc['doc_id']))
# Was Accepted but Now Not Accepteds adjustment
# TODO(twd2): should we use $ne? can $ne be indexed?
psdocs = problem.get_multi_status(domain_id=domain_id,
doc_id=pdoc['doc_id'],
status={'$gt': constant.record.STATUS_ACCEPTED},
rp={'$gt': 0.0})
execute = False
async for psdoc in psdocs:
rp = 0.0
delta_rp = rp - psdoc['rp']
execute = True
status_bulk.find({'_id': psdoc['_id']}).update_one({'$set': {'rp': rp}})
# (pid, uid) is unique.
assert psdoc['uid'] not in dudoc_incs
dudoc_incs[psdoc['uid']] = {'rp': delta_rp}
if order > 0 or execute:
_logger.info('Committing')
await status_bulk.execute()
# users' rp
user_coll = db.coll('domain.user')
user_bulk = user_coll.initialize_unordered_bulk_op()
execute = False
_logger.info('Updating users')
for uid, dudoc_inc in dudoc_incs.items():
if abs(dudoc_inc['rp']) > RP_MIN_DELTA:
execute = True
user_bulk.find({'domain_id': domain_id, 'uid': uid}).upsert().update_one({'$inc': dudoc_inc})
if execute:
_logger.info('Committing')
await user_bulk.execute()
示例4: ensure_indexes
async def ensure_indexes():
coll = db.coll('domain')
await coll.create_index('owner_uid')
await coll.create_index('name')
user_coll = db.coll('domain.user')
await user_coll.create_index('uid')
await user_coll.create_index([('domain_id', 1),
('uid', 1)], unique=True)
await user_coll.create_index([('domain_id', 1),
('role', 1)], sparse=True)
await user_coll.create_index([('domain_id', 1),
('rp', -1)])
await user_coll.create_index([('domain_id', 1),
('rank', 1)])
示例5: ensure_indexes
async def ensure_indexes():
coll = db.coll('oplog')
await coll.create_index('uid')
# type delete document
await coll.create_index([('doc.domain_id', 1),
('doc.doc_type', 1),
('doc.doc_id', 1)], sparse=True)
示例6: get_most_recent_session_by_uid
async def get_most_recent_session_by_uid(uid: int):
"""Get the most recent session by uid."""
coll = db.coll('token')
doc = await coll.find_one({'uid': uid,
'token_type': {'$in': [TYPE_SAVED_SESSION, TYPE_UNSAVED_SESSION]}},
sort=[('update_at', -1)])
return doc
示例7: delete_by_uid
async def delete_by_uid(uid: int):
"""Delete all tokens by uid."""
coll = db.coll('token')
result = await coll.delete_many({'uid': uid,
'token_type': {'$in': [TYPE_SAVED_SESSION,
TYPE_UNSAVED_SESSION]}})
return bool(result.deleted_count)
示例8: sync_usage
async def sync_usage():
_logger.info('Userfile length group by user')
pipeline = [
{
'$match': {'domain_id': userfile.STORE_DOMAIN_ID,
'doc_type': document.TYPE_USERFILE}
},
{
'$group': {
'_id': '$owner_uid',
'usage_userfile': {'$sum': '$length'}
}
}
]
coll = db.coll('domain.user')
await coll.update_many({'domain_id': userfile.STORE_DOMAIN_ID},
{'$set': {'usage_userfile': 0}})
bulk = coll.initialize_unordered_bulk_op()
execute = False
_logger.info('Counting')
async for adoc in await db.coll('document').aggregate(pipeline):
bulk.find({'domain_id': userfile.STORE_DOMAIN_ID,
'uid': adoc['_id']}) \
.update_one({'$set': {'usage_userfile': adoc['usage_userfile']}})
execute = True
if execute:
_logger.info('Committing')
await bulk.execute()
示例9: training
async def training(domain_id: str):
_logger.info('Training')
pipeline = [
{
'$match': {'domain_id': domain_id, 'doc_type': document.TYPE_TRAINING}
},
{
'$group': {
'_id': '$doc_id',
'enroll': {'$sum': '$enroll'}
}
}
]
coll = db.coll('document')
await coll.update_many({'domain_id': domain_id, 'doc_type': document.TYPE_TRAINING},
{'$set': {'enroll': 0}})
bulk = coll.initialize_unordered_bulk_op()
execute = False
_logger.info('Counting')
async for adoc in await db.coll('document.status').aggregate(pipeline):
bulk.find({'domain_id': domain_id,
'doc_type': document.TYPE_TRAINING,
'doc_id': adoc['_id']}) \
.update_one({'$set': {'enroll': adoc['enroll']}})
execute = True
if execute:
_logger.info('Committing')
await bulk.execute()
示例10: get_db_version
async def get_db_version():
coll = db.coll('system')
doc = await coll.find_one({'_id': 'db_version'})
if doc is None:
return 0
else:
return doc['value']
示例11: problem
async def problem(domain_id: str):
_logger.info('Problem')
pipeline = [
{
'$match': {'domain_id': domain_id, 'doc_type': document.TYPE_PROBLEM}
},
{
'$group': {
'_id': '$owner_uid',
'num_problems': {'$sum': 1}
}
}
]
user_coll = db.coll('domain.user')
await user_coll.update_many({'domain_id': domain_id},
{'$set': {'num_problems': 0}})
user_coll = user_coll.initialize_unordered_bulk_op()
execute = False
_logger.info('Counting')
async for adoc in await db.coll('document').aggregate(pipeline):
user_coll.find({'domain_id': domain_id,
'uid': adoc['_id']}) \
.upsert().update_one({'$set': {'num_problems': adoc['num_problems']}})
execute = True
if execute:
_logger.info('Committing')
await user_coll.execute()
示例12: discussion
async def discussion(domain_id: str):
_logger.info('Discussion')
pipeline = [
{
'$match': {'domain_id': domain_id, 'doc_type': document.TYPE_DISCUSSION_REPLY}
},
{
'$group': {
'_id': '$parent_doc_id',
'num_replies': {'$sum': 1}
}
}
]
coll = db.coll('document')
await coll.update_many({'domain_id': domain_id, 'doc_type': document.TYPE_DISCUSSION},
{'$set': {'num_replies': 0}})
bulk = coll.initialize_unordered_bulk_op()
execute = False
_logger.info('Counting')
async for adoc in await db.coll('document').aggregate(pipeline):
bulk.find({'domain_id': domain_id,
'doc_type': document.TYPE_DISCUSSION,
'doc_id': adoc['_id']}) \
.update_one({'$set': {'num_replies': adoc['num_replies']}})
execute = True
if execute:
_logger.info('Committing')
await bulk.execute()
示例13: release_lock
async def release_lock(lock_name: str, lock_value: int):
coll = db.coll('system')
result = await coll.update_one(filter={'_id': 'lock_' + lock_name, 'value': lock_value},
update={'$set': {'value': 0}})
if result.matched_count == 0:
return None
return True
示例14: set
async def set(domain_id: str, doc_type: int, doc_id: convert_doc_id, **kwargs):
coll = db.coll('document')
doc = await coll.find_one_and_update(filter={'domain_id': domain_id,
'doc_type': doc_type,
'doc_id': doc_id},
update={'$set': kwargs},
return_document=ReturnDocument.AFTER)
return doc
示例15: delete_roles
async def delete_roles(domain_id: str, roles):
roles = list(set(roles))
for role in roles:
validator.check_role(role)
if role in builtin.BUILTIN_ROLE_DESCRIPTORS:
raise error.ModifyBuiltinRoleError(domain_id, role)
for domain in builtin.DOMAINS:
if domain['_id'] == domain_id:
raise error.BuiltinDomainError(domain_id)
user_coll = db.coll('domain.user')
await user_coll.update_many({'domain_id': domain_id, 'role': {'$in': list(roles)}},
{'$unset': {'role': ''}})
coll = db.coll('domain')
return await coll.find_one_and_update(filter={'_id': domain_id},
update={'$unset': dict(('roles.{0}'.format(role), '')
for role in roles)},
return_document=ReturnDocument.AFTER)